View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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.ArrayList;
34  import java.util.LinkedHashMap;
35  import java.util.List;
36  import java.util.Locale;
37  import java.util.Map;
38  
39  import org.apache.http.Consts;
40  import org.apache.http.Header;
41  import org.apache.http.HeaderElement;
42  import org.apache.http.HttpEntity;
43  import org.apache.http.NameValuePair;
44  import org.apache.http.ParseException;
45  import org.apache.http.annotation.ThreadingBehavior;
46  import org.apache.http.annotation.Contract;
47  import org.apache.http.message.BasicHeaderValueFormatter;
48  import org.apache.http.message.BasicHeaderValueParser;
49  import org.apache.http.message.BasicNameValuePair;
50  import org.apache.http.message.ParserCursor;
51  import org.apache.http.util.Args;
52  import org.apache.http.util.CharArrayBuffer;
53  import org.apache.http.util.TextUtils;
54  
55  /**
56   * Content type information consisting of a MIME type and an optional charset.
57   * <p>
58   * This class makes no attempts to verify validity of the MIME type.
59   * The input parameters of the {@link #create(String, String)} method, however, may not
60   * contain characters {@code <">, <;>, <,>} reserved by the HTTP specification.
61   *
62   * @since 4.2
63   */
64  @Contract(threading = ThreadingBehavior.IMMUTABLE)
65  public final class ContentType implements Serializable {
66  
67      private static final long serialVersionUID = -7768694718232371896L;
68  
69      // constants
70      public static final ContentType APPLICATION_ATOM_XML = create(
71              "application/atom+xml", Consts.ISO_8859_1);
72      public static final ContentType APPLICATION_FORM_URLENCODED = create(
73              "application/x-www-form-urlencoded", Consts.ISO_8859_1);
74      public static final ContentType APPLICATION_JSON = create(
75              "application/json", Consts.UTF_8);
76      public static final ContentType APPLICATION_OCTET_STREAM = create(
77              "application/octet-stream", (Charset) null);
78      public static final ContentType APPLICATION_SVG_XML = create(
79              "application/svg+xml", Consts.ISO_8859_1);
80      public static final ContentType APPLICATION_XHTML_XML = create(
81              "application/xhtml+xml", Consts.ISO_8859_1);
82      public static final ContentType APPLICATION_XML = create(
83              "application/xml", Consts.ISO_8859_1);
84      public static final ContentType MULTIPART_FORM_DATA = create(
85              "multipart/form-data", Consts.ISO_8859_1);
86      public static final ContentType TEXT_HTML = create(
87              "text/html", Consts.ISO_8859_1);
88      public static final ContentType TEXT_PLAIN = create(
89              "text/plain", Consts.ISO_8859_1);
90      public static final ContentType TEXT_XML = create(
91              "text/xml", Consts.ISO_8859_1);
92      public static final ContentType WILDCARD = create(
93              "*/*", (Charset) null);
94  
95      // defaults
96      public static final ContentType DEFAULT_TEXT = TEXT_PLAIN;
97      public static final ContentType DEFAULT_BINARY = APPLICATION_OCTET_STREAM;
98  
99      private final String mimeType;
100     private final Charset charset;
101     private final NameValuePair[] params;
102 
103     ContentType(
104             final String mimeType,
105             final Charset charset) {
106         this.mimeType = mimeType;
107         this.charset = charset;
108         this.params = null;
109     }
110 
111     ContentType(
112             final String mimeType,
113             final Charset charset,
114             final NameValuePair[] params) {
115         this.mimeType = mimeType;
116         this.charset = charset;
117         this.params = params;
118     }
119 
120     public String getMimeType() {
121         return this.mimeType;
122     }
123 
124     public Charset getCharset() {
125         return this.charset;
126     }
127 
128     /**
129      * @since 4.3
130      */
131     public String getParameter(final String name) {
132         Args.notEmpty(name, "Parameter name");
133         if (this.params == null) {
134             return null;
135         }
136         for (final NameValuePair param: this.params) {
137             if (param.getName().equalsIgnoreCase(name)) {
138                 return param.getValue();
139             }
140         }
141         return null;
142     }
143 
144     /**
145      * Generates textual representation of this content type which can be used as the value
146      * of a {@code Content-Type} header.
147      */
148     @Override
149     public String toString() {
150         final CharArrayBuffer buf = new CharArrayBuffer(64);
151         buf.append(this.mimeType);
152         if (this.params != null) {
153             buf.append("; ");
154             BasicHeaderValueFormatter.INSTANCE.formatParameters(buf, this.params, false);
155         } else if (this.charset != null) {
156             buf.append("; charset=");
157             buf.append(this.charset.name());
158         }
159         return buf.toString();
160     }
161 
162     private static boolean valid(final String s) {
163         for (int i = 0; i < s.length(); i++) {
164             final char ch = s.charAt(i);
165             if (ch == '"' || ch == ',' || ch == ';') {
166                 return false;
167             }
168         }
169         return true;
170     }
171 
172     /**
173      * Creates a new instance of {@link ContentType}.
174      *
175      * @param mimeType MIME type. It may not be {@code null} or empty. It may not contain
176      *        characters {@code <">, <;>, <,>} reserved by the HTTP specification.
177      * @param charset charset.
178      * @return content type
179      */
180     public static ContentType create(final String mimeType, final Charset charset) {
181         final String type = Args.notBlank(mimeType, "MIME type").toLowerCase(Locale.ROOT);
182         Args.check(valid(type), "MIME type may not contain reserved characters");
183         return new ContentType(type, charset);
184     }
185 
186     /**
187      * Creates a new instance of {@link ContentType} without a charset.
188      *
189      * @param mimeType MIME type. It may not be {@code null} or empty. It may not contain
190      *        characters {@code <">, <;>, <,>} reserved by the HTTP specification.
191      * @return content type
192      */
193     public static ContentType create(final String mimeType) {
194         return new ContentType(mimeType, (Charset) null);
195     }
196 
197     /**
198      * Creates a new instance of {@link ContentType}.
199      *
200      * @param mimeType MIME type. It may not be {@code null} or empty. It may not contain
201      *        characters {@code <">, <;>, <,>} reserved by the HTTP specification.
202      * @param charset charset. It may not contain characters {@code <">, <;>, <,>} reserved by the HTTP
203      *        specification. This parameter is optional.
204      * @return content type
205      * @throws UnsupportedCharsetException Thrown when the named charset is not available in
206      * this instance of the Java virtual machine
207      */
208     public static ContentType create(
209             final String mimeType, final String charset) throws UnsupportedCharsetException {
210         return create(mimeType, !TextUtils.isBlank(charset) ? Charset.forName(charset) : null);
211     }
212 
213     private static ContentType create(final HeaderElement helem, final boolean strict) {
214         return create(helem.getName(), helem.getParameters(), strict);
215     }
216 
217     private static ContentType create(final String mimeType, final NameValuePair[] params, final boolean strict) {
218         Charset charset = null;
219         for (final NameValuePair param: params) {
220             if (param.getName().equalsIgnoreCase("charset")) {
221                 final String s = param.getValue();
222                 if (!TextUtils.isBlank(s)) {
223                     try {
224                         charset =  Charset.forName(s);
225                     } catch (UnsupportedCharsetException ex) {
226                         if (strict) {
227                             throw ex;
228                         }
229                     }
230                 }
231                 break;
232             }
233         }
234         return new ContentType(mimeType, charset, params != null && params.length > 0 ? params : null);
235     }
236 
237     /**
238      * Creates a new instance of {@link ContentType} with the given parameters.
239      *
240      * @param mimeType MIME type. It may not be {@code null} or empty. It may not contain
241      *        characters {@code <">, <;>, <,>} reserved by the HTTP specification.
242      * @param params parameters.
243      * @return content type
244      *
245      * @since 4.4
246      */
247     public static ContentType create(
248             final String mimeType, final NameValuePair... params) throws UnsupportedCharsetException {
249         final String type = Args.notBlank(mimeType, "MIME type").toLowerCase(Locale.ROOT);
250         Args.check(valid(type), "MIME type may not contain reserved characters");
251         return create(mimeType, params, true);
252     }
253 
254     /**
255      * Parses textual representation of {@code Content-Type} value.
256      *
257      * @param s text
258      * @return content type
259      * @throws ParseException if the given text does not represent a valid
260      * {@code Content-Type} value.
261      * @throws UnsupportedCharsetException Thrown when the named charset is not available in
262      * this instance of the Java virtual machine
263      */
264     public static ContentType parse(
265             final String s) throws ParseException, UnsupportedCharsetException {
266         Args.notNull(s, "Content type");
267         final CharArrayBuffer buf = new CharArrayBuffer(s.length());
268         buf.append(s);
269         final ParserCursor cursor = new ParserCursor(0, s.length());
270         final HeaderElement[] elements = BasicHeaderValueParser.INSTANCE.parseElements(buf, cursor);
271         if (elements.length > 0) {
272             return create(elements[0], true);
273         } else {
274             throw new ParseException("Invalid content type: " + s);
275         }
276     }
277 
278     /**
279      * Extracts {@code Content-Type} value from {@link HttpEntity} exactly as
280      * specified by the {@code Content-Type} header of the entity. Returns {@code null}
281      * if not specified.
282      *
283      * @param entity HTTP entity
284      * @return content type
285      * @throws ParseException if the given text does not represent a valid
286      * {@code Content-Type} value.
287      * @throws UnsupportedCharsetException Thrown when the named charset is not available in
288      * this instance of the Java virtual machine
289      */
290     public static ContentType get(
291             final HttpEntity entity) throws ParseException, UnsupportedCharsetException {
292         if (entity == null) {
293             return null;
294         }
295         final Header header = entity.getContentType();
296         if (header != null) {
297             final HeaderElement[] elements = header.getElements();
298             if (elements.length > 0) {
299                 return create(elements[0], true);
300             }
301         }
302         return null;
303     }
304 
305     /**
306      * Extracts {@code Content-Type} value from {@link HttpEntity}. Returns {@code null}
307      * if not specified or incorrect (could not be parsed)..
308      *
309      * @param entity HTTP entity
310      * @return content type
311      *
312      * @since 4.4
313      *
314      */
315     public static ContentType getLenient(final HttpEntity entity) {
316         if (entity == null) {
317             return null;
318         }
319         final Header header = entity.getContentType();
320         if (header != null) {
321             try {
322                 final HeaderElement[] elements = header.getElements();
323                 if (elements.length > 0) {
324                     return create(elements[0], false);
325                 }
326             } catch (ParseException ex) {
327                 return null;
328             }
329         }
330         return null;
331     }
332 
333     /**
334      * Extracts {@code Content-Type} value from {@link HttpEntity} or returns the default value
335      * {@link #DEFAULT_TEXT} if not explicitly specified.
336      *
337      * @param entity HTTP entity
338      * @return content type
339      * @throws ParseException if the given text does not represent a valid
340      * {@code Content-Type} value.
341      * @throws UnsupportedCharsetException Thrown when the named charset is not available in
342      * this instance of the Java virtual machine
343      */
344     public static ContentType getOrDefault(
345             final HttpEntity entity) throws ParseException, UnsupportedCharsetException {
346         final ContentType contentType = get(entity);
347         return contentType != null ? contentType : DEFAULT_TEXT;
348     }
349 
350     /**
351      * Extracts {@code Content-Type} value from {@link HttpEntity} or returns the default value
352      * {@link #DEFAULT_TEXT} if not explicitly specified or incorrect (could not be parsed).
353      *
354      * @param entity HTTP entity
355      * @return content type
356      *
357      * @since 4.4
358      */
359     public static ContentType getLenientOrDefault(
360             final HttpEntity entity) throws ParseException, UnsupportedCharsetException {
361         final ContentType contentType = get(entity);
362         return contentType != null ? contentType : DEFAULT_TEXT;
363     }
364 
365     /**
366      * Creates a new instance with this MIME type and the given Charset.
367      *
368      * @param charset charset
369      * @return a new instance with this MIME type and the given Charset.
370      * @since 4.3
371      */
372     public ContentType withCharset(final Charset charset) {
373         return create(this.getMimeType(), charset);
374     }
375 
376     /**
377      * Creates a new instance with this MIME type and the given Charset name.
378      *
379      * @param charset name
380      * @return a new instance with this MIME type and the given Charset name.
381      * @throws UnsupportedCharsetException Thrown when the named charset is not available in
382      * this instance of the Java virtual machine
383      * @since 4.3
384      */
385     public ContentType withCharset(final String charset) {
386         return create(this.getMimeType(), charset);
387     }
388 
389     /**
390      * Creates a new instance with this MIME type and the given parameters.
391      *
392      * @param params
393      * @return a new instance with this MIME type and the given parameters.
394      * @since 4.4
395      */
396     public ContentType withParameters(
397             final NameValuePair... params) throws UnsupportedCharsetException {
398         if (params.length == 0) {
399             return this;
400         }
401         final Map<String, String> paramMap = new LinkedHashMap<String, String>();
402         if (this.params != null) {
403             for (NameValuePair param: this.params) {
404                 paramMap.put(param.getName(), param.getValue());
405             }
406         }
407         for (NameValuePair param: params) {
408             paramMap.put(param.getName(), param.getValue());
409         }
410         final List<NameValuePair> newParams = new ArrayList<NameValuePair>(paramMap.size() + 1);
411         if (this.charset != null && !paramMap.containsKey("charset")) {
412             newParams.add(new BasicNameValuePair("charset", this.charset.name()));
413         }
414         for (Map.Entry<String, String> entry: paramMap.entrySet()) {
415             newParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
416         }
417         return create(this.getMimeType(), newParams.toArray(new NameValuePair[newParams.size()]), true);
418     }
419 
420 }