1 package com.griddynamics.jagger.invoker.v2;
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;
18 import java.util.HashMap;
19 import java.util.List;
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;
39 @SuppressWarnings({
"unused",
"unchecked"})
78 private Map<String, Object> clientParams;
80 private RestTemplate restTemplate;
83 clientParams =
new HashMap<>();
84 restTemplate =
new RestTemplate();
85 restTemplate.setRequestFactory(getSslAllTrustRequestFactory());
90 this.clientParams.putAll(clientParams);
91 setRestTemplateParams(this.clientParams);
97 return execute(endpoint);
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());
105 responseEntity = restTemplate.exchange(endpointURI, query.getMethod(), requestEntity, byte[].class);
107 return mapToJHttpResponse(responseEntity);
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);
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);
132 throw new IllegalArgumentException(format(
"Unknown parameter name '%s'!", parameterKey));
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;
144 private <T> RequestEntity<T> mapToRequestEntity(JHttpQuery<T> query, URI endpointURI) {
145 return new RequestEntity<>(query.getBody(), query.getHeaders(), query.getMethod(), endpointURI);
148 private <T> RequestEntity<T> mapToRequestEntity(URI endpointURI) {
149 return new RequestEntity<>(HttpMethod.GET, endpointURI);
152 private <T> JHttpResponse<T> mapToJHttpResponse(ResponseEntity<T> responseEntity) {
153 return new JHttpResponse<>(responseEntity.getStatusCode(), responseEntity.getBody(), responseEntity.getHeaders());
161 this.clientParams = clientParams;