You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Steve Krulewitz <sh...@mm.st> on 2004/03/24 00:35:25 UTC

Serving XHTML with Cocoon - Another solution

I really wasn't happy with using all that pipeline stuff just because 
Internet Explorer does not understand "application/xhtml+xml".  In the 
non-Cocoon world, it seems like the answer to this problem is adding 
rewrite rules to Apache to modify the response based on the user agent. 
  This is explained clearly in Mark Pilgrim's "The Road to XHTML 2.0: 
MIME Types" [1].  If you are not using Apache, another option is to use 
a servlet filter.  The following code is more of a general content type 
replacer, and it works on Tomcat:

package com.skrul.filters;

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/*
  * Much of this code was borred from
  * 
http://www-106.ibm.com/developerworks/java/library/j-tomcat/?open&l=101,t=grj,p=TomcatTricks
  *
  */
public class IEContentTypeFilter implements Filter {

     String userAgent = null;
     Map replace = null;

     public void init(FilterConfig fc) throws ServletException {

         userAgent = fc.getInitParameter("useragent");
         replace = new HashMap();
         Enumeration e = fc.getInitParameterNames();
         while(e.hasMoreElements()) {
             String key = (String) e.nextElement();
             if(key.startsWith("search_")) {
                 String a[] = key.split("_");
                 replace.put(fc.getInitParameter(key), 
fc.getInitParameter("replace_" + a[1]));
             }
         }
         replace = Collections.unmodifiableMap(replace);
     }

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

         String browserDet = ((HttpServletRequest) 
request).getHeader("User-Agent").toLowerCase();
         if (browserDet.indexOf(userAgent) != -1)  {
             ReplaceContentTypeWrapper wrapped = new 
ReplaceContentTypeWrapper(response, replace);
             chain.doFilter(request, wrapped);
         }
         else {
             chain.doFilter(request, response);
         }
     }

     public void destroy() {
     }

     class ReplaceContentTypeWrapper extends HttpServletResponseWrapper {
         String search;
         Map replace;

         public ReplaceContentTypeWrapper(ServletResponse inResp, Map 
replace) throws java.io.IOException {
              super((HttpServletResponse) inResp);
              this.replace = replace;
         }

         public void setContentType(String contentType) {
             if(replace.containsKey(contentType)) {
                 super.setContentType((String) replace.get(contentType));
             }
             else {
                 super.setContentType(contentType);
             }
         }
     }
}

You need to configure this in your web.xml, and the following lines must 
come under the web-app element but before the servlet element:

<filter>
   <filter-name>IEContentTypeFilter</filter-name>
     <filter-class>com.skrul.filters.IEContentTypeFilter</filter-class>
   <init-param>
     <param-name>useragent</param-name>
     <param-value>msie</param-value>
   </init-param>
   <init-param>
     <param-name>search_1</param-name>
     <param-value>application/xhtml+xml; charset=utf-8</param-value>
   </init-param>
   <init-param>
     <param-name>replace_1</param-name>
     <param-value>text/html; charset=utf-8</param-value>
   </init-param>
</filter>

<filter-mapping>
   <filter-name>IEContentTypeFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

cheers,
-steve

[1] http://www.xml.com/pub/a/2003/03/19/dive-into-xml.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org