From 2b2e58ea2685160809594318b389c31a4fab9d92 Mon Sep 17 00:00:00 2001 From: Salvatore La Manna Date: Thu, 17 Sep 2020 16:44:30 +0200 Subject: [PATCH] Inizio nuovo Post --- .../mwg/sismica/bo/util/HttpServiceUtil.java | 81 +++++++++++++++++++ .../docroot/WEB-INF/src/service.properties | 4 +- 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 liferay-plugins-sdk-6.2/portlets/portos-bo-portlet/docroot/WEB-INF/service/it/mwg/sismica/bo/util/HttpServiceUtil.java diff --git a/liferay-plugins-sdk-6.2/portlets/portos-bo-portlet/docroot/WEB-INF/service/it/mwg/sismica/bo/util/HttpServiceUtil.java b/liferay-plugins-sdk-6.2/portlets/portos-bo-portlet/docroot/WEB-INF/service/it/mwg/sismica/bo/util/HttpServiceUtil.java new file mode 100644 index 00000000..5b698001 --- /dev/null +++ b/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; + } +} diff --git a/liferay-plugins-sdk-6.2/portlets/portos-bo-portlet/docroot/WEB-INF/src/service.properties b/liferay-plugins-sdk-6.2/portlets/portos-bo-portlet/docroot/WEB-INF/src/service.properties index 5409a0ba..c911bcbf 100644 --- a/liferay-plugins-sdk-6.2/portlets/portos-bo-portlet/docroot/WEB-INF/src/service.properties +++ b/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 ##