Porting #1450 for Dalston release
This commit is contained in:
@@ -16,17 +16,16 @@
|
||||
|
||||
package org.springframework.cloud.netflix.zuul.filters.post;
|
||||
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import com.netflix.config.DynamicBooleanProperty;
|
||||
import com.netflix.config.DynamicIntProperty;
|
||||
import com.netflix.config.DynamicPropertyFactory;
|
||||
@@ -37,8 +36,6 @@ import com.netflix.zuul.constants.ZuulHeaders;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.util.HTTPRequestUtils;
|
||||
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@@ -56,6 +53,17 @@ public class SendResponseFilter extends ZuulFilter {
|
||||
private static DynamicBooleanProperty SET_CONTENT_LENGTH = DynamicPropertyFactory
|
||||
.getInstance()
|
||||
.getBooleanProperty(ZuulConstants.ZUUL_SET_CONTENT_LENGTH, false);
|
||||
private boolean useServlet31 = true;
|
||||
|
||||
public SendResponseFilter() {
|
||||
super();
|
||||
// To support Servlet API 3.0.1 we need to check if setcontentLengthLong exists
|
||||
try {
|
||||
HttpServletResponse.class.getMethod("setContentLengthLong");
|
||||
} catch(NoSuchMethodException e) {
|
||||
useServlet31 = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String filterType() {
|
||||
@@ -137,8 +145,8 @@ public class SendResponseFilter extends ZuulFilter {
|
||||
"gzip expected but not "
|
||||
+ "received assuming unencoded response "
|
||||
+ RequestContext.getCurrentContext()
|
||||
.getRequest().getRequestURL()
|
||||
.toString());
|
||||
.getRequest().getRequestURL()
|
||||
.toString());
|
||||
inputStream = is;
|
||||
}
|
||||
}
|
||||
@@ -210,10 +218,21 @@ public class SendResponseFilter extends ZuulFilter {
|
||||
// Only inserts Content-Length if origin provides it and origin response is not
|
||||
// gzipped
|
||||
if (SET_CONTENT_LENGTH.get()) {
|
||||
if (contentLength != null && !ctx.getResponseGZipped()) {
|
||||
servletResponse.setContentLengthLong(contentLength);
|
||||
if ( contentLength != null && !ctx.getResponseGZipped()) {
|
||||
if(useServlet31) {
|
||||
servletResponse.setContentLengthLong(contentLength);
|
||||
} else {
|
||||
//Try and set some kind of content length if we can safely convert the Long to an int
|
||||
if (isLongSafe(contentLength)) {
|
||||
servletResponse.setContentLength(contentLength.intValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private boolean isLongSafe(long value) {
|
||||
return value <= Integer.MAX_VALUE && value >= Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -170,7 +170,6 @@ public class FormBodyWrapperFilter extends ZuulFilter {
|
||||
return this.contentLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLengthLong() {
|
||||
return getContentLength();
|
||||
}
|
||||
|
||||
@@ -16,28 +16,25 @@
|
||||
|
||||
package org.springframework.cloud.netflix.zuul.filters.route;
|
||||
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import com.netflix.client.ClientException;
|
||||
import com.netflix.hystrix.exception.HystrixRuntimeException;
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.exception.ZuulException;
|
||||
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
@CommonsLog
|
||||
public class RibbonRoutingFilter extends ZuulFilter {
|
||||
|
||||
@@ -45,13 +42,20 @@ public class RibbonRoutingFilter extends ZuulFilter {
|
||||
protected ProxyRequestHelper helper;
|
||||
protected RibbonCommandFactory<?> ribbonCommandFactory;
|
||||
protected List<RibbonRequestCustomizer> requestCustomizers;
|
||||
private boolean useServlet31 = true;
|
||||
|
||||
public RibbonRoutingFilter(ProxyRequestHelper helper,
|
||||
RibbonCommandFactory<?> ribbonCommandFactory,
|
||||
List<RibbonRequestCustomizer> requestCustomizers) {
|
||||
RibbonCommandFactory<?> ribbonCommandFactory,
|
||||
List<RibbonRequestCustomizer> requestCustomizers) {
|
||||
this.helper = helper;
|
||||
this.ribbonCommandFactory = ribbonCommandFactory;
|
||||
this.requestCustomizers = requestCustomizers;
|
||||
// To support Servlet API 3.0.1 we need to check if getcontentLengthLong exists
|
||||
try {
|
||||
HttpServletResponse.class.getMethod("getContentLengthLong");
|
||||
} catch(NoSuchMethodException e) {
|
||||
useServlet31 = false;
|
||||
}
|
||||
}
|
||||
|
||||
public RibbonRoutingFilter(RibbonCommandFactory<?> ribbonCommandFactory) {
|
||||
@@ -119,8 +123,10 @@ public class RibbonRoutingFilter extends ZuulFilter {
|
||||
// remove double slashes
|
||||
uri = uri.replace("//", "/");
|
||||
|
||||
long contentLength = useServlet31 ? request.getContentLengthLong(): request.getContentLength();
|
||||
|
||||
return new RibbonCommandContext(serviceId, verb, uri, retryable, headers, params,
|
||||
requestEntity, this.requestCustomizers, request.getContentLengthLong());
|
||||
requestEntity, this.requestCustomizers, contentLength);
|
||||
}
|
||||
|
||||
protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user