37
37
/**
38
38
* Represents an Internet Media Type, as defined in the HTTP specification.
39
39
*
40
- * <p>Consists of a {@linkplain #getType() type} and a {@linkplain #getSubtype() subtype}. Also has functionality to
41
- * parse media types from a string using {@link #parseMediaType(String)}, or multiple comma-separated media types using
42
- * {@link #parseMediaTypes(String)}.
40
+ * <p>Consists of a {@linkplain #getType() type} and a {@linkplain #getSubtype() subtype}.
41
+ * Also has functionality to parse media types from a string using {@link #parseMediaType(String)},
42
+ * or multiple comma-separated media types using {@link #parseMediaTypes(String)}.
43
43
*
44
44
* @author Arjen Poutsma
45
45
* @author Juergen Hoeller
46
- * @see <a href="http://tools.ietf.org/html/rfc2616#section-3.7">HTTP 1.1, section 3.7</a>
47
46
* @since 3.0
47
+ * @see <a href="http://tools.ietf.org/html/rfc2616#section-3.7">HTTP 1.1, section 3.7</a>
48
48
*/
49
49
public class MediaType implements Comparable <MediaType > {
50
50
@@ -118,6 +118,7 @@ public class MediaType implements Comparable<MediaType> {
118
118
* */
119
119
public final static MediaType TEXT_XML ;
120
120
121
+
121
122
private static final BitSet TOKEN ;
122
123
123
124
private static final String WILDCARD_TYPE = "*" ;
@@ -184,11 +185,10 @@ public class MediaType implements Comparable<MediaType> {
184
185
TEXT_XML = new MediaType ("text" ,"xml" );
185
186
}
186
187
188
+
187
189
/**
188
190
* Create a new {@link MediaType} for the given primary type.
189
- *
190
191
* <p>The {@linkplain #getSubtype() subtype} is set to <code>*</code>, parameters empty.
191
- *
192
192
* @param type the primary type
193
193
* @throws IllegalArgumentException if any of the parameters contain illegal characters
194
194
*/
@@ -197,8 +197,8 @@ public MediaType(String type) {
197
197
}
198
198
199
199
/**
200
- * Create a new {@link MediaType} for the given primary type and subtype. <p>The parameters are empty.
201
- *
200
+ * Create a new {@link MediaType} for the given primary type and subtype.
201
+ * <p>The parameters are empty.
202
202
* @param type the primary type
203
203
* @param subtype the subtype
204
204
* @throws IllegalArgumentException if any of the parameters contain illegal characters
@@ -209,7 +209,6 @@ public MediaType(String type, String subtype) {
209
209
210
210
/**
211
211
* Create a new {@link MediaType} for the given type, subtype, and character set.
212
- *
213
212
* @param type the primary type
214
213
* @param subtype the subtype
215
214
* @param charSet the character set
@@ -232,9 +231,8 @@ public MediaType(String type, String subtype, double qualityValue) {
232
231
}
233
232
234
233
/**
235
- * Copy-constructor that copies the type and subtype of the given {@link MediaType}, and allows for different
236
- * parameter.
237
- *
234
+ * Copy-constructor that copies the type and subtype of the given {@link MediaType},
235
+ * and allows for different parameter.
238
236
* @param other the other media type
239
237
* @param parameters the parameters, may be <code>null</code>
240
238
* @throws IllegalArgumentException if any of the parameters contain illegal characters
@@ -245,7 +243,6 @@ public MediaType(MediaType other, Map<String, String> parameters) {
245
243
246
244
/**
247
245
* Create a new {@link MediaType} for the given type, subtype, and parameters.
248
- *
249
246
* @param type the primary type
250
247
* @param subtype the subtype
251
248
* @param parameters the parameters, may be <code>null</code>
@@ -275,7 +272,6 @@ public MediaType(String type, String subtype, Map<String, String> parameters) {
275
272
276
273
/**
277
274
* Checks the given token string for illegal characters, as defined in RFC 2616, section 2.2.
278
- *
279
275
* @throws IllegalArgumentException in case of illegal characters
280
276
* @see <a href="http://tools.ietf.org/html/rfc2616#section-2.2">HTTP 1.1, section 2.2</a>
281
277
*/
@@ -295,7 +291,8 @@ private void checkParameters(String attribute, String value) {
295
291
if (PARAM_QUALITY_FACTOR .equals (attribute )) {
296
292
value = unquote (value );
297
293
double d = Double .parseDouble (value );
298
- Assert .isTrue (d >= 0D && d <= 1D , "Invalid quality value \" " + value + "\" : should be between 0.0 and 1.0" );
294
+ Assert .isTrue (d >= 0D && d <= 1D ,
295
+ "Invalid quality value \" " + value + "\" : should be between 0.0 and 1.0" );
299
296
}
300
297
else if (PARAM_CHARSET .equals (attribute )) {
301
298
value = unquote (value );
@@ -317,24 +314,29 @@ private String unquote(String s) {
317
314
return isQuotedString (s ) ? s .substring (1 , s .length () - 1 ) : s ;
318
315
}
319
316
320
- /** Return the primary type. */
317
+ /**
318
+ * Return the primary type.
319
+ */
321
320
public String getType () {
322
321
return this .type ;
323
322
}
324
323
325
- /** Indicate whether the {@linkplain #getType() type} is the wildcard character <code>*</code> or not. */
324
+ /**
325
+ * Indicate whether the {@linkplain #getType() type} is the wildcard character <code>*</code> or not.
326
+ */
326
327
public boolean isWildcardType () {
327
328
return WILDCARD_TYPE .equals (type );
328
329
}
329
330
330
- /** Return the subtype. */
331
+ /**
332
+ * Return the subtype.
333
+ */
331
334
public String getSubtype () {
332
335
return this .subtype ;
333
336
}
334
337
335
338
/**
336
339
* Indicate whether the {@linkplain #getSubtype() subtype} is the wildcard character <code>*</code> or not.
337
- *
338
340
* @return whether the subtype is <code>*</code>
339
341
*/
340
342
public boolean isWildcardSubtype () {
@@ -343,7 +345,6 @@ public boolean isWildcardSubtype() {
343
345
344
346
/**
345
347
* Return the character set, as indicated by a <code>charset</code> parameter, if any.
346
- *
347
348
* @return the character set; or <code>null</code> if not available
348
349
*/
349
350
public Charset getCharSet () {
@@ -352,8 +353,8 @@ public Charset getCharSet() {
352
353
}
353
354
354
355
/**
355
- * Return the quality value, as indicated by a <code>q</code> parameter, if any. Defaults to <code>1.0</code>.
356
- *
356
+ * Return the quality value, as indicated by a <code>q</code> parameter, if any.
357
+ * Defaults to <code>1.0</code>.
357
358
* @return the quality factory
358
359
*/
359
360
public double getQualityValue () {
@@ -363,7 +364,6 @@ public double getQualityValue() {
363
364
364
365
/**
365
366
* Return a generic parameter value, given a parameter name.
366
- *
367
367
* @param name the parameter name
368
368
* @return the parameter value; or <code>null</code> if not present
369
369
*/
@@ -373,10 +373,8 @@ public String getParameter(String name) {
373
373
374
374
/**
375
375
* Indicate whether this {@link MediaType} includes the given media type.
376
- *
377
376
* <p>For instance, {@code text/*} includes {@code text/plain}, {@code text/html}, and {@code application/*+xml}
378
377
* includes {@code application/soap+xml}, etc. This method is non-symmetic.
379
- *
380
378
* @param other the reference media type with which to compare
381
379
* @return <code>true</code> if this media type includes the given media type; <code>false</code> otherwise
382
380
*/
@@ -410,10 +408,8 @@ else if (this.type.equals(other.type)) {
410
408
411
409
/**
412
410
* Indicate whether this {@link MediaType} is compatible with the given media type.
413
- *
414
- * <p>For instance, {@code text/*} is compatible with {@code text/plain}, {@code text/html}, and vice versa. In
415
- * effect, this method is similar to {@link #includes(MediaType)}, except that it's symmetric.
416
- *
411
+ * <p>For instance, {@code text/*} is compatible with {@code text/plain}, {@code text/html}, and vice versa.
412
+ * In effect, this method is similar to {@link #includes(MediaType)}, except that it's symmetric.
417
413
* @param other the reference media type with which to compare
418
414
* @return <code>true</code> if this media type is compatible with the given media type; <code>false</code> otherwise
419
415
*/
@@ -449,7 +445,6 @@ else if (this.type.equals(other.type)) {
449
445
450
446
/**
451
447
* Compares this {@link MediaType} to another alphabetically.
452
- *
453
448
* @param other media type to compare to
454
449
* @see #sortBySpecificity(List)
455
450
*/
@@ -527,7 +522,7 @@ private void appendTo(StringBuilder builder) {
527
522
appendTo (this .parameters , builder );
528
523
}
529
524
530
- private static void appendTo (Map <String , String > map , StringBuilder builder ) {
525
+ private void appendTo (Map <String , String > map , StringBuilder builder ) {
531
526
for (Map .Entry <String , String > entry : map .entrySet ()) {
532
527
builder .append (';' );
533
528
builder .append (entry .getKey ());
@@ -536,9 +531,19 @@ private static void appendTo(Map<String, String> map, StringBuilder builder) {
536
531
}
537
532
}
538
533
534
+
535
+ /**
536
+ * Parse the given String value into a {@link MediaType} object,
537
+ * with this method name following the 'valueOf' naming convention
538
+ * (as supported by {@link org.springframework.core.convert.ConversionService}.
539
+ * @see #parseMediaType(String)
540
+ */
541
+ public static MediaType valueOf (String value ) {
542
+ return parseMediaType (value );
543
+ }
544
+
539
545
/**
540
546
* Parse the given String into a single {@link MediaType}.
541
- *
542
547
* @param mediaType the string to parse
543
548
* @return the media type
544
549
* @throws IllegalArgumentException if the string cannot be parsed
@@ -581,9 +586,8 @@ public static MediaType parseMediaType(String mediaType) {
581
586
582
587
583
588
/**
584
- * Parse the given, comma-seperated string into a list of {@link MediaType} objects. <p>This method can be used to
585
- * parse an Accept or Content-Type header.
586
- *
589
+ * Parse the given, comma-seperated string into a list of {@link MediaType} objects.
590
+ * <p>This method can be used to parse an Accept or Content-Type header.
587
591
* @param mediaTypes the string to parse
588
592
* @return the list of media types
589
593
* @throws IllegalArgumentException if the string cannot be parsed
@@ -602,9 +606,7 @@ public static List<MediaType> parseMediaTypes(String mediaTypes) {
602
606
603
607
/**
604
608
* Return a string representation of the given list of {@link MediaType} objects.
605
- *
606
609
* <p>This method can be used to for an {@code Accept} or {@code Content-Type} header.
607
- *
608
610
* @param mediaTypes the string to parse
609
611
* @return the list of media types
610
612
* @throws IllegalArgumentException if the String cannot be parsed
@@ -623,7 +625,6 @@ public static String toString(Collection<MediaType> mediaTypes) {
623
625
624
626
/**
625
627
* Sorts the given list of {@link MediaType} objects by specificity.
626
- *
627
628
* <p>Given two media types:
628
629
* <ol>
629
630
* <li>if either media type has a {@linkplain #isWildcardType() wildcard type}, then the media type without the
@@ -639,14 +640,12 @@ public static String toString(Collection<MediaType> mediaTypes) {
639
640
* <li>if the two media types have a different amount of {@linkplain #getParameter(String) parameters}, then the
640
641
* media type with the most parameters is ordered before the other.</li>
641
642
* </ol>
642
- *
643
643
* <p>For example:
644
644
* <blockquote>audio/basic < audio/* < */*</blockquote>
645
645
* <blockquote>audio/* < audio/*;q=0.7; audio/*;q=0.3</blockquote>
646
646
* <blockquote>audio/basic;level=1 < audio/basic</blockquote>
647
647
* <blockquote>audio/basic == text/html</blockquote>
648
648
* <blockquote>audio/basic == audio/wave</blockquote>
649
- *
650
649
* @param mediaTypes the list of media types to be sorted
651
650
* @see <a href="http://tools.ietf.org/html/rfc2616#section-14.1">HTTP 1.1, section 14.1</a>
652
651
*/
@@ -659,7 +658,6 @@ public static void sortBySpecificity(List<MediaType> mediaTypes) {
659
658
660
659
/**
661
660
* Sorts the given list of {@link MediaType} objects by quality value.
662
- *
663
661
* <p>Given two media types:
664
662
* <ol>
665
663
* <li>if the two media types have different {@linkplain #getQualityValue() quality value}, then the media type
@@ -675,7 +673,6 @@ public static void sortBySpecificity(List<MediaType> mediaTypes) {
675
673
* <li>if the two media types have a different amount of {@linkplain #getParameter(String) parameters}, then the
676
674
* media type with the most parameters is ordered before the other.</li>
677
675
* </ol>
678
- *
679
676
* @param mediaTypes the list of media types to be sorted
680
677
* @see #getQualityValue()
681
678
*/
@@ -686,6 +683,7 @@ public static void sortByQualityValue(List<MediaType> mediaTypes) {
686
683
}
687
684
}
688
685
686
+
689
687
static final Comparator <MediaType > SPECIFICITY_COMPARATOR = new Comparator <MediaType >() {
690
688
691
689
public int compare (MediaType mediaType1 , MediaType mediaType2 ) {
@@ -714,7 +712,8 @@ else if (!mediaType1.getSubtype().equals(mediaType2.getSubtype())) { // audio/ba
714
712
int qualityComparison = Double .compare (quality2 , quality1 );
715
713
if (qualityComparison != 0 ) {
716
714
return qualityComparison ; // audio/*;q=0.7 < audio/*;q=0.3
717
- } else {
715
+ }
716
+ else {
718
717
int paramsSize1 = mediaType1 .parameters .size ();
719
718
int paramsSize2 = mediaType2 .parameters .size ();
720
719
return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1 )); // audio/basic;level=1 < audio/basic
@@ -724,6 +723,7 @@ else if (!mediaType1.getSubtype().equals(mediaType2.getSubtype())) { // audio/ba
724
723
}
725
724
};
726
725
726
+
727
727
static final Comparator <MediaType > QUALITY_VALUE_COMPARATOR = new Comparator <MediaType >() {
728
728
729
729
public int compare (MediaType mediaType1 , MediaType mediaType2 ) {
@@ -751,12 +751,14 @@ else if (mediaType2.isWildcardSubtype() && !mediaType1.isWildcardSubtype()) { //
751
751
}
752
752
else if (!mediaType1 .getSubtype ().equals (mediaType2 .getSubtype ())) { // audio/basic == audio/wave
753
753
return 0 ;
754
- } else {
754
+ }
755
+ else {
755
756
int paramsSize1 = mediaType1 .parameters .size ();
756
757
int paramsSize2 = mediaType2 .parameters .size ();
757
758
return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1 )); // audio/basic;level=1 < audio/basic
758
759
}
759
760
}
760
761
}
761
762
};
763
+
762
764
}
0 commit comments