use of org.openkilda.store.common.model.ApiRequestDto in project open-kilda by telstra.
the class OAuthService method refreshToken.
private Token refreshToken(final String url, final String refreshToken) throws AuthenticationException, RestCallFailedException {
String headers = HttpHeaders.CONTENT_TYPE + ":application/x-www-form-urlencoded";
String payload = "grant_type=refresh_token&refresh_token=" + refreshToken;
ApiRequestDto apiRequestDto = new ApiRequestDto(url, HttpMethod.POST, headers, payload);
HttpResponse response = restClientManager.invoke(apiRequestDto);
return restClientManager.getResponse(response, Token.class);
}
use of org.openkilda.store.common.model.ApiRequestDto 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.store.common.model.ApiRequestDto in project open-kilda by telstra.
the class OAuthService method generateToken.
private Token generateToken(final String correlationId, final String url, final String userName, final String password) throws AuthenticationException, RestCallFailedException {
String headers = HttpHeaders.CONTENT_TYPE + ":application/x-www-form-urlencoded";
String payload = "grant_type=password&username=" + userName + "&password=" + password;
ApiRequestDto apiRequestDto = new ApiRequestDto(url, HttpMethod.POST, headers, payload);
HttpResponse response = restClientManager.invoke(apiRequestDto);
return restClientManager.getResponse(response, Token.class);
}
use of org.openkilda.store.common.model.ApiRequestDto in project open-kilda by telstra.
the class OAuthService method getHttpResponse.
private HttpResponse getHttpResponse(UrlDto request, AuthConfigDto authDto) {
try {
String accessToken = getToken((OauthTwoConfigDto) authDto);
if (request.getHeader() != null) {
request.setHeader(request.getHeader() + "\nAuthorization:" + accessToken);
} else {
request.setHeader("Authorization:" + accessToken);
}
HttpMethod httpMethod = null;
if (("POST").equalsIgnoreCase(request.getMethodType())) {
httpMethod = HttpMethod.POST;
} else if (("PUT").equalsIgnoreCase(request.getMethodType())) {
httpMethod = HttpMethod.PUT;
} else if (("DELETE").equalsIgnoreCase(request.getMethodType())) {
httpMethod = HttpMethod.DELETE;
} else {
httpMethod = HttpMethod.GET;
}
ApiRequestDto apiRequestDto = new ApiRequestDto(request.getUrl(), httpMethod, request.getHeader(), request.getBody());
if (request.getParams() != null) {
prepareRequest.preprocessApiRequest(apiRequestDto, request.getParams());
}
return restClientManager.invoke(apiRequestDto);
} catch (RestCallFailedException | AuthenticationException e) {
e.printStackTrace();
}
return null;
}
Aggregations