寻找HttpServletResponseWrapper的捕获impl

JavaEE API附带了HttpServletResponseWrapper,引用javadoc,“提供了一个方便的HttpServletResponse接口实现,可以由希望调整Servlet响应的开发人员进行子类化。” 没有子类化,这个类只是将所有调用传递给包装的响应对象。 请求有一个类似的包装器。

有人能指出我提供这些类的有用子类实现的任何实用程序库吗? 特别是,我正在寻找响应包装器的子类,它捕获写入的响应(作为String,byte [],无论什么是合适的)并通过API方法公开它。

我自己写了一个相当半成品的版本,但坦率地说,我不应该这样做,而且我宁愿扔掉它并使用一个现成的。

我不知道任何实现,即使只需写入ByteArrayOutputStream就可以轻松调整gzip示例。 您还可以通过查看其他响应包装器实现来获取想法:

  • SiteMesh的
  • DWR也使用它

原始答案:

JavaWorld Filter代码中有经典的文章, 其中包含Servlet 2.3模型 。 您可以找到包装请求和响应的示例:

  • 压缩响应

    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月!),但它很好地certificate了它的用法。

在过去,我使用了spring框架中提供的对象。 特别是,这应该工作: http : //static.springsource.org/spring/docs/2.0.x/api/org/springframework/mock/web/MockHttpServletRequest.html