代码之家  ›  专栏  ›  技术社区  ›  skaffman

寻找httpservletresponsewrapper的捕获impl

  •  4
  • skaffman  · 技术社区  · 16 年前

    Javaee API附带了httpServletResponseWrapper,引用JavaDoc,“它提供了httpServletResponse接口的一个方便的实现,开发人员可以对该接口进行子类化,以适应来自servlet的响应。”在不进行子类化的情况下,该类只将所有调用传递到包装的响应对象。有一个类似的请求包装器。

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

    我自己写了一个相当不成熟的版本,但坦白地说,我不应该这样做,我宁愿把它扔掉,用一个现成的版本。

    2 回复  |  直到 13 年前
        1
  •  4
  •   David Rabinowitz    13 年前

    我不知道任何实现,即使gzip示例只需编写到bytearrayOutputstream就可以很容易地进行改编。您还可以通过查看以下位置的其他响应包装器实现来获取想法:

    原始答案:

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

        2
  •  1
  •   sillydino    16 年前

    在过去,我使用了Spring框架中提供的对象。尤其是,这一点应该有效: http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mock/web/MockHttpServletRequest.html