请问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,这么做有什么意义呢?
...全文
182 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
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;
}
}

}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧