Represents an <a href="http://en.wikipedia.org/wiki/Internet_media_type">Internet Media Type</a> (also known as a MIME Type or Content Type). This class also supports the concept of media ranges <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1">defined by HTTP/1.1</a>. As such
| 72 | * @author Gregory Kick |
| 73 | */ |
| 74 | @GwtCompatible |
| 75 | @Immutable |
| 76 | public final class MediaType { |
| 77 | private static final String CHARSET_ATTRIBUTE = "charset"; |
| 78 | private static final ImmutableListMultimap<String, String> UTF_8_CONSTANT_PARAMETERS = |
| 79 | ImmutableListMultimap.of(CHARSET_ATTRIBUTE, Ascii.toLowerCase(UTF_8.name())); |
| 80 | |
| 81 | /** Matcher for type, subtype and attributes. */ |
| 82 | private static final CharMatcher TOKEN_MATCHER = |
| 83 | ascii() |
| 84 | .and(javaIsoControl().negate()) |
| 85 | .and(CharMatcher.isNot(' ')) |
| 86 | .and(CharMatcher.noneOf("()<>@,;:\\\"/[]?=")); |
| 87 | |
| 88 | private static final CharMatcher QUOTED_TEXT_MATCHER = ascii().and(CharMatcher.noneOf("\"\\\r")); |
| 89 | |
| 90 | /* |
| 91 | * This matches the same characters as linear-white-space from RFC 822, but we make no effort to |
| 92 | * enforce any particular rules with regards to line folding as stated in the class docs. |
| 93 | */ |
| 94 | private static final CharMatcher LINEAR_WHITE_SPACE = CharMatcher.anyOf(" \t\r\n"); |
| 95 | |
| 96 | // TODO(gak): make these public? |
| 97 | private static final String APPLICATION_TYPE = "application"; |
| 98 | private static final String AUDIO_TYPE = "audio"; |
| 99 | private static final String IMAGE_TYPE = "image"; |
| 100 | private static final String TEXT_TYPE = "text"; |
| 101 | private static final String VIDEO_TYPE = "video"; |
| 102 | private static final String FONT_TYPE = "font"; |
| 103 | |
| 104 | private static final String WILDCARD = "*"; |
| 105 | |
| 106 | private static final Map<MediaType, MediaType> knownTypes = new HashMap<>(); |
| 107 | |
| 108 | private static MediaType createConstant(String type, String subtype) { |
| 109 | MediaType mediaType = |
| 110 | addKnownType(new MediaType(type, subtype, ImmutableListMultimap.<String, String>of())); |
| 111 | mediaType.parsedCharset = Optional.absent(); |
| 112 | return mediaType; |
| 113 | } |
| 114 | |
| 115 | private static MediaType createConstantUtf8(String type, String subtype) { |
| 116 | MediaType mediaType = addKnownType(new MediaType(type, subtype, UTF_8_CONSTANT_PARAMETERS)); |
| 117 | mediaType.parsedCharset = Optional.of(UTF_8); |
| 118 | return mediaType; |
| 119 | } |
| 120 | |
| 121 | @CanIgnoreReturnValue |
| 122 | private static MediaType addKnownType(MediaType mediaType) { |
| 123 | knownTypes.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 | * |
nothing calls this directly
no test coverage detected