Merge pull request #1339 from alenavorozhbieva/master
* pull1339: Support DELETE method body for SimpleHostRoutingFilter
This commit is contained in:
@@ -63,6 +63,7 @@ import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
|
||||
import org.apache.http.message.BasicHttpRequest;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
|
||||
@@ -271,34 +272,13 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
|
||||
URL host = RequestContext.getCurrentContext().getRouteHost();
|
||||
HttpHost httpHost = getHttpHost(host);
|
||||
uri = StringUtils.cleanPath((host.getPath() + uri).replaceAll("/{2,}", "/"));
|
||||
HttpRequest httpRequest;
|
||||
int contentLength = request.getContentLength();
|
||||
InputStreamEntity entity = new InputStreamEntity(requestEntity, contentLength,
|
||||
request.getContentType() != null
|
||||
? ContentType.create(request.getContentType()) : null);
|
||||
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;
|
||||
default:
|
||||
httpRequest = new BasicHttpRequest(verb,
|
||||
uri + this.helper.getQueryString(params));
|
||||
log.debug(uri + this.helper.getQueryString(params));
|
||||
}
|
||||
|
||||
HttpRequest httpRequest = buildHttpRequest(verb, uri, entity, headers, params);
|
||||
try {
|
||||
httpRequest.setHeaders(convertHeaders(headers));
|
||||
log.debug(httpHost.getHostName() + " " + httpHost.getPort() + " "
|
||||
+ httpHost.getSchemeName());
|
||||
HttpResponse zuulResponse = forwardRequest(httpclient, httpHost, httpRequest);
|
||||
@@ -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) {
|
||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
|
||||
for (Header header : headers) {
|
||||
|
||||
@@ -16,6 +16,11 @@
|
||||
|
||||
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.junit.After;
|
||||
import org.junit.Test;
|
||||
@@ -26,6 +31,7 @@ import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -80,6 +86,18 @@ public class SimpleHostRoutingFilterTests {
|
||||
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() {
|
||||
this.context.register(PropertyPlaceholderAutoConfiguration.class,
|
||||
TestConfiguration.class);
|
||||
|
||||
Reference in New Issue
Block a user