在JBOSS下的java中修改ServerletFilter中的Content-Type

时间:2016-11-25 02:59:03

标签: java tomcat servlets jboss content-type

我尝试使用ServerletFilter更改ServeletResponse的内容类型。 我在web.xml中的过滤器中设置相关属性,然后在ServerletFilter的doFilter方法中进行设置。

实际要求是将内容类型发送为text/xml; charset=utf-8。但是,当我将其设置为此时,它始终以text/xml;charset=utf-8发送。没有空间。 我知道他们之间没有空间的含义。但我正在和一个需要这个问题的错误客户打交道。

然后我尝试发送一些其他受控角色,它适用于 tomcat (7.0.64)服务器中的STX one(text / xml; charset = utf-8)。 但它对 JBOSS (版本:4)无效。

输出 Tomcat text/xml; charset=utf-8 输出 JBOSS text/xml;charset=utf-8

我怀疑这是否是JBOSS和Tomcat之间的编码冲突( ASCII / UTF-8 )? 有人可以就此提出一个想法。

过滤:

<filter>
    <filter-name>HeaderFilter</filter-name>
    <filter-class>filters.HeaderFilter</filter-class>
      <init-param>
        <param-name>content-type</param-name>
        <param-value>text/xml;&#8194;charset=utf-8</param-value>
      </init-param>
      <init-param>
        <param-name>Content_type</param-name>
        <param-value>text/xml; charset=utf-8</param-value>
      </init-param>
      <init-param>
        <param-name>rq_content-type</param-name>
        <param-value>text/xml; charset=utf-8</param-value>
      </init-param>
      <init-param>
        <param-name>Header-X</param-name>
        <param-value>Value-X</param-value>
      </init-param>
  </filter>

  <filter-mapping>
    <filter-name>HeaderFilter</filter-name>
    <url-pattern>/services/SiteMinderService</url-pattern>
  </filter-mapping>

ServeletFilter代码:

 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        Enumeration<String> en = httpServletRequest.getHeaderNames();
        while (en.hasMoreElements()) {
            String n = en.nextElement();
            System.out.println("doFilter().Already set > "+n+" : "+httpServletRequest.getHeader(n));
        }

        MutableServeletRequest mutableServeletRequest = new MutableServeletRequest((HttpServletRequest) request);

        for (Map.Entry<String, String> rqCustomHeaderEntry : rqCustomHeaders.entrySet()) {
            mutableServeletRequest.putHeader(rqCustomHeaderEntry.getKey(), rqCustomHeaderEntry.getValue()); 
        }

        for (Map.Entry<String, String> rsCustomHeaderEntry : rsCustomHeaders.entrySet()) {

            String key = rsCustomHeaderEntry.getKey();
            String value = rsCustomHeaderEntry.getValue();

            httpServletResponse.addHeader(key, value);

            if("content-type".equalsIgnoreCase(key)){
                httpServletResponse.setContentType(value);
            }

        }

        chain.doFilter(mutableServeletRequest, new HttpServletResponseWrapper(httpServletResponse) {
            @Override
            public void setContentType(final String type) {
               System.out.println("setContentType().I will not set your content-type ("+type+")!");
            }
        });

    }

0 个答案:

没有答案