blob: dbfdbb8db82162ccbdd188c12bbd880e8cbabce5 [file] [log] [blame]
package pta.de.core.communication;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import pta.de.core.common.Globals;
import pta.de.core.exceptions.HttpStatusException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class RestServiceWrapper {
private static final Logger LOGGER = Logger.getLogger(RestServiceWrapper.class.getName());
private boolean useHttps;
public RestServiceWrapper(boolean https) {
this.useHttps = https;
}
public String performGetRequest(String restFunctionWithParams) throws HttpStatusException {
LOGGER.debug("CompleteUrl: " + restFunctionWithParams);
// create HTTP Client
CloseableHttpClient httpClient = createHttpsClient();
// create new Request with given URL
HttpGet getRequest = new HttpGet(restFunctionWithParams);
getRequest.addHeader("accept", Globals.HEADER_JSON_UTF8);
HttpResponse response;
// Execute request an catch response
try {
response = httpClient.execute(getRequest);
} catch (IOException e) {
String errtext = "Communication to <" + restFunctionWithParams + "> failed!";
LOGGER.warn(errtext, e);
throw new HttpStatusException(HttpStatus.SC_SERVICE_UNAVAILABLE);
}
return createJson(response);
}
public String performPostRequest(String restFunctionWithParams, String data) throws HttpStatusException {
// create HTTP Client
CloseableHttpClient httpClient = createHttpsClient();
// create new Post Request with given URL
HttpPost postRequest = new HttpPost(restFunctionWithParams);
// add additional header to getRequest which accepts application/JSON data
postRequest.addHeader("accept", Globals.HEADER_JSON_UTF8);
postRequest.addHeader("Content-Type", Globals.HEADER_JSON_UTF8);
postRequest.setEntity(new StringEntity(data, StandardCharsets.UTF_8));
HttpResponse response;
// Execute request an catch response
try {
response = httpClient.execute(postRequest);
} catch (IOException e) {
String errtext = "Communication to <" + restFunctionWithParams + "> failed!";
LOGGER.warn(errtext, e);
throw new HttpStatusException(HttpStatus.SC_SERVICE_UNAVAILABLE);
}
return createJson(response);
}
private CloseableHttpClient createHttpsClient() throws HttpStatusException {
if (useHttps) {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (Exception e) {
LOGGER.error(e);
throw new HttpStatusException(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
} else {
return HttpClientBuilder.create().build();
}
}
private String createJson(HttpResponse response) throws HttpStatusException {
String retJson;
try {
retJson = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
} catch (IOException e) {
LOGGER.error(e);
throw new HttpStatusException(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
return retJson;
}
}