寻找 HttpServletResponseWrapper 的捕获实现 [英] Looking for a capturing impl of HttpServletResponseWrapper
问题描述
JavaEE API 随附 HttpServletResponseWrapper,引用 javadoc,它提供了 HttpServletResponse 接口的方便实现,可以由希望调整来自 Servlet 的响应的开发人员子类化."没有子类化,这个类只是将所有调用传递给包装的响应对象.有一个类似的请求包装器.
The JavaEE API comes with the HttpServletResponseWrapper which, to quote the javadoc, "provides a convenient implementation of the HttpServletResponse interface that can be subclassed by developers wishing to adapt the response from a Servlet." Without subclassing, this class just passes all calls through to the wrapped response object. There's a similar wrapper for requests.
任何人都可以向我指出提供这些类的有用子类实现的任何实用程序库吗?特别是,我正在寻找响应包装器的子类,它捕获写入的响应(作为字符串、字节 [],任何合适的)并通过 API 方法公开它.
Can anyone point me at any utility libraries that provide useful subclass implementations of these classes? Particularly, I'm looking for a subclass of the response wrapper that captures the written response (as a String, byte[], whatever's appropriate) and exposes it via an API method.
我自己写了一个相当不成熟的版本,但坦率地说,我不应该这样做,我宁愿把它扔掉并使用现成的.
I've written a rather half-baked version myself, but frankly, I shouldn't have to, and I'd rather throw it away and use one off the shelf.
推荐答案
我不知道有任何实现,即使只需写入 ByteArrayOutputStream 即可轻松调整 gzip 示例.您还可以通过查看其他响应包装器实现来获取想法:
I am not aware of any implementation, even though the gzip example can be adapted easily by just writing to a ByteArrayOutputStream. You can also take ideas by looking at other response wrapper implementations at:
- SiteMesh
- DWR also uses it
原答案:
JavaWorld里有经典文章过滤代码使用 Servlet 2.3 模型.您可以找到包装请求和响应的示例:
There is the classic article in JavaWorld Filter code with Servlet 2.3 model. You can find examples for wrapped request and response:
public class CompressionResponseWrapper extends HttpServletResponseWrapper {
protected ServletOutputStream stream = null;
protected PrintWriter writer = null;
protected int threshold = 0;
protected HttpServletResponse origResponse = null;
public CompressionResponseWrapper(HttpServletResponse response) {
super(response);
origResponse = response;
}
public void setCompressionThreshold(int threshold) {
this.threshold = threshold;
}
public ServletOutputStream createOutputStream() throws IOException {
return (new CompressionResponseStream(origResponse));
}
public ServletOutputStream getOutputStream() throws IOException {
if (writer != null) {
throw new IllegalStateException("getWriter() has already been " +
"called for this response");
}
if (stream == null) {
stream = createOutputStream();
}
((CompressionResponseStream) stream).setCommit(true);
((CompressionResponseStream) stream).setBuffer(threshold);
return stream;
}
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();
((CompressionResponseStream) stream).setCommit(true);
((CompressionResponseStream) stream).setBuffer(threshold);
writer = new PrintWriter(stream);
return writer;
}
}
public class MultipartWrapper extends HttpServletRequestWrapper {
MultipartRequest mreq = null;
public MultipartWrapper(HttpServletRequest req, String dir)
throws IOException {
super(req);
mreq = new MultipartRequest(req, dir);
}
// Methods to replace HSR methods
public Enumeration getParameterNames() {
return mreq.getParameterNames();
}
public String getParameter(String name) {
return mreq.getParameter(name);
}
public String[] getParameterValues(String name) {
return mreq.getParameterValues(name);
}
public Map getParameterMap() {
Map map = new HashMap();
Enumeration enum = getParameterNames();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
map.put(name, mreq.getParameterValues(name));
}
return map;
}
// Methods only in MultipartRequest
public Enumeration getFileNames() {
return mreq.getFileNames();
}
public String getFilesystemName(String name) {
return mreq.getFilesystemName(name);
}
public String getContentType(String name) {
return mreq.getContentType(name);
}
public File getFile(String name) {
return mreq.getFile(name);
}
}
代码有点旧(2001 年 6 月!),但它很好地演示了用法.
The code is a bit old (june 2001!), but it demonstrate the usage well.
这篇关于寻找 HttpServletResponseWrapper 的捕获实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!