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  public class ContentTypeUtils
27  {
28      public static final String HTML_CONTENT_TYPE = "text/html";
29      public static final String TEXT_ANY_CONTENT_TYPE = "text/*";
30      public static final String ANY_CONTENT_TYPE = "*/*";
31  
32      public static final String[] HTML_ALLOWED_CONTENT_TYPES = {HTML_CONTENT_TYPE, 
33          ANY_CONTENT_TYPE, TEXT_ANY_CONTENT_TYPE};
34      
35      public static final String XHTML_CONTENT_TYPE = "application/xhtml+xml";
36      public static final String APPLICATION_XML_CONTENT_TYPE = "application/xml";
37      public static final String TEXT_XML_CONTENT_TYPE = "text/xml";
38      
39      public static final String[] XHTML_ALLOWED_CONTENT_TYPES = {XHTML_CONTENT_TYPE, 
40          APPLICATION_XML_CONTENT_TYPE, TEXT_XML_CONTENT_TYPE};
41      
42      public static final String[] AJAX_XHTML_ALLOWED_CONTENT_TYPES = {XHTML_CONTENT_TYPE};
43  
44  
45      /**
46       * Indicate if the passes content type match one of the options passed. 
47       */
48      public static boolean containsContentType(String contentType, String[] allowedContentTypes)
49      {
50          if (allowedContentTypes == null)
51          {
52              return false;
53          }
54          for (int i = 0; i < allowedContentTypes.length; i++)
55          {
56              if (allowedContentTypes[i].indexOf(contentType) != -1)
57              {
58                  return true;
59              }
60          }
61          return false;
62      }
63  
64      public static String chooseWriterContentType(String contentTypeListString, 
65              String[] htmlContentTypes, String[] xhtmlContentTypes)
66      {
67          String[] contentTypeList = splitContentTypeListString(contentTypeListString);
68          String[] supportedContentTypeArray = HtmlRendererUtils.getSupportedContentTypes();
69          String selectedContentType = null;
70          for (int i = 0; i < supportedContentTypeArray.length; i++)
71          {
72              String supportedContentType = supportedContentTypeArray[i].trim();
73  
74              for (int j = 0; j < contentTypeList.length; j++)
75              {
76                  String contentType = (String) contentTypeList[j];
77  
78                  if (contentType.indexOf(supportedContentType) != -1)
79                  {
80                      if (containsContentType(contentType, htmlContentTypes))
81                      {
82                          selectedContentType = HTML_CONTENT_TYPE;
83                      }
84                      else if (containsContentType(contentType, xhtmlContentTypes))
85                      {
86                          selectedContentType = XHTML_CONTENT_TYPE;
87                      }
88                      break;
89                  }
90              }
91              if (selectedContentType != null)
92              {
93                  break;
94              }
95          }
96          return selectedContentType;
97      }
98      
99      public static String[] splitContentTypeListString(String contentTypeListString)
100     {
101         String[] splittedArray = StringUtils.splitShortString(contentTypeListString, ',');
102         for (int i = 0; i < splittedArray.length; i++)
103         {
104             int semicolonIndex = splittedArray[i].indexOf(';');
105             if (semicolonIndex != -1)
106             {
107                 splittedArray[i] = splittedArray[i].substring(0,semicolonIndex);
108             }
109         }
110         return splittedArray;
111     }
112     
113     public static String getContentTypeFromAcceptHeader(FacesContext context)
114     {
115         String contentTypeListString = (String) context.getExternalContext()
116             .getRequestHeaderMap().get("Accept");
117         // There is a windows mobile IE client (6.12) sending
118         // "application/vnd.wap.mms-message;*/*"
119         // Note that the Accept header should be written as 
120         // "application/vnd.wap.mms-message,*/*" ,
121         // so this is bug of the client. Anyway, this is a workaround ...
122         if (contentTypeListString != null
123                 && contentTypeListString
124                         .startsWith("application/vnd.wap.mms-message;*/*"))
125         {
126             contentTypeListString = "*/*";
127         }
128         return contentTypeListString;
129     }
130 
131 }