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 | 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 | |
|
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 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
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 | |
} |