Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.-Smile-.
spring-framework
提交
444378c4
S
spring-framework
项目概览
.-Smile-.
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
444378c4
编写于
6月 15, 2009
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-5825 - ShallowEtagHeaderFilter doesn't work: response body is empty
上级
c254924b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
78 addition
and
19 deletion
+78
-19
org.springframework.web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java
...g/springframework/web/filter/ShallowEtagHeaderFilter.java
+51
-18
org.springframework.web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTest.java
...ringframework/web/filter/ShallowEtagHeaderFilterTest.java
+27
-1
未找到文件。
org.springframework.web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java
浏览文件 @
444378c4
...
...
@@ -20,7 +20,7 @@ import java.io.ByteArrayOutputStream;
import
java.io.IOException
;
import
java.io.OutputStreamWriter
;
import
java.io.PrintWriter
;
import
java.io.
Writer
;
import
java.io.
UnsupportedEncodingException
;
import
javax.servlet.FilterChain
;
import
javax.servlet.ServletException
;
import
javax.servlet.ServletOutputStream
;
...
...
@@ -32,12 +32,12 @@ import org.springframework.util.FileCopyUtils;
import
org.springframework.util.Md5HashUtils
;
/**
* {@link javax.servlet.Filter} that generates an <code>ETag</code> value based on the content
*
on the response. This ETag is compared to the <code>If-None-Match</code> header of the request.
*
If these headers are equal, the resonse
content is not sent, but rather a 304 "Not Modified" status.
* {@link javax.servlet.Filter} that generates an <code>ETag</code> value based on the content
on the response. This
*
ETag is compared to the <code>If-None-Match</code> header of the request. If these headers are equal, the resonse
* 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})
*
is
still rendered. As such, this filter only saves bandwidth, not server performance.
* <p>Since the ETag is based on the response content, the response (or {@link org.springframework.web.servlet.View})
is
* still rendered. As such, this filter only saves bandwidth, not server performance.
*
* @author Arjen Poutsma
* @since 3.0
...
...
@@ -48,7 +48,6 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
private
static
String
HEADER_IF_NONE_MATCH
=
"If-None-Match"
;
@Override
protected
void
doFilterInternal
(
HttpServletRequest
request
,
HttpServletResponse
response
,
FilterChain
filterChain
)
throws
ServletException
,
IOException
{
...
...
@@ -78,8 +77,9 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
}
/**
* Generate the ETag header value from the given response body byte array.
* <p>The default implementation generates an MD5 hash.
* Generate the ETag header value from the given response body byte array. <p>The default implementation generates an
* MD5 hash.
*
* @param bytes the response bdoy as byte array
* @return the ETag header value
* @see Md5HashUtils
...
...
@@ -91,11 +91,10 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
return
builder
.
toString
();
}
/**
* {@link HttpServletRequest} wrapper that buffers all content written to the
*
{@linkplain #getOutputStream() output stream} and {@linkplain #getWriter() writer},
*
and allows this content to be retrieved via a {@link #toByteArray()
byte array}.
* {@link HttpServletRequest} wrapper that buffers all content written to the
{@linkplain #getOutputStream() output
*
stream} and {@linkplain #getWriter() writer}, and allows this content to be retrieved via a {@link #toByteArray()
* byte array}.
*/
private
static
class
ShallowEtagResponseWrapper
extends
HttpServletResponseWrapper
{
...
...
@@ -118,10 +117,8 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
public
PrintWriter
getWriter
()
throws
IOException
{
if
(
this
.
writer
==
null
)
{
String
characterEncoding
=
getCharacterEncoding
();
Writer
targetWriter
=
(
characterEncoding
!=
null
?
new
OutputStreamWriter
(
this
.
outputStream
,
characterEncoding
)
:
new
OutputStreamWriter
(
this
.
outputStream
));
this
.
writer
=
new
PrintWriter
(
targetWriter
);
this
.
writer
=
(
characterEncoding
!=
null
?
new
ResponsePrintWriter
(
characterEncoding
)
:
new
ResponsePrintWriter
());
}
return
this
.
writer
;
}
...
...
@@ -141,14 +138,50 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
return
this
.
content
.
toByteArray
();
}
private
class
ResponseServletOutputStream
extends
ServletOutputStream
{
@Override
public
void
write
(
int
b
)
throws
IOException
{
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
();
}
}
}
}
org.springframework.web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTest.java
浏览文件 @
444378c4
/*
* 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");
* you may not use this file except in compliance with the License.
...
...
@@ -90,4 +90,30 @@ public class ShallowEtagHeaderFilterTest {
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录