Sfoglia il codice sorgente

Inizio nuovo Post

master
Salvatore La Manna 4 anni fa
parent
commit
2b2e58ea26
  1. 81
      liferay-plugins-sdk-6.2/portlets/portos-bo-portlet/docroot/WEB-INF/service/it/mwg/sismica/bo/util/HttpServiceUtil.java
  2. 4
      liferay-plugins-sdk-6.2/portlets/portos-bo-portlet/docroot/WEB-INF/src/service.properties

81
liferay-plugins-sdk-6.2/portlets/portos-bo-portlet/docroot/WEB-INF/service/it/mwg/sismica/bo/util/HttpServiceUtil.java

@ -0,0 +1,81 @@
package it.mwg.sismica.bo.util;
import it.tref.liferay.portos.bo.service.ConfigurazioneLocalServiceUtil;
import it.tref.liferay.portos.bo.shared.protocollo.model.Protocollo;
import it.tref.liferay.portos.bo.shared.util.ConfigurazioneConstants;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.HttpUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Http.Options;
public class HttpServiceUtil {
private static final Log _log = LogFactoryUtil.getLog(HttpServiceUtil.class);
public static JSONObject postProtocolloBean(long companyId, Protocollo protocolloBean) throws Exception {
flexjson.JSONSerializer serializer = new flexjson.JSONSerializer();
serializer.exclude("*.class");
String json = serializer.serialize(protocolloBean);
String url = ConfigurazioneLocalServiceUtil.findByC_ChiaveString(companyId,
ConfigurazioneConstants.PROTOCOLLAZIONE_SERVICE_URL);
return postJson(url, json);
}
public static JSONObject postJson(String url, String json) throws Exception {
JSONObject risposta = null;
HttpPost request = new HttpPost(url);
request.setHeader("Content-type", ContentTypes.APPLICATION_JSON);
request.setEntity(new StringEntity(json));
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity(), StringPool.UTF8);
client.close();
if (statusCode == HttpServletResponse.SC_OK || statusCode == HttpServletResponse.SC_CREATED) {
risposta = JSONFactoryUtil.createJSONObject(responseBody);
_log.info(url + " || " + json + " || " + risposta);
} else {
_log.info(url + " || " + json + " || " + statusCode + ": " + responseBody);
throw new PortalException(statusCode + StringPool.SPACE + responseBody);
}
return risposta;
}
public static String getUrl(String url) throws Exception {
return getUrl(url, StringPool.BLANK);
}
public static String getUrl(String url, String defaultValue) throws Exception {
Options options = new Options();
options.setLocation(url);
options.setPost(false);
String responseBody = HttpUtil.URLtoString(options);
_log.info(url + " || " + responseBody);
if (options.getResponse().getResponseCode() != HttpServletResponse.SC_OK) {
throw new Exception(url + ": " + options.getResponse().getResponseCode());
// responseBody = defaultValue;
}
return responseBody;
}
}

4
liferay-plugins-sdk-6.2/portlets/portos-bo-portlet/docroot/WEB-INF/src/service.properties

@ -13,8 +13,8 @@
##
build.namespace=portos_bo
build.number=2405
build.date=1600340894413
build.number=2406
build.date=1600353835805
build.auto.upgrade=true
##

Caricamento…
Annulla
Salva