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
27
28
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
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
123
124
125
126
127 if (contentTypeListString != null
128 && contentTypeListString
129 .startsWith("application/vnd.wap.mms-message;*/*"))
130 {
131 contentTypeListString = "*/*";
132 }
133 return contentTypeListString;
134 }
135
136 }