|
|
@@ -0,0 +1,202 @@
|
|
|
+package cz.senslog.watchdog.util.http;
|
|
|
+
|
|
|
+import cz.senslog.watchdog.util.StringUtils;
|
|
|
+import org.apache.http.Header;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpMessage;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.client.methods.HttpRequestBase;
|
|
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
+import org.apache.http.conn.ssl.TrustStrategy;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.http.ssl.SSLContextBuilder;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URISyntaxException;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.KeyStoreException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.apache.http.HttpHeaders.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * The class {@code HttpClient} represents a wrapper for {@link org.apache.http.client.HttpClient}.
|
|
|
+ * Provides functionality of sending GET and POST request. Otherwise is returned response with {@see #BAD_REQUEST}.
|
|
|
+ *
|
|
|
+ * @author Lukas Cerny
|
|
|
+ * @version 1.0
|
|
|
+ * @since 1.0
|
|
|
+ */
|
|
|
+public class HttpClient {
|
|
|
+
|
|
|
+ /** Instance of http client. */
|
|
|
+ private final org.apache.http.client.HttpClient client;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Factory method to create a new instance of client.
|
|
|
+ * @return new instance of {@code HttpClient}.
|
|
|
+ */
|
|
|
+ public static HttpClient newHttpClient() {
|
|
|
+ return new HttpClient(HttpClientBuilder.create());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static HttpClient newHttpSSLClient() {
|
|
|
+ try {
|
|
|
+ SSLContextBuilder builder = new SSLContextBuilder();
|
|
|
+ builder.loadTrustMaterial(null, (TrustStrategy) (chain, authType) -> true);
|
|
|
+
|
|
|
+ SSLConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(builder.build(),
|
|
|
+ SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
|
|
+ return new HttpClient(HttpClientBuilder.create().setSSLSocketFactory(sslSF));
|
|
|
+
|
|
|
+ } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Private constructors sets http client.
|
|
|
+ */
|
|
|
+ private HttpClient(HttpClientBuilder httpClientBuilder) {
|
|
|
+ this.client = httpClientBuilder.build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sends http request.
|
|
|
+ * @param request - virtual request.
|
|
|
+ * @return virtual response.
|
|
|
+ */
|
|
|
+ public HttpResponse send(HttpRequest request) {
|
|
|
+ try {
|
|
|
+ switch (request.getMethod()) {
|
|
|
+ case GET: return sendGet(request);
|
|
|
+ case POST: return sendPost(request);
|
|
|
+ default: return HttpResponse.newBuilder()
|
|
|
+ .body("Request does not contain method definition.")
|
|
|
+ .status(HttpCode.METHOD_NOT_ALLOWED).build();
|
|
|
+ }
|
|
|
+ } catch (URISyntaxException e) {
|
|
|
+ return HttpResponse.newBuilder()
|
|
|
+ .body(e.getMessage()).status(HttpCode.BAD_REQUEST)
|
|
|
+ .build();
|
|
|
+ } catch (IOException e) {
|
|
|
+ return HttpResponse.newBuilder()
|
|
|
+ .body(e.getMessage()).status(HttpCode.SERVER_ERROR)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sends GET request.
|
|
|
+ * @param request - virtual request.
|
|
|
+ * @return virtual response of the request.
|
|
|
+ * @throws URISyntaxException throws if host url is not valid.
|
|
|
+ * @throws IOException throws if anything happen during sending.
|
|
|
+ */
|
|
|
+ private HttpResponse sendGet(HttpRequest request) throws IOException, URISyntaxException {
|
|
|
+
|
|
|
+ URI uri = request.getUrl().toURI();
|
|
|
+ HttpGet requestGet = new HttpGet(uri);
|
|
|
+ setBasicHeaders(request, requestGet);
|
|
|
+
|
|
|
+ org.apache.http.HttpResponse responseGet = client.execute(requestGet);
|
|
|
+
|
|
|
+ HttpResponse response = HttpResponse.newBuilder()
|
|
|
+ .status(responseGet.getStatusLine().getStatusCode())
|
|
|
+ .headers(getHeaders(responseGet))
|
|
|
+ .body(getBody(responseGet.getEntity()))
|
|
|
+ .build();
|
|
|
+
|
|
|
+ EntityUtils.consume(responseGet.getEntity());
|
|
|
+
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sends POST request.
|
|
|
+ * @param request - virtual request.
|
|
|
+ * @return virtual response of the request.
|
|
|
+ * @throws URISyntaxException throws if host url is not valid.
|
|
|
+ * @throws IOException throws if anything happen during sending.
|
|
|
+ */
|
|
|
+ private HttpResponse sendPost(HttpRequest request) throws URISyntaxException, IOException {
|
|
|
+
|
|
|
+ URI uri = request.getUrl().toURI();
|
|
|
+ HttpPost requestPost = new HttpPost(uri);
|
|
|
+ setBasicHeaders(request, requestPost);
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(request.getContentType())) {
|
|
|
+ requestPost.setHeader(CONTENT_TYPE, request.getContentType());
|
|
|
+ }
|
|
|
+
|
|
|
+ requestPost.setEntity(new StringEntity(request.getBody()));
|
|
|
+
|
|
|
+ org.apache.http.HttpResponse responsePost = client.execute(requestPost);
|
|
|
+
|
|
|
+ HttpResponse response = HttpResponse.newBuilder()
|
|
|
+ .headers(getHeaders(requestPost))
|
|
|
+ .status(responsePost.getStatusLine().getStatusCode())
|
|
|
+ .body(getBody(responsePost.getEntity()))
|
|
|
+ .build();
|
|
|
+
|
|
|
+ EntityUtils.consume(responsePost.getEntity());
|
|
|
+
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sets basic headers to each request.
|
|
|
+ * @param userRequest - virtual request.
|
|
|
+ * @param httpRequest - real request prepared to send.
|
|
|
+ */
|
|
|
+ private void setBasicHeaders(HttpRequest userRequest, HttpRequestBase httpRequest) {
|
|
|
+
|
|
|
+ httpRequest.setHeader(USER_AGENT, "SenslogConnector/1.0");
|
|
|
+ httpRequest.setHeader(CACHE_CONTROL, "no-cache");
|
|
|
+
|
|
|
+ for (Map.Entry<String, String> headerEntry : userRequest.getHeaders().entrySet()) {
|
|
|
+ httpRequest.setHeader(headerEntry.getKey(), headerEntry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns map of headers from the response.
|
|
|
+ * @param response - response message.
|
|
|
+ * @return map of headers.
|
|
|
+ */
|
|
|
+ private Map<String, String> getHeaders(HttpMessage response) {
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ for (Header header : response.getAllHeaders()) {
|
|
|
+ headers.put(header.getName(), header.getValue());
|
|
|
+ }
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns body from the response.
|
|
|
+ * @param entity - response entity.
|
|
|
+ * @return string body of the response.
|
|
|
+ * @throws IOException can not get body from the response.
|
|
|
+ */
|
|
|
+ private String getBody(HttpEntity entity) throws IOException {
|
|
|
+ if (entity == null) return "";
|
|
|
+ InputStream contentStream = entity.getContent();
|
|
|
+ InputStreamReader bodyStream = new InputStreamReader(contentStream);
|
|
|
+ BufferedReader rd = new BufferedReader(bodyStream);
|
|
|
+ StringBuilder bodyBuffer = new StringBuilder();
|
|
|
+ String line;
|
|
|
+ while ((line = rd.readLine()) != null) {
|
|
|
+ bodyBuffer.append(line);
|
|
|
+ }
|
|
|
+ return bodyBuffer.toString();
|
|
|
+ }
|
|
|
+}
|