• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.net;
18 
19 import static com.google.common.base.CharMatcher.ASCII;
20 import static com.google.common.base.CharMatcher.JAVA_ISO_CONTROL;
21 import static com.google.common.base.Charsets.UTF_8;
22 import static com.google.common.base.Preconditions.checkArgument;
23 import static com.google.common.base.Preconditions.checkNotNull;
24 import static com.google.common.base.Preconditions.checkState;
25 
26 import com.google.common.annotations.Beta;
27 import com.google.common.annotations.GwtCompatible;
28 import com.google.common.base.Ascii;
29 import com.google.common.base.CharMatcher;
30 import com.google.common.base.Function;
31 import com.google.common.base.Joiner;
32 import com.google.common.base.Joiner.MapJoiner;
33 import com.google.common.base.MoreObjects;
34 import com.google.common.base.Objects;
35 import com.google.common.base.Optional;
36 import com.google.common.collect.ImmutableListMultimap;
37 import com.google.common.collect.ImmutableMultiset;
38 import com.google.common.collect.ImmutableSet;
39 import com.google.common.collect.Iterables;
40 import com.google.common.collect.Maps;
41 import com.google.common.collect.Multimap;
42 import com.google.common.collect.Multimaps;
43 
44 import java.nio.charset.Charset;
45 import java.nio.charset.IllegalCharsetNameException;
46 import java.nio.charset.UnsupportedCharsetException;
47 import java.util.Collection;
48 import java.util.Map;
49 import java.util.Map.Entry;
50 
51 import javax.annotation.Nullable;
52 import javax.annotation.concurrent.Immutable;
53 
54 /**
55  * Represents an <a href="http://en.wikipedia.org/wiki/Internet_media_type">Internet Media Type</a>
56  * (also known as a MIME Type or Content Type). This class also supports the concept of media ranges
57  * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1">defined by HTTP/1.1</a>.
58  * As such, the {@code *} character is treated as a wildcard and is used to represent any acceptable
59  * type or subtype value. A media type may not have wildcard type with a declared subtype. The
60  * {@code *} character has no special meaning as part of a parameter. All values for type, subtype,
61  * parameter attributes or parameter values must be valid according to RFCs
62  * <a href="http://www.ietf.org/rfc/rfc2045.txt">2045</a> and
63  * <a href="http://www.ietf.org/rfc/rfc2046.txt">2046</a>.
64  *
65  * <p>All portions of the media type that are case-insensitive (type, subtype, parameter attributes)
66  * are normalized to lowercase. The value of the {@code charset} parameter is normalized to
67  * lowercase, but all others are left as-is.
68  *
69  * <p>Note that this specifically does <strong>not</strong> represent the value of the MIME
70  * {@code Content-Type} header and as such has no support for header-specific considerations such as
71  * line folding and comments.
72  *
73  * <p>For media types that take a charset the predefined constants default to UTF-8 and have a
74  * "_UTF_8" suffix. To get a version without a character set, use {@link #withoutParameters}.
75  *
76  * @since 12.0
77  *
78  * @author Gregory Kick
79  */
80 @Beta
81 @GwtCompatible
82 @Immutable
83 public final class MediaType {
84   private static final String CHARSET_ATTRIBUTE = "charset";
85   private static final ImmutableListMultimap<String, String> UTF_8_CONSTANT_PARAMETERS =
86       ImmutableListMultimap.of(CHARSET_ATTRIBUTE, Ascii.toLowerCase(UTF_8.name()));
87 
88   /** Matcher for type, subtype and attributes. */
89   private static final CharMatcher TOKEN_MATCHER = ASCII.and(JAVA_ISO_CONTROL.negate())
90       .and(CharMatcher.isNot(' '))
91       .and(CharMatcher.noneOf("()<>@,;:\\\"/[]?="));
92   private static final CharMatcher QUOTED_TEXT_MATCHER = ASCII
93       .and(CharMatcher.noneOf("\"\\\r"));
94   /*
95    * This matches the same characters as linear-white-space from RFC 822, but we make no effort to
96    * enforce any particular rules with regards to line folding as stated in the class docs.
97    */
98   private static final CharMatcher LINEAR_WHITE_SPACE = CharMatcher.anyOf(" \t\r\n");
99 
100   // TODO(gak): make these public?
101   private static final String APPLICATION_TYPE = "application";
102   private static final String AUDIO_TYPE = "audio";
103   private static final String IMAGE_TYPE = "image";
104   private static final String TEXT_TYPE = "text";
105   private static final String VIDEO_TYPE = "video";
106 
107   private static final String WILDCARD = "*";
108 
109   private static final Map<MediaType, MediaType> KNOWN_TYPES = Maps.newHashMap();
110 
createConstant(String type, String subtype)111   private static MediaType createConstant(String type, String subtype) {
112     return addKnownType(new MediaType(type, subtype, ImmutableListMultimap.<String, String>of()));
113   }
114 
createConstantUtf8(String type, String subtype)115   private static MediaType createConstantUtf8(String type, String subtype) {
116     return addKnownType(new MediaType(type, subtype, UTF_8_CONSTANT_PARAMETERS));
117   }
118 
addKnownType(MediaType mediaType)119   private static MediaType addKnownType(MediaType mediaType) {
120     KNOWN_TYPES.put(mediaType, mediaType);
121     return mediaType;
122   }
123 
124   /*
125    * The following constants are grouped by their type and ordered alphabetically by the constant
126    * name within that type. The constant name should be a sensible identifier that is closest to the
127    * "common name" of the media.  This is often, but not necessarily the same as the subtype.
128    *
129    * Be sure to declare all constants with the type and subtype in all lowercase. For types that
130    * take a charset (e.g. all text/* types), default to UTF-8 and suffix the constant name with
131    * "_UTF_8".
132    */
133 
134   public static final MediaType ANY_TYPE = createConstant(WILDCARD, WILDCARD);
135   public static final MediaType ANY_TEXT_TYPE = createConstant(TEXT_TYPE, WILDCARD);
136   public static final MediaType ANY_IMAGE_TYPE = createConstant(IMAGE_TYPE, WILDCARD);
137   public static final MediaType ANY_AUDIO_TYPE = createConstant(AUDIO_TYPE, WILDCARD);
138   public static final MediaType ANY_VIDEO_TYPE = createConstant(VIDEO_TYPE, WILDCARD);
139   public static final MediaType ANY_APPLICATION_TYPE = createConstant(APPLICATION_TYPE, WILDCARD);
140 
141   /* text types */
142   public static final MediaType CACHE_MANIFEST_UTF_8 =
143       createConstantUtf8(TEXT_TYPE, "cache-manifest");
144   public static final MediaType CSS_UTF_8 = createConstantUtf8(TEXT_TYPE, "css");
145   public static final MediaType CSV_UTF_8 = createConstantUtf8(TEXT_TYPE, "csv");
146   public static final MediaType HTML_UTF_8 = createConstantUtf8(TEXT_TYPE, "html");
147   public static final MediaType I_CALENDAR_UTF_8 = createConstantUtf8(TEXT_TYPE, "calendar");
148   public static final MediaType PLAIN_TEXT_UTF_8 = createConstantUtf8(TEXT_TYPE, "plain");
149   /**
150    * <a href="http://www.rfc-editor.org/rfc/rfc4329.txt">RFC 4329</a> declares
151    * {@link #JAVASCRIPT_UTF_8 application/javascript} to be the correct media type for JavaScript,
152    * but this may be necessary in certain situations for compatibility.
153    */
154   public static final MediaType TEXT_JAVASCRIPT_UTF_8 = createConstantUtf8(TEXT_TYPE, "javascript");
155   /**
156    * <a href="http://www.iana.org/assignments/media-types/text/tab-separated-values">
157    * Tab separated values</a>.
158    *
159    * @since 15.0
160    */
161   public static final MediaType TSV_UTF_8 = createConstantUtf8(TEXT_TYPE, "tab-separated-values");
162   public static final MediaType VCARD_UTF_8 = createConstantUtf8(TEXT_TYPE, "vcard");
163   public static final MediaType WML_UTF_8 = createConstantUtf8(TEXT_TYPE, "vnd.wap.wml");
164   /**
165    * As described in <a href="http://www.ietf.org/rfc/rfc3023.txt">RFC 3023</a>, this constant
166    * ({@code text/xml}) is used for XML documents that are "readable by casual users."
167    * {@link #APPLICATION_XML_UTF_8} is provided for documents that are intended for applications.
168    */
169   public static final MediaType XML_UTF_8 = createConstantUtf8(TEXT_TYPE, "xml");
170 
171   /* image types */
172   public static final MediaType BMP = createConstant(IMAGE_TYPE, "bmp");
173   /**
174    * The media type for the <a href="http://en.wikipedia.org/wiki/Camera_Image_File_Format">Canon
175    * Image File Format</a> ({@code crw} files), a widely-used "raw image" format for cameras. It is
176    * found in {@code /etc/mime.types}, e.g. in <href=
177    * "http://anonscm.debian.org/gitweb/?p=collab-maint/mime-support.git;a=blob;f=mime.types;hb=HEAD"
178    * >Debian 3.48-1</a>.
179    *
180    * @since 15.0
181    */
182   public static final MediaType CRW = createConstant(IMAGE_TYPE, "x-canon-crw");
183   public static final MediaType GIF = createConstant(IMAGE_TYPE, "gif");
184   public static final MediaType ICO = createConstant(IMAGE_TYPE, "vnd.microsoft.icon");
185   public static final MediaType JPEG = createConstant(IMAGE_TYPE, "jpeg");
186   public static final MediaType PNG = createConstant(IMAGE_TYPE, "png");
187   /**
188    * The media type for the Photoshop File Format ({@code psd} files) as defined by <a href=
189    * "http://www.iana.org/assignments/media-types/image/vnd.adobe.photoshop">IANA</a>, and found in
190    * {@code /etc/mime.types}, e.g. <a href=
191    * "http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types"></a> of the Apache
192    * <a href="http://httpd.apache.org/">HTTPD project</a>; for the specification, see
193    * <href="http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/PhotoshopFileFormats.htm">
194    * Adobe Photoshop Document Format</a> and <a href=
195    * "http://en.wikipedia.org/wiki/Adobe_Photoshop#File_format">Wikipedia</a>; this is the regular
196    * output/input of Photoshop (which can also export to various image formats; note that files with
197    * extension "PSB" are in a distinct but related format).
198    * <p>This is a more recent replacement for the older, experimental type
199    * {@code x-photoshop}: <a href="http://tools.ietf.org/html/rfc2046#section-6">RFC-2046.6</a>.
200    *
201    * @since 15.0
202    */
203   public static final MediaType PSD = createConstant(IMAGE_TYPE, "vnd.adobe.photoshop");
204   public static final MediaType SVG_UTF_8 = createConstantUtf8(IMAGE_TYPE, "svg+xml");
205   public static final MediaType TIFF = createConstant(IMAGE_TYPE, "tiff");
206   public static final MediaType WEBP = createConstant(IMAGE_TYPE, "webp");
207 
208   /* audio types */
209   public static final MediaType MP4_AUDIO = createConstant(AUDIO_TYPE, "mp4");
210   public static final MediaType MPEG_AUDIO = createConstant(AUDIO_TYPE, "mpeg");
211   public static final MediaType OGG_AUDIO = createConstant(AUDIO_TYPE, "ogg");
212   public static final MediaType WEBM_AUDIO = createConstant(AUDIO_TYPE, "webm");
213 
214   /* video types */
215   public static final MediaType MP4_VIDEO = createConstant(VIDEO_TYPE, "mp4");
216   public static final MediaType MPEG_VIDEO = createConstant(VIDEO_TYPE, "mpeg");
217   public static final MediaType OGG_VIDEO = createConstant(VIDEO_TYPE, "ogg");
218   public static final MediaType QUICKTIME = createConstant(VIDEO_TYPE, "quicktime");
219   public static final MediaType WEBM_VIDEO = createConstant(VIDEO_TYPE, "webm");
220   public static final MediaType WMV = createConstant(VIDEO_TYPE, "x-ms-wmv");
221 
222   /* application types */
223   /**
224    * As described in <a href="http://www.ietf.org/rfc/rfc3023.txt">RFC 3023</a>, this constant
225    * ({@code application/xml}) is used for XML documents that are "unreadable by casual users."
226    * {@link #XML_UTF_8} is provided for documents that may be read by users.
227    */
228   public static final MediaType APPLICATION_XML_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "xml");
229   public static final MediaType ATOM_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "atom+xml");
230   public static final MediaType BZIP2 = createConstant(APPLICATION_TYPE, "x-bzip2");
231 
232   /**
233    * Media type for <a href="http://en.wikipedia.org/wiki/Embedded_OpenType">Embedded OpenType</a>
234    * fonts. This is
235    * <a href="http://www.iana.org/assignments/media-types/application/vnd.ms-fontobject">registered
236    * </a> with the IANA.
237    *
238    * @since 17.0
239    */
240   public static final MediaType EOT = createConstant(APPLICATION_TYPE, "vnd.ms-fontobject");
241   /**
242    * As described in the <a href="http://idpf.org/epub">International Digital Publishing Forum</a>
243    * EPUB is the distribution and interchange format standard for digital publications and
244    * documents. This media type is defined in the
245    * <a href="http://www.idpf.org/epub/30/spec/epub30-ocf.html">EPUB Open Container Format</a>
246    * specification.
247    *
248    * @since 15.0
249    */
250   public static final MediaType EPUB = createConstant(APPLICATION_TYPE, "epub+zip");
251   public static final MediaType FORM_DATA = createConstant(APPLICATION_TYPE,
252       "x-www-form-urlencoded");
253   /**
254    * As described in <a href="https://www.rsa.com/rsalabs/node.asp?id=2138">PKCS #12: Personal
255    * Information Exchange Syntax Standard</a>, PKCS #12 defines an archive file format for storing
256    * many cryptography objects as a single file.
257    *
258    * @since 15.0
259    */
260   public static final MediaType KEY_ARCHIVE = createConstant(APPLICATION_TYPE, "pkcs12");
261   /**
262    * This is a non-standard media type, but is commonly used in serving hosted binary files as it is
263    * <a href="http://code.google.com/p/browsersec/wiki/Part2#Survey_of_content_sniffing_behaviors">
264    * known not to trigger content sniffing in current browsers</a>. It <i>should not</i> be used in
265    * other situations as it is not specified by any RFC and does not appear in the <a href=
266    * "http://www.iana.org/assignments/media-types">/IANA MIME Media Types</a> list. Consider
267    * {@link #OCTET_STREAM} for binary data that is not being served to a browser.
268    *
269    *
270    * @since 14.0
271    */
272   public static final MediaType APPLICATION_BINARY = createConstant(APPLICATION_TYPE, "binary");
273   public static final MediaType GZIP = createConstant(APPLICATION_TYPE, "x-gzip");
274    /**
275     * <a href="http://www.rfc-editor.org/rfc/rfc4329.txt">RFC 4329</a> declares this to be the
276     * correct media type for JavaScript, but {@link #TEXT_JAVASCRIPT_UTF_8 text/javascript} may be
277     * necessary in certain situations for compatibility.
278     */
279   public static final MediaType JAVASCRIPT_UTF_8 =
280       createConstantUtf8(APPLICATION_TYPE, "javascript");
281   public static final MediaType JSON_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "json");
282   public static final MediaType KML = createConstant(APPLICATION_TYPE, "vnd.google-earth.kml+xml");
283   public static final MediaType KMZ = createConstant(APPLICATION_TYPE, "vnd.google-earth.kmz");
284   public static final MediaType MBOX = createConstant(APPLICATION_TYPE, "mbox");
285 
286   /**
287    * Media type for
288    * <a href="http://goo.gl/1pGBFm">Apple over-the-air mobile configuration profiles</a>.
289    *
290    * @since 18.0
291    */
292   public static final MediaType APPLE_MOBILE_CONFIG =
293       createConstant(APPLICATION_TYPE, "x-apple-aspen-config");
294   public static final MediaType MICROSOFT_EXCEL = createConstant(APPLICATION_TYPE, "vnd.ms-excel");
295   public static final MediaType MICROSOFT_POWERPOINT =
296       createConstant(APPLICATION_TYPE, "vnd.ms-powerpoint");
297   public static final MediaType MICROSOFT_WORD = createConstant(APPLICATION_TYPE, "msword");
298   public static final MediaType OCTET_STREAM = createConstant(APPLICATION_TYPE, "octet-stream");
299   public static final MediaType OGG_CONTAINER = createConstant(APPLICATION_TYPE, "ogg");
300   public static final MediaType OOXML_DOCUMENT = createConstant(APPLICATION_TYPE,
301       "vnd.openxmlformats-officedocument.wordprocessingml.document");
302   public static final MediaType OOXML_PRESENTATION = createConstant(APPLICATION_TYPE,
303       "vnd.openxmlformats-officedocument.presentationml.presentation");
304   public static final MediaType OOXML_SHEET =
305       createConstant(APPLICATION_TYPE, "vnd.openxmlformats-officedocument.spreadsheetml.sheet");
306   public static final MediaType OPENDOCUMENT_GRAPHICS =
307       createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.graphics");
308   public static final MediaType OPENDOCUMENT_PRESENTATION =
309       createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.presentation");
310   public static final MediaType OPENDOCUMENT_SPREADSHEET =
311       createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.spreadsheet");
312   public static final MediaType OPENDOCUMENT_TEXT =
313       createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.text");
314   public static final MediaType PDF = createConstant(APPLICATION_TYPE, "pdf");
315   public static final MediaType POSTSCRIPT = createConstant(APPLICATION_TYPE, "postscript");
316   /**
317    * <a href="http://tools.ietf.org/html/draft-rfernando-protocol-buffers-00">Protocol buffers</a>
318    *
319    * @since 15.0
320    */
321   public static final MediaType PROTOBUF = createConstant(APPLICATION_TYPE, "protobuf");
322   public static final MediaType RDF_XML_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "rdf+xml");
323   public static final MediaType RTF_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "rtf");
324   /**
325    * Media type for SFNT fonts (which includes
326    * <a href="http://en.wikipedia.org/wiki/TrueType/">TrueType</a> and
327    * <a href="http://en.wikipedia.org/wiki/OpenType/">OpenType</a> fonts). This is
328    * <a href="http://www.iana.org/assignments/media-types/application/font-sfnt">registered</a>
329    * with the IANA.
330    *
331    * @since 17.0
332    */
333   public static final MediaType SFNT = createConstant(APPLICATION_TYPE, "font-sfnt");
334   public static final MediaType SHOCKWAVE_FLASH = createConstant(APPLICATION_TYPE,
335       "x-shockwave-flash");
336   public static final MediaType SKETCHUP = createConstant(APPLICATION_TYPE, "vnd.sketchup.skp");
337   public static final MediaType TAR = createConstant(APPLICATION_TYPE, "x-tar");
338   /**
339    * Media type for the
340    * <a href="http://en.wikipedia.org/wiki/Web_Open_Font_Format">Web Open Font Format</a> (WOFF)
341    * <a href="http://www.w3.org/TR/WOFF/">defined</a> by the W3C. This is
342    * <a href="http://www.iana.org/assignments/media-types/application/font-woff">registered</a>
343    * with the IANA.
344    *
345    * @since 17.0
346    */
347   public static final MediaType WOFF = createConstant(APPLICATION_TYPE, "font-woff");
348   public static final MediaType XHTML_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "xhtml+xml");
349   /**
350    * Media type for Extensible Resource Descriptors. This is not yet registered with the IANA, but
351    * it is specified by OASIS in the
352    * <a href="http://docs.oasis-open.org/xri/xrd/v1.0/cd02/xrd-1.0-cd02.html"> XRD definition</a>
353    * and implemented in projects such as
354    * <a href="http://code.google.com/p/webfinger/">WebFinger</a>.
355    */
356   public static final MediaType XRD_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "xrd+xml");
357   public static final MediaType ZIP = createConstant(APPLICATION_TYPE, "zip");
358 
359   private final String type;
360   private final String subtype;
361   private final ImmutableListMultimap<String, String> parameters;
362 
MediaType(String type, String subtype, ImmutableListMultimap<String, String> parameters)363   private MediaType(String type, String subtype,
364       ImmutableListMultimap<String, String> parameters) {
365     this.type = type;
366     this.subtype = subtype;
367     this.parameters = parameters;
368   }
369 
370   /** Returns the top-level media type.  For example, {@code "text"} in {@code "text/plain"}. */
type()371   public String type() {
372     return type;
373   }
374 
375   /** Returns the media subtype.  For example, {@code "plain"} in {@code "text/plain"}. */
subtype()376   public String subtype() {
377     return subtype;
378   }
379 
380   /** Returns a multimap containing the parameters of this media type. */
parameters()381   public ImmutableListMultimap<String, String> parameters() {
382     return parameters;
383   }
384 
parametersAsMap()385   private Map<String, ImmutableMultiset<String>> parametersAsMap() {
386     return Maps.transformValues(parameters.asMap(),
387         new Function<Collection<String>, ImmutableMultiset<String>>() {
388           @Override public ImmutableMultiset<String> apply(Collection<String> input) {
389             return ImmutableMultiset.copyOf(input);
390           }
391         });
392   }
393 
394   /**
395    * Returns an optional charset for the value of the charset parameter if it is specified.
396    *
397    * @throws IllegalStateException if multiple charset values have been set for this media type
398    * @throws IllegalCharsetNameException if a charset value is present, but illegal
399    * @throws UnsupportedCharsetException if a charset value is present, but no support is available
400    *     in this instance of the Java virtual machine
401    */
402   public Optional<Charset> charset() {
403     ImmutableSet<String> charsetValues = ImmutableSet.copyOf(parameters.get(CHARSET_ATTRIBUTE));
404     switch (charsetValues.size()) {
405       case 0:
406         return Optional.absent();
407       case 1:
408         return Optional.of(Charset.forName(Iterables.getOnlyElement(charsetValues)));
409       default:
410         throw new IllegalStateException("Multiple charset values defined: " + charsetValues);
411     }
412   }
413 
414   /**
415    * Returns a new instance with the same type and subtype as this instance, but without any
416    * parameters.
417    */
418   public MediaType withoutParameters() {
419     return parameters.isEmpty() ? this : create(type, subtype);
420   }
421 
422   /**
423    * <em>Replaces</em> all parameters with the given parameters.
424    *
425    * @throws IllegalArgumentException if any parameter or value is invalid
426    */
427   public MediaType withParameters(Multimap<String, String> parameters) {
428     return create(type, subtype, parameters);
429   }
430 
431   /**
432    * <em>Replaces</em> all parameters with the given attribute with a single parameter with the
433    * given value. If multiple parameters with the same attributes are necessary use
434    * {@link #withParameters}. Prefer {@link #withCharset} for setting the {@code charset} parameter
435    * when using a {@link Charset} object.
436    *
437    * @throws IllegalArgumentException if either {@code attribute} or {@code value} is invalid
438    */
439   public MediaType withParameter(String attribute, String value) {
440     checkNotNull(attribute);
441     checkNotNull(value);
442     String normalizedAttribute = normalizeToken(attribute);
443     ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
444     for (Entry<String, String> entry : parameters.entries()) {
445       String key = entry.getKey();
446       if (!normalizedAttribute.equals(key)) {
447         builder.put(key, entry.getValue());
448       }
449     }
450     builder.put(normalizedAttribute, normalizeParameterValue(normalizedAttribute, value));
451     MediaType mediaType = new MediaType(type, subtype, builder.build());
452     // Return one of the constants if the media type is a known type.
453     return MoreObjects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType);
454   }
455 
456   /**
457    * Returns a new instance with the same type and subtype as this instance, with the
458    * {@code charset} parameter set to the {@link Charset#name name} of the given charset. Only one
459    * {@code charset} parameter will be present on the new instance regardless of the number set on
460    * this one.
461    *
462    * <p>If a charset must be specified that is not supported on this JVM (and thus is not
463    * representable as a {@link Charset} instance, use {@link #withParameter}.
464    */
465   public MediaType withCharset(Charset charset) {
466     checkNotNull(charset);
467     return withParameter(CHARSET_ATTRIBUTE, charset.name());
468   }
469 
470   /** Returns true if either the type or subtype is the wildcard. */
471   public boolean hasWildcard() {
472     return WILDCARD.equals(type) || WILDCARD.equals(subtype);
473   }
474 
475   /**
476    * Returns {@code true} if this instance falls within the range (as defined by
477    * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">the HTTP Accept header</a>)
478    * given by the argument according to three criteria:
479    *
480    * <ol>
481    * <li>The type of the argument is the wildcard or equal to the type of this instance.
482    * <li>The subtype of the argument is the wildcard or equal to the subtype of this instance.
483    * <li>All of the parameters present in the argument are present in this instance.
484    * </ol>
485    *
486    * <p>For example: <pre>   {@code
487    *   PLAIN_TEXT_UTF_8.is(PLAIN_TEXT_UTF_8) // true
488    *   PLAIN_TEXT_UTF_8.is(HTML_UTF_8) // false
489    *   PLAIN_TEXT_UTF_8.is(ANY_TYPE) // true
490    *   PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE) // true
491    *   PLAIN_TEXT_UTF_8.is(ANY_IMAGE_TYPE) // false
492    *   PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE.withCharset(UTF_8)) // true
493    *   PLAIN_TEXT_UTF_8.withoutParameters().is(ANY_TEXT_TYPE.withCharset(UTF_8)) // false
494    *   PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE.withCharset(UTF_16)) // false}</pre>
495    *
496    * <p>Note that while it is possible to have the same parameter declared multiple times within a
497    * media type this method does not consider the number of occurrences of a parameter.  For
498    * example, {@code "text/plain; charset=UTF-8"} satisfies
499    * {@code "text/plain; charset=UTF-8; charset=UTF-8"}.
500    */
501   public boolean is(MediaType mediaTypeRange) {
502     return (mediaTypeRange.type.equals(WILDCARD) || mediaTypeRange.type.equals(this.type))
503         && (mediaTypeRange.subtype.equals(WILDCARD) || mediaTypeRange.subtype.equals(this.subtype))
504         && this.parameters.entries().containsAll(mediaTypeRange.parameters.entries());
505   }
506 
507   /**
508    * Creates a new media type with the given type and subtype.
509    *
510    * @throws IllegalArgumentException if type or subtype is invalid or if a wildcard is used for the
511    * type, but not the subtype.
512    */
513   public static MediaType create(String type, String subtype) {
514     return create(type, subtype, ImmutableListMultimap.<String, String>of());
515   }
516 
517   /**
518    * Creates a media type with the "application" type and the given subtype.
519    *
520    * @throws IllegalArgumentException if subtype is invalid
521    */
522   static MediaType createApplicationType(String subtype) {
523     return create(APPLICATION_TYPE, subtype);
524   }
525 
526   /**
527    * Creates a media type with the "audio" type and the given subtype.
528    *
529    * @throws IllegalArgumentException if subtype is invalid
530    */
531   static MediaType createAudioType(String subtype) {
532     return create(AUDIO_TYPE, subtype);
533   }
534 
535   /**
536    * Creates a media type with the "image" type and the given subtype.
537    *
538    * @throws IllegalArgumentException if subtype is invalid
539    */
540   static MediaType createImageType(String subtype) {
541     return create(IMAGE_TYPE, subtype);
542   }
543 
544   /**
545    * Creates a media type with the "text" type and the given subtype.
546    *
547    * @throws IllegalArgumentException if subtype is invalid
548    */
549   static MediaType createTextType(String subtype) {
550     return create(TEXT_TYPE, subtype);
551   }
552 
553   /**
554    * Creates a media type with the "video" type and the given subtype.
555    *
556    * @throws IllegalArgumentException if subtype is invalid
557    */
558   static MediaType createVideoType(String subtype) {
559     return create(VIDEO_TYPE, subtype);
560   }
561 
562   private static MediaType create(String type, String subtype,
563       Multimap<String, String> parameters) {
564     checkNotNull(type);
565     checkNotNull(subtype);
566     checkNotNull(parameters);
567     String normalizedType = normalizeToken(type);
568     String normalizedSubtype = normalizeToken(subtype);
569     checkArgument(!WILDCARD.equals(normalizedType) || WILDCARD.equals(normalizedSubtype),
570         "A wildcard type cannot be used with a non-wildcard subtype");
571     ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
572     for (Entry<String, String> entry : parameters.entries()) {
573       String attribute = normalizeToken(entry.getKey());
574       builder.put(attribute, normalizeParameterValue(attribute, entry.getValue()));
575     }
576     MediaType mediaType = new MediaType(normalizedType, normalizedSubtype, builder.build());
577     // Return one of the constants if the media type is a known type.
578     return MoreObjects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType);
579   }
580 
581   private static String normalizeToken(String token) {
582     checkArgument(TOKEN_MATCHER.matchesAllOf(token));
583     return Ascii.toLowerCase(token);
584   }
585 
586   private static String normalizeParameterValue(String attribute, String value) {
587     return CHARSET_ATTRIBUTE.equals(attribute) ? Ascii.toLowerCase(value) : value;
588   }
589 
590   /**
591    * Parses a media type from its string representation.
592    *
593    * @throws IllegalArgumentException if the input is not parsable
594    */
595   public static MediaType parse(String input) {
596     checkNotNull(input);
597     Tokenizer tokenizer = new Tokenizer(input);
598     try {
599       String type = tokenizer.consumeToken(TOKEN_MATCHER);
600       tokenizer.consumeCharacter('/');
601       String subtype = tokenizer.consumeToken(TOKEN_MATCHER);
602       ImmutableListMultimap.Builder<String, String> parameters = ImmutableListMultimap.builder();
603       while (tokenizer.hasMore()) {
604         tokenizer.consumeCharacter(';');
605         tokenizer.consumeTokenIfPresent(LINEAR_WHITE_SPACE);
606         String attribute = tokenizer.consumeToken(TOKEN_MATCHER);
607         tokenizer.consumeCharacter('=');
608         final String value;
609         if ('"' == tokenizer.previewChar()) {
610           tokenizer.consumeCharacter('"');
611           StringBuilder valueBuilder = new StringBuilder();
612           while ('"' != tokenizer.previewChar()) {
613             if ('\\' == tokenizer.previewChar()) {
614               tokenizer.consumeCharacter('\\');
615               valueBuilder.append(tokenizer.consumeCharacter(ASCII));
616             } else {
617               valueBuilder.append(tokenizer.consumeToken(QUOTED_TEXT_MATCHER));
618             }
619           }
620           value = valueBuilder.toString();
621           tokenizer.consumeCharacter('"');
622         } else {
623           value = tokenizer.consumeToken(TOKEN_MATCHER);
624         }
625         parameters.put(attribute, value);
626       }
627       return create(type, subtype, parameters.build());
628     } catch (IllegalStateException e) {
629       throw new IllegalArgumentException("Could not parse '" + input + "'", e);
630     }
631   }
632 
633   private static final class Tokenizer {
634     final String input;
635     int position = 0;
636 
637     Tokenizer(String input) {
638       this.input = input;
639     }
640 
641     String consumeTokenIfPresent(CharMatcher matcher) {
642       checkState(hasMore());
643       int startPosition = position;
644       position = matcher.negate().indexIn(input, startPosition);
645       return hasMore() ? input.substring(startPosition, position) : input.substring(startPosition);
646     }
647 
648     String consumeToken(CharMatcher matcher) {
649       int startPosition = position;
650       String token = consumeTokenIfPresent(matcher);
651       checkState(position != startPosition);
652       return token;
653     }
654 
655     char consumeCharacter(CharMatcher matcher) {
656       checkState(hasMore());
657       char c = previewChar();
658       checkState(matcher.matches(c));
659       position++;
660       return c;
661     }
662 
663     char consumeCharacter(char c) {
664       checkState(hasMore());
665       checkState(previewChar() == c);
666       position++;
667       return c;
668     }
669 
670     char previewChar() {
671       checkState(hasMore());
672       return input.charAt(position);
673     }
674 
675     boolean hasMore() {
676       return (position >= 0) && (position < input.length());
677     }
678   }
679 
680   @Override public boolean equals(@Nullable Object obj) {
681     if (obj == this) {
682       return true;
683     } else if (obj instanceof MediaType) {
684       MediaType that = (MediaType) obj;
685       return this.type.equals(that.type)
686           && this.subtype.equals(that.subtype)
687           // compare parameters regardless of order
688           && this.parametersAsMap().equals(that.parametersAsMap());
689     } else {
690       return false;
691     }
692   }
693 
694   @Override public int hashCode() {
695     return Objects.hashCode(type, subtype, parametersAsMap());
696   }
697 
698   private static final MapJoiner PARAMETER_JOINER = Joiner.on("; ").withKeyValueSeparator("=");
699 
700   /**
701    * Returns the string representation of this media type in the format described in <a
702    * href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>.
703    */
704   @Override public String toString() {
705     StringBuilder builder = new StringBuilder().append(type).append('/').append(subtype);
706     if (!parameters.isEmpty()) {
707       builder.append("; ");
708       Multimap<String, String> quotedParameters = Multimaps.transformValues(parameters,
709           new Function<String, String>() {
710             @Override public String apply(String value) {
711               return TOKEN_MATCHER.matchesAllOf(value) ? value : escapeAndQuote(value);
712             }
713           });
714       PARAMETER_JOINER.appendTo(builder, quotedParameters.entries());
715     }
716     return builder.toString();
717   }
718 
719   private static String escapeAndQuote(String value) {
720     StringBuilder escaped = new StringBuilder(value.length() + 16).append('"');
721     for (char ch : value.toCharArray()) {
722       if (ch == '\r' || ch == '\\' || ch == '"') {
723         escaped.append('\\');
724       }
725       escaped.append(ch);
726     }
727     return escaped.append('"').toString();
728   }
729 
730 }
731