1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
31
31
import org .springframework .util .MultiValueMap ;
32
32
33
33
/**
34
- * An implementation of {@link MediaTypeFileExtensionResolver} that maintains a lookup
35
- * from extension to MediaType.
34
+ * An implementation of {@code MediaTypeFileExtensionResolver} that maintains
35
+ * lookups between file extensions and MediaTypes in both directions.
36
+ *
37
+ * <p>Initially created with a map of file extensions and media types.
38
+ * Subsequently sub-classes can use {@link #addMapping} to add more mappings.
36
39
*
37
40
* @author Rossen Stoyanchev
38
41
* @since 3.2
39
42
*/
40
43
public class MappingMediaTypeFileExtensionResolver implements MediaTypeFileExtensionResolver {
41
44
42
- private final ConcurrentMap <String , MediaType > mediaTypes = new ConcurrentHashMap <String , MediaType >(64 );
45
+ private final ConcurrentMap <String , MediaType > mediaTypes =
46
+ new ConcurrentHashMap <String , MediaType >(64 );
43
47
44
- private final MultiValueMap <MediaType , String > fileExtensions = new LinkedMultiValueMap <MediaType , String >();
48
+ private final MultiValueMap <MediaType , String > fileExtensions =
49
+ new LinkedMultiValueMap <MediaType , String >();
45
50
46
51
private final List <String > allFileExtensions = new LinkedList <String >();
47
52
48
53
49
54
/**
50
- * Create an instance with the given mappings between extensions and media types.
51
- * @throws IllegalArgumentException if a media type string cannot be parsed
55
+ * Create an instance with the given map of file extensions and media types.
52
56
*/
53
57
public MappingMediaTypeFileExtensionResolver (Map <String , MediaType > mediaTypes ) {
54
58
if (mediaTypes != null ) {
@@ -61,10 +65,22 @@ public MappingMediaTypeFileExtensionResolver(Map<String, MediaType> mediaTypes)
61
65
}
62
66
63
67
68
+ protected List <MediaType > getAllMediaTypes () {
69
+ return new ArrayList <MediaType >(this .mediaTypes .values ());
70
+ }
71
+
64
72
/**
65
- * Find the file extensions mapped to the given MediaType.
66
- * @return 0 or more extensions, never {@code null}
73
+ * Map an extension to a MediaType. Ignore if extension already mapped.
67
74
*/
75
+ protected void addMapping (String extension , MediaType mediaType ) {
76
+ MediaType previous = this .mediaTypes .putIfAbsent (extension , mediaType );
77
+ if (previous == null ) {
78
+ this .fileExtensions .add (mediaType , extension );
79
+ this .allFileExtensions .add (extension );
80
+ }
81
+ }
82
+
83
+
68
84
@ Override
69
85
public List <String > resolveFileExtensions (MediaType mediaType ) {
70
86
List <String > fileExtensions = this .fileExtensions .get (mediaType );
@@ -76,27 +92,12 @@ public List<String> getAllFileExtensions() {
76
92
return Collections .unmodifiableList (this .allFileExtensions );
77
93
}
78
94
79
- protected List <MediaType > getAllMediaTypes () {
80
- return new ArrayList <MediaType >(this .mediaTypes .values ());
81
- }
82
-
83
95
/**
84
- * Return the MediaType mapped to the given extension.
85
- * @return a MediaType for the key or {@code null}
96
+ * Use this method for a reverse lookup from extension to MediaType .
97
+ * @return a MediaType for the key, or {@code null} if none found
86
98
*/
87
99
protected MediaType lookupMediaType (String extension ) {
88
- return this .mediaTypes .get (extension );
89
- }
90
-
91
- /**
92
- * Map a MediaType to an extension or ignore if the extensions is already mapped.
93
- */
94
- protected void addMapping (String extension , MediaType mediaType ) {
95
- MediaType previous = this .mediaTypes .putIfAbsent (extension , mediaType );
96
- if (previous == null ) {
97
- this .fileExtensions .add (mediaType , extension );
98
- this .allFileExtensions .add (extension );
99
- }
100
+ return this .mediaTypes .get (extension .toLowerCase (Locale .ENGLISH ));
100
101
}
101
102
102
103
}
0 commit comments