寻找HttpServletResponseWrapper的捕获impl

时间:2009-07-20 10:25:47

标签: java servlets

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

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

我自己写了一个相当半生不熟的版本,但坦率地说,我不应该这样做,而是宁可把它扔掉,也不要使用现成的版本。

2 个答案:

答案 0 :(得分:4)

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

原始回答:

JavaWorld Filter code with Servlet 2.3 model中有一篇经典文章。您可以找到包装请求和响应的示例:

  • Compression the 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;
      }
    }
    
  • Handling file upload

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

答案 1 :(得分:1)

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