forked from googleapis/google-http-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpMediaType.java
340 lines (297 loc) · 11 KB
/
HttpMediaType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (c) 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.api.client.http;
import com.google.api.client.util.Preconditions;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* HTTP Media-type as specified in the HTTP RFC (
* {@link "http://tools.ietf.org/html/rfc2616#section-3.7"}).
*
* <p>
* Implementation is not thread-safe.
* </p>
*
* @author Matthias Linder (mlinder)
* @since 1.10
*/
public final class HttpMediaType {
/** Matches a valid media type or '*' (examples: "text" or "*"). */
private static final Pattern TYPE_REGEX;
/** Matches a valid token which might be used as a type, key parameter or key value. */
private static final Pattern TOKEN_REGEX;
/** The pattern matching the full HTTP media type string. */
private static final Pattern FULL_MEDIA_TYPE_REGEX;
/** The pattern matching a single parameter (key, value) at a time. */
private static final Pattern PARAMETER_REGEX;
/** The main type of the media type, for example {@code "text"}. */
private String type = "application";
/** The sub type of the media type, for example {@code "plain"}. */
private String subType = "octet-stream";
/** Additional parameters to the media type, for example {@code "charset=utf-8"}. */
private final SortedMap<String, String> parameters = new TreeMap<String, String>();
/** The last build result or {@code null}. */
private String cachedBuildResult;
/** Initialize all Patterns used for parsing. */
static {
// TYPE_REGEX: Very restrictive regex accepting valid types and '*' for e.g. "text/*".
// http://tools.ietf.org/html/rfc4288#section-4.2
TYPE_REGEX = Pattern.compile("[\\w!#$&.+\\-\\^_]+|[*]");
// TOKEN_REGEX: Restrictive (but less than TYPE_REGEX) regex accepting valid tokens.
// http://tools.ietf.org/html/rfc2045#section-5.1
TOKEN_REGEX =
Pattern.compile("[\\p{ASCII}&&[^\\p{Cntrl} ;/=\\[\\]\\(\\)\\<\\>\\@\\,\\:\\\"\\?\\=]]+");
// FULL_MEDIA_TYPE_REGEX: Unrestrictive regex matching the general structure of the media type.
// Used to split a Content-Type string into different parts. Unrestrictive so that invalid char
// detection can be done on a per-type/parameter basis.
String typeOrKey = "[^\\s/=;\"]+"; // only disallow separators
String wholeParameterSection = ";.*";
FULL_MEDIA_TYPE_REGEX = Pattern.compile(
"\\s*(" + typeOrKey + ")/(" + typeOrKey + ")" + // main type (G1)/sub type (G2)
"\\s*(" + wholeParameterSection + ")?", Pattern.DOTALL); // parameters (G3) or null
// PARAMETER_REGEX: Semi-restrictive regex matching each parameter in the parameter section.
// We also allow multipart values here (http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html)
// although those do not fully conform to the HTTP spec.
String quotedParameterValue = "\"([^\"]*)\"";
String unquotedParameterValue = "[^\\s;\"]*";
String parameterValue = quotedParameterValue + "|" + unquotedParameterValue;
PARAMETER_REGEX = Pattern.compile("\\s*;\\s*(" + typeOrKey + ")" + // parameter key (G1)
"=(" + parameterValue + ")"); // G2 (if quoted) and else G3
}
/**
* Initializes the {@link HttpMediaType} by setting the specified media type.
* @param type main media type, for example {@code "text"}
* @param subType sub media type, for example {@code "plain"}
*/
public HttpMediaType(String type, String subType) {
setType(type);
setSubType(subType);
}
/**
* Creates a {@link HttpMediaType} by parsing the specified media type string.
*
* @param mediaType full media type string, for example {@code "text/plain; charset=utf-8"}
*/
public HttpMediaType(String mediaType) {
fromString(mediaType);
}
/**
* Sets the (main) media type, for example {@code "text"}.
*
* @param type main/major media type
*/
public HttpMediaType setType(String type) {
Preconditions.checkArgument(
TYPE_REGEX.matcher(type).matches(), "Type contains reserved characters");
this.type = type;
cachedBuildResult = null;
return this;
}
/**
* Returns the main media type, for example {@code "text"}, or {@code null} for '*'.
*/
public String getType() {
return type;
}
/**
* Sets the sub media type, for example {@code "plain"} when using {@code "text"}.
*
* @param subType sub media type
*/
public HttpMediaType setSubType(String subType) {
Preconditions.checkArgument(
TYPE_REGEX.matcher(subType).matches(), "Subtype contains reserved characters");
this.subType = subType;
cachedBuildResult = null;
return this;
}
/**
* Returns the sub media type, for example {@code "plain"} when using {@code "text"}.
*/
public String getSubType() {
return subType;
}
/**
* Sets the full media type by parsing a full content-type string, for example
* {@code "text/plain; foo=bar"}.
*
* <p>
* This method will not clear existing parameters. Use {@link #clearParameters()} if this behavior
* is required.
* </p>
*
* @param combinedType full media type in the {@code "maintype/subtype; key=value"} format.
*/
private HttpMediaType fromString(String combinedType) {
Matcher matcher = FULL_MEDIA_TYPE_REGEX.matcher(combinedType);
Preconditions.checkArgument(
matcher.matches(), "Type must be in the 'maintype/subtype; parameter=value' format");
setType(matcher.group(1));
setSubType(matcher.group(2));
String params = matcher.group(3);
if (params != null) {
matcher = PARAMETER_REGEX.matcher(params);
while (matcher.find()) {
// 1=key, 2=valueWithQuotes, 3=valueWithoutQuotes
String key = matcher.group(1);
String value = matcher.group(3);
if (value == null) {
value = matcher.group(2);
}
setParameter(key, value);
}
}
return this;
}
/**
* Sets the media parameter to the specified value.
*
* @param name case-insensitive name of the parameter
* @param value value of the parameter or {@code null} to remove
*/
public HttpMediaType setParameter(String name, String value) {
if (value == null) {
removeParameter(name);
return this;
}
Preconditions.checkArgument(
TOKEN_REGEX.matcher(name).matches(), "Name contains reserved characters");
cachedBuildResult = null;
parameters.put(name.toLowerCase(Locale.US), value);
return this;
}
/**
* Returns the value of the specified parameter or {@code null} if not found.
*
* @param name name of the parameter
*/
public String getParameter(String name) {
return parameters.get(name.toLowerCase(Locale.US));
}
/**
* Removes the specified media parameter.
*
* @param name parameter to remove
*/
public HttpMediaType removeParameter(String name) {
cachedBuildResult = null;
parameters.remove(name.toLowerCase(Locale.US));
return this;
}
/**
* Removes all set parameters from this media type.
*/
public void clearParameters() {
cachedBuildResult = null;
parameters.clear();
}
/**
* Returns an unmodifiable map of all specified parameters. Parameter names will be stored in
* lower-case in this map.
*/
public Map<String, String> getParameters() {
return Collections.unmodifiableMap(parameters);
}
/**
* Returns whether the given value matches the regular expression for "token" as specified in <a
* href="http://tools.ietf.org/html/rfc2616#section-2.2">RFC 2616 section 2.2</a>.
*/
static boolean matchesToken(String value) {
return TOKEN_REGEX.matcher(value).matches();
}
private static String quoteString(String unquotedString) {
String escapedString = unquotedString.replace("\\", "\\\\"); // change \ to \\
escapedString = escapedString.replace("\"", "\\\""); // change " to \"
return "\"" + escapedString + "\"";
}
/**
* Builds the full media type string which can be passed in the Content-Type header.
*/
public String build() {
if (cachedBuildResult != null) {
return cachedBuildResult;
}
StringBuilder str = new StringBuilder();
str.append(type);
str.append('/');
str.append(subType);
if (parameters != null) {
for (Entry<String, String> entry : parameters.entrySet()) {
String value = entry.getValue();
str.append("; ");
str.append(entry.getKey());
str.append("=");
str.append(!matchesToken(value) ? quoteString(value) : value);
}
}
cachedBuildResult = str.toString();
return cachedBuildResult;
}
@Override
public String toString() {
return build();
}
/**
* Returns {@code true} if the specified media type has both the same type and subtype, or
* {@code false} if they don't match or the media type is {@code null}.
*/
public boolean equalsIgnoreParameters(HttpMediaType mediaType) {
return mediaType != null && getType().equalsIgnoreCase(mediaType.getType())
&& getSubType().equalsIgnoreCase(mediaType.getSubType());
}
/**
* Returns {@code true} if the two specified media types have the same type and subtype, or if
* both types are {@code null}.
*/
public static boolean equalsIgnoreParameters(String mediaTypeA, String mediaTypeB) {
// TODO(mlinder): Make the HttpMediaType.isSameType implementation more performant.
return (mediaTypeA == null && mediaTypeB == null) || mediaTypeA != null && mediaTypeB != null
&& new HttpMediaType(mediaTypeA).equalsIgnoreParameters(new HttpMediaType(mediaTypeB));
}
/**
* Sets the charset parameter of the media type.
*
* @param charset new value for the charset parameter or {@code null} to remove
*/
public HttpMediaType setCharsetParameter(Charset charset) {
setParameter("charset", charset == null ? null : charset.name());
return this;
}
/**
* Returns the specified charset or {@code null} if unset.
*/
public Charset getCharsetParameter() {
String value = getParameter("charset");
return value == null ? null : Charset.forName(value);
}
@Override
public int hashCode() {
return build().hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof HttpMediaType)) {
return false;
}
HttpMediaType otherType = (HttpMediaType) obj;
return equalsIgnoreParameters(otherType) && parameters.equals(otherType.parameters);
}
}