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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * ====================================================================
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/>.
28 package name.gumartinm.weather.information.httpclient;
30 import java.nio.charset.Charset;
31 import java.nio.charset.UnsupportedCharsetException;
32 import java.util.Locale;
34 import org.apache.http.Header;
35 import org.apache.http.HeaderElement;
36 import org.apache.http.HttpEntity;
37 import org.apache.http.NameValuePair;
38 import org.apache.http.ParseException;
39 import org.apache.http.message.BasicHeaderValueParser;
42 * Content type information consisting of a MIME type and an optional charset.
44 * This class makes no attempts to verify validity of the MIME type.
45 * The input parameters of the {@link #create(String, String)} method, however, may not
46 * contain characters <">, <;>, <,> reserved by the HTTP specification.
51 public final class ContentType {
54 public static final ContentType APPLICATION_ATOM_XML = create(
55 "application/atom+xml", Consts.ISO_8859_1);
56 public static final ContentType APPLICATION_FORM_URLENCODED = create(
57 "application/x-www-form-urlencoded", Consts.ISO_8859_1);
58 public static final ContentType APPLICATION_JSON = create(
59 "application/json", Consts.UTF_8);
60 public static final ContentType APPLICATION_OCTET_STREAM = create(
61 "application/octet-stream", (Charset) null);
62 public static final ContentType APPLICATION_SVG_XML = create(
63 "application/svg+xml", Consts.ISO_8859_1);
64 public static final ContentType APPLICATION_XHTML_XML = create(
65 "application/xhtml+xml", Consts.ISO_8859_1);
66 public static final ContentType APPLICATION_XML = create(
67 "application/xml", Consts.ISO_8859_1);
68 public static final ContentType MULTIPART_FORM_DATA = create(
69 "multipart/form-data", Consts.ISO_8859_1);
70 public static final ContentType TEXT_HTML = create(
71 "text/html", Consts.ISO_8859_1);
72 public static final ContentType TEXT_PLAIN = create(
73 "text/plain", Consts.ISO_8859_1);
74 public static final ContentType TEXT_XML = create(
75 "text/xml", Consts.ISO_8859_1);
76 public static final ContentType WILDCARD = create(
77 "*/*", (Charset) null);
80 public static final ContentType DEFAULT_TEXT = TEXT_PLAIN;
81 public static final ContentType DEFAULT_BINARY = APPLICATION_OCTET_STREAM;
83 private final String mimeType;
84 private final Charset charset;
87 * Given a MIME type and a character set, constructs a ContentType.
88 * @param mimeType The MIME type to use for the ContentType header.
89 * @param charset The optional character set to use with the ContentType header.
90 * @throws UnsupportedCharsetException
91 * If no support for the named charset is available in this Java virtual machine
93 ContentType(final String mimeType, final Charset charset) {
94 this.mimeType = mimeType;
95 this.charset = charset;
98 public String getMimeType() {
102 public Charset getCharset() {
107 * Converts a ContentType to a string which can be used as a ContentType header.
108 * If a charset is provided by the ContentType, it will be included in the string.
111 public String toString() {
112 final StringBuilder buf = new StringBuilder();
113 buf.append(this.mimeType);
114 if (this.charset != null) {
115 buf.append("; charset=");
116 buf.append(this.charset);
118 return buf.toString();
121 private static boolean valid(final String s) {
122 for (int i = 0; i < s.length(); i++) {
123 final char ch = s.charAt(i);
124 if ((ch == '"') || (ch == ',') || (ch == ';')) {
132 * Creates a new instance of {@link ContentType}.
134 * @param mimeType MIME type. It may not be <code>null</code> or empty. It may not contain
135 * characters <">, <;>, <,> reserved by the HTTP specification.
136 * @param charset charset.
137 * @return content type
139 public static ContentType create(final String mimeType, final Charset charset) {
140 if (mimeType == null) {
141 throw new IllegalArgumentException("MIME type may not be null");
143 final String type = mimeType.trim().toLowerCase(Locale.US);
144 if (type.length() == 0) {
145 throw new IllegalArgumentException("MIME type may not be empty");
148 throw new IllegalArgumentException("MIME type may not contain reserved characters");
150 return new ContentType(type, charset);
154 * Creates a new instance of {@link ContentType} without a charset.
156 * @param mimeType MIME type. It may not be <code>null</code> or empty. It may not contain
157 * characters <">, <;>, <,> reserved by the HTTP specification.
158 * @return content type
160 public static ContentType create(final String mimeType) {
161 return new ContentType(mimeType, (Charset) null);
165 * Creates a new instance of {@link ContentType}.
167 * @param mimeType MIME type. It may not be <code>null</code> or empty. It may not contain
168 * characters <">, <;>, <,> reserved by the HTTP specification.
169 * @param charset charset. It may not contain characters <">, <;>, <,> reserved by the HTTP
170 * specification. This parameter is optional.
171 * @return content type
173 public static ContentType create(
174 final String mimeType, final String charset) throws UnsupportedCharsetException {
175 return create(mimeType, charset != null ? Charset.forName(charset) : null);
178 private static ContentType create(final HeaderElement helem) {
179 final String mimeType = helem.getName();
180 String charset = null;
181 final NameValuePair param = helem.getParameterByName("charset");
183 charset = param.getValue();
185 return create(mimeType, charset);
189 * Parses textual representation of <code>Content-Type</code> value.
192 * @return content type
193 * @throws ParseException if the given text does not represent a valid
194 * <code>Content-Type</code> value.
196 public static ContentType parse(
197 final String s) throws ParseException, UnsupportedCharsetException {
199 throw new IllegalArgumentException("Content type may not be null");
201 final HeaderElement[] elements = BasicHeaderValueParser.parseElements(s, null);
202 if (elements.length > 0) {
203 return create(elements[0]);
205 throw new ParseException("Invalid content type: " + s);
210 * Extracts <code>Content-Type</code> value from {@link HttpEntity} exactly as
211 * specified by the <code>Content-Type</code> header of the entity. Returns <code>null</code>
214 * @param entity HTTP entity
215 * @return content type
216 * @throws ParseException if the given text does not represent a valid
217 * <code>Content-Type</code> value.
219 public static ContentType get(
220 final HttpEntity entity) throws ParseException, UnsupportedCharsetException {
221 if (entity == null) {
224 final Header header = entity.getContentType();
225 if (header != null) {
226 final HeaderElement[] elements = header.getElements();
227 if (elements.length > 0) {
228 return create(elements[0]);
235 * Extracts <code>Content-Type</code> value from {@link HttpEntity} or returns default value
236 * if not explicitly specified.
238 * @param entity HTTP entity
239 * @return content type
240 * @throws ParseException if the given text does not represent a valid
241 * <code>Content-Type</code> value.
243 public static ContentType getOrDefault(final HttpEntity entity) throws ParseException {
244 final ContentType contentType = get(entity);
245 return contentType != null ? contentType : DEFAULT_TEXT;