gpt4 book ai didi

java - 寻找 HttpServletResponseWrapper 的捕获实现

转载 作者:搜寻专家 更新时间:2023-11-01 02:34:46 24 4
gpt4 key购买 nike

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

任何人都可以指出任何提供这些类的有用子类实现的实用程序库吗?特别是,我正在寻找响应包装器的子类,它捕获书面响应(作为字符串、byte[],任何合适的)并通过 API 方法公开它。

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

最佳答案

我不知道有任何实现,即使 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 月!),但它很好地演示了用法。

关于java - 寻找 HttpServletResponseWrapper 的捕获实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1152786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com