Search in sources :

Example 1 with ApiRequestDto

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);
}
Also used : ApiRequestDto(org.openkilda.store.common.model.ApiRequestDto) HttpResponse(org.apache.http.HttpResponse)

Example 2 with ApiRequestDto

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) SSLContext(javax.net.ssl.SSLContext) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpPatch(org.apache.http.client.methods.HttpPatch) AuthPropertyService(org.openkilda.service.AuthPropertyService) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) Map(java.util.Map) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) URI(java.net.URI) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ExternalSystemException(org.openkilda.exception.ExternalSystemException) IAuthConstants(org.openkilda.constants.IAuthConstants) HttpHeaders(org.springframework.http.HttpHeaders) StringEntity(org.apache.http.entity.StringEntity) List(java.util.List) StringUtil(org.openkilda.utility.StringUtil) HttpGet(org.apache.http.client.methods.HttpGet) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) Entry(java.util.Map.Entry) HttpClients(org.apache.http.impl.client.HttpClients) RequestContext(org.openkilda.auth.model.RequestContext) RegistryBuilder(org.apache.http.config.RegistryBuilder) HashMap(java.util.HashMap) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RestCallFailedException(org.openkilda.exception.RestCallFailedException) ServerContext(org.openkilda.auth.context.ServerContext) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) HttpDelete(org.apache.http.client.methods.HttpDelete) ErrorMessage(org.openkilda.model.response.ErrorMessage) HttpClient(org.apache.http.client.HttpClient) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(org.springframework.http.HttpMethod) UnauthorizedException(org.openkilda.exception.UnauthorizedException) IOException(java.io.IOException) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) HttpError(org.openkilda.constants.HttpError) IoUtil(org.openkilda.utility.IoUtil) HttpStatus(org.springframework.http.HttpStatus) Component(org.springframework.stereotype.Component) HttpPut(org.apache.http.client.methods.HttpPut) ApiRequestDto(org.openkilda.store.common.model.ApiRequestDto) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HashMap(java.util.HashMap) HttpGet(org.apache.http.client.methods.HttpGet) RestCallFailedException(org.openkilda.exception.RestCallFailedException) HttpResponse(org.apache.http.HttpResponse) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HttpPut(org.apache.http.client.methods.HttpPut) HttpPatch(org.apache.http.client.methods.HttpPatch) ExternalSystemException(org.openkilda.exception.ExternalSystemException) RestCallFailedException(org.openkilda.exception.RestCallFailedException) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) UnauthorizedException(org.openkilda.exception.UnauthorizedException) IOException(java.io.IOException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) StringEntity(org.apache.http.entity.StringEntity) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) HttpMethod(org.springframework.http.HttpMethod)

Example 3 with ApiRequestDto

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);
}
Also used : ApiRequestDto(org.openkilda.store.common.model.ApiRequestDto) HttpResponse(org.apache.http.HttpResponse)

Example 4 with ApiRequestDto

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;
}
Also used : AuthenticationException(org.openkilda.exception.AuthenticationException) RestCallFailedException(org.openkilda.exception.RestCallFailedException) ApiRequestDto(org.openkilda.store.common.model.ApiRequestDto) HttpMethod(org.springframework.http.HttpMethod)

Aggregations

ApiRequestDto (org.openkilda.store.common.model.ApiRequestDto)4 HttpResponse (org.apache.http.HttpResponse)3 RestCallFailedException (org.openkilda.exception.RestCallFailedException)2 HttpMethod (org.springframework.http.HttpMethod)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 IOException (java.io.IOException)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 SSLContext (javax.net.ssl.SSLContext)1 HttpClient (org.apache.http.client.HttpClient)1 HttpDelete (org.apache.http.client.methods.HttpDelete)1 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpPatch (org.apache.http.client.methods.HttpPatch)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpPut (org.apache.http.client.methods.HttpPut)1