View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.shared.renderkit;
20  
21  import javax.faces.context.FacesContext;
22  
23  import org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils;
24  import org.apache.myfaces.shared.util.StringUtils;
25  
26  /**
27   * 
28   * @author Leonardo Uribe
29   *
30   */
31  public class ContentTypeUtils
32  {
33      public static final String HTML_CONTENT_TYPE = "text/html";
34      public static final String TEXT_ANY_CONTENT_TYPE = "text/*";
35      public static final String ANY_CONTENT_TYPE = "*/*";
36  
37      public static final String[] HTML_ALLOWED_CONTENT_TYPES = {HTML_CONTENT_TYPE, 
38          ANY_CONTENT_TYPE, TEXT_ANY_CONTENT_TYPE};
39      
40      public static final String XHTML_CONTENT_TYPE = "application/xhtml+xml";
41      public static final String APPLICATION_XML_CONTENT_TYPE = "application/xml";
42      public static final String TEXT_XML_CONTENT_TYPE = "text/xml";
43      
44      public static final String[] XHTML_ALLOWED_CONTENT_TYPES = {XHTML_CONTENT_TYPE, 
45          APPLICATION_XML_CONTENT_TYPE, TEXT_XML_CONTENT_TYPE};
46      
47      public static final String[] AJAX_XHTML_ALLOWED_CONTENT_TYPES = {XHTML_CONTENT_TYPE};
48  
49  
50      /**
51       * Indicate if the passes content type match one of the options passed. 
52       */
53      public static boolean containsContentType(String contentType, String[] allowedContentTypes)
54      {
55          if (allowedContentTypes == null)
56          {
57              return false;
58          }
59          for (int i = 0; i < allowedContentTypes.length; i++)
60          {
61              if (allowedContentTypes[i].indexOf(contentType) != -1)
62              {
63                  return true;
64              }
65          }
66          return false;
67      }
68  
69      public static String chooseWriterContentType(String contentTypeListString, 
70              String[] htmlContentTypes, String[] xhtmlContentTypes)
71      {
72          String[] contentTypeList = splitContentTypeListString(contentTypeListString);
73          String[] supportedContentTypeArray = HtmlRendererUtils.getSupportedContentTypes();
74          String selectedContentType = null;
75          for (int i = 0; i < supportedContentTypeArray.length; i++)
76          {
77              String supportedContentType = supportedContentTypeArray[i].trim();
78  
79              for (int j = 0; j < contentTypeList.length; j++)
80              {
81                  String contentType = (String) contentTypeList[j];
82  
83                  if (contentType.indexOf(supportedContentType) != -1)
84                  {
85                      if (containsContentType(contentType, htmlContentTypes))
86                      {
87                          selectedContentType = HTML_CONTENT_TYPE;
88                      }
89                      else if (containsContentType(contentType, xhtmlContentTypes))
90                      {
91                          selectedContentType = XHTML_CONTENT_TYPE;
92                      }
93                      break;
94                  }
95              }
96              if (selectedContentType != null)
97              {
98                  break;
99              }
100         }
101         return selectedContentType;
102     }
103     
104     public static String[] splitContentTypeListString(String contentTypeListString)
105     {
106         String[] splittedArray = StringUtils.splitShortString(contentTypeListString, ',');
107         for (int i = 0; i < splittedArray.length; i++)
108         {
109             int semicolonIndex = splittedArray[i].indexOf(";");
110             if (semicolonIndex != -1)
111             {
112                 splittedArray[i] = splittedArray[i].substring(0,semicolonIndex);
113             }
114         }
115         return splittedArray;
116     }
117     
118     public static String getContentTypeFromAcceptHeader(FacesContext context)
119     {
120         String contentTypeListString = (String) context.getExternalContext()
121             .getRequestHeaderMap().get("Accept");
122         // There is a windows mobile IE client (6.12) sending
123         // "application/vnd.wap.mms-message;*/*"
124         // Note that the Accept header should be written as 
125         // "application/vnd.wap.mms-message,*/*" ,
126         // so this is bug of the client. Anyway, this is a workaround ...
127         if (contentTypeListString != null
128                 && contentTypeListString
129                         .startsWith("application/vnd.wap.mms-message;*/*"))
130         {
131             contentTypeListString = "*/*";
132         }
133         return contentTypeListString;
134     }
135 
136 }