Jagger
 All Classes Namespaces Files Functions Variables Groups Pages
SpringBasedHttpClient.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.invoker.v2;
2 
3 import org.apache.http.conn.ssl.NoopHostnameVerifier;
4 import org.apache.http.impl.client.CloseableHttpClient;
5 import org.apache.http.impl.client.HttpClients;
6 import org.springframework.http.HttpMethod;
7 import org.springframework.http.RequestEntity;
8 import org.springframework.http.ResponseEntity;
9 import org.springframework.http.client.ClientHttpRequestFactory;
10 import org.springframework.http.client.ClientHttpRequestInterceptor;
11 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
12 import org.springframework.http.converter.HttpMessageConverter;
13 import org.springframework.web.client.ResponseErrorHandler;
14 import org.springframework.web.client.RestTemplate;
15 import org.springframework.web.util.UriTemplateHandler;
16 
17 import java.net.URI;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 
22 
23 import static com.griddynamics.jagger.invoker.v2.SpringBasedHttpClient.JSpringBasedHttpClientParameters.DEFAULT_URI_VARIABLES;
24 import static com.griddynamics.jagger.invoker.v2.SpringBasedHttpClient.JSpringBasedHttpClientParameters.ERROR_HANDLER;
25 import static com.griddynamics.jagger.invoker.v2.SpringBasedHttpClient.JSpringBasedHttpClientParameters.INTERCEPTORS;
26 import static com.griddynamics.jagger.invoker.v2.SpringBasedHttpClient.JSpringBasedHttpClientParameters.MESSAGE_CONVERTERS;
27 import static com.griddynamics.jagger.invoker.v2.SpringBasedHttpClient.JSpringBasedHttpClientParameters.REQUEST_FACTORY;
28 import static com.griddynamics.jagger.invoker.v2.SpringBasedHttpClient.JSpringBasedHttpClientParameters.URI_TEMPLATE_HANDLER;
29 import static java.lang.String.format;
30 
39 @SuppressWarnings({"unused", "unchecked"})
40 public class SpringBasedHttpClient implements JHttpClient {
41 
48  DEFAULT_URI_VARIABLES("defaultUriVariables"),
49  ERROR_HANDLER("errorHandler"),
50  MESSAGE_CONVERTERS("messageConverters"),
51  URI_TEMPLATE_HANDLER("uriTemplateHandler"),
52  INTERCEPTORS("interceptors"),
53  REQUEST_FACTORY("requestFactory");
54 
55  private String value;
56 
58  this.value = value;
59  }
60 
61  public String getValue() {
62  return value;
63  }
64  }
65 
78  private Map<String, Object> clientParams;
79 
80  private RestTemplate restTemplate;
81 
83  clientParams = new HashMap<>();
84  restTemplate = new RestTemplate();
85  restTemplate.setRequestFactory(getSslAllTrustRequestFactory());
86  }
87 
88  public SpringBasedHttpClient(Map<String, Object> clientParams) {
89  this();
90  this.clientParams.putAll(clientParams);
91  setRestTemplateParams(this.clientParams);
92  }
93 
94  @Override
95  public JHttpResponse execute(JHttpEndpoint endpoint, JHttpQuery query) {
96  if (query == null)
97  return execute(endpoint);
98 
99  URI endpointURI = endpoint.getURI(query.getPath(), query.getQueryParams());
100  RequestEntity requestEntity = mapToRequestEntity(query, endpointURI);
101  ResponseEntity responseEntity;
102  if (query.getResponseBodyType() != null) {
103  responseEntity = restTemplate.exchange(endpointURI, query.getMethod(), requestEntity, query.getResponseBodyType());
104  } else {
105  responseEntity = restTemplate.exchange(endpointURI, query.getMethod(), requestEntity, byte[].class);
106  }
107  return mapToJHttpResponse(responseEntity);
108  }
109 
110  public JHttpResponse execute(JHttpEndpoint endpoint) {
111  URI endpointURI = endpoint.getURI();
112  RequestEntity requestEntity = mapToRequestEntity(endpointURI);
113  ResponseEntity responseEntity = restTemplate.exchange(endpointURI, HttpMethod.GET, requestEntity, byte[].class);
114  return mapToJHttpResponse(responseEntity);
115  }
116 
117  private void setRestTemplateParams(Map<String, Object> clientParams) {
118  clientParams.forEach((parameterKey, parameterVal) -> {
119  if (parameterKey.equals(DEFAULT_URI_VARIABLES.value)) {
120  restTemplate.setDefaultUriVariables((Map<String, ?>) parameterVal);
121  } else if (parameterKey.equals(ERROR_HANDLER.value)) {
122  restTemplate.setErrorHandler((ResponseErrorHandler) parameterVal);
123  } else if (parameterKey.equals(MESSAGE_CONVERTERS.value)) {
124  restTemplate.setMessageConverters((List<HttpMessageConverter<?>>) parameterVal);
125  } else if (parameterKey.equals(URI_TEMPLATE_HANDLER.value)) {
126  restTemplate.setUriTemplateHandler((UriTemplateHandler) parameterVal);
127  } else if (parameterKey.equals(INTERCEPTORS.value)) {
128  restTemplate.setInterceptors((List<ClientHttpRequestInterceptor>) parameterVal);
129  } else if (parameterKey.equals(REQUEST_FACTORY.value)) {
130  restTemplate.setRequestFactory((ClientHttpRequestFactory) parameterVal);
131  } else {
132  throw new IllegalArgumentException(format("Unknown parameter name '%s'!", parameterKey));
133  }
134  });
135  }
136 
137  private HttpComponentsClientHttpRequestFactory getSslAllTrustRequestFactory() {
138  HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
139  CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
140  requestFactory.setHttpClient(httpClient);
141  return requestFactory;
142  }
143 
144  private <T> RequestEntity<T> mapToRequestEntity(JHttpQuery<T> query, URI endpointURI) {
145  return new RequestEntity<>(query.getBody(), query.getHeaders(), query.getMethod(), endpointURI);
146  }
147 
148  private <T> RequestEntity<T> mapToRequestEntity(URI endpointURI) {
149  return new RequestEntity<>(HttpMethod.GET, endpointURI);
150  }
151 
152  private <T> JHttpResponse<T> mapToJHttpResponse(ResponseEntity<T> responseEntity) {
153  return new JHttpResponse<>(responseEntity.getStatusCode(), responseEntity.getBody(), responseEntity.getHeaders());
154  }
155 
156  public Map<String, Object> getClientParams() {
157  return clientParams;
158  }
159 
160  public void setClientParams(Map<String, Object> clientParams) {
161  this.clientParams = clientParams;
162  }
163 }