Commit 2e46bdf9 by Ryan Baxter

Porting #1450 for Dalston release

parent 18b9c46a
...@@ -16,17 +16,16 @@ ...@@ -16,17 +16,16 @@
package org.springframework.cloud.netflix.zuul.filters.post; package org.springframework.cloud.netflix.zuul.filters.post;
import lombok.extern.apachecommons.CommonsLog;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.List; import java.util.List;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import com.netflix.config.DynamicBooleanProperty; import com.netflix.config.DynamicBooleanProperty;
import com.netflix.config.DynamicIntProperty; import com.netflix.config.DynamicIntProperty;
import com.netflix.config.DynamicPropertyFactory; import com.netflix.config.DynamicPropertyFactory;
...@@ -37,8 +36,6 @@ import com.netflix.zuul.constants.ZuulHeaders; ...@@ -37,8 +36,6 @@ import com.netflix.zuul.constants.ZuulHeaders;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.util.HTTPRequestUtils; import com.netflix.zuul.util.HTTPRequestUtils;
import lombok.extern.apachecommons.CommonsLog;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
*/ */
...@@ -56,6 +53,17 @@ public class SendResponseFilter extends ZuulFilter { ...@@ -56,6 +53,17 @@ public class SendResponseFilter extends ZuulFilter {
private static DynamicBooleanProperty SET_CONTENT_LENGTH = DynamicPropertyFactory private static DynamicBooleanProperty SET_CONTENT_LENGTH = DynamicPropertyFactory
.getInstance() .getInstance()
.getBooleanProperty(ZuulConstants.ZUUL_SET_CONTENT_LENGTH, false); .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 @Override
public String filterType() { public String filterType() {
...@@ -210,10 +218,21 @@ public class SendResponseFilter extends ZuulFilter { ...@@ -210,10 +218,21 @@ public class SendResponseFilter extends ZuulFilter {
// Only inserts Content-Length if origin provides it and origin response is not // Only inserts Content-Length if origin provides it and origin response is not
// gzipped // gzipped
if (SET_CONTENT_LENGTH.get()) { if (SET_CONTENT_LENGTH.get()) {
if (contentLength != null && !ctx.getResponseGZipped()) { if ( contentLength != null && !ctx.getResponseGZipped()) {
if(useServlet31) {
servletResponse.setContentLengthLong(contentLength); 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;
}
} }
\ No newline at end of file
...@@ -170,7 +170,6 @@ public class FormBodyWrapperFilter extends ZuulFilter { ...@@ -170,7 +170,6 @@ public class FormBodyWrapperFilter extends ZuulFilter {
return this.contentLength; return this.contentLength;
} }
@Override
public long getContentLengthLong() { public long getContentLengthLong() {
return getContentLength(); return getContentLength();
} }
......
...@@ -16,28 +16,25 @@ ...@@ -16,28 +16,25 @@
package org.springframework.cloud.netflix.zuul.filters.route; package org.springframework.cloud.netflix.zuul.filters.route;
import lombok.extern.apachecommons.CommonsLog;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer; import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper; import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import com.netflix.client.ClientException; import com.netflix.client.ClientException;
import com.netflix.hystrix.exception.HystrixRuntimeException; import com.netflix.hystrix.exception.HystrixRuntimeException;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException; import com.netflix.zuul.exception.ZuulException;
import lombok.extern.apachecommons.CommonsLog;
@CommonsLog @CommonsLog
public class RibbonRoutingFilter extends ZuulFilter { public class RibbonRoutingFilter extends ZuulFilter {
...@@ -45,6 +42,7 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -45,6 +42,7 @@ public class RibbonRoutingFilter extends ZuulFilter {
protected ProxyRequestHelper helper; protected ProxyRequestHelper helper;
protected RibbonCommandFactory<?> ribbonCommandFactory; protected RibbonCommandFactory<?> ribbonCommandFactory;
protected List<RibbonRequestCustomizer> requestCustomizers; protected List<RibbonRequestCustomizer> requestCustomizers;
private boolean useServlet31 = true;
public RibbonRoutingFilter(ProxyRequestHelper helper, public RibbonRoutingFilter(ProxyRequestHelper helper,
RibbonCommandFactory<?> ribbonCommandFactory, RibbonCommandFactory<?> ribbonCommandFactory,
...@@ -52,6 +50,12 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -52,6 +50,12 @@ public class RibbonRoutingFilter extends ZuulFilter {
this.helper = helper; this.helper = helper;
this.ribbonCommandFactory = ribbonCommandFactory; this.ribbonCommandFactory = ribbonCommandFactory;
this.requestCustomizers = requestCustomizers; 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) { public RibbonRoutingFilter(RibbonCommandFactory<?> ribbonCommandFactory) {
...@@ -119,8 +123,10 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -119,8 +123,10 @@ public class RibbonRoutingFilter extends ZuulFilter {
// remove double slashes // remove double slashes
uri = uri.replace("//", "/"); uri = uri.replace("//", "/");
long contentLength = useServlet31 ? request.getContentLengthLong(): request.getContentLength();
return new RibbonCommandContext(serviceId, verb, uri, retryable, headers, params, 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 { protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {
......
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