17
17
package org .apache .dubbo .remoting .http12 .message .codec ;
18
18
19
19
import org .apache .dubbo .common .URL ;
20
+ import org .apache .dubbo .common .config .Configuration ;
21
+ import org .apache .dubbo .common .config .ConfigurationUtils ;
20
22
import org .apache .dubbo .common .utils .Assert ;
23
+ import org .apache .dubbo .common .utils .StringUtils ;
21
24
import org .apache .dubbo .remoting .http12 .exception .UnsupportedMediaTypeException ;
22
25
import org .apache .dubbo .remoting .http12 .message .HttpMessageDecoder ;
23
26
import org .apache .dubbo .remoting .http12 .message .HttpMessageDecoderFactory ;
24
27
import org .apache .dubbo .remoting .http12 .message .HttpMessageEncoder ;
25
28
import org .apache .dubbo .remoting .http12 .message .HttpMessageEncoderFactory ;
29
+ import org .apache .dubbo .rpc .Constants ;
26
30
import org .apache .dubbo .rpc .model .FrameworkModel ;
27
31
32
+ import java .util .Collections ;
33
+ import java .util .HashSet ;
28
34
import java .util .List ;
29
35
import java .util .Map ;
30
36
import java .util .Optional ;
37
+ import java .util .Set ;
31
38
import java .util .concurrent .ConcurrentHashMap ;
32
39
33
40
public final class CodecUtils {
@@ -37,13 +44,20 @@ public final class CodecUtils {
37
44
private final List <HttpMessageEncoderFactory > encoderFactories ;
38
45
private final Map <String , Optional <HttpMessageEncoderFactory >> encoderCache = new ConcurrentHashMap <>();
39
46
private final Map <String , Optional <HttpMessageDecoderFactory >> decoderCache = new ConcurrentHashMap <>();
47
+ private Set <String > disallowedContentTypes = Collections .emptySet ();
40
48
41
49
public CodecUtils (FrameworkModel frameworkModel ) {
42
50
this .frameworkModel = frameworkModel ;
43
51
decoderFactories = frameworkModel .getActivateExtensions (HttpMessageDecoderFactory .class );
44
52
encoderFactories = frameworkModel .getActivateExtensions (HttpMessageEncoderFactory .class );
45
53
decoderFactories .forEach (f -> decoderCache .putIfAbsent (f .mediaType ().getName (), Optional .of (f )));
46
54
encoderFactories .forEach (f -> encoderCache .putIfAbsent (f .mediaType ().getName (), Optional .of (f )));
55
+
56
+ Configuration configuration = ConfigurationUtils .getGlobalConfiguration (frameworkModel .defaultApplication ());
57
+ String allowContentTypes = configuration .getString (Constants .H2_SETTINGS_DISALLOWED_CONTENT_TYPES , null );
58
+ if (allowContentTypes != null ) {
59
+ disallowedContentTypes = new HashSet <>(StringUtils .tokenizeToList (allowContentTypes ));
60
+ }
47
61
}
48
62
49
63
public HttpMessageDecoder determineHttpMessageDecoder (URL url , String mediaType ) {
@@ -69,9 +83,10 @@ public HttpMessageEncoder determineHttpMessageEncoder(String mediaType) {
69
83
public Optional <HttpMessageDecoderFactory > determineHttpMessageDecoderFactory (String mediaType ) {
70
84
Assert .notNull (mediaType , "mediaType must not be null" );
71
85
return decoderCache .computeIfAbsent (mediaType , k -> {
72
- for (HttpMessageDecoderFactory decoderFactory : decoderFactories ) {
73
- if (decoderFactory .supports (k )) {
74
- return Optional .of (decoderFactory );
86
+ for (HttpMessageDecoderFactory factory : decoderFactories ) {
87
+ if (factory .supports (k )
88
+ && !disallowedContentTypes .contains (factory .mediaType ().getName ())) {
89
+ return Optional .of (factory );
75
90
}
76
91
}
77
92
return Optional .empty ();
@@ -81,9 +96,10 @@ public Optional<HttpMessageDecoderFactory> determineHttpMessageDecoderFactory(St
81
96
public Optional <HttpMessageEncoderFactory > determineHttpMessageEncoderFactory (String mediaType ) {
82
97
Assert .notNull (mediaType , "mediaType must not be null" );
83
98
return encoderCache .computeIfAbsent (mediaType , k -> {
84
- for (HttpMessageEncoderFactory encoderFactory : encoderFactories ) {
85
- if (encoderFactory .supports (k )) {
86
- return Optional .of (encoderFactory );
99
+ for (HttpMessageEncoderFactory factory : encoderFactories ) {
100
+ if (factory .supports (k )
101
+ && !disallowedContentTypes .contains (factory .mediaType ().getName ())) {
102
+ return Optional .of (factory );
87
103
}
88
104
}
89
105
return Optional .empty ();
0 commit comments