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);
}
}
}
Aggregations