Salvatore La Manna
4 anni fa
2 ha cambiato i file con 83 aggiunte e 2 eliminazioni
@ -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; |
||||
} |
||||
} |
Caricamento…
Reference in new issue