Remove dependency on jersey for ProxyRequestHelper
This commit is contained in:
@@ -10,12 +10,12 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
|
||||
import org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter;
|
||||
import org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter;
|
||||
import org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter;
|
||||
import org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter;
|
||||
import org.springframework.cloud.netflix.zuul.filters.pre.Servlet30WrapperFilter;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.ProxyRequestHelper;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@@ -13,27 +13,29 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.netflix.zuul.filters.route;
|
||||
package org.springframework.cloud.netflix.zuul.filters;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.boot.actuate.trace.TraceRepository;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.util.HTTPRequestUtils;
|
||||
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -41,6 +43,12 @@ import com.sun.jersey.core.util.MultivaluedMapImpl;
|
||||
*/
|
||||
public class ProxyRequestHelper {
|
||||
|
||||
/**
|
||||
* Zuul context key for a collection of ignored headers for the current request.
|
||||
* Pre-filters can set this up as a set of lowercase strings.
|
||||
*/
|
||||
public static final String IGNORED_HEADERS = "ignoredHeaders";
|
||||
|
||||
public static final String CONTENT_ENCODING = "Content-Encoding";
|
||||
|
||||
private TraceRepository traces;
|
||||
@@ -49,12 +57,12 @@ public class ProxyRequestHelper {
|
||||
this.traces = traces;
|
||||
}
|
||||
|
||||
public MultivaluedMap<String, String> buildZuulRequestQueryParams(
|
||||
public MultiValueMap<String, String> buildZuulRequestQueryParams(
|
||||
HttpServletRequest request) {
|
||||
|
||||
Map<String, List<String>> map = HTTPRequestUtils.getInstance().getQueryParams();
|
||||
|
||||
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
|
||||
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
||||
if (map == null)
|
||||
return params;
|
||||
|
||||
@@ -67,34 +75,34 @@ public class ProxyRequestHelper {
|
||||
return params;
|
||||
}
|
||||
|
||||
public MultivaluedMap<String, String> buildZuulRequestHeaders(
|
||||
public MultiValueMap<String, String> buildZuulRequestHeaders(
|
||||
HttpServletRequest request) {
|
||||
|
||||
RequestContext context = RequestContext.getCurrentContext();
|
||||
|
||||
MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
|
||||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
||||
Enumeration<?> headerNames = request.getHeaderNames();
|
||||
if (headerNames != null) {
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String name = (String) headerNames.nextElement();
|
||||
String value = request.getHeader(name);
|
||||
if (isIncludedHeader(name))
|
||||
headers.putSingle(name, value);
|
||||
headers.set(name, value);
|
||||
}
|
||||
}
|
||||
Map<String, String> zuulRequestHeaders = context.getZuulRequestHeaders();
|
||||
|
||||
for (String header : zuulRequestHeaders.keySet()) {
|
||||
headers.putSingle(header, zuulRequestHeaders.get(header));
|
||||
headers.set(header, zuulRequestHeaders.get(header));
|
||||
}
|
||||
|
||||
headers.putSingle("accept-encoding", "deflate, gzip");
|
||||
headers.set("accept-encoding", "deflate, gzip");
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
public void setResponse(int status, InputStream entity,
|
||||
Map<String, Collection<String>> headers) throws IOException {
|
||||
MultiValueMap<String, String> headers) throws IOException {
|
||||
RequestContext context = RequestContext.getCurrentContext();
|
||||
|
||||
RequestContext.getCurrentContext().setResponseStatusCode(status);
|
||||
@@ -115,7 +123,7 @@ public class ProxyRequestHelper {
|
||||
}
|
||||
context.setResponseGZipped(isOriginResponseGzipped);
|
||||
|
||||
for (Entry<String, Collection<String>> header : headers.entrySet()) {
|
||||
for (Entry<String, List<String>> header : headers.entrySet()) {
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
String name = header.getKey();
|
||||
for (String value : header.getValue()) {
|
||||
@@ -132,8 +140,28 @@ public class ProxyRequestHelper {
|
||||
|
||||
}
|
||||
|
||||
public void addIgnoredHeaders(String... names) {
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
if (!ctx.containsKey(IGNORED_HEADERS)) {
|
||||
ctx.set(IGNORED_HEADERS, new HashSet<String>());
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<String> set = (Set<String>) ctx.get(IGNORED_HEADERS);
|
||||
for (String name : names) {
|
||||
set.add(name.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isIncludedHeader(String headerName) {
|
||||
switch (headerName.toLowerCase()) {
|
||||
String name = headerName.toLowerCase();
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
if (ctx.containsKey(IGNORED_HEADERS)) {
|
||||
Object object = ctx.get(IGNORED_HEADERS);
|
||||
if (object instanceof Collection && ((Collection<?>) object).contains(name)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
switch (name) {
|
||||
case "host":
|
||||
case "connection":
|
||||
case "content-length":
|
||||
@@ -147,9 +175,8 @@ public class ProxyRequestHelper {
|
||||
}
|
||||
|
||||
public Map<String, Object> debug(String verb, String uri,
|
||||
MultivaluedMap<String, String> headers,
|
||||
MultivaluedMap<String, String> params, InputStream requestEntity)
|
||||
throws IOException {
|
||||
MultiValueMap<String, String> headers, MultiValueMap<String, String> params,
|
||||
InputStream requestEntity) throws IOException {
|
||||
|
||||
Map<String, Object> info = new LinkedHashMap<String, Object>();
|
||||
if (traces != null) {
|
||||
@@ -195,14 +222,14 @@ public class ProxyRequestHelper {
|
||||
}
|
||||
|
||||
public void appendDebug(Map<String, Object> info, int status,
|
||||
Map<String, Collection<String>> headers) {
|
||||
MultiValueMap<String, String> headers) {
|
||||
if (traces != null) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> trace = (Map<String, Object>) info.get("headers");
|
||||
Map<String, Object> output = new LinkedHashMap<String, Object>();
|
||||
trace.put("response", output);
|
||||
info.put("status", "" + status);
|
||||
for (Entry<String, Collection<String>> key : headers.entrySet()) {
|
||||
for (Entry<String, List<String>> key : headers.entrySet()) {
|
||||
Collection<String> collection = key.getValue();
|
||||
Object value = collection;
|
||||
if (collection.size() < 2) {
|
||||
@@ -213,18 +240,6 @@ public class ProxyRequestHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public void appendDebug(Map<String, Object> info, int status,
|
||||
MultivaluedMap<String, String> headers) {
|
||||
if (traces != null) {
|
||||
Map<String, Collection<String>> map = new LinkedHashMap<String, Collection<String>>();
|
||||
for (Entry<String, List<String>> key : headers.entrySet()) {
|
||||
Collection<String> collection = key.getValue();
|
||||
map.put(key.getKey(), collection);
|
||||
}
|
||||
appendDebug(info, status, map);
|
||||
}
|
||||
}
|
||||
|
||||
private void debugRequestEntity(Map<String, Object> info, InputStream inputStream)
|
||||
throws IOException {
|
||||
String entity = IOUtils.toString(inputStream);
|
||||
@@ -2,8 +2,11 @@ package org.springframework.cloud.netflix.zuul.filters.route;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@@ -12,6 +15,9 @@ import javax.ws.rs.core.MultivaluedMap;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import com.netflix.client.ClientException;
|
||||
import com.netflix.client.http.HttpRequest.Verb;
|
||||
@@ -21,7 +27,7 @@ import com.netflix.niws.client.http.RestClient;
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.exception.ZuulException;
|
||||
import com.netflix.zuul.util.HTTPRequestUtils;
|
||||
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
||||
|
||||
public class RibbonRoutingFilter extends ZuulFilter {
|
||||
|
||||
@@ -63,8 +69,8 @@ public class RibbonRoutingFilter extends ZuulFilter {
|
||||
RequestContext context = RequestContext.getCurrentContext();
|
||||
HttpServletRequest request = context.getRequest();
|
||||
|
||||
MultivaluedMap<String, String> headers = helper.buildZuulRequestHeaders(request);
|
||||
MultivaluedMap<String, String> params = helper
|
||||
MultiValueMap<String, String> headers = helper.buildZuulRequestHeaders(request);
|
||||
MultiValueMap<String, String> params = helper
|
||||
.buildZuulRequestQueryParams(request);
|
||||
Verb verb = getVerb(request);
|
||||
InputStream requestEntity = getRequestBody(request);
|
||||
@@ -94,18 +100,18 @@ public class RibbonRoutingFilter extends ZuulFilter {
|
||||
}
|
||||
|
||||
private HttpResponse forward(RestClient restClient, Verb verb, String uri,
|
||||
MultivaluedMap<String, String> headers,
|
||||
MultivaluedMap<String, String> params, InputStream requestEntity)
|
||||
throws Exception {
|
||||
MultiValueMap<String, String> headers, MultiValueMap<String, String> params,
|
||||
InputStream requestEntity) throws Exception {
|
||||
|
||||
Map<String, Object> info = helper.debug(verb.verb(), uri, headers, params,
|
||||
requestEntity);
|
||||
|
||||
RibbonCommand command = new RibbonCommand(restClient, verb, uri, headers, params,
|
||||
requestEntity);
|
||||
RibbonCommand command = new RibbonCommand(restClient, verb, uri,
|
||||
convertHeaders(headers), convertHeaders(params), requestEntity);
|
||||
try {
|
||||
HttpResponse response = command.execute();
|
||||
helper.appendDebug(info, response.getStatus(), response.getHeaders());
|
||||
helper.appendDebug(info, response.getStatus(),
|
||||
revertHeaders(response.getHeaders()));
|
||||
return response;
|
||||
}
|
||||
catch (HystrixRuntimeException e) {
|
||||
@@ -124,6 +130,24 @@ public class RibbonRoutingFilter extends ZuulFilter {
|
||||
|
||||
}
|
||||
|
||||
private MultiValueMap<String, String> revertHeaders(
|
||||
Map<String, Collection<String>> headers) {
|
||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
|
||||
for (Entry<String, Collection<String>> entry : headers.entrySet()) {
|
||||
map.put(entry.getKey(), new ArrayList<String>(entry.getValue()));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private MultivaluedMap<String, String> convertHeaders(
|
||||
MultiValueMap<String, String> headers) {
|
||||
MultivaluedMap<String, String> map = new MultivaluedMapImpl();
|
||||
for (Entry<String, List<String>> entry : headers.entrySet()) {
|
||||
map.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private InputStream getRequestBody(HttpServletRequest request) {
|
||||
InputStream requestEntity = null;
|
||||
// ApacheHttpClient4Handler does not support body in delete requests
|
||||
@@ -168,7 +192,8 @@ public class RibbonRoutingFilter extends ZuulFilter {
|
||||
|
||||
private void setResponse(HttpResponse resp) throws ClientException, IOException {
|
||||
helper.setResponse(resp.getStatus(),
|
||||
!resp.hasEntity() ? null : resp.getInputStream(), resp.getHeaders());
|
||||
!resp.hasEntity() ? null : resp.getInputStream(),
|
||||
revertHeaders(resp.getHeaders()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.springframework.cloud.netflix.zuul.filters.route;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.Socket;
|
||||
@@ -21,16 +20,13 @@ import java.util.Map;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
@@ -55,15 +51,16 @@ import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.netflix.config.DynamicIntProperty;
|
||||
import com.netflix.config.DynamicPropertyFactory;
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.constants.ZuulConstants;
|
||||
import com.netflix.zuul.context.Debug;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.util.HTTPRequestUtils;
|
||||
|
||||
public class SimpleHostRoutingFilter extends ZuulFilter {
|
||||
|
||||
@@ -215,8 +212,8 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
|
||||
public Object run() {
|
||||
RequestContext context = RequestContext.getCurrentContext();
|
||||
HttpServletRequest request = context.getRequest();
|
||||
MultivaluedMap<String, String> headers = helper.buildZuulRequestHeaders(request);
|
||||
MultivaluedMap<String, String> params = helper
|
||||
MultiValueMap<String, String> headers = helper.buildZuulRequestHeaders(request);
|
||||
MultiValueMap<String, String> params = helper
|
||||
.buildZuulRequestQueryParams(request);
|
||||
String verb = getVerb(request);
|
||||
InputStream requestEntity = getRequestBody(request);
|
||||
@@ -240,8 +237,8 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
|
||||
}
|
||||
|
||||
private HttpResponse forward(HttpClient httpclient, String verb, String uri,
|
||||
HttpServletRequest request, MultivaluedMap<String, String> headers,
|
||||
MultivaluedMap<String, String> params, InputStream requestEntity)
|
||||
HttpServletRequest request, MultiValueMap<String, String> headers,
|
||||
MultiValueMap<String, String> params, InputStream requestEntity)
|
||||
throws Exception {
|
||||
|
||||
Map<String, Object> info = helper
|
||||
@@ -289,8 +286,8 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
|
||||
|
||||
}
|
||||
|
||||
private Map<String, Collection<String>> revertHeaders(Header[] headers) {
|
||||
Map<String, Collection<String>> map = new LinkedHashMap<String, Collection<String>>();
|
||||
private MultiValueMap<String, String> revertHeaders(Header[] headers) {
|
||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
|
||||
for (Header header : headers) {
|
||||
String name = header.getName();
|
||||
if (!map.containsKey(name)) {
|
||||
@@ -301,7 +298,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
|
||||
return map;
|
||||
}
|
||||
|
||||
private Header[] convertHeaders(MultivaluedMap<String, String> headers) {
|
||||
private Header[] convertHeaders(MultiValueMap<String, String> headers) {
|
||||
List<Header> list = new ArrayList<>();
|
||||
for (String name : headers.keySet()) {
|
||||
for (String value : headers.get(name)) {
|
||||
|
||||
Reference in New Issue
Block a user