add support for PATCH and TRACE methods

Although elasticsearch doesn't support these methods (RestController doesn't even allow to register handler for them), the RestClient should allow to send requests using them.
This commit is contained in:
javanna 2016-05-31 16:33:24 +02:00 committed by Luca Cavanna
parent 51e487fa55
commit 83c6e736de
1 changed files with 19 additions and 21 deletions

View File

@ -30,9 +30,11 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions; import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
@ -160,8 +162,7 @@ public final class RestClient implements Closeable {
lastSeenException = addSuppressedException(lastSeenException, e); lastSeenException = addSuppressedException(lastSeenException, e);
continue; continue;
} }
ElasticsearchResponse elasticsearchResponse = new ElasticsearchResponse(request.getRequestLine(), ElasticsearchResponse elasticsearchResponse = new ElasticsearchResponse(request.getRequestLine(), host, response);
host, response);
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
if (statusCode < 300 || (request.getMethod().equals(HttpHead.METHOD_NAME) && statusCode == 404) ) { if (statusCode < 300 || (request.getMethod().equals(HttpHead.METHOD_NAME) && statusCode == 404) ) {
RequestLogger.log(logger, "request succeeded", request, host, response); RequestLogger.log(logger, "request succeeded", request, host, response);
@ -288,40 +289,37 @@ public final class RestClient implements Closeable {
private static HttpRequestBase createHttpRequest(String method, URI uri, HttpEntity entity) { private static HttpRequestBase createHttpRequest(String method, URI uri, HttpEntity entity) {
switch(method.toUpperCase(Locale.ROOT)) { switch(method.toUpperCase(Locale.ROOT)) {
case HttpDeleteWithEntity.METHOD_NAME: case HttpDeleteWithEntity.METHOD_NAME:
HttpDeleteWithEntity httpDeleteWithEntity = new HttpDeleteWithEntity(uri); return addRequestBody(new HttpDeleteWithEntity(uri), entity);
addRequestBody(httpDeleteWithEntity, entity);
return httpDeleteWithEntity;
case HttpGetWithEntity.METHOD_NAME: case HttpGetWithEntity.METHOD_NAME:
HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity(uri); return addRequestBody(new HttpGetWithEntity(uri), entity);
addRequestBody(httpGetWithEntity, entity);
return httpGetWithEntity;
case HttpHead.METHOD_NAME: case HttpHead.METHOD_NAME:
if (entity != null) { return addRequestBody(new HttpHead(uri), entity);
throw new UnsupportedOperationException("HEAD with body is not supported");
}
return new HttpHead(uri);
case HttpOptions.METHOD_NAME: case HttpOptions.METHOD_NAME:
if (entity != null) { return addRequestBody(new HttpOptions(uri), entity);
throw new UnsupportedOperationException("OPTIONS with body is not supported"); case HttpPatch.METHOD_NAME:
} return addRequestBody(new HttpPatch(uri), entity);
return new HttpOptions(uri);
case HttpPost.METHOD_NAME: case HttpPost.METHOD_NAME:
HttpPost httpPost = new HttpPost(uri); HttpPost httpPost = new HttpPost(uri);
addRequestBody(httpPost, entity); addRequestBody(httpPost, entity);
return httpPost; return httpPost;
case HttpPut.METHOD_NAME: case HttpPut.METHOD_NAME:
HttpPut httpPut = new HttpPut(uri); return addRequestBody(new HttpPut(uri), entity);
addRequestBody(httpPut, entity); case HttpTrace.METHOD_NAME:
return httpPut; return addRequestBody(new HttpTrace(uri), entity);
default: default:
throw new UnsupportedOperationException("http method not supported: " + method); throw new UnsupportedOperationException("http method not supported: " + method);
} }
} }
private static void addRequestBody(HttpEntityEnclosingRequestBase httpRequest, HttpEntity entity) { private static HttpRequestBase addRequestBody(HttpRequestBase httpRequest, HttpEntity entity) {
if (entity != null) { if (entity != null) {
httpRequest.setEntity(entity); if (httpRequest instanceof HttpEntityEnclosingRequestBase) {
((HttpEntityEnclosingRequestBase)httpRequest).setEntity(entity);
} else {
throw new UnsupportedOperationException(httpRequest.getMethod() + " with body is not supported");
}
} }
return httpRequest;
} }
private static URI buildUri(String path, Map<String, String> params) { private static URI buildUri(String path, Map<String, String> params) {