小编典典

Servlet过滤器包装器-更改内容类型时遇到麻烦

java

我有RESTful Web服务,该服务由javascript使用。该服务返回的内容类型为“ application /
json”。但是,对于IE,内容类型必须为“ text /
html”。因此,当检测到IE作为客户端时,我编写了一个过滤器和包装程序来更改内容类型。我的逻辑似乎对内容类型没有影响。我究竟做错了什么?

过滤器:

public class IE8Filter implements Filter {

    private Logger logger = LoggerHelper.getLogger();

    @Override
    public void destroy() {}

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {

         HttpServletRequest request = (HttpServletRequest) req;
         HttpServletResponse response = (HttpServletResponse) res;

         String userAgent = request.getHeader("User-Agent");
         logger.debugf("User Agent  = '%s'", userAgent);

         IE8FilterResponseWrapper wrapper = new IE8FilterResponseWrapper(response);



         chain.doFilter(req, wrapper);


         if (userAgent.contains("MSIE 8") || userAgent.contains("MSIE 7")) {

             wrapper.setContentType("text/html");
             logger.debugf("Content Type  = '%s'", wrapper.getContentType());
         }


    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {}

}

包装器:

public class IE8FilterResponseWrapper extends HttpServletResponseWrapper {


    private String contentType;

    public IE8FilterResponseWrapper(HttpServletResponse response) {
        super(response);
    }

    public void setContentType(String type) { 
        this.contentType = type;
        super.setContentType(type); 
    }

     public String getContentType() { 
         return contentType; 
     } 
}

阅读 314

收藏
2020-11-23

共1个答案

小编典典

我找到了答案。诀窍是防止我的Web服务使用包装器设置内容类型:

public class IE8FilterResponseWrapper extends HttpServletResponseWrapper {


    public IE8FilterResponseWrapper(HttpServletResponse response) {
        super(response);
    }

    public void forceContentType(String type) {
        super.setContentType(type); 
    }

    public void setContentType(String type) {

    }

    public void setHeader(String name, String value) {

        if (!name.equals("Content-Type")) {
            super.setHeader(name, value);
        }
    }

    public void addHeader(String name, String value) {
        if (!name.equals("Content-Type")) {
            super.addHeader(name, value);
        }

    }

     public String getContentType() { 
         return super.getContentType(); 
     } 
}

现在我的过滤器如下所示:

public class IE8Filter implements Filter {

    private Logger logger = LoggerHelper.getLogger();

    @Override
    public void destroy() {}

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {

         HttpServletRequest request = (HttpServletRequest) req;
         HttpServletResponse response = (HttpServletResponse) res;

         String userAgent = request.getHeader("User-Agent");
         logger.debugf("User Agent  = '%s'", userAgent);

         IE8FilterResponseWrapper wrapper = new IE8FilterResponseWrapper(response);

         if (userAgent.contains("MSIE 8") || userAgent.contains("MSIE 7")) {

             wrapper.forceContentType("text/html");
             chain.doFilter(req, wrapper);

         }
         else {
             chain.doFilter(req, res);
         }


    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {}

}

我不确定这是否是打算使用包装器的方式,但确实可行。

2020-11-23