请问httpservletresponsewrapper有什么用

homepna 2006-03-13 02:02:25
我在下面的一段程序中看到的:
public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain )
throws IOException, ServletException {

request.setCharacterEncoding("utf-8");
if (compressionThreshold == 0) {
chain.doFilter(request, response);
return;
}
boolean supportCompression = false;
if (request instanceof HttpServletRequest) {

// Are we allowed to compress ?
String s = request.getParameter("gzip");
if ("false".equals(s)) {
chain.doFilter(request, response);
return;
}

Enumeration e =
((HttpServletRequest)request).getHeaders("Accept-Encoding");
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
if (name.indexOf("gzip") != -1) {
//log.trace("supports compression");
supportCompression = true;
} else {
//log.trace("no support for compresion");
}
}
}

if (!supportCompression) {
//log.trace("doFilter gets called wo compression");
chain.doFilter(request, response);
return;
} else {
if (response instanceof HttpServletResponse) {
CompressionServletResponseWrapper wrappedResponse =
new CompressionServletResponseWrapper((HttpServletResponse)response);
//minhnn
//wrappedResponse.setDebugLevel(debug);
wrappedResponse.setCompressionThreshold(compressionThreshold);
//log.trace("doFilter gets called with compression");
try {
chain.doFilter(request, wrappedResponse);
} finally {
wrappedResponse.finishResponse();
}
return;
}
}
}
其中CompressionServletResponseWrapper 继承了httpservletresponsewrapper,这么做有什么意义呢?
...全文
122 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
homepna 2006-03-13
刚看了篇文章,了解了,不过不知道大家实践中有没有用过它来节省带宽,我是没用过
回复
homepna 2006-03-13
下面是CompressionServletResponseWrapper:
public class CompressionServletResponseWrapper extends
HttpServletResponseWrapper {

// minhnn : I remove the log because log4j does not support TRACE level
private static Log log = LogFactory
.getLog(CompressionServletResponseWrapper.class);

// ----------------------------------------------------- Constructor

/**
* Calls the parent constructor which creates a ServletResponse adaptor
* wrapping the given response object.
*/

public CompressionServletResponseWrapper(HttpServletResponse response) {
super(response);
origResponse = response;
}

// ----------------------------------------------------- Instance Variables

/**
* Original response
*/

protected HttpServletResponse origResponse = null;

/**
* Descriptive information about this Response implementation.
*/

protected static final String info = "CompressionServletResponseWrapper";

/**
* The ServletOutputStream that has been returned by
* <code>getOutputStream()</code>, if any.
*/

protected ServletOutputStream stream = null;

/**
* The PrintWriter that has been returned by <code>getWriter()</code>, if
* any.
*/

protected PrintWriter writer = null;

/**
* The threshold number to compress
*/
protected int threshold = 0;

/**
* Content type
*/
protected String contentType = null;

// --------------------------------------------------------- Public Methods

/**
* Set content type
*/
public void setContentType(String contentType) {
this.contentType = contentType;
origResponse.setContentType(contentType);
}

/**
* Set threshold number
*/
public void setCompressionThreshold(int threshold) {
this.threshold = threshold;
}

/**
* Create and return a ServletOutputStream to write the content associated
* with this Response.
*
* @exception IOException
* if an input/output error occurs
*/
public ServletOutputStream createOutputStream() throws IOException {
// log.trace("createOutputStream gets called");

CompressionResponseStream stream = new CompressionResponseStream(
origResponse);
// stream.setDebugLevel(debug);
stream.setBuffer(threshold);

return stream;

}

/**
* Finish a response.
*/
public void finishResponse() {
try {
if (writer != null) {
writer.close();
} else {
if (stream != null)
stream.close();
}
} catch (IOException e) {
}
}

// ------------------------------------------------ ServletResponse Methods

/**
* Flush the buffer and commit this response.
*
* @exception IOException
* if an input/output error occurs
*/
public void flushBuffer() throws IOException {

if (stream != null) {
// minhnn @todo : how to ensure the stream still open ???
((CompressionResponseStream) stream).flush();
}

}

/**
* Return the servlet output stream associated with this Response.
*
* @exception IllegalStateException
* if <code>getWriter</code> has already been called for
* this response
* @exception IOException
* if an input/output error occurs
*/
public ServletOutputStream getOutputStream() throws IOException {

if (writer != null)
throw new IllegalStateException(
"getWriter() has already been called for this response");

if (stream == null) {
stream = createOutputStream();
}
// log.trace("stream is set to " + stream + " in getOutputStream");

return (stream);

}

/**
* Return the writer associated with this Response.
*
* @exception IllegalStateException
* if <code>getOutputStream</code> has already been called
* for this response
* @exception IOException
* if an input/output error occurs
*/
public PrintWriter getWriter() throws IOException {

if (writer != null)
return (writer);

if (stream != null)
throw new IllegalStateException(
"getOutputStream() has already been called for this response");

stream = createOutputStream();
String charEnc = origResponse.getCharacterEncoding();
if (charEnc != null) {
writer = new PrintWriter(new OutputStreamWriter(stream, charEnc));
} else {
writer = new PrintWriter(stream);
}

return (writer);

}

public void setContentLength(int length) {
}

/**
* Returns character from content type. This method was taken from tomcat.
*
* @author rajo
*/
private static String getCharsetFromContentType(String type) {

if (type == null) {
return null;
}
int semi = type.indexOf(";");
if (semi == -1) {
return null;
}
String afterSemi = type.substring(semi + 1);
int charsetLocation = afterSemi.indexOf("charset=");
if (charsetLocation == -1) {
return null;
} else {
String afterCharset = afterSemi.substring(charsetLocation + 8);
String encoding = afterCharset.trim();
return encoding;
}
}

}
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2006-03-13 02:02
社区公告
暂无公告