Unverified Commit acb12ea7 by Alena Committed by Spencer Gibb

Support DELETE method body for SimpleHostRoutingFilter

fixes gh-1329
parent e94c35d3
...@@ -63,6 +63,7 @@ import org.apache.http.impl.client.HttpClientBuilder; ...@@ -63,6 +63,7 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.message.BasicHttpRequest; import org.apache.http.message.BasicHttpRequest;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper; import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
...@@ -271,34 +272,13 @@ public class SimpleHostRoutingFilter extends ZuulFilter { ...@@ -271,34 +272,13 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
URL host = RequestContext.getCurrentContext().getRouteHost(); URL host = RequestContext.getCurrentContext().getRouteHost();
HttpHost httpHost = getHttpHost(host); HttpHost httpHost = getHttpHost(host);
uri = StringUtils.cleanPath((host.getPath() + uri).replaceAll("/{2,}", "/")); uri = StringUtils.cleanPath((host.getPath() + uri).replaceAll("/{2,}", "/"));
HttpRequest httpRequest;
int contentLength = request.getContentLength(); int contentLength = request.getContentLength();
InputStreamEntity entity = new InputStreamEntity(requestEntity, contentLength, InputStreamEntity entity = new InputStreamEntity(requestEntity, contentLength,
request.getContentType() != null request.getContentType() != null
? ContentType.create(request.getContentType()) : null); ? ContentType.create(request.getContentType()) : null);
switch (verb.toUpperCase()) {
case "POST": HttpRequest httpRequest = buildHttpRequest(verb, uri, entity, headers, params);
HttpPost httpPost = new HttpPost(uri + this.helper.getQueryString(params));
httpRequest = httpPost;
httpPost.setEntity(entity);
break;
case "PUT":
HttpPut httpPut = new HttpPut(uri + this.helper.getQueryString(params));
httpRequest = httpPut;
httpPut.setEntity(entity);
break;
case "PATCH":
HttpPatch httpPatch = new HttpPatch(uri + this.helper.getQueryString(params));
httpRequest = httpPatch;
httpPatch.setEntity(entity);
break;
default:
httpRequest = new BasicHttpRequest(verb,
uri + this.helper.getQueryString(params));
log.debug(uri + this.helper.getQueryString(params));
}
try { try {
httpRequest.setHeaders(convertHeaders(headers));
log.debug(httpHost.getHostName() + " " + httpHost.getPort() + " " log.debug(httpHost.getHostName() + " " + httpHost.getPort() + " "
+ httpHost.getSchemeName()); + httpHost.getSchemeName());
HttpResponse zuulResponse = forwardRequest(httpclient, httpHost, httpRequest); HttpResponse zuulResponse = forwardRequest(httpclient, httpHost, httpRequest);
...@@ -314,6 +294,43 @@ public class SimpleHostRoutingFilter extends ZuulFilter { ...@@ -314,6 +294,43 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
} }
} }
protected HttpRequest buildHttpRequest (String verb, String uri, InputStreamEntity entity,
MultiValueMap<String, String> headers, MultiValueMap<String, String> params)
{
HttpRequest httpRequest;
switch (verb.toUpperCase()) {
case "POST":
HttpPost httpPost = new HttpPost(uri + this.helper.getQueryString(params));
httpRequest = httpPost;
httpPost.setEntity(entity);
break;
case "PUT":
HttpPut httpPut = new HttpPut(uri + this.helper.getQueryString(params));
httpRequest = httpPut;
httpPut.setEntity(entity);
break;
case "PATCH":
HttpPatch httpPatch = new HttpPatch(uri + this.helper.getQueryString(params));
httpRequest = httpPatch;
httpPatch.setEntity(entity);
break;
case "DELETE":
BasicHttpEntityEnclosingRequest entityRequest = new BasicHttpEntityEnclosingRequest(verb,
uri + this.helper.getQueryString(params));
httpRequest = entityRequest;
entityRequest.setEntity(entity);
break;
default:
httpRequest = new BasicHttpRequest(verb,
uri + this.helper.getQueryString(params));
log.debug(uri + this.helper.getQueryString(params));
}
httpRequest.setHeaders(convertHeaders(headers));
return httpRequest;
}
private MultiValueMap<String, String> revertHeaders(Header[] headers) { private MultiValueMap<String, String> revertHeaders(Header[] headers) {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
for (Header header : headers) { for (Header header : headers) {
......
...@@ -16,6 +16,11 @@ ...@@ -16,6 +16,11 @@
package org.springframework.cloud.netflix.zuul.filters.route; package org.springframework.cloud.netflix.zuul.filters.route;
import java.io.ByteArrayInputStream;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
...@@ -26,6 +31,7 @@ import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; ...@@ -26,6 +31,7 @@ import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.LinkedMultiValueMap;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
...@@ -80,6 +86,18 @@ public class SimpleHostRoutingFilterTests { ...@@ -80,6 +86,18 @@ public class SimpleHostRoutingFilterTests {
assertEquals(20, connMgr.getDefaultMaxPerRoute()); assertEquals(20, connMgr.getDefaultMaxPerRoute());
} }
@Test
public void deleteRequestBuiltWithBody() {
setupContext();
InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1}));
HttpRequest httpRequest = getFilter().buildHttpRequest("DELETE", "uri", inputStreamEntity,
new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>());
assertTrue(httpRequest instanceof HttpEntityEnclosingRequest);
HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest;
assertTrue(httpEntityEnclosingRequest.getEntity() != null);
}
private void setupContext() { private void setupContext() {
this.context.register(PropertyPlaceholderAutoConfiguration.class, this.context.register(PropertyPlaceholderAutoConfiguration.class,
TestConfiguration.class); TestConfiguration.class);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment