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 * As described in the <a href="http://idpf.org/epub">International Digital Publishing Forum</a> 232 * EPUB is the distribution and interchange format standard for digital publications and 233 * documents. This media type is defined in the 234 * <a href="http://www.idpf.org/epub/30/spec/epub30-ocf.html">EPUB Open Container Format</a> 235 * specification. 236 * 237 * @since 15.0 238 */ 239 public static final MediaType EPUB = createConstant(APPLICATION_TYPE, "epub+zip"); 240 public static final MediaType FORM_DATA = createConstant(APPLICATION_TYPE, 241 "x-www-form-urlencoded"); 242 /** 243 * As described in <a href="https://www.rsa.com/rsalabs/node.asp?id=2138">PKCS #12: Personal 244 * Information Exchange Syntax Standard</a>, PKCS #12 defines an archive file format for storing 245 * many cryptography objects as a single file. 246 * 247 * @since 15.0 248 */ 249 public static final MediaType KEY_ARCHIVE = createConstant(APPLICATION_TYPE, "pkcs12"); 250 /** 251 * This is a non-standard media type, but is commonly used in serving hosted binary files as it is 252 * <a href="http://code.google.com/p/browsersec/wiki/Part2#Survey_of_content_sniffing_behaviors"> 253 * known not to trigger content sniffing in current browsers</a>. It <i>should not</i> be used in 254 * other situations as it is not specified by any RFC and does not appear in the <a href= 255 * "http://www.iana.org/assignments/media-types">/IANA MIME Media Types</a> list. Consider 256 * {@link #OCTET_STREAM} for binary data that is not being served to a browser. 257 * 258 * 259 * @since 14.0 260 */ 261 public static final MediaType APPLICATION_BINARY = createConstant(APPLICATION_TYPE, "binary"); 262 public static final MediaType GZIP = createConstant(APPLICATION_TYPE, "x-gzip"); 263 /** 264 * <a href="http://www.rfc-editor.org/rfc/rfc4329.txt">RFC 4329</a> declares this to be the 265 * correct media type for JavaScript, but {@link #TEXT_JAVASCRIPT_UTF_8 text/javascript} may be 266 * necessary in certain situations for compatibility. 267 */ 268 public static final MediaType JAVASCRIPT_UTF_8 = 269 createConstantUtf8(APPLICATION_TYPE, "javascript"); 270 public static final MediaType JSON_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "json"); 271 public static final MediaType KML = createConstant(APPLICATION_TYPE, "vnd.google-earth.kml+xml"); 272 public static final MediaType KMZ = createConstant(APPLICATION_TYPE, "vnd.google-earth.kmz"); 273 public static final MediaType MBOX = createConstant(APPLICATION_TYPE, "mbox"); 274 public static final MediaType MICROSOFT_EXCEL = createConstant(APPLICATION_TYPE, "vnd.ms-excel"); 275 public static final MediaType MICROSOFT_POWERPOINT = 276 createConstant(APPLICATION_TYPE, "vnd.ms-powerpoint"); 277 public static final MediaType MICROSOFT_WORD = createConstant(APPLICATION_TYPE, "msword"); 278 public static final MediaType OCTET_STREAM = createConstant(APPLICATION_TYPE, "octet-stream"); 279 public static final MediaType OGG_CONTAINER = createConstant(APPLICATION_TYPE, "ogg"); 280 public static final MediaType OOXML_DOCUMENT = createConstant(APPLICATION_TYPE, 281 "vnd.openxmlformats-officedocument.wordprocessingml.document"); 282 public static final MediaType OOXML_PRESENTATION = createConstant(APPLICATION_TYPE, 283 "vnd.openxmlformats-officedocument.presentationml.presentation"); 284 public static final MediaType OOXML_SHEET = 285 createConstant(APPLICATION_TYPE, "vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 286 public static final MediaType OPENDOCUMENT_GRAPHICS = 287 createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.graphics"); 288 public static final MediaType OPENDOCUMENT_PRESENTATION = 289 createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.presentation"); 290 public static final MediaType OPENDOCUMENT_SPREADSHEET = 291 createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.spreadsheet"); 292 public static final MediaType OPENDOCUMENT_TEXT = 293 createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.text"); 294 public static final MediaType PDF = createConstant(APPLICATION_TYPE, "pdf"); 295 public static final MediaType POSTSCRIPT = createConstant(APPLICATION_TYPE, "postscript"); 296 /** 297 * <a href="http://tools.ietf.org/html/draft-rfernando-protocol-buffers-00">Protocol buffers</a> 298 * 299 * @since 15.0 300 */ 301 public static final MediaType PROTOBUF = createConstant(APPLICATION_TYPE, "protobuf"); 302 public static final MediaType RDF_XML_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "rdf+xml"); 303 public static final MediaType RTF_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "rtf"); 304 public static final MediaType SHOCKWAVE_FLASH = createConstant(APPLICATION_TYPE, 305 "x-shockwave-flash"); 306 public static final MediaType SKETCHUP = createConstant(APPLICATION_TYPE, "vnd.sketchup.skp"); 307 public static final MediaType TAR = createConstant(APPLICATION_TYPE, "x-tar"); 308 public static final MediaType XHTML_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "xhtml+xml"); 309 /** 310 * Media type for Extensible Resource Descriptors. This is not yet registered with the IANA, but 311 * it is specified by OASIS in the 312 * <a href="http://docs.oasis-open.org/xri/xrd/v1.0/cd02/xrd-1.0-cd02.html"> XRD definition</a> 313 * and implemented in projects such as 314 * <a href="http://code.google.com/p/webfinger/">WebFinger</a>. 315 */ 316 public static final MediaType XRD_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "xrd+xml"); 317 public static final MediaType ZIP = createConstant(APPLICATION_TYPE, "zip"); 318 319 private final String type; 320 private final String subtype; 321 private final ImmutableListMultimap<String, String> parameters; 322 323 private MediaType(String type, String subtype, 324 ImmutableListMultimap<String, String> parameters) { 325 this.type = type; 326 this.subtype = subtype; 327 this.parameters = parameters; 328 } 329 330 /** Returns the top-level media type. For example, {@code "text"} in {@code "text/plain"}. */ 331 public String type() { 332 return type; 333 } 334 335 /** Returns the media subtype. For example, {@code "plain"} in {@code "text/plain"}. */ 336 public String subtype() { 337 return subtype; 338 } 339 340 /** Returns a multimap containing the parameters of this media type. */ 341 public ImmutableListMultimap<String, String> parameters() { 342 return parameters; 343 } 344 345 private Map<String, ImmutableMultiset<String>> parametersAsMap() { 346 return Maps.transformValues(parameters.asMap(), 347 new Function<Collection<String>, ImmutableMultiset<String>>() { 348 @Override public ImmutableMultiset<String> apply(Collection<String> input) { 349 return ImmutableMultiset.copyOf(input); 350 } 351 }); 352 } 353 354 /** 355 * Returns an optional charset for the value of the charset parameter if it is specified. 356 * 357 * @throws IllegalStateException if multiple charset values have been set for this media type 358 * @throws IllegalCharsetNameException if a charset value is present, but illegal 359 * @throws UnsupportedCharsetException if a charset value is present, but no support is available 360 * in this instance of the Java virtual machine 361 */ 362 public Optional<Charset> charset() { 363 ImmutableSet<String> charsetValues = ImmutableSet.copyOf(parameters.get(CHARSET_ATTRIBUTE)); 364 switch (charsetValues.size()) { 365 case 0: 366 return Optional.absent(); 367 case 1: 368 return Optional.of(Charset.forName(Iterables.getOnlyElement(charsetValues))); 369 default: 370 throw new IllegalStateException("Multiple charset values defined: " + charsetValues); 371 } 372 } 373 374 /** 375 * Returns a new instance with the same type and subtype as this instance, but without any 376 * parameters. 377 */ 378 public MediaType withoutParameters() { 379 return parameters.isEmpty() ? this : create(type, subtype); 380 } 381 382 /** 383 * <em>Replaces</em> all parameters with the given parameters. 384 * 385 * @throws IllegalArgumentException if any parameter or value is invalid 386 */ 387 public MediaType withParameters(Multimap<String, String> parameters) { 388 return create(type, subtype, parameters); 389 } 390 391 /** 392 * <em>Replaces</em> all parameters with the given attribute with a single parameter with the 393 * given value. If multiple parameters with the same attributes are necessary use 394 * {@link #withParameters}. Prefer {@link #withCharset} for setting the {@code charset} parameter 395 * when using a {@link Charset} object. 396 * 397 * @throws IllegalArgumentException if either {@code attribute} or {@code value} is invalid 398 */ 399 public MediaType withParameter(String attribute, String value) { 400 checkNotNull(attribute); 401 checkNotNull(value); 402 String normalizedAttribute = normalizeToken(attribute); 403 ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder(); 404 for (Entry<String, String> entry : parameters.entries()) { 405 String key = entry.getKey(); 406 if (!normalizedAttribute.equals(key)) { 407 builder.put(key, entry.getValue()); 408 } 409 } 410 builder.put(normalizedAttribute, normalizeParameterValue(normalizedAttribute, value)); 411 MediaType mediaType = new MediaType(type, subtype, builder.build()); 412 // Return one of the constants if the media type is a known type. 413 return Objects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType); 414 } 415 416 /** 417 * Returns a new instance with the same type and subtype as this instance, with the 418 * {@code charset} parameter set to the {@link Charset#name name} of the given charset. Only one 419 * {@code charset} parameter will be present on the new instance regardless of the number set on 420 * this one. 421 * 422 * <p>If a charset must be specified that is not supported on this JVM (and thus is not 423 * representable as a {@link Charset} instance, use {@link #withParameter}. 424 */ 425 public MediaType withCharset(Charset charset) { 426 checkNotNull(charset); 427 return withParameter(CHARSET_ATTRIBUTE, charset.name()); 428 } 429 430 /** Returns true if either the type or subtype is the wildcard. */ 431 public boolean hasWildcard() { 432 return WILDCARD.equals(type) || WILDCARD.equals(subtype); 433 } 434 435 /** 436 * Returns {@code true} if this instance falls within the range (as defined by 437 * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">the HTTP Accept header</a>) 438 * given by the argument according to three criteria: 439 * 440 * <ol> 441 * <li>The type of the argument is the wildcard or equal to the type of this instance. 442 * <li>The subtype of the argument is the wildcard or equal to the subtype of this instance. 443 * <li>All of the parameters present in the argument are present in this instance. 444 * </ol> 445 * 446 * <p>For example: <pre> {@code 447 * PLAIN_TEXT_UTF_8.is(PLAIN_TEXT_UTF_8) // true 448 * PLAIN_TEXT_UTF_8.is(HTML_UTF_8) // false 449 * PLAIN_TEXT_UTF_8.is(ANY_TYPE) // true 450 * PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE) // true 451 * PLAIN_TEXT_UTF_8.is(ANY_IMAGE_TYPE) // false 452 * PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE.withCharset(UTF_8)) // true 453 * PLAIN_TEXT_UTF_8.withoutParameters().is(ANY_TEXT_TYPE.withCharset(UTF_8)) // false 454 * PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE.withCharset(UTF_16)) // false}</pre> 455 * 456 * <p>Note that while it is possible to have the same parameter declared multiple times within a 457 * media type this method does not consider the number of occurrences of a parameter. For 458 * example, {@code "text/plain; charset=UTF-8"} satisfies 459 * {@code "text/plain; charset=UTF-8; charset=UTF-8"}. 460 */ 461 public boolean is(MediaType mediaTypeRange) { 462 return (mediaTypeRange.type.equals(WILDCARD) || mediaTypeRange.type.equals(this.type)) 463 && (mediaTypeRange.subtype.equals(WILDCARD) || mediaTypeRange.subtype.equals(this.subtype)) 464 && this.parameters.entries().containsAll(mediaTypeRange.parameters.entries()); 465 } 466 467 /** 468 * Creates a new media type with the given type and subtype. 469 * 470 * @throws IllegalArgumentException if type or subtype is invalid or if a wildcard is used for the 471 * type, but not the subtype. 472 */ 473 public static MediaType create(String type, String subtype) { 474 return create(type, subtype, ImmutableListMultimap.<String, String>of()); 475 } 476 477 /** 478 * Creates a media type with the "application" type and the given subtype. 479 * 480 * @throws IllegalArgumentException if subtype is invalid 481 */ 482 static MediaType createApplicationType(String subtype) { 483 return create(APPLICATION_TYPE, subtype); 484 } 485 486 /** 487 * Creates a media type with the "audio" type and the given subtype. 488 * 489 * @throws IllegalArgumentException if subtype is invalid 490 */ 491 static MediaType createAudioType(String subtype) { 492 return create(AUDIO_TYPE, subtype); 493 } 494 495 /** 496 * Creates a media type with the "image" type and the given subtype. 497 * 498 * @throws IllegalArgumentException if subtype is invalid 499 */ 500 static MediaType createImageType(String subtype) { 501 return create(IMAGE_TYPE, subtype); 502 } 503 504 /** 505 * Creates a media type with the "text" type and the given subtype. 506 * 507 * @throws IllegalArgumentException if subtype is invalid 508 */ 509 static MediaType createTextType(String subtype) { 510 return create(TEXT_TYPE, subtype); 511 } 512 513 /** 514 * Creates a media type with the "video" type and the given subtype. 515 * 516 * @throws IllegalArgumentException if subtype is invalid 517 */ 518 static MediaType createVideoType(String subtype) { 519 return create(VIDEO_TYPE, subtype); 520 } 521 522 private static MediaType create(String type, String subtype, 523 Multimap<String, String> parameters) { 524 checkNotNull(type); 525 checkNotNull(subtype); 526 checkNotNull(parameters); 527 String normalizedType = normalizeToken(type); 528 String normalizedSubtype = normalizeToken(subtype); 529 checkArgument(!WILDCARD.equals(normalizedType) || WILDCARD.equals(normalizedSubtype), 530 "A wildcard type cannot be used with a non-wildcard subtype"); 531 ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder(); 532 for (Entry<String, String> entry : parameters.entries()) { 533 String attribute = normalizeToken(entry.getKey()); 534 builder.put(attribute, normalizeParameterValue(attribute, entry.getValue())); 535 } 536 MediaType mediaType = new MediaType(normalizedType, normalizedSubtype, builder.build()); 537 // Return one of the constants if the media type is a known type. 538 return Objects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType); 539 } 540 541 private static String normalizeToken(String token) { 542 checkArgument(TOKEN_MATCHER.matchesAllOf(token)); 543 return Ascii.toLowerCase(token); 544 } 545 546 private static String normalizeParameterValue(String attribute, String value) { 547 return CHARSET_ATTRIBUTE.equals(attribute) ? Ascii.toLowerCase(value) : value; 548 } 549 550 /** 551 * Parses a media type from its string representation. 552 * 553 * @throws IllegalArgumentException if the input is not parsable 554 */ 555 public static MediaType parse(String input) { 556 checkNotNull(input); 557 Tokenizer tokenizer = new Tokenizer(input); 558 try { 559 String type = tokenizer.consumeToken(TOKEN_MATCHER); 560 tokenizer.consumeCharacter('/'); 561 String subtype = tokenizer.consumeToken(TOKEN_MATCHER); 562 ImmutableListMultimap.Builder<String, String> parameters = ImmutableListMultimap.builder(); 563 while (tokenizer.hasMore()) { 564 tokenizer.consumeCharacter(';'); 565 tokenizer.consumeTokenIfPresent(LINEAR_WHITE_SPACE); 566 String attribute = tokenizer.consumeToken(TOKEN_MATCHER); 567 tokenizer.consumeCharacter('='); 568 final String value; 569 if ('"' == tokenizer.previewChar()) { 570 tokenizer.consumeCharacter('"'); 571 StringBuilder valueBuilder = new StringBuilder(); 572 while ('"' != tokenizer.previewChar()) { 573 if ('\\' == tokenizer.previewChar()) { 574 tokenizer.consumeCharacter('\\'); 575 valueBuilder.append(tokenizer.consumeCharacter(ASCII)); 576 } else { 577 valueBuilder.append(tokenizer.consumeToken(QUOTED_TEXT_MATCHER)); 578 } 579 } 580 value = valueBuilder.toString(); 581 tokenizer.consumeCharacter('"'); 582 } else { 583 value = tokenizer.consumeToken(TOKEN_MATCHER); 584 } 585 parameters.put(attribute, value); 586 } 587 return create(type, subtype, parameters.build()); 588 } catch (IllegalStateException e) { 589 throw new IllegalArgumentException(e); 590 } 591 } 592 593 private static final class Tokenizer { 594 final String input; 595 int position = 0; 596 597 Tokenizer(String input) { 598 this.input = input; 599 } 600 601 String consumeTokenIfPresent(CharMatcher matcher) { 602 checkState(hasMore()); 603 int startPosition = position; 604 position = matcher.negate().indexIn(input, startPosition); 605 return hasMore() ? input.substring(startPosition, position) : input.substring(startPosition); 606 } 607 608 String consumeToken(CharMatcher matcher) { 609 int startPosition = position; 610 String token = consumeTokenIfPresent(matcher); 611 checkState(position != startPosition); 612 return token; 613 } 614 615 char consumeCharacter(CharMatcher matcher) { 616 checkState(hasMore()); 617 char c = previewChar(); 618 checkState(matcher.matches(c)); 619 position++; 620 return c; 621 } 622 623 char consumeCharacter(char c) { 624 checkState(hasMore()); 625 checkState(previewChar() == c); 626 position++; 627 return c; 628 } 629 630 char previewChar() { 631 checkState(hasMore()); 632 return input.charAt(position); 633 } 634 635 boolean hasMore() { 636 return (position >= 0) && (position < input.length()); 637 } 638 } 639 640 @Override public boolean equals(@Nullable Object obj) { 641 if (obj == this) { 642 return true; 643 } else if (obj instanceof MediaType) { 644 MediaType that = (MediaType) obj; 645 return this.type.equals(that.type) 646 && this.subtype.equals(that.subtype) 647 // compare parameters regardless of order 648 && this.parametersAsMap().equals(that.parametersAsMap()); 649 } else { 650 return false; 651 } 652 } 653 654 @Override public int hashCode() { 655 return Objects.hashCode(type, subtype, parametersAsMap()); 656 } 657 658 private static final MapJoiner PARAMETER_JOINER = Joiner.on("; ").withKeyValueSeparator("="); 659 660 /** 661 * Returns the string representation of this media type in the format described in <a 662 * href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>. 663 */ 664 @Override public String toString() { 665 StringBuilder builder = new StringBuilder().append(type).append('/').append(subtype); 666 if (!parameters.isEmpty()) { 667 builder.append("; "); 668 Multimap<String, String> quotedParameters = Multimaps.transformValues(parameters, 669 new Function<String, String>() { 670 @Override public String apply(String value) { 671 return TOKEN_MATCHER.matchesAllOf(value) ? value : escapeAndQuote(value); 672 } 673 }); 674 PARAMETER_JOINER.appendTo(builder, quotedParameters.entries()); 675 } 676 return builder.toString(); 677 } 678 679 private static String escapeAndQuote(String value) { 680 StringBuilder escaped = new StringBuilder(value.length() + 16).append('"'); 681 for (char ch : value.toCharArray()) { 682 if (ch == '\r' || ch == '\\' || ch == '"') { 683 escaped.append('\\'); 684 } 685 escaped.append(ch); 686 } 687 return escaped.append('"').toString(); 688 } 689 690}