SPR-6759 - Jetty 7 doesn't like ShallowEtagHeaderFilter
This commit is contained in:
parent
1bbe93e535
commit
0a4a09a09d
|
|
@ -57,26 +57,61 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
||||||
filterChain.doFilter(request, responseWrapper);
|
filterChain.doFilter(request, responseWrapper);
|
||||||
|
|
||||||
byte[] body = responseWrapper.toByteArray();
|
byte[] body = responseWrapper.toByteArray();
|
||||||
String responseETag = generateETagHeaderValue(body);
|
int statusCode = responseWrapper.getStatusCode();
|
||||||
response.setHeader(HEADER_ETAG, responseETag);
|
|
||||||
|
|
||||||
String requestETag = request.getHeader(HEADER_IF_NONE_MATCH);
|
if (isEligibleForEtag(request, responseWrapper, statusCode, body)) {
|
||||||
if (responseETag.equals(requestETag)) {
|
String responseETag = generateETagHeaderValue(body);
|
||||||
if (logger.isTraceEnabled()) {
|
response.setHeader(HEADER_ETAG, responseETag);
|
||||||
logger.trace("ETag [" + responseETag + "] equal to If-None-Match, sending 304");
|
|
||||||
|
String requestETag = request.getHeader(HEADER_IF_NONE_MATCH);
|
||||||
|
if (responseETag.equals(requestETag)) {
|
||||||
|
if (logger.isTraceEnabled()) {
|
||||||
|
logger.trace("ETag [" + responseETag + "] equal to If-None-Match, sending 304");
|
||||||
|
}
|
||||||
|
response.setContentLength(0);
|
||||||
|
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (logger.isTraceEnabled()) {
|
||||||
|
logger.trace("ETag [" + responseETag + "] not equal to If-None-Match [" + requestETag +
|
||||||
|
"], sending normal response");
|
||||||
|
}
|
||||||
|
copyBodyToResponse(body, response);
|
||||||
}
|
}
|
||||||
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (logger.isTraceEnabled()) {
|
if (logger.isTraceEnabled()) {
|
||||||
logger.trace("ETag [" + responseETag + "] not equal to If-None-Match [" + requestETag +
|
logger.trace("Response with status code [" + statusCode + "] not eligible for ETag");
|
||||||
"], sending normal response");
|
|
||||||
}
|
}
|
||||||
response.setContentLength(body.length);
|
copyBodyToResponse(body, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyBodyToResponse(byte[] body, HttpServletResponse response) throws IOException {
|
||||||
|
response.setContentLength(body.length);
|
||||||
|
if (body.length > 0) {
|
||||||
FileCopyUtils.copy(body, response.getOutputStream());
|
FileCopyUtils.copy(body, response.getOutputStream());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates whether the given request and response are eligible for ETag generation.
|
||||||
|
*
|
||||||
|
* <p>Default implementation returns {@code true} for response status codes in the {@code 2xx} series.
|
||||||
|
*
|
||||||
|
* @param request the HTTP request
|
||||||
|
* @param response the HTTP response
|
||||||
|
* @param responseStatusCode the HTTP response status code
|
||||||
|
* @param responseBody the response body
|
||||||
|
* @return {@code true} if eligible for ETag generation; {@code false} otherwise
|
||||||
|
*/
|
||||||
|
protected boolean isEligibleForEtag(HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
int responseStatusCode,
|
||||||
|
byte[] responseBody) {
|
||||||
|
return (responseStatusCode >= 200 && responseStatusCode < 300);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate the ETag header value from the given response body byte array. <p>The default implementation generates an
|
* Generate the ETag header value from the given response body byte array. <p>The default implementation generates an
|
||||||
* MD5 hash.
|
* MD5 hash.
|
||||||
|
|
@ -105,10 +140,36 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
private PrintWriter writer;
|
private PrintWriter writer;
|
||||||
|
|
||||||
|
private int statusCode = -1;
|
||||||
|
|
||||||
private ShallowEtagResponseWrapper(HttpServletResponse response) {
|
private ShallowEtagResponseWrapper(HttpServletResponse response) {
|
||||||
super(response);
|
super(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setStatus(int sc) {
|
||||||
|
super.setStatus(sc);
|
||||||
|
this.statusCode = sc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setStatus(int sc, String sm) {
|
||||||
|
super.setStatus(sc, sm);
|
||||||
|
this.statusCode = sc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendError(int sc) throws IOException {
|
||||||
|
super.sendError(sc);
|
||||||
|
this.statusCode = sc;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendError(int sc, String msg) throws IOException {
|
||||||
|
super.sendError(sc, msg);
|
||||||
|
this.statusCode = sc;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServletOutputStream getOutputStream() {
|
public ServletOutputStream getOutputStream() {
|
||||||
return this.outputStream;
|
return this.outputStream;
|
||||||
|
|
@ -135,6 +196,10 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
|
||||||
resetBuffer();
|
resetBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getStatusCode() {
|
||||||
|
return statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
private byte[] toByteArray() {
|
private byte[] toByteArray() {
|
||||||
return this.content.toByteArray();
|
return this.content.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,15 @@ public class ShallowEtagHeaderFilterTest {
|
||||||
filter = new ShallowEtagHeaderFilter();
|
filter = new ShallowEtagHeaderFilter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isEligibleForEtag() {
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
|
||||||
|
assertTrue(filter.isEligibleForEtag(request, response, 200, new byte[0]));
|
||||||
|
assertFalse(filter.isEligibleForEtag(request, response, 300, new byte[0]));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterNoMatch() throws Exception {
|
public void filterNoMatch() throws Exception {
|
||||||
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
|
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue