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