Coverage Report - org.apache.myfaces.shared.renderkit.ContentTypeUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ContentTypeUtils
37%
13/35
30%
8/26
4.75
 
 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  0
 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  1
     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  1
     public static final String[] XHTML_ALLOWED_CONTENT_TYPES = {XHTML_CONTENT_TYPE, 
 40  
         APPLICATION_XML_CONTENT_TYPE, TEXT_XML_CONTENT_TYPE};
 41  
     
 42  1
     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  10
         if (allowedContentTypes == null)
 51  
         {
 52  0
             return false;
 53  
         }
 54  18
         for (int i = 0; i < allowedContentTypes.length; i++)
 55  
         {
 56  18
             if (allowedContentTypes[i].indexOf(contentType) != -1)
 57  
             {
 58  10
                 return true;
 59  
             }
 60  
         }
 61  0
         return false;
 62  
     }
 63  
 
 64  
     public static String chooseWriterContentType(String contentTypeListString, 
 65  
             String[] htmlContentTypes, String[] xhtmlContentTypes)
 66  
     {
 67  0
         String[] contentTypeList = splitContentTypeListString(contentTypeListString);
 68  0
         String[] supportedContentTypeArray = HtmlRendererUtils.getSupportedContentTypes();
 69  0
         String selectedContentType = null;
 70  0
         for (int i = 0; i < supportedContentTypeArray.length; i++)
 71  
         {
 72  0
             String supportedContentType = supportedContentTypeArray[i].trim();
 73  
 
 74  0
             for (int j = 0; j < contentTypeList.length; j++)
 75  
             {
 76  0
                 String contentType = (String) contentTypeList[j];
 77  
 
 78  0
                 if (contentType.indexOf(supportedContentType) != -1)
 79  
                 {
 80  0
                     if (containsContentType(contentType, htmlContentTypes))
 81  
                     {
 82  0
                         selectedContentType = HTML_CONTENT_TYPE;
 83  
                     }
 84  0
                     else if (containsContentType(contentType, xhtmlContentTypes))
 85  
                     {
 86  0
                         selectedContentType = XHTML_CONTENT_TYPE;
 87  
                     }
 88  
                     break;
 89  
                 }
 90  
             }
 91  0
             if (selectedContentType != null)
 92  
             {
 93  0
                 break;
 94  
             }
 95  
         }
 96  0
         return selectedContentType;
 97  
     }
 98  
     
 99  
     public static String[] splitContentTypeListString(String contentTypeListString)
 100  
     {
 101  2
         String[] splittedArray = StringUtils.splitShortString(contentTypeListString, ',');
 102  6
         for (int i = 0; i < splittedArray.length; i++)
 103  
         {
 104  4
             int semicolonIndex = splittedArray[i].indexOf(';');
 105  4
             if (semicolonIndex != -1)
 106  
             {
 107  1
                 splittedArray[i] = splittedArray[i].substring(0,semicolonIndex);
 108  
             }
 109  
         }
 110  2
         return splittedArray;
 111  
     }
 112  
     
 113  
     public static String getContentTypeFromAcceptHeader(FacesContext context)
 114  
     {
 115  0
         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  0
         if (contentTypeListString != null
 123  
                 && contentTypeListString
 124  
                         .startsWith("application/vnd.wap.mms-message;*/*"))
 125  
         {
 126  0
             contentTypeListString = "*/*";
 127  
         }
 128  0
         return contentTypeListString;
 129  
     }
 130  
 
 131  
 }