提交 444378c4 编写于 作者: A Arjen Poutsma

SPR-5825 - ShallowEtagHeaderFilter doesn't work: response body is empty

上级 c254924b
...@@ -20,7 +20,7 @@ import java.io.ByteArrayOutputStream; ...@@ -20,7 +20,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Writer; import java.io.UnsupportedEncodingException;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
...@@ -32,12 +32,12 @@ import org.springframework.util.FileCopyUtils; ...@@ -32,12 +32,12 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.util.Md5HashUtils; import org.springframework.util.Md5HashUtils;
/** /**
* {@link javax.servlet.Filter} that generates an <code>ETag</code> value based on the content * {@link javax.servlet.Filter} that generates an <code>ETag</code> value based on the content on the response. This
* on the response. This ETag is compared to the <code>If-None-Match</code> header of the request. * ETag is compared to the <code>If-None-Match</code> header of the request. If these headers are equal, the resonse
* If these headers are equal, the resonse content is not sent, but rather a 304 "Not Modified" status. * content is not sent, but rather a 304 "Not Modified" status.
* *
* <p>Since the ETag is based on the response content, the response (or {@link org.springframework.web.servlet.View}) * <p>Since the ETag is based on the response content, the response (or {@link org.springframework.web.servlet.View}) is
* is still rendered. As such, this filter only saves bandwidth, not server performance. * still rendered. As such, this filter only saves bandwidth, not server performance.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0 * @since 3.0
...@@ -48,7 +48,6 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { ...@@ -48,7 +48,6 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
private static String HEADER_IF_NONE_MATCH = "If-None-Match"; private static String HEADER_IF_NONE_MATCH = "If-None-Match";
@Override @Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException { throws ServletException, IOException {
...@@ -78,8 +77,9 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { ...@@ -78,8 +77,9 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
} }
/** /**
* Generate the ETag header value from the given response body byte array. * Generate the ETag header value from the given response body byte array. <p>The default implementation generates an
* <p>The default implementation generates an MD5 hash. * MD5 hash.
*
* @param bytes the response bdoy as byte array * @param bytes the response bdoy as byte array
* @return the ETag header value * @return the ETag header value
* @see Md5HashUtils * @see Md5HashUtils
...@@ -91,11 +91,10 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { ...@@ -91,11 +91,10 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
return builder.toString(); return builder.toString();
} }
/** /**
* {@link HttpServletRequest} wrapper that buffers all content written to the * {@link HttpServletRequest} wrapper that buffers all content written to the {@linkplain #getOutputStream() output
* {@linkplain #getOutputStream() output stream} and {@linkplain #getWriter() writer}, * stream} and {@linkplain #getWriter() writer}, and allows this content to be retrieved via a {@link #toByteArray()
* and allows this content to be retrieved via a {@link #toByteArray() byte array}. * byte array}.
*/ */
private static class ShallowEtagResponseWrapper extends HttpServletResponseWrapper { private static class ShallowEtagResponseWrapper extends HttpServletResponseWrapper {
...@@ -118,10 +117,8 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { ...@@ -118,10 +117,8 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
public PrintWriter getWriter() throws IOException { public PrintWriter getWriter() throws IOException {
if (this.writer == null) { if (this.writer == null) {
String characterEncoding = getCharacterEncoding(); String characterEncoding = getCharacterEncoding();
Writer targetWriter = (characterEncoding != null ? this.writer = (characterEncoding != null ? new ResponsePrintWriter(characterEncoding) :
new OutputStreamWriter(this.outputStream, characterEncoding) : new ResponsePrintWriter());
new OutputStreamWriter(this.outputStream));
this.writer = new PrintWriter(targetWriter);
} }
return this.writer; return this.writer;
} }
...@@ -141,14 +138,50 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { ...@@ -141,14 +138,50 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
return this.content.toByteArray(); return this.content.toByteArray();
} }
private class ResponseServletOutputStream extends ServletOutputStream { private class ResponseServletOutputStream extends ServletOutputStream {
@Override @Override
public void write(int b) throws IOException { public void write(int b) throws IOException {
content.write(b); content.write(b);
} }
@Override
public void write(byte[] b, int off, int len) throws IOException {
content.write(b, off, len);
}
}
private class ResponsePrintWriter extends PrintWriter {
private ResponsePrintWriter() {
super(new OutputStreamWriter(content));
}
private ResponsePrintWriter(String characterEncoding) throws UnsupportedEncodingException {
super(new OutputStreamWriter(content, characterEncoding));
}
@Override
public void write(char buf[], int off, int len) {
super.write(buf, off, len);
super.flush();
}
@Override
public void write(String s, int off, int len) {
super.write(s, off, len);
super.flush();
}
@Override
public void write(int c) {
super.write(c);
super.flush();
}
} }
} }
} }
/* /*
* Copyright ${YEAR} the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -90,4 +90,30 @@ public class ShallowEtagHeaderFilterTest { ...@@ -90,4 +90,30 @@ public class ShallowEtagHeaderFilterTest {
assertArrayEquals("Invalid content", new byte[0], response.getContentAsByteArray()); assertArrayEquals("Invalid content", new byte[0], response.getContentAsByteArray());
} }
@Test
public void filterWriter() throws Exception {
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
String etag = "\"0b10a8db164e0754105b7a99be72e3fe5\"";
request.addHeader("If-None-Match", etag);
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = new FilterChain() {
public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse)
throws IOException, ServletException {
assertEquals("Invalid request passed", request, filterRequest);
((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
String responseBody = "Hello World";
FileCopyUtils.copy(responseBody, filterResponse.getWriter());
}
};
filter.doFilter(request, response, filterChain);
assertEquals("Invalid status", 304, response.getStatus());
assertEquals("Invalid ETag header", "\"0b10a8db164e0754105b7a99be72e3fe5\"", response.getHeader("ETag"));
assertEquals("Invalid Content-Length header", 0, response.getContentLength());
assertArrayEquals("Invalid content", new byte[0], response.getContentAsByteArray());
}
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册