1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.entity;
29
30 import java.io.Serializable;
31 import java.nio.charset.Charset;
32 import java.nio.charset.UnsupportedCharsetException;
33 import java.util.Locale;
34
35 import org.apache.http.Consts;
36 import org.apache.http.Header;
37 import org.apache.http.HeaderElement;
38 import org.apache.http.HttpEntity;
39 import org.apache.http.NameValuePair;
40 import org.apache.http.ParseException;
41 import org.apache.http.annotation.Immutable;
42 import org.apache.http.message.BasicHeaderValueParser;
43
44
45
46
47
48
49
50
51
52
53 @Immutable
54 public final class ContentType implements Serializable {
55
56 private static final long serialVersionUID = -7768694718232371896L;
57
58
59 public static final ContentType APPLICATION_ATOM_XML = create(
60 "application/atom+xml", Consts.ISO_8859_1);
61 public static final ContentType APPLICATION_FORM_URLENCODED = create(
62 "application/x-www-form-urlencoded", Consts.ISO_8859_1);
63 public static final ContentType APPLICATION_JSON = create(
64 "application/json", Consts.UTF_8);
65 public static final ContentType APPLICATION_OCTET_STREAM = create(
66 "application/octet-stream", (Charset) null);
67 public static final ContentType APPLICATION_SVG_XML = create(
68 "application/svg+xml", Consts.ISO_8859_1);
69 public static final ContentType APPLICATION_XHTML_XML = create(
70 "application/xhtml+xml", Consts.ISO_8859_1);
71 public static final ContentType APPLICATION_XML = create(
72 "application/xml", Consts.ISO_8859_1);
73 public static final ContentType MULTIPART_FORM_DATA = create(
74 "multipart/form-data", Consts.ISO_8859_1);
75 public static final ContentType TEXT_HTML = create(
76 "text/html", Consts.ISO_8859_1);
77 public static final ContentType TEXT_PLAIN = create(
78 "text/plain", Consts.ISO_8859_1);
79 public static final ContentType TEXT_XML = create(
80 "text/xml", Consts.ISO_8859_1);
81 public static final ContentType WILDCARD = create(
82 "*/*", (Charset) null);
83
84
85 public static final ContentType DEFAULT_TEXT = TEXT_PLAIN;
86 public static final ContentType DEFAULT_BINARY = APPLICATION_OCTET_STREAM;
87
88 private final String mimeType;
89 private final Charset charset;
90
91
92
93
94
95
96
97
98 ContentType(final String mimeType, final Charset charset) {
99 this.mimeType = mimeType;
100 this.charset = charset;
101 }
102
103 public String getMimeType() {
104 return this.mimeType;
105 }
106
107 public Charset getCharset() {
108 return this.charset;
109 }
110
111
112
113
114
115 @Override
116 public String toString() {
117 StringBuilder buf = new StringBuilder();
118 buf.append(this.mimeType);
119 if (this.charset != null) {
120 buf.append("; charset=");
121 buf.append(this.charset.name());
122 }
123 return buf.toString();
124 }
125
126 private static boolean valid(final String s) {
127 for (int i = 0; i < s.length(); i++) {
128 char ch = s.charAt(i);
129 if (ch == '"' || ch == ',' || ch == ';') {
130 return false;
131 }
132 }
133 return true;
134 }
135
136
137
138
139
140
141
142
143
144 public static ContentType create(final String mimeType, final Charset charset) {
145 if (mimeType == null) {
146 throw new IllegalArgumentException("MIME type may not be null");
147 }
148 String type = mimeType.trim().toLowerCase(Locale.US);
149 if (type.length() == 0) {
150 throw new IllegalArgumentException("MIME type may not be empty");
151 }
152 if (!valid(type)) {
153 throw new IllegalArgumentException("MIME type may not contain reserved characters");
154 }
155 return new ContentType(type, charset);
156 }
157
158
159
160
161
162
163
164
165 public static ContentType create(final String mimeType) {
166 return new ContentType(mimeType, (Charset) null);
167 }
168
169
170
171
172
173
174
175
176
177
178 public static ContentType create(
179 final String mimeType, final String charset) throws UnsupportedCharsetException {
180 return create(mimeType,
181 (charset != null && charset.length() > 0) ? Charset.forName(charset) : null);
182 }
183
184 private static ContentType create(final HeaderElement helem) {
185 String mimeType = helem.getName();
186 String charset = null;
187 NameValuePair param = helem.getParameterByName("charset");
188 if (param != null) {
189 charset = param.getValue();
190 }
191 return create(mimeType, charset);
192 }
193
194
195
196
197
198
199
200
201
202 public static ContentType parse(
203 final String s) throws ParseException, UnsupportedCharsetException {
204 if (s == null) {
205 throw new IllegalArgumentException("Content type may not be null");
206 }
207 HeaderElement[] elements = BasicHeaderValueParser.parseElements(s, null);
208 if (elements.length > 0) {
209 return create(elements[0]);
210 } else {
211 throw new ParseException("Invalid content type: " + s);
212 }
213 }
214
215
216
217
218
219
220
221
222
223
224
225 public static ContentType get(
226 final HttpEntity entity) throws ParseException, UnsupportedCharsetException {
227 if (entity == null) {
228 return null;
229 }
230 Header header = entity.getContentType();
231 if (header != null) {
232 HeaderElement[] elements = header.getElements();
233 if (elements.length > 0) {
234 return create(elements[0]);
235 }
236 }
237 return null;
238 }
239
240
241
242
243
244
245
246
247
248
249 public static ContentType getOrDefault(
250 final HttpEntity entity) throws ParseException, UnsupportedCharsetException {
251 ContentType contentType = get(entity);
252 return contentType != null ? contentType : DEFAULT_TEXT;
253 }
254
255 }