|
|
@ -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,11 +272,33 @@ 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); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpRequest httpRequest = buildHttpRequest(verb, uri, entity, headers, params); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
log.debug(httpHost.getHostName() + " " + httpHost.getPort() + " " |
|
|
|
|
|
|
|
+ httpHost.getSchemeName()); |
|
|
|
|
|
|
|
HttpResponse zuulResponse = forwardRequest(httpclient, httpHost, httpRequest); |
|
|
|
|
|
|
|
this.helper.appendDebug(info, zuulResponse.getStatusLine().getStatusCode(), |
|
|
|
|
|
|
|
revertHeaders(zuulResponse.getAllHeaders())); |
|
|
|
|
|
|
|
return zuulResponse; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
finally { |
|
|
|
|
|
|
|
// When HttpClient instance is no longer needed,
|
|
|
|
|
|
|
|
// shut down the connection manager to ensure
|
|
|
|
|
|
|
|
// immediate deallocation of all system resources
|
|
|
|
|
|
|
|
// httpclient.getConnectionManager().shutdown();
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected HttpRequest buildHttpRequest (String verb, String uri, InputStreamEntity entity, |
|
|
|
|
|
|
|
MultiValueMap<String, String> headers, MultiValueMap<String, String> params) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
HttpRequest httpRequest; |
|
|
|
|
|
|
|
|
|
|
|
switch (verb.toUpperCase()) { |
|
|
|
switch (verb.toUpperCase()) { |
|
|
|
case "POST": |
|
|
|
case "POST": |
|
|
|
HttpPost httpPost = new HttpPost(uri + this.helper.getQueryString(params)); |
|
|
|
HttpPost httpPost = new HttpPost(uri + this.helper.getQueryString(params)); |
|
|
@ -292,26 +315,20 @@ public class SimpleHostRoutingFilter extends ZuulFilter { |
|
|
|
httpRequest = httpPatch; |
|
|
|
httpRequest = httpPatch; |
|
|
|
httpPatch.setEntity(entity); |
|
|
|
httpPatch.setEntity(entity); |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
case "DELETE": |
|
|
|
|
|
|
|
BasicHttpEntityEnclosingRequest entityRequest = new BasicHttpEntityEnclosingRequest(verb, |
|
|
|
|
|
|
|
uri + this.helper.getQueryString(params)); |
|
|
|
|
|
|
|
httpRequest = entityRequest; |
|
|
|
|
|
|
|
entityRequest.setEntity(entity); |
|
|
|
|
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
httpRequest = new BasicHttpRequest(verb, |
|
|
|
httpRequest = new BasicHttpRequest(verb, |
|
|
|
uri + this.helper.getQueryString(params)); |
|
|
|
uri + this.helper.getQueryString(params)); |
|
|
|
log.debug(uri + this.helper.getQueryString(params)); |
|
|
|
log.debug(uri + this.helper.getQueryString(params)); |
|
|
|
} |
|
|
|
} |
|
|
|
try { |
|
|
|
|
|
|
|
httpRequest.setHeaders(convertHeaders(headers)); |
|
|
|
httpRequest.setHeaders(convertHeaders(headers)); |
|
|
|
log.debug(httpHost.getHostName() + " " + httpHost.getPort() + " " |
|
|
|
return httpRequest; |
|
|
|
+ httpHost.getSchemeName()); |
|
|
|
|
|
|
|
HttpResponse zuulResponse = forwardRequest(httpclient, httpHost, httpRequest); |
|
|
|
|
|
|
|
this.helper.appendDebug(info, zuulResponse.getStatusLine().getStatusCode(), |
|
|
|
|
|
|
|
revertHeaders(zuulResponse.getAllHeaders())); |
|
|
|
|
|
|
|
return zuulResponse; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
finally { |
|
|
|
|
|
|
|
// When HttpClient instance is no longer needed,
|
|
|
|
|
|
|
|
// shut down the connection manager to ensure
|
|
|
|
|
|
|
|
// immediate deallocation of all system resources
|
|
|
|
|
|
|
|
// httpclient.getConnectionManager().shutdown();
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private MultiValueMap<String, String> revertHeaders(Header[] headers) { |
|
|
|
private MultiValueMap<String, String> revertHeaders(Header[] headers) { |
|
|
|