22
22
import java .util .List ;
23
23
import java .util .function .Consumer ;
24
24
25
+ import io .netty .buffer .PooledByteBufAllocator ;
26
+
25
27
import org .springframework .core .ReactiveAdapterRegistry ;
28
+ import org .springframework .core .codec .ByteArrayDecoder ;
29
+ import org .springframework .core .codec .ByteArrayEncoder ;
30
+ import org .springframework .core .codec .ByteBufferDecoder ;
31
+ import org .springframework .core .codec .ByteBufferEncoder ;
32
+ import org .springframework .core .codec .CharSequenceEncoder ;
33
+ import org .springframework .core .codec .DataBufferDecoder ;
34
+ import org .springframework .core .codec .DataBufferEncoder ;
26
35
import org .springframework .core .codec .Decoder ;
27
36
import org .springframework .core .codec .Encoder ;
37
+ import org .springframework .core .codec .StringDecoder ;
28
38
import org .springframework .core .io .buffer .DataBufferFactory ;
29
- import org .springframework .core .io .buffer .DefaultDataBufferFactory ;
39
+ import org .springframework .core .io .buffer .NettyDataBufferFactory ;
30
40
import org .springframework .lang .Nullable ;
41
+ import org .springframework .util .AntPathMatcher ;
31
42
import org .springframework .util .Assert ;
43
+ import org .springframework .util .MimeTypeUtils ;
44
+ import org .springframework .util .RouteMatcher ;
45
+ import org .springframework .util .SimpleRouteMatcher ;
32
46
33
47
/**
34
48
* Default, package-private {@link RSocketStrategies} implementation.
@@ -42,18 +56,25 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
42
56
43
57
private final List <Decoder <?>> decoders ;
44
58
45
- private final ReactiveAdapterRegistry adapterRegistry ;
59
+ private final RouteMatcher routeMatcher ;
60
+
61
+ private final MetadataExtractor metadataExtractor ;
46
62
47
63
private final DataBufferFactory bufferFactory ;
48
64
65
+ private final ReactiveAdapterRegistry adapterRegistry ;
66
+
49
67
50
68
private DefaultRSocketStrategies (List <Encoder <?>> encoders , List <Decoder <?>> decoders ,
51
- ReactiveAdapterRegistry adapterRegistry , DataBufferFactory bufferFactory ) {
69
+ RouteMatcher routeMatcher , MetadataExtractor metadataExtractor ,
70
+ DataBufferFactory bufferFactory , ReactiveAdapterRegistry adapterRegistry ) {
52
71
53
72
this .encoders = Collections .unmodifiableList (encoders );
54
73
this .decoders = Collections .unmodifiableList (decoders );
55
- this .adapterRegistry = adapterRegistry ;
74
+ this .routeMatcher = routeMatcher ;
75
+ this .metadataExtractor = metadataExtractor ;
56
76
this .bufferFactory = bufferFactory ;
77
+ this .adapterRegistry = adapterRegistry ;
57
78
}
58
79
59
80
@@ -68,15 +89,25 @@ public List<Decoder<?>> decoders() {
68
89
}
69
90
70
91
@ Override
71
- public ReactiveAdapterRegistry reactiveAdapterRegistry () {
72
- return this .adapterRegistry ;
92
+ public RouteMatcher routeMatcher () {
93
+ return this .routeMatcher ;
94
+ }
95
+
96
+ @ Override
97
+ public MetadataExtractor metadataExtractor () {
98
+ return this .metadataExtractor ;
73
99
}
74
100
75
101
@ Override
76
102
public DataBufferFactory dataBufferFactory () {
77
103
return this .bufferFactory ;
78
104
}
79
105
106
+ @ Override
107
+ public ReactiveAdapterRegistry reactiveAdapterRegistry () {
108
+ return this .adapterRegistry ;
109
+ }
110
+
80
111
81
112
/**
82
113
* Default RSocketStrategies.Builder implementation.
@@ -87,21 +118,42 @@ static class DefaultRSocketStrategiesBuilder implements RSocketStrategies.Builde
87
118
88
119
private final List <Decoder <?>> decoders = new ArrayList <>();
89
120
121
+ @ Nullable
122
+ private RouteMatcher routeMatcher ;
123
+
124
+ @ Nullable
125
+ private MetadataExtractor metadataExtractor ;
126
+
90
127
private ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry .getSharedInstance ();
91
128
92
129
@ Nullable
93
- private DataBufferFactory dataBufferFactory ;
130
+ private DataBufferFactory bufferFactory ;
131
+
132
+
133
+ DefaultRSocketStrategiesBuilder () {
134
+
135
+ // Order of decoders may be significant for default data MimeType
136
+ // selection in RSocketRequester.Builder
137
+
138
+ this .decoders .add (StringDecoder .allMimeTypes ());
139
+ this .decoders .add (new ByteBufferDecoder ());
140
+ this .decoders .add (new ByteArrayDecoder ());
141
+ this .decoders .add (new DataBufferDecoder ());
94
142
95
- public DefaultRSocketStrategiesBuilder () {
143
+ this .encoders .add (CharSequenceEncoder .allMimeTypes ());
144
+ this .encoders .add (new ByteBufferEncoder ());
145
+ this .encoders .add (new ByteArrayEncoder ());
146
+ this .encoders .add (new DataBufferEncoder ());
96
147
}
97
148
98
- public DefaultRSocketStrategiesBuilder (RSocketStrategies other ) {
149
+ DefaultRSocketStrategiesBuilder (RSocketStrategies other ) {
99
150
this .encoders .addAll (other .encoders ());
100
151
this .decoders .addAll (other .decoders ());
101
152
this .adapterRegistry = other .reactiveAdapterRegistry ();
102
- this .dataBufferFactory = other .dataBufferFactory ();
153
+ this .bufferFactory = other .dataBufferFactory ();
103
154
}
104
155
156
+
105
157
@ Override
106
158
public Builder encoder (Encoder <?>... encoders ) {
107
159
this .encoders .addAll (Arrays .asList (encoders ));
@@ -126,6 +178,18 @@ public Builder decoders(Consumer<List<Decoder<?>>> consumer) {
126
178
return this ;
127
179
}
128
180
181
+ @ Override
182
+ public Builder routeMatcher (RouteMatcher routeMatcher ) {
183
+ this .routeMatcher = routeMatcher ;
184
+ return this ;
185
+ }
186
+
187
+ @ Override
188
+ public Builder metadataExtractor (MetadataExtractor metadataExtractor ) {
189
+ this .metadataExtractor = metadataExtractor ;
190
+ return this ;
191
+ }
192
+
129
193
@ Override
130
194
public Builder reactiveAdapterStrategy (ReactiveAdapterRegistry registry ) {
131
195
Assert .notNull (registry , "ReactiveAdapterRegistry is required" );
@@ -135,14 +199,34 @@ public Builder reactiveAdapterStrategy(ReactiveAdapterRegistry registry) {
135
199
136
200
@ Override
137
201
public Builder dataBufferFactory (DataBufferFactory bufferFactory ) {
138
- this .dataBufferFactory = bufferFactory ;
202
+ this .bufferFactory = bufferFactory ;
139
203
return this ;
140
204
}
141
205
142
206
@ Override
143
207
public RSocketStrategies build () {
144
- return new DefaultRSocketStrategies (this .encoders , this .decoders , this .adapterRegistry ,
145
- this .dataBufferFactory != null ? this .dataBufferFactory : new DefaultDataBufferFactory ());
208
+ return new DefaultRSocketStrategies (
209
+ this .encoders , this .decoders ,
210
+ this .routeMatcher != null ? this .routeMatcher : initRouteMatcher (),
211
+ this .metadataExtractor != null ? this .metadataExtractor : initMetadataExtractor (),
212
+ this .bufferFactory != null ? this .bufferFactory : initBufferFactory (),
213
+ this .adapterRegistry );
214
+ }
215
+
216
+ private RouteMatcher initRouteMatcher () {
217
+ AntPathMatcher pathMatcher = new AntPathMatcher ();
218
+ pathMatcher .setPathSeparator ("." );
219
+ return new SimpleRouteMatcher (pathMatcher );
220
+ }
221
+
222
+ private MetadataExtractor initMetadataExtractor () {
223
+ DefaultMetadataExtractor extractor = new DefaultMetadataExtractor ();
224
+ extractor .metadataToExtract (MimeTypeUtils .TEXT_PLAIN , String .class , MetadataExtractor .ROUTE_KEY );
225
+ return extractor ;
226
+ }
227
+
228
+ private DataBufferFactory initBufferFactory () {
229
+ return new NettyDataBufferFactory (PooledByteBufAllocator .DEFAULT );
146
230
}
147
231
}
148
232
0 commit comments