001/* 002 * Copyright (C) 2011 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.net; 016 017import static com.google.common.base.CharMatcher.ascii; 018import static com.google.common.base.CharMatcher.javaIsoControl; 019import static com.google.common.base.Charsets.UTF_8; 020import static com.google.common.base.Preconditions.checkArgument; 021import static com.google.common.base.Preconditions.checkNotNull; 022import static com.google.common.base.Preconditions.checkState; 023 024import com.google.common.annotations.Beta; 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.base.Ascii; 027import com.google.common.base.CharMatcher; 028import com.google.common.base.Function; 029import com.google.common.base.Joiner; 030import com.google.common.base.Joiner.MapJoiner; 031import com.google.common.base.MoreObjects; 032import com.google.common.base.Objects; 033import com.google.common.base.Optional; 034import com.google.common.collect.ImmutableListMultimap; 035import com.google.common.collect.ImmutableMultiset; 036import com.google.common.collect.ImmutableSet; 037import com.google.common.collect.Maps; 038import com.google.common.collect.Multimap; 039import com.google.common.collect.Multimaps; 040import com.google.errorprone.annotations.Immutable; 041import com.google.errorprone.annotations.concurrent.LazyInit; 042import java.nio.charset.Charset; 043import java.nio.charset.IllegalCharsetNameException; 044import java.nio.charset.UnsupportedCharsetException; 045import java.util.Collection; 046import java.util.Map; 047import java.util.Map.Entry; 048import org.checkerframework.checker.nullness.qual.Nullable; 049 050/** 051 * Represents an <a href="http://en.wikipedia.org/wiki/Internet_media_type">Internet Media Type</a> 052 * (also known as a MIME Type or Content Type). This class also supports the concept of media ranges 053 * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1">defined by HTTP/1.1</a>. 054 * As such, the {@code *} character is treated as a wildcard and is used to represent any acceptable 055 * type or subtype value. A media type may not have wildcard type with a declared subtype. The 056 * {@code *} character has no special meaning as part of a parameter. All values for type, subtype, 057 * parameter attributes or parameter values must be valid according to RFCs <a 058 * href="http://www.ietf.org/rfc/rfc2045.txt">2045</a> and <a 059 * href="http://www.ietf.org/rfc/rfc2046.txt">2046</a>. 060 * 061 * <p>All portions of the media type that are case-insensitive (type, subtype, parameter attributes) 062 * are normalized to lowercase. The value of the {@code charset} parameter is normalized to 063 * lowercase, but all others are left as-is. 064 * 065 * <p>Note that this specifically does <strong>not</strong> represent the value of the MIME {@code 066 * Content-Type} header and as such has no support for header-specific considerations such as line 067 * folding and comments. 068 * 069 * <p>For media types that take a charset the predefined constants default to UTF-8 and have a 070 * "_UTF_8" suffix. To get a version without a character set, use {@link #withoutParameters}. 071 * 072 * @since 12.0 073 * @author Gregory Kick 074 */ 075@Beta 076@GwtCompatible 077@Immutable 078public final class MediaType { 079 private static final String CHARSET_ATTRIBUTE = "charset"; 080 private static final ImmutableListMultimap<String, String> UTF_8_CONSTANT_PARAMETERS = 081 ImmutableListMultimap.of(CHARSET_ATTRIBUTE, Ascii.toLowerCase(UTF_8.name())); 082 083 /** Matcher for type, subtype and attributes. */ 084 private static final CharMatcher TOKEN_MATCHER = 085 ascii() 086 .and(javaIsoControl().negate()) 087 .and(CharMatcher.isNot(' ')) 088 .and(CharMatcher.noneOf("()<>@,;:\\\"/[]?=")); 089 090 private static final CharMatcher QUOTED_TEXT_MATCHER = ascii().and(CharMatcher.noneOf("\"\\\r")); 091 092 /* 093 * This matches the same characters as linear-white-space from RFC 822, but we make no effort to 094 * enforce any particular rules with regards to line folding as stated in the class docs. 095 */ 096 private static final CharMatcher LINEAR_WHITE_SPACE = CharMatcher.anyOf(" \t\r\n"); 097 098 // TODO(gak): make these public? 099 private static final String APPLICATION_TYPE = "application"; 100 private static final String AUDIO_TYPE = "audio"; 101 private static final String IMAGE_TYPE = "image"; 102 private static final String TEXT_TYPE = "text"; 103 private static final String VIDEO_TYPE = "video"; 104 105 private static final String WILDCARD = "*"; 106 107 private static final Map<MediaType, MediaType> KNOWN_TYPES = Maps.newHashMap(); 108 109 private static MediaType createConstant(String type, String subtype) { 110 MediaType mediaType = 111 addKnownType(new MediaType(type, subtype, ImmutableListMultimap.<String, String>of())); 112 mediaType.parsedCharset = Optional.absent(); 113 return mediaType; 114 } 115 116 private static MediaType createConstantUtf8(String type, String subtype) { 117 MediaType mediaType = addKnownType(new MediaType(type, subtype, UTF_8_CONSTANT_PARAMETERS)); 118 mediaType.parsedCharset = Optional.of(UTF_8); 119 return mediaType; 120 } 121 122 private static MediaType addKnownType(MediaType mediaType) { 123 KNOWN_TYPES.put(mediaType, mediaType); 124 return mediaType; 125 } 126 127 /* 128 * The following constants are grouped by their type and ordered alphabetically by the constant 129 * name within that type. The constant name should be a sensible identifier that is closest to the 130 * "common name" of the media. This is often, but not necessarily the same as the subtype. 131 * 132 * Be sure to declare all constants with the type and subtype in all lowercase. For types that 133 * take a charset (e.g. all text/* types), default to UTF-8 and suffix the constant name with 134 * "_UTF_8". 135 */ 136 137 public static final MediaType ANY_TYPE = createConstant(WILDCARD, WILDCARD); 138 public static final MediaType ANY_TEXT_TYPE = createConstant(TEXT_TYPE, WILDCARD); 139 public static final MediaType ANY_IMAGE_TYPE = createConstant(IMAGE_TYPE, WILDCARD); 140 public static final MediaType ANY_AUDIO_TYPE = createConstant(AUDIO_TYPE, WILDCARD); 141 public static final MediaType ANY_VIDEO_TYPE = createConstant(VIDEO_TYPE, WILDCARD); 142 public static final MediaType ANY_APPLICATION_TYPE = createConstant(APPLICATION_TYPE, WILDCARD); 143 144 /* text types */ 145 public static final MediaType CACHE_MANIFEST_UTF_8 = 146 createConstantUtf8(TEXT_TYPE, "cache-manifest"); 147 public static final MediaType CSS_UTF_8 = createConstantUtf8(TEXT_TYPE, "css"); 148 public static final MediaType CSV_UTF_8 = createConstantUtf8(TEXT_TYPE, "csv"); 149 public static final MediaType HTML_UTF_8 = createConstantUtf8(TEXT_TYPE, "html"); 150 public static final MediaType I_CALENDAR_UTF_8 = createConstantUtf8(TEXT_TYPE, "calendar"); 151 public static final MediaType PLAIN_TEXT_UTF_8 = createConstantUtf8(TEXT_TYPE, "plain"); 152 153 /** 154 * <a href="http://www.rfc-editor.org/rfc/rfc4329.txt">RFC 4329</a> declares {@link 155 * #JAVASCRIPT_UTF_8 application/javascript} to be the correct media type for JavaScript, but this 156 * may be necessary in certain situations for compatibility. 157 */ 158 public static final MediaType TEXT_JAVASCRIPT_UTF_8 = createConstantUtf8(TEXT_TYPE, "javascript"); 159 /** 160 * <a href="http://www.iana.org/assignments/media-types/text/tab-separated-values">Tab separated 161 * values</a>. 162 * 163 * @since 15.0 164 */ 165 public static final MediaType TSV_UTF_8 = createConstantUtf8(TEXT_TYPE, "tab-separated-values"); 166 167 public static final MediaType VCARD_UTF_8 = createConstantUtf8(TEXT_TYPE, "vcard"); 168 public static final MediaType WML_UTF_8 = createConstantUtf8(TEXT_TYPE, "vnd.wap.wml"); 169 170 /** 171 * As described in <a href="http://www.ietf.org/rfc/rfc3023.txt">RFC 3023</a>, this constant 172 * ({@code text/xml}) is used for XML documents that are "readable by casual users." {@link 173 * #APPLICATION_XML_UTF_8} is provided for documents that are intended for applications. 174 */ 175 public static final MediaType XML_UTF_8 = createConstantUtf8(TEXT_TYPE, "xml"); 176 177 /** 178 * As described in <a href="https://w3c.github.io/webvtt/#iana-text-vtt">the VTT spec</a>, this is 179 * used for Web Video Text Tracks (WebVTT) files, used with the HTML5 track element. 180 * 181 * @since 20.0 182 */ 183 public static final MediaType VTT_UTF_8 = createConstantUtf8(TEXT_TYPE, "vtt"); 184 185 /* image types */ 186 public static final MediaType BMP = createConstant(IMAGE_TYPE, "bmp"); 187 188 /** 189 * The media type for the <a href="http://en.wikipedia.org/wiki/Camera_Image_File_Format">Canon 190 * Image File Format</a> ({@code crw} files), a widely-used "raw image" format for cameras. It is 191 * found in {@code /etc/mime.types}, e.g. in <a href= 192 * "http://anonscm.debian.org/gitweb/?p=collab-maint/mime-support.git;a=blob;f=mime.types;hb=HEAD" 193 * >Debian 3.48-1</a>. 194 * 195 * @since 15.0 196 */ 197 public static final MediaType CRW = createConstant(IMAGE_TYPE, "x-canon-crw"); 198 199 public static final MediaType GIF = createConstant(IMAGE_TYPE, "gif"); 200 public static final MediaType ICO = createConstant(IMAGE_TYPE, "vnd.microsoft.icon"); 201 public static final MediaType JPEG = createConstant(IMAGE_TYPE, "jpeg"); 202 public static final MediaType PNG = createConstant(IMAGE_TYPE, "png"); 203 204 /** 205 * The media type for the Photoshop File Format ({@code psd} files) as defined by <a 206 * href="http://www.iana.org/assignments/media-types/image/vnd.adobe.photoshop">IANA</a>, and 207 * found in {@code /etc/mime.types}, e.g. <a 208 * href="http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types"></a> of the 209 * Apache <a href="http://httpd.apache.org/">HTTPD project</a>; for the specification, see <a 210 * href="http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/PhotoshopFileFormats.htm"> 211 * Adobe Photoshop Document Format</a> and <a 212 * href="http://en.wikipedia.org/wiki/Adobe_Photoshop#File_format">Wikipedia</a>; this is the 213 * regular output/input of Photoshop (which can also export to various image formats; note that 214 * files with extension "PSB" are in a distinct but related format). 215 * 216 * <p>This is a more recent replacement for the older, experimental type {@code x-photoshop}: <a 217 * href="http://tools.ietf.org/html/rfc2046#section-6">RFC-2046.6</a>. 218 * 219 * @since 15.0 220 */ 221 public static final MediaType PSD = createConstant(IMAGE_TYPE, "vnd.adobe.photoshop"); 222 223 public static final MediaType SVG_UTF_8 = createConstantUtf8(IMAGE_TYPE, "svg+xml"); 224 public static final MediaType TIFF = createConstant(IMAGE_TYPE, "tiff"); 225 public static final MediaType WEBP = createConstant(IMAGE_TYPE, "webp"); 226 227 /* audio types */ 228 public static final MediaType MP4_AUDIO = createConstant(AUDIO_TYPE, "mp4"); 229 public static final MediaType MPEG_AUDIO = createConstant(AUDIO_TYPE, "mpeg"); 230 public static final MediaType OGG_AUDIO = createConstant(AUDIO_TYPE, "ogg"); 231 public static final MediaType WEBM_AUDIO = createConstant(AUDIO_TYPE, "webm"); 232 233 /** 234 * Media type for L16 audio, as defined by <a href="https://tools.ietf.org/html/rfc2586">RFC 235 * 2586</a>. 236 * 237 * @since 24.1 238 */ 239 public static final MediaType L16_AUDIO = createConstant(AUDIO_TYPE, "l16"); 240 241 /** 242 * Media type for L24 audio, as defined by <a href="https://tools.ietf.org/html/rfc3190">RFC 243 * 3190</a>. 244 * 245 * @since 20.0 246 */ 247 public static final MediaType L24_AUDIO = createConstant(AUDIO_TYPE, "l24"); 248 249 /** 250 * Media type for Basic Audio, as defined by <a 251 * href="http://tools.ietf.org/html/rfc2046#section-4.3">RFC 2046</a>. 252 * 253 * @since 20.0 254 */ 255 public static final MediaType BASIC_AUDIO = createConstant(AUDIO_TYPE, "basic"); 256 257 /** 258 * Media type for Advanced Audio Coding. For more information, see <a 259 * href="https://en.wikipedia.org/wiki/Advanced_Audio_Coding">Advanced Audio Coding</a>. 260 * 261 * @since 20.0 262 */ 263 public static final MediaType AAC_AUDIO = createConstant(AUDIO_TYPE, "aac"); 264 265 /** 266 * Media type for Vorbis Audio, as defined by <a href="http://tools.ietf.org/html/rfc5215">RFC 267 * 5215</a>. 268 * 269 * @since 20.0 270 */ 271 public static final MediaType VORBIS_AUDIO = createConstant(AUDIO_TYPE, "vorbis"); 272 273 /** 274 * Media type for Windows Media Audio. For more information, see <a 275 * href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd562994(v=vs.85).aspx">file 276 * name extensions for Windows Media metafiles</a>. 277 * 278 * @since 20.0 279 */ 280 public static final MediaType WMA_AUDIO = createConstant(AUDIO_TYPE, "x-ms-wma"); 281 282 /** 283 * Media type for Windows Media metafiles. For more information, see <a 284 * href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd562994(v=vs.85).aspx">file 285 * name extensions for Windows Media metafiles</a>. 286 * 287 * @since 20.0 288 */ 289 public static final MediaType WAX_AUDIO = createConstant(AUDIO_TYPE, "x-ms-wax"); 290 291 /** 292 * Media type for Real Audio. For more information, see <a 293 * href="http://service.real.com/help/faq/rp8/configrp8win.html">this link</a>. 294 * 295 * @since 20.0 296 */ 297 public static final MediaType VND_REAL_AUDIO = createConstant(AUDIO_TYPE, "vnd.rn-realaudio"); 298 299 /** 300 * Media type for WAVE format, as defined by <a href="https://tools.ietf.org/html/rfc2361">RFC 301 * 2361</a>. 302 * 303 * @since 20.0 304 */ 305 public static final MediaType VND_WAVE_AUDIO = createConstant(AUDIO_TYPE, "vnd.wave"); 306 307 /* video types */ 308 public static final MediaType MP4_VIDEO = createConstant(VIDEO_TYPE, "mp4"); 309 public static final MediaType MPEG_VIDEO = createConstant(VIDEO_TYPE, "mpeg"); 310 public static final MediaType OGG_VIDEO = createConstant(VIDEO_TYPE, "ogg"); 311 public static final MediaType QUICKTIME = createConstant(VIDEO_TYPE, "quicktime"); 312 public static final MediaType WEBM_VIDEO = createConstant(VIDEO_TYPE, "webm"); 313 public static final MediaType WMV = createConstant(VIDEO_TYPE, "x-ms-wmv"); 314 315 /** 316 * Media type for Flash video. For more information, see <a href= 317 * "http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d48.html" 318 * >this link</a>. 319 * 320 * @since 20.0 321 */ 322 public static final MediaType FLV_VIDEO = createConstant(VIDEO_TYPE, "x-flv"); 323 324 /** 325 * Media type for the 3GP multimedia container format. For more information, see <a 326 * href="ftp://www.3gpp.org/tsg_sa/TSG_SA/TSGS_23/Docs/PDF/SP-040065.pdf#page=10">3GPP TS 327 * 26.244</a>. 328 * 329 * @since 20.0 330 */ 331 public static final MediaType THREE_GPP_VIDEO = createConstant(VIDEO_TYPE, "3gpp"); 332 333 /** 334 * Media type for the 3G2 multimedia container format. For more information, see <a 335 * href="http://www.3gpp2.org/Public_html/specs/C.S0050-B_v1.0_070521.pdf#page=16">3GPP2 336 * C.S0050-B</a>. 337 * 338 * @since 20.0 339 */ 340 public static final MediaType THREE_GPP2_VIDEO = createConstant(VIDEO_TYPE, "3gpp2"); 341 342 /* application types */ 343 /** 344 * As described in <a href="http://www.ietf.org/rfc/rfc3023.txt">RFC 3023</a>, this constant 345 * ({@code application/xml}) is used for XML documents that are "unreadable by casual users." 346 * {@link #XML_UTF_8} is provided for documents that may be read by users. 347 */ 348 public static final MediaType APPLICATION_XML_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "xml"); 349 350 public static final MediaType ATOM_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "atom+xml"); 351 public static final MediaType BZIP2 = createConstant(APPLICATION_TYPE, "x-bzip2"); 352 353 /** 354 * Media type for <a href="https://www.dartlang.org/articles/embedding-in-html/">dart files</a>. 355 * 356 * @since 19.0 357 */ 358 public static final MediaType DART_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "dart"); 359 360 /** 361 * Media type for <a href="https://goo.gl/2QoMvg">Apple Passbook</a>. 362 * 363 * @since 19.0 364 */ 365 public static final MediaType APPLE_PASSBOOK = 366 createConstant(APPLICATION_TYPE, "vnd.apple.pkpass"); 367 368 /** 369 * Media type for <a href="http://en.wikipedia.org/wiki/Embedded_OpenType">Embedded OpenType</a> 370 * fonts. This is <a 371 * href="http://www.iana.org/assignments/media-types/application/vnd.ms-fontobject">registered 372 * </a> with the IANA. 373 * 374 * @since 17.0 375 */ 376 public static final MediaType EOT = createConstant(APPLICATION_TYPE, "vnd.ms-fontobject"); 377 378 /** 379 * As described in the <a href="http://idpf.org/epub">International Digital Publishing Forum</a> 380 * EPUB is the distribution and interchange format standard for digital publications and 381 * documents. This media type is defined in the <a 382 * href="http://www.idpf.org/epub/30/spec/epub30-ocf.html">EPUB Open Container Format</a> 383 * specification. 384 * 385 * @since 15.0 386 */ 387 public static final MediaType EPUB = createConstant(APPLICATION_TYPE, "epub+zip"); 388 389 public static final MediaType FORM_DATA = 390 createConstant(APPLICATION_TYPE, "x-www-form-urlencoded"); 391 392 /** 393 * As described in <a href="https://www.rsa.com/rsalabs/node.asp?id=2138">PKCS #12: Personal 394 * Information Exchange Syntax Standard</a>, PKCS #12 defines an archive file format for storing 395 * many cryptography objects as a single file. 396 * 397 * @since 15.0 398 */ 399 public static final MediaType KEY_ARCHIVE = createConstant(APPLICATION_TYPE, "pkcs12"); 400 401 /** 402 * This is a non-standard media type, but is commonly used in serving hosted binary files as it is 403 * <a href="http://code.google.com/p/browsersec/wiki/Part2#Survey_of_content_sniffing_behaviors"> 404 * known not to trigger content sniffing in current browsers</a>. It <i>should not</i> be used in 405 * other situations as it is not specified by any RFC and does not appear in the <a 406 * href="http://www.iana.org/assignments/media-types">/IANA MIME Media Types</a> list. Consider 407 * {@link #OCTET_STREAM} for binary data that is not being served to a browser. 408 * 409 * @since 14.0 410 */ 411 public static final MediaType APPLICATION_BINARY = createConstant(APPLICATION_TYPE, "binary"); 412 413 public static final MediaType GZIP = createConstant(APPLICATION_TYPE, "x-gzip"); 414 415 /** 416 * Media type for the <a href="https://tools.ietf.org/html/draft-kelly-json-hal-08#section-3">JSON 417 * Hypertext Application Language (HAL) documents</a>. 418 * 419 * @since 26.0 420 */ 421 public static final MediaType HAL_JSON = createConstant(APPLICATION_TYPE, "hal+json"); 422 423 /** 424 * <a href="http://www.rfc-editor.org/rfc/rfc4329.txt">RFC 4329</a> declares this to be the 425 * correct media type for JavaScript, but {@link #TEXT_JAVASCRIPT_UTF_8 text/javascript} may be 426 * necessary in certain situations for compatibility. 427 */ 428 public static final MediaType JAVASCRIPT_UTF_8 = 429 createConstantUtf8(APPLICATION_TYPE, "javascript"); 430 431 public static final MediaType JSON_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "json"); 432 433 /** 434 * Media type for the <a href="http://www.w3.org/TR/appmanifest/">Manifest for a web 435 * application</a>. 436 * 437 * @since 19.0 438 */ 439 public static final MediaType MANIFEST_JSON_UTF_8 = 440 createConstantUtf8(APPLICATION_TYPE, "manifest+json"); 441 442 public static final MediaType KML = createConstant(APPLICATION_TYPE, "vnd.google-earth.kml+xml"); 443 public static final MediaType KMZ = createConstant(APPLICATION_TYPE, "vnd.google-earth.kmz"); 444 public static final MediaType MBOX = createConstant(APPLICATION_TYPE, "mbox"); 445 446 /** 447 * Media type for <a href="http://goo.gl/1pGBFm">Apple over-the-air mobile configuration 448 * profiles</a>. 449 * 450 * @since 18.0 451 */ 452 public static final MediaType APPLE_MOBILE_CONFIG = 453 createConstant(APPLICATION_TYPE, "x-apple-aspen-config"); 454 455 public static final MediaType MICROSOFT_EXCEL = createConstant(APPLICATION_TYPE, "vnd.ms-excel"); 456 public static final MediaType MICROSOFT_POWERPOINT = 457 createConstant(APPLICATION_TYPE, "vnd.ms-powerpoint"); 458 public static final MediaType MICROSOFT_WORD = createConstant(APPLICATION_TYPE, "msword"); 459 460 /** 461 * Media type for NaCl applications. For more information see <a 462 * href="https://developer.chrome.com/native-client/devguide/coding/application-structure">the 463 * Developer Guide for Native Client Application Structure</a>. 464 * 465 * @since 20.0 466 */ 467 public static final MediaType NACL_APPLICATION = createConstant(APPLICATION_TYPE, "x-nacl"); 468 469 /** 470 * Media type for NaCl portable applications. For more information see <a 471 * href="https://developer.chrome.com/native-client/devguide/coding/application-structure">the 472 * Developer Guide for Native Client Application Structure</a>. 473 * 474 * @since 20.0 475 */ 476 public static final MediaType NACL_PORTABLE_APPLICATION = 477 createConstant(APPLICATION_TYPE, "x-pnacl"); 478 479 public static final MediaType OCTET_STREAM = createConstant(APPLICATION_TYPE, "octet-stream"); 480 481 public static final MediaType OGG_CONTAINER = createConstant(APPLICATION_TYPE, "ogg"); 482 public static final MediaType OOXML_DOCUMENT = 483 createConstant( 484 APPLICATION_TYPE, "vnd.openxmlformats-officedocument.wordprocessingml.document"); 485 public static final MediaType OOXML_PRESENTATION = 486 createConstant( 487 APPLICATION_TYPE, "vnd.openxmlformats-officedocument.presentationml.presentation"); 488 public static final MediaType OOXML_SHEET = 489 createConstant(APPLICATION_TYPE, "vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 490 public static final MediaType OPENDOCUMENT_GRAPHICS = 491 createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.graphics"); 492 public static final MediaType OPENDOCUMENT_PRESENTATION = 493 createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.presentation"); 494 public static final MediaType OPENDOCUMENT_SPREADSHEET = 495 createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.spreadsheet"); 496 public static final MediaType OPENDOCUMENT_TEXT = 497 createConstant(APPLICATION_TYPE, "vnd.oasis.opendocument.text"); 498 public static final MediaType PDF = createConstant(APPLICATION_TYPE, "pdf"); 499 public static final MediaType POSTSCRIPT = createConstant(APPLICATION_TYPE, "postscript"); 500 501 /** 502 * <a href="http://tools.ietf.org/html/draft-rfernando-protocol-buffers-00">Protocol buffers</a> 503 * 504 * @since 15.0 505 */ 506 public static final MediaType PROTOBUF = createConstant(APPLICATION_TYPE, "protobuf"); 507 508 public static final MediaType RDF_XML_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "rdf+xml"); 509 public static final MediaType RTF_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "rtf"); 510 511 /** 512 * Media type for SFNT fonts (which includes <a 513 * href="http://en.wikipedia.org/wiki/TrueType/">TrueType</a> and <a 514 * href="http://en.wikipedia.org/wiki/OpenType/">OpenType</a> fonts). This is <a 515 * href="http://www.iana.org/assignments/media-types/application/font-sfnt">registered</a> with 516 * the IANA. 517 * 518 * @since 17.0 519 */ 520 public static final MediaType SFNT = createConstant(APPLICATION_TYPE, "font-sfnt"); 521 522 public static final MediaType SHOCKWAVE_FLASH = 523 createConstant(APPLICATION_TYPE, "x-shockwave-flash"); 524 public static final MediaType SKETCHUP = createConstant(APPLICATION_TYPE, "vnd.sketchup.skp"); 525 526 /** 527 * As described in <a href="http://www.ietf.org/rfc/rfc3902.txt">RFC 3902</a>, this constant 528 * ({@code application/soap+xml}) is used to identify SOAP 1.2 message envelopes that have been 529 * serialized with XML 1.0. 530 * 531 * <p>For SOAP 1.1 messages, see {@code XML_UTF_8} per <a 532 * href="http://www.w3.org/TR/2000/NOTE-SOAP-20000508/">W3C Note on Simple Object Access Protocol 533 * (SOAP) 1.1</a> 534 * 535 * @since 20.0 536 */ 537 public static final MediaType SOAP_XML_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "soap+xml"); 538 539 public static final MediaType TAR = createConstant(APPLICATION_TYPE, "x-tar"); 540 541 /** 542 * Media type for the <a href="http://en.wikipedia.org/wiki/Web_Open_Font_Format">Web Open Font 543 * Format</a> (WOFF) <a href="http://www.w3.org/TR/WOFF/">defined</a> by the W3C. This is <a 544 * href="http://www.iana.org/assignments/media-types/application/font-woff">registered</a> with 545 * the IANA. 546 * 547 * @since 17.0 548 */ 549 public static final MediaType WOFF = createConstant(APPLICATION_TYPE, "font-woff"); 550 551 /** 552 * Media type for the <a href="http://en.wikipedia.org/wiki/Web_Open_Font_Format">Web Open Font 553 * Format</a> (WOFF) version 2 <a href="https://www.w3.org/TR/WOFF2/">defined</a> by the W3C. 554 * 555 * @since 20.0 556 */ 557 public static final MediaType WOFF2 = createConstant(APPLICATION_TYPE, "font-woff2"); 558 559 public static final MediaType XHTML_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "xhtml+xml"); 560 561 /** 562 * Media type for Extensible Resource Descriptors. This is not yet registered with the IANA, but 563 * it is specified by OASIS in the <a 564 * href="http://docs.oasis-open.org/xri/xrd/v1.0/cd02/xrd-1.0-cd02.html">XRD definition</a> and 565 * implemented in projects such as <a href="http://code.google.com/p/webfinger/">WebFinger</a>. 566 */ 567 public static final MediaType XRD_UTF_8 = createConstantUtf8(APPLICATION_TYPE, "xrd+xml"); 568 569 public static final MediaType ZIP = createConstant(APPLICATION_TYPE, "zip"); 570 571 private final String type; 572 private final String subtype; 573 private final ImmutableListMultimap<String, String> parameters; 574 575 @LazyInit private String toString; 576 577 @LazyInit private int hashCode; 578 579 @LazyInit private Optional<Charset> parsedCharset; 580 581 private MediaType(String type, String subtype, ImmutableListMultimap<String, String> parameters) { 582 this.type = type; 583 this.subtype = subtype; 584 this.parameters = parameters; 585 } 586 587 /** Returns the top-level media type. For example, {@code "text"} in {@code "text/plain"}. */ 588 public String type() { 589 return type; 590 } 591 592 /** Returns the media subtype. For example, {@code "plain"} in {@code "text/plain"}. */ 593 public String subtype() { 594 return subtype; 595 } 596 597 /** Returns a multimap containing the parameters of this media type. */ 598 public ImmutableListMultimap<String, String> parameters() { 599 return parameters; 600 } 601 602 private Map<String, ImmutableMultiset<String>> parametersAsMap() { 603 return Maps.transformValues( 604 parameters.asMap(), 605 new Function<Collection<String>, ImmutableMultiset<String>>() { 606 @Override 607 public ImmutableMultiset<String> apply(Collection<String> input) { 608 return ImmutableMultiset.copyOf(input); 609 } 610 }); 611 } 612 613 /** 614 * Returns an optional charset for the value of the charset parameter if it is specified. 615 * 616 * @throws IllegalStateException if multiple charset values have been set for this media type 617 * @throws IllegalCharsetNameException if a charset value is present, but illegal 618 * @throws UnsupportedCharsetException if a charset value is present, but no support is available 619 * in this instance of the Java virtual machine 620 */ 621 public Optional<Charset> charset() { 622 // racy single-check idiom, this is safe because Optional is immutable. 623 Optional<Charset> local = parsedCharset; 624 if (local == null) { 625 String value = null; 626 local = Optional.absent(); 627 for (String currentValue : parameters.get(CHARSET_ATTRIBUTE)) { 628 if (value == null) { 629 value = currentValue; 630 local = Optional.of(Charset.forName(value)); 631 } else if (!value.equals(currentValue)) { 632 throw new IllegalStateException( 633 "Multiple charset values defined: " + value + ", " + currentValue); 634 } 635 } 636 parsedCharset = local; 637 } 638 return local; 639 } 640 641 /** 642 * Returns a new instance with the same type and subtype as this instance, but without any 643 * parameters. 644 */ 645 public MediaType withoutParameters() { 646 return parameters.isEmpty() ? this : create(type, subtype); 647 } 648 649 /** 650 * <em>Replaces</em> all parameters with the given parameters. 651 * 652 * @throws IllegalArgumentException if any parameter or value is invalid 653 */ 654 public MediaType withParameters(Multimap<String, String> parameters) { 655 return create(type, subtype, parameters); 656 } 657 658 /** 659 * <em>Replaces</em> all parameters with the given attribute with parameters using the given 660 * values. If there are no values, any existing parameters with the given attribute are removed. 661 * 662 * @throws IllegalArgumentException if either {@code attribute} or {@code values} is invalid 663 * @since 24.0 664 */ 665 public MediaType withParameters(String attribute, Iterable<String> values) { 666 checkNotNull(attribute); 667 checkNotNull(values); 668 String normalizedAttribute = normalizeToken(attribute); 669 ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder(); 670 for (Entry<String, String> entry : parameters.entries()) { 671 String key = entry.getKey(); 672 if (!normalizedAttribute.equals(key)) { 673 builder.put(key, entry.getValue()); 674 } 675 } 676 for (String value : values) { 677 builder.put(normalizedAttribute, normalizeParameterValue(normalizedAttribute, value)); 678 } 679 MediaType mediaType = new MediaType(type, subtype, builder.build()); 680 // if the attribute isn't charset, we can just inherit the current parsedCharset 681 if (!normalizedAttribute.equals(CHARSET_ATTRIBUTE)) { 682 mediaType.parsedCharset = this.parsedCharset; 683 } 684 // Return one of the constants if the media type is a known type. 685 return MoreObjects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType); 686 } 687 688 /** 689 * <em>Replaces</em> all parameters with the given attribute with a single parameter with the 690 * given value. If multiple parameters with the same attributes are necessary use {@link 691 * #withParameters(String, Iterable)}. Prefer {@link #withCharset} for setting the {@code charset} 692 * parameter when using a {@link Charset} object. 693 * 694 * @throws IllegalArgumentException if either {@code attribute} or {@code value} is invalid 695 */ 696 public MediaType withParameter(String attribute, String value) { 697 return withParameters(attribute, ImmutableSet.of(value)); 698 } 699 700 /** 701 * Returns a new instance with the same type and subtype as this instance, with the {@code 702 * charset} parameter set to the {@link Charset#name name} of the given charset. Only one {@code 703 * charset} parameter will be present on the new instance regardless of the number set on this 704 * one. 705 * 706 * <p>If a charset must be specified that is not supported on this JVM (and thus is not 707 * representable as a {@link Charset} instance, use {@link #withParameter}. 708 */ 709 public MediaType withCharset(Charset charset) { 710 checkNotNull(charset); 711 MediaType withCharset = withParameter(CHARSET_ATTRIBUTE, charset.name()); 712 // precache the charset so we don't need to parse it 713 withCharset.parsedCharset = Optional.of(charset); 714 return withCharset; 715 } 716 717 /** Returns true if either the type or subtype is the wildcard. */ 718 public boolean hasWildcard() { 719 return WILDCARD.equals(type) || WILDCARD.equals(subtype); 720 } 721 722 /** 723 * Returns {@code true} if this instance falls within the range (as defined by <a 724 * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">the HTTP Accept header</a>) given 725 * by the argument according to three criteria: 726 * 727 * <ol> 728 * <li>The type of the argument is the wildcard or equal to the type of this instance. 729 * <li>The subtype of the argument is the wildcard or equal to the subtype of this instance. 730 * <li>All of the parameters present in the argument are present in this instance. 731 * </ol> 732 * 733 * <p>For example: 734 * 735 * <pre>{@code 736 * PLAIN_TEXT_UTF_8.is(PLAIN_TEXT_UTF_8) // true 737 * PLAIN_TEXT_UTF_8.is(HTML_UTF_8) // false 738 * PLAIN_TEXT_UTF_8.is(ANY_TYPE) // true 739 * PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE) // true 740 * PLAIN_TEXT_UTF_8.is(ANY_IMAGE_TYPE) // false 741 * PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE.withCharset(UTF_8)) // true 742 * PLAIN_TEXT_UTF_8.withoutParameters().is(ANY_TEXT_TYPE.withCharset(UTF_8)) // false 743 * PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE.withCharset(UTF_16)) // false 744 * }</pre> 745 * 746 * <p>Note that while it is possible to have the same parameter declared multiple times within a 747 * media type this method does not consider the number of occurrences of a parameter. For example, 748 * {@code "text/plain; charset=UTF-8"} satisfies {@code "text/plain; charset=UTF-8; 749 * charset=UTF-8"}. 750 */ 751 public boolean is(MediaType mediaTypeRange) { 752 return (mediaTypeRange.type.equals(WILDCARD) || mediaTypeRange.type.equals(this.type)) 753 && (mediaTypeRange.subtype.equals(WILDCARD) || mediaTypeRange.subtype.equals(this.subtype)) 754 && this.parameters.entries().containsAll(mediaTypeRange.parameters.entries()); 755 } 756 757 /** 758 * Creates a new media type with the given type and subtype. 759 * 760 * @throws IllegalArgumentException if type or subtype is invalid or if a wildcard is used for the 761 * type, but not the subtype. 762 */ 763 public static MediaType create(String type, String subtype) { 764 MediaType mediaType = create(type, subtype, ImmutableListMultimap.<String, String>of()); 765 mediaType.parsedCharset = Optional.absent(); 766 return mediaType; 767 } 768 769 private static MediaType create( 770 String type, String subtype, Multimap<String, String> parameters) { 771 checkNotNull(type); 772 checkNotNull(subtype); 773 checkNotNull(parameters); 774 String normalizedType = normalizeToken(type); 775 String normalizedSubtype = normalizeToken(subtype); 776 checkArgument( 777 !WILDCARD.equals(normalizedType) || WILDCARD.equals(normalizedSubtype), 778 "A wildcard type cannot be used with a non-wildcard subtype"); 779 ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder(); 780 for (Entry<String, String> entry : parameters.entries()) { 781 String attribute = normalizeToken(entry.getKey()); 782 builder.put(attribute, normalizeParameterValue(attribute, entry.getValue())); 783 } 784 MediaType mediaType = new MediaType(normalizedType, normalizedSubtype, builder.build()); 785 // Return one of the constants if the media type is a known type. 786 return MoreObjects.firstNonNull(KNOWN_TYPES.get(mediaType), mediaType); 787 } 788 789 /** 790 * Creates a media type with the "application" type and the given subtype. 791 * 792 * @throws IllegalArgumentException if subtype is invalid 793 */ 794 static MediaType createApplicationType(String subtype) { 795 return create(APPLICATION_TYPE, subtype); 796 } 797 798 /** 799 * Creates a media type with the "audio" type and the given subtype. 800 * 801 * @throws IllegalArgumentException if subtype is invalid 802 */ 803 static MediaType createAudioType(String subtype) { 804 return create(AUDIO_TYPE, subtype); 805 } 806 807 /** 808 * Creates a media type with the "image" type and the given subtype. 809 * 810 * @throws IllegalArgumentException if subtype is invalid 811 */ 812 static MediaType createImageType(String subtype) { 813 return create(IMAGE_TYPE, subtype); 814 } 815 816 /** 817 * Creates a media type with the "text" type and the given subtype. 818 * 819 * @throws IllegalArgumentException if subtype is invalid 820 */ 821 static MediaType createTextType(String subtype) { 822 return create(TEXT_TYPE, subtype); 823 } 824 825 /** 826 * Creates a media type with the "video" type and the given subtype. 827 * 828 * @throws IllegalArgumentException if subtype is invalid 829 */ 830 static MediaType createVideoType(String subtype) { 831 return create(VIDEO_TYPE, subtype); 832 } 833 834 private static String normalizeToken(String token) { 835 checkArgument(TOKEN_MATCHER.matchesAllOf(token)); 836 return Ascii.toLowerCase(token); 837 } 838 839 private static String normalizeParameterValue(String attribute, String value) { 840 return CHARSET_ATTRIBUTE.equals(attribute) ? Ascii.toLowerCase(value) : value; 841 } 842 843 /** 844 * Parses a media type from its string representation. 845 * 846 * @throws IllegalArgumentException if the input is not parsable 847 */ 848 public static MediaType parse(String input) { 849 checkNotNull(input); 850 Tokenizer tokenizer = new Tokenizer(input); 851 try { 852 String type = tokenizer.consumeToken(TOKEN_MATCHER); 853 tokenizer.consumeCharacter('/'); 854 String subtype = tokenizer.consumeToken(TOKEN_MATCHER); 855 ImmutableListMultimap.Builder<String, String> parameters = ImmutableListMultimap.builder(); 856 while (tokenizer.hasMore()) { 857 tokenizer.consumeTokenIfPresent(LINEAR_WHITE_SPACE); 858 tokenizer.consumeCharacter(';'); 859 tokenizer.consumeTokenIfPresent(LINEAR_WHITE_SPACE); 860 String attribute = tokenizer.consumeToken(TOKEN_MATCHER); 861 tokenizer.consumeCharacter('='); 862 final String value; 863 if ('"' == tokenizer.previewChar()) { 864 tokenizer.consumeCharacter('"'); 865 StringBuilder valueBuilder = new StringBuilder(); 866 while ('"' != tokenizer.previewChar()) { 867 if ('\\' == tokenizer.previewChar()) { 868 tokenizer.consumeCharacter('\\'); 869 valueBuilder.append(tokenizer.consumeCharacter(ascii())); 870 } else { 871 valueBuilder.append(tokenizer.consumeToken(QUOTED_TEXT_MATCHER)); 872 } 873 } 874 value = valueBuilder.toString(); 875 tokenizer.consumeCharacter('"'); 876 } else { 877 value = tokenizer.consumeToken(TOKEN_MATCHER); 878 } 879 parameters.put(attribute, value); 880 } 881 return create(type, subtype, parameters.build()); 882 } catch (IllegalStateException e) { 883 throw new IllegalArgumentException("Could not parse '" + input + "'", e); 884 } 885 } 886 887 private static final class Tokenizer { 888 final String input; 889 int position = 0; 890 891 Tokenizer(String input) { 892 this.input = input; 893 } 894 895 String consumeTokenIfPresent(CharMatcher matcher) { 896 checkState(hasMore()); 897 int startPosition = position; 898 position = matcher.negate().indexIn(input, startPosition); 899 return hasMore() ? input.substring(startPosition, position) : input.substring(startPosition); 900 } 901 902 String consumeToken(CharMatcher matcher) { 903 int startPosition = position; 904 String token = consumeTokenIfPresent(matcher); 905 checkState(position != startPosition); 906 return token; 907 } 908 909 char consumeCharacter(CharMatcher matcher) { 910 checkState(hasMore()); 911 char c = previewChar(); 912 checkState(matcher.matches(c)); 913 position++; 914 return c; 915 } 916 917 char consumeCharacter(char c) { 918 checkState(hasMore()); 919 checkState(previewChar() == c); 920 position++; 921 return c; 922 } 923 924 char previewChar() { 925 checkState(hasMore()); 926 return input.charAt(position); 927 } 928 929 boolean hasMore() { 930 return (position >= 0) && (position < input.length()); 931 } 932 } 933 934 @Override 935 public boolean equals(@Nullable Object obj) { 936 if (obj == this) { 937 return true; 938 } else if (obj instanceof MediaType) { 939 MediaType that = (MediaType) obj; 940 return this.type.equals(that.type) 941 && this.subtype.equals(that.subtype) 942 // compare parameters regardless of order 943 && this.parametersAsMap().equals(that.parametersAsMap()); 944 } else { 945 return false; 946 } 947 } 948 949 @Override 950 public int hashCode() { 951 // racy single-check idiom 952 int h = hashCode; 953 if (h == 0) { 954 h = Objects.hashCode(type, subtype, parametersAsMap()); 955 hashCode = h; 956 } 957 return h; 958 } 959 960 private static final MapJoiner PARAMETER_JOINER = Joiner.on("; ").withKeyValueSeparator("="); 961 962 /** 963 * Returns the string representation of this media type in the format described in <a 964 * href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>. 965 */ 966 @Override 967 public String toString() { 968 // racy single-check idiom, safe because String is immutable 969 String result = toString; 970 if (result == null) { 971 result = computeToString(); 972 toString = result; 973 } 974 return result; 975 } 976 977 private String computeToString() { 978 StringBuilder builder = new StringBuilder().append(type).append('/').append(subtype); 979 if (!parameters.isEmpty()) { 980 builder.append("; "); 981 Multimap<String, String> quotedParameters = 982 Multimaps.transformValues( 983 parameters, 984 new Function<String, String>() { 985 @Override 986 public String apply(String value) { 987 return TOKEN_MATCHER.matchesAllOf(value) ? value : escapeAndQuote(value); 988 } 989 }); 990 PARAMETER_JOINER.appendTo(builder, quotedParameters.entries()); 991 } 992 return builder.toString(); 993 } 994 995 private static String escapeAndQuote(String value) { 996 StringBuilder escaped = new StringBuilder(value.length() + 16).append('"'); 997 for (int i = 0; i < value.length(); i++) { 998 char ch = value.charAt(i); 999 if (ch == '\r' || ch == '\\' || ch == '"') { 1000 escaped.append('\\'); 1001 } 1002 escaped.append(ch); 1003 } 1004 return escaped.append('"').toString(); 1005 } 1006}