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.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   * Content type information consisting of a MIME type and an optional charset.
46   * <p/>
47   * This class makes no attempts to verify validity of the MIME type.
48   * The input parameters of the {@link #create(String, String)} method, however, may not
49   * contain characters <">, <;>, <,> reserved by the HTTP specification.
50   *
51   * @since 4.2
52   */
53  @Immutable
54  public final class ContentType implements Serializable {
55  
56      private static final long serialVersionUID = -7768694718232371896L;
57      
58      // constants
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      // defaults
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       * Given a MIME type and a character set, constructs a ContentType.
93       * @param mimeType The MIME type to use for the ContentType header.
94       * @param charset The optional character set to use with the ContentType header.
95       * @throws  UnsupportedCharsetException
96       *          If no support for the named charset is available in this Java virtual machine
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      * Converts a ContentType to a string which can be used as a ContentType header.
113      * If a charset is provided by the ContentType, it will be included in the string.
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      * Creates a new instance of {@link ContentType}.
138      *
139      * @param mimeType MIME type. It may not be <code>null</code> or empty. It may not contain
140      *        characters <">, <;>, <,> reserved by the HTTP specification.
141      * @param charset charset.
142      * @return content type
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      * Creates a new instance of {@link ContentType} without a charset.
160      *
161      * @param mimeType MIME type. It may not be <code>null</code> or empty. It may not contain
162      *        characters <">, <;>, <,> reserved by the HTTP specification.
163      * @return content type
164      */
165     public static ContentType create(final String mimeType) {
166         return new ContentType(mimeType, (Charset) null);
167     }
168     
169     /**
170      * Creates a new instance of {@link ContentType}.
171      *
172      * @param mimeType MIME type. It may not be <code>null</code> or empty. It may not contain
173      *        characters <">, <;>, <,> reserved by the HTTP specification.
174      * @param charset charset. It may not contain characters <">, <;>, <,> reserved by the HTTP
175      *        specification. This parameter is optional.
176      * @return content type
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      * Parses textual representation of <code>Content-Type</code> value.
196      *
197      * @param s text
198      * @return content type
199      * @throws ParseException if the given text does not represent a valid
200      * <code>Content-Type</code> value.
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      * Extracts <code>Content-Type</code> value from {@link HttpEntity} exactly as
217      * specified by the <code>Content-Type</code> header of the entity. Returns <code>null</code>
218      * if not specified.
219      *
220      * @param entity HTTP entity
221      * @return content type
222      * @throws ParseException if the given text does not represent a valid
223      * <code>Content-Type</code> value.
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      * Extracts <code>Content-Type</code> value from {@link HttpEntity} or returns default value
242      * if not explicitly specified.
243      *
244      * @param entity HTTP entity
245      * @return content type
246      * @throws ParseException if the given text does not represent a valid
247      * <code>Content-Type</code> value.
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 }