1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
118
119
120
121
122 if (contentTypeListString != null
123 && contentTypeListString
124 .startsWith("application/vnd.wap.mms-message;*/*"))
125 {
126 contentTypeListString = "*/*";
127 }
128 return contentTypeListString;
129 }
130
131 }