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