1 package org.openkilda.helper;
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.http.HttpHeaders;
8 import org.springframework.http.HttpMethod;
9 import org.springframework.http.HttpStatus;
10 import org.springframework.stereotype.Component;
12 import com.fasterxml.jackson.databind.ObjectMapper;
13 import com.fasterxml.jackson.databind.type.TypeFactory;
15 import java.io.IOException;
16 import java.util.List;
18 import org.apache.http.HttpResponse;
19 import org.apache.http.client.HttpClient;
20 import org.apache.http.client.methods.HttpDelete;
21 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
22 import org.apache.http.client.methods.HttpGet;
23 import org.apache.http.client.methods.HttpPatch;
24 import org.apache.http.client.methods.HttpPost;
25 import org.apache.http.client.methods.HttpPut;
26 import org.apache.http.client.methods.HttpUriRequest;
27 import org.apache.http.entity.StringEntity;
28 import org.apache.http.impl.client.HttpClients;
46 private static final Logger LOGGER = LoggerFactory.getLogger(
RestClientManager.class);
52 private ObjectMapper mapper;
65 public HttpResponse
invoke(
final String apiUrl,
final HttpMethod httpMethod,
66 final String payload,
final String contentType,
final String basicAuth) {
67 LOGGER.info(
"[invoke] - Start");
69 HttpResponse httpResponse = null;
72 HttpClient client = HttpClients.createDefault();
73 HttpUriRequest httpUriRequest = null;
74 HttpEntityEnclosingRequestBase httpEntityEnclosingRequest = null;
77 if (HttpMethod.POST.equals(httpMethod)) {
78 httpEntityEnclosingRequest =
new HttpPost(apiUrl);
79 }
else if (HttpMethod.PUT.equals(httpMethod)) {
80 httpEntityEnclosingRequest =
new HttpPut(apiUrl);
81 }
else if (HttpMethod.DELETE.equals(httpMethod)) {
82 httpUriRequest =
new HttpDelete(apiUrl);
83 }
else if (HttpMethod.PATCH.equals(httpMethod)) {
84 httpUriRequest =
new HttpPatch(apiUrl);
86 httpUriRequest =
new HttpGet(apiUrl);
89 if (!HttpMethod.POST.equals(httpMethod) && !HttpMethod.PUT.equals(httpMethod)) {
92 LOGGER.debug(
"[invoke] Setting authorization in header as " 98 if (HttpMethod.POST.equals(httpMethod) || HttpMethod.PUT.equals(httpMethod)) {
99 LOGGER.info(
"[invoke] Executing POST/ PUT request : httpEntityEnclosingRequest : " 100 + httpEntityEnclosingRequest +
" : payload : " + payload);
102 httpEntityEnclosingRequest.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
105 httpEntityEnclosingRequest.setEntity(
new StringEntity(payload));
106 httpResponse = client.execute(httpEntityEnclosingRequest);
107 LOGGER.debug(
"[invoke] Call executed successfully");
109 LOGGER.info(
"[invoke] Executing : httpUriRequest : " + httpUriRequest);
110 httpResponse = client.execute(httpUriRequest);
111 LOGGER.info(
"[invoke] Call executed successfully");
114 }
catch (Exception e) {
115 LOGGER.error(
"[invoke] Exception: ", e);
118 LOGGER.info(
"[invoke] - End");
130 @SuppressWarnings(
"unchecked")
131 public <T> List<T> getResponseList(final HttpResponse response, final Class<T> responseClass) {
132 return getResponse(response, List.class, responseClass);
143 public <T> T getResponse(
final HttpResponse response,
final Class<T> responseClass) {
144 return getResponse(response, responseClass, null);
157 private <T, E> T getResponse(
final HttpResponse response,
final Class<T> responseClass,
158 final Class<E> dependentClass) {
161 LOGGER.info(
"[getResponse] : StatusCode " + response.getStatusLine().getStatusCode());
163 if (response.getStatusLine().getStatusCode() != HttpStatus.NO_CONTENT.value()) {
164 String responseEntity = IoUtil.toString(response.getEntity().getContent());
166 LOGGER.debug(
"[getResponse] : response object " + responseEntity);
167 if (!(HttpStatus.valueOf(response.getStatusLine().getStatusCode())
168 .is2xxSuccessful() && response.getEntity() != null)) {
169 String errorMessage = null;
171 if (responseEntity.startsWith(
"[")) {
172 responseEntity = responseEntity.replaceFirst(
"]",
"").trim();
174 if (responseEntity.endsWith(
"]")) {
175 responseEntity = responseEntity.replace(
"]",
"").trim();
179 mapper.readValue(responseEntity, ErrorMessage.class).getMessage();
181 }
catch (Exception e) {
182 if (response.getStatusLine().getStatusCode() == HttpStatus.UNAUTHORIZED
184 throw new UnauthorizedException(HttpError.UNAUTHORIZED.getMessage());
187 LOGGER.error(
"[getResponse] Exception :", e);
189 authPropertyService.getError(
190 IAuthConstants.Code.RESPONSE_PARSING_FAIL_ERROR)
192 throw new RestCallFailedException(errorMessage);
195 LOGGER.error(
"[getResponse] Exception : " + responseEntity);
196 throw new ExternalSystemException(response.getStatusLine().getStatusCode(),
200 if (dependentClass == null) {
201 obj = mapper.readValue(responseEntity, responseClass);
204 mapper.readValue(responseEntity, TypeFactory.defaultInstance()
205 .constructCollectionLikeType(responseClass, dependentClass));
209 }
catch (IOException e) {
210 throw new RestCallFailedException(e.getMessage());
222 LOGGER.debug(
"[isValidResponse] Response Code " + response.getStatusLine().getStatusCode());
223 boolean isValid = response.getStatusLine().getStatusCode() >= HttpStatus.OK.value()
224 && response.getStatusLine().getStatusCode() < HttpStatus.MULTIPLE_CHOICES.value()
225 && response.getEntity() != null;
230 String content =
IoUtil.
toString(response.getEntity().getContent());
231 LOGGER.error(
"[getResponse] Invalid Response. Status Code: " 232 + response.getStatusLine().getStatusCode() +
", content: " + content);
235 }
catch (IOException exception) {
236 LOGGER.error(
"[getResponse] Exception :" + exception.getMessage(), exception);
static boolean isValidResponse(final HttpResponse response)
static String toString(final InputStream inputStream)
static boolean isNullOrEmpty(final String data)
HttpResponse invoke(final String apiUrl, final HttpMethod httpMethod, final String payload, final String contentType, final String basicAuth)