use of org.openkilda.exception.RestCallFailedException in project open-kilda by telstra.
the class OAuthService method getResponse.
@Override
public <T> T getResponse(UrlDto request, AuthConfigDto authDto, Class<T> responseClass) {
T obj = null;
try {
HttpResponse response = getHttpResponse(request, authDto);
obj = restClientManager.getResponse(response, responseClass);
} catch (RestCallFailedException e) {
e.printStackTrace();
}
return obj;
}
use of org.openkilda.exception.RestCallFailedException in project open-kilda by telstra.
the class RestClientManager method getResponse.
/**
* Gets the response.
*
* @param <T> the generic type
* @param <E> the element type
* @param response the response
* @param responseClass the response class
* @param dependentClass the dependent class
* @return the response
*/
private <T, E> T getResponse(final HttpResponse response, final Class<T> responseClass, final Class<E> dependentClass) {
T obj = null;
try {
LOGGER.info("[getResponse] : StatusCode " + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() != HttpStatus.NO_CONTENT.value()) {
String responseEntity = IoUtil.toString(response.getEntity().getContent());
LOGGER.debug("[getResponse] : response object " + responseEntity);
if (!(HttpStatus.valueOf(response.getStatusLine().getStatusCode()).is2xxSuccessful() && response.getEntity() != null)) {
String errorMessage = null;
try {
if (responseEntity.startsWith("[")) {
responseEntity = responseEntity.replaceFirst("]", "").trim();
}
if (responseEntity.endsWith("]")) {
responseEntity = responseEntity.replace("]", "").trim();
}
errorMessage = mapper.readValue(responseEntity, ErrorMessage.class).getMessage();
} catch (Exception e) {
if (response.getStatusLine().getStatusCode() == HttpStatus.UNAUTHORIZED.value()) {
throw new UnauthorizedException(HttpError.UNAUTHORIZED.getMessage());
}
LOGGER.error("Error occurred while retriving response from third party service provider", e);
errorMessage = authPropertyService.getError(IAuthConstants.Code.RESPONSE_PARSING_FAIL_ERROR).getMessage();
throw new RestCallFailedException(errorMessage);
}
LOGGER.error("Error occurred while retriving response from third party service provider:" + responseEntity);
throw new ExternalSystemException(response.getStatusLine().getStatusCode(), errorMessage);
} else {
if (dependentClass == null) {
if (responseClass != null) {
obj = mapper.readValue(responseEntity, responseClass);
}
} else {
obj = mapper.readValue(responseEntity, TypeFactory.defaultInstance().constructCollectionLikeType(responseClass, dependentClass));
}
}
}
} catch (IOException e) {
throw new RestCallFailedException(e.getMessage());
}
return obj;
}
use of org.openkilda.exception.RestCallFailedException in project open-kilda by telstra.
the class RestClientManager method invoke.
/**
* Invoke.
*
* @param apiUrl the api url
* @param httpMethod the http method
* @param payload the payload
* @param contentType the content type
* @param basicAuth the basic auth
* @return the http response
*/
public HttpResponse invoke(final String apiUrl, final HttpMethod httpMethod, final String payload, final String contentType, final String basicAuth) {
HttpResponse httpResponse = null;
try {
RequestContext requestContext = serverContext.getRequestContext();
HttpClient client = HttpClients.createDefault();
HttpUriRequest httpUriRequest = null;
HttpEntityEnclosingRequestBase httpEntityEnclosingRequest = null;
// Initializing Request
if (HttpMethod.POST.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpPost(apiUrl);
} else if (HttpMethod.PUT.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpPut(apiUrl);
} else if (HttpMethod.DELETE.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpEntityEnclosingRequestBase() {
@Override
public String getMethod() {
return "DELETE";
}
};
} else if (HttpMethod.PATCH.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpPatch(apiUrl);
} else {
httpUriRequest = new HttpGet(apiUrl);
}
if (!HttpMethod.POST.equals(httpMethod) && !HttpMethod.PUT.equals(httpMethod) && !HttpMethod.PATCH.equals(httpMethod) && !HttpMethod.DELETE.equals(httpMethod)) {
// Setting Required Headers
if (!StringUtil.isNullOrEmpty(basicAuth)) {
LOGGER.debug("[invoke] Setting authorization in header as " + IAuthConstants.Header.AUTHORIZATION);
httpUriRequest.setHeader(IAuthConstants.Header.AUTHORIZATION, basicAuth);
httpUriRequest.setHeader(IAuthConstants.Header.CORRELATION_ID, requestContext.getCorrelationId());
}
}
if (HttpMethod.POST.equals(httpMethod) || HttpMethod.PUT.equals(httpMethod) || HttpMethod.PATCH.equals(httpMethod)) {
LOGGER.info("[invoke] Executing POST/ PUT request : httpEntityEnclosingRequest : " + httpEntityEnclosingRequest + " : payload : " + payload);
// Setting POST/PUT related headers
httpEntityEnclosingRequest.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
httpEntityEnclosingRequest.setHeader(IAuthConstants.Header.AUTHORIZATION, basicAuth);
httpEntityEnclosingRequest.setHeader(IAuthConstants.Header.CORRELATION_ID, requestContext.getCorrelationId());
// Setting request payload
httpEntityEnclosingRequest.setEntity(new StringEntity(payload));
httpResponse = client.execute(httpEntityEnclosingRequest);
LOGGER.debug("[invoke] Call executed successfully");
} else if (HttpMethod.DELETE.equals(httpMethod)) {
httpEntityEnclosingRequest.setURI(URI.create(apiUrl));
LOGGER.info("[invoke] Executing DELETE request : httpDeleteRequest : " + httpEntityEnclosingRequest + " : payload : " + payload);
// Setting DELETE related headers
httpEntityEnclosingRequest.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
httpEntityEnclosingRequest.setHeader(IAuthConstants.Header.EXTRA_AUTH, String.valueOf(System.currentTimeMillis()));
httpEntityEnclosingRequest.setHeader(IAuthConstants.Header.AUTHORIZATION, basicAuth);
httpEntityEnclosingRequest.setHeader(IAuthConstants.Header.CORRELATION_ID, requestContext.getCorrelationId());
// Setting request payload
httpEntityEnclosingRequest.setEntity(new StringEntity(payload));
httpResponse = client.execute(httpEntityEnclosingRequest);
LOGGER.debug("[invoke] Call executed successfully");
} else {
LOGGER.info("[invoke] Executing : httpUriRequest : " + httpUriRequest);
httpResponse = client.execute(httpUriRequest);
LOGGER.info("[invoke] Call executed successfully");
}
} catch (Exception e) {
LOGGER.error("Error occurred while trying to communicate third party service provider", e);
throw new RestCallFailedException(e);
}
return httpResponse;
}
use of org.openkilda.exception.RestCallFailedException in project open-kilda by telstra.
the class RestClientManager method invoke.
/**
* Invoke.
*
* @param apiRequestDto the api request dto
* @return the http response
*/
public HttpResponse invoke(final ApiRequestDto apiRequestDto) {
HttpResponse httpResponse = null;
String url = apiRequestDto.getUrl();
String headers = apiRequestDto.getHeader();
HttpMethod httpMethod = apiRequestDto.getHttpMethod();
String payload = apiRequestDto.getPayload();
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (x509CertChain, authType) -> true).build();
CloseableHttpClient client = HttpClientBuilder.create().setSSLContext(sslContext).setConnectionManager(new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)).build())).build();
HttpUriRequest httpUriRequest = null;
HttpEntityEnclosingRequestBase httpEntityEnclosingRequest = null;
// Initializing Request
if (HttpMethod.POST.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpPost(url);
} else if (HttpMethod.PUT.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpPut(url);
} else if (HttpMethod.DELETE.equals(httpMethod)) {
httpUriRequest = new HttpDelete(url);
} else if (HttpMethod.PATCH.equals(httpMethod)) {
httpUriRequest = new HttpPatch(url);
} else {
httpUriRequest = new HttpGet(url);
}
Map<String, String> headersMap = new HashMap<String, String>();
if (!HttpMethod.POST.equals(httpMethod) && !HttpMethod.PUT.equals(httpMethod)) {
if (!StringUtil.isNullOrEmpty(headers)) {
for (String header : headers.split("\n")) {
getHeaders(headersMap, header);
for (Entry<String, String> headerEntrySet : headersMap.entrySet()) {
httpUriRequest.setHeader(headerEntrySet.getKey(), headerEntrySet.getValue());
}
}
}
}
if (HttpMethod.POST.equals(httpMethod) || HttpMethod.PUT.equals(httpMethod)) {
LOGGER.info("[invoke] Executing POST/ PUT request : httpEntityEnclosingRequest : " + httpEntityEnclosingRequest);
if (!StringUtil.isNullOrEmpty(headers)) {
for (String header : headers.split("\n")) {
getHeaders(headersMap, header);
for (Entry<String, String> headerEntrySet : headersMap.entrySet()) {
httpEntityEnclosingRequest.setHeader(headerEntrySet.getKey(), headerEntrySet.getValue());
}
}
}
// Setting request payload
httpEntityEnclosingRequest.setEntity(new StringEntity(payload));
httpResponse = client.execute(httpEntityEnclosingRequest);
LOGGER.debug("[invoke] Call executed successfully");
} else {
LOGGER.info("[invoke] Executing : httpUriRequest : " + httpUriRequest);
httpResponse = client.execute(httpUriRequest);
LOGGER.info("[invoke] Call executed successfully");
}
} catch (Exception e) {
LOGGER.error("Error occurred while trying to communicate third party service provider", e);
throw new RestCallFailedException(e);
}
return httpResponse;
}
use of org.openkilda.exception.RestCallFailedException in project open-kilda by telstra.
the class OAuthService method getResponseList.
@Override
public <T> List<T> getResponseList(UrlDto request, AuthConfigDto authDto, Class<T> responseClass) {
List<T> obj = null;
try {
HttpResponse response = getHttpResponse(request, authDto);
obj = restClientManager.getResponseList(response, responseClass);
} catch (RestCallFailedException e) {
e.printStackTrace();
}
return obj;
}
Aggregations