Search in sources :

Example 1 with HttpPatch

use of org.apache.olingo.client.core.http.HttpPatch in project tdi-studio-se by Talend.

the class DynamicsCRMClient method createAndExecuteRequest.

/**
     * Created and executes a request
     * 
     * @param uri the request URI
     * @param httpEntity the entity to send.
     * @param method HTTP method
     * 
     * @return the response to the request.
     * @throws ServiceUnavailableException
     */
protected HttpResponse createAndExecuteRequest(URI uri, HttpEntity httpEntity, HttpMethod method) throws ServiceUnavailableException {
    boolean hasRetried = false;
    while (true) {
        try {
            httpClient = (DefaultHttpClient) httpClientFactory.create(null, null);
            HttpRequestBase request = null;
            if (method == HttpMethod.POST) {
                request = new HttpPost(uri);
            } else if (method == HttpMethod.PATCH) {
                request = new HttpPatch(uri);
            } else if (method == HttpMethod.DELETE) {
                request = new HttpDelete(uri);
            } else {
                throw new HttpClientException("Unsupported operation:" + method);
            }
            request.addHeader(HttpHeader.AUTHORIZATION, "Bearer " + authResult.getAccessToken());
            if (request instanceof HttpEntityEnclosingRequestBase) {
                ((HttpEntityEnclosingRequestBase) request).setEntity(httpEntity);
            }
            HttpResponse response = httpClient.execute(request);
            if (isResponseSuccess(response.getStatusLine().getStatusCode())) {
                request.releaseConnection();
                EntityUtils.consume(response.getEntity());
                return response;
            } else {
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED && !hasRetried) {
                    refreshToken();
                    hasRetried = true;
                    continue;
                }
                HttpEntity entity = response.getEntity();
                String message = null;
                if (entity != null) {
                    message = odataClient.getDeserializer(ContentType.JSON).toError(entity.getContent()).getMessage();
                } else {
                    message = response.getStatusLine().getReasonPhrase();
                }
                throw new HttpClientException(message);
            }
        } catch (Exception e) {
            throw new HttpClientException(e);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpClientException(org.apache.olingo.client.api.http.HttpClientException) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) HttpPatch(org.apache.olingo.client.core.http.HttpPatch) URISyntaxException(java.net.URISyntaxException) ServiceUnavailableException(javax.naming.ServiceUnavailableException) ODataClientErrorException(org.apache.olingo.client.api.communication.ODataClientErrorException) HttpClientException(org.apache.olingo.client.api.http.HttpClientException)

Aggregations

URISyntaxException (java.net.URISyntaxException)1 ServiceUnavailableException (javax.naming.ServiceUnavailableException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpDelete (org.apache.http.client.methods.HttpDelete)1 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 ODataClientErrorException (org.apache.olingo.client.api.communication.ODataClientErrorException)1 HttpClientException (org.apache.olingo.client.api.http.HttpClientException)1 HttpPatch (org.apache.olingo.client.core.http.HttpPatch)1