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

时间:2014-01-16 18:25:54

标签: java servlet-filters

我有一个由javascript使用的RESTful Web服务。此服务返回内容类型“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; 
     } 
}

1 个答案:

答案 0 :(得分:3)

我找到了答案。诀窍是阻止我的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 {}

}

我不确定这是不是打算如何使用包装,但是它确实有效。