-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
CodecConfigurer.java
259 lines (226 loc) · 8.46 KB
/
CodecConfigurer.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
/*
* Copyright 2002-2019 the original author or authors.
*
* 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
*
* https://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 org.springframework.http.codec;
import java.util.List;
import java.util.function.Consumer;
import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.Encoder;
import org.springframework.lang.Nullable;
/**
* Defines a common interface for configuring either client or server HTTP
* message readers and writers. This is used as follows:
* <ul>
* <li>Use {@link ClientCodecConfigurer#create()} or
* {@link ServerCodecConfigurer#create()} to create an instance.
* <li>Use {@link #defaultCodecs()} to customize HTTP message readers or writers
* registered by default.
* <li>Use {@link #customCodecs()} to add custom HTTP message readers or writers.
* <li>Use {@link #getReaders()} and {@link #getWriters()} to obtain the list of
* configured HTTP message readers and writers.
* </ul>
*
* <p>HTTP message readers and writers are divided into 3 categories that are
* ordered as follows:
* <ol>
* <li>Typed readers and writers that support specific types, e.g. byte[], String.
* <li>Object readers and writers, e.g. JSON, XML.
* <li>Catch-all readers or writers, e.g. String with any media type.
* </ol>
*
* <p>Typed and object readers are further sub-divided and ordered as follows:
* <ol>
* <li>Default HTTP reader and writer registrations.
* <li>Custom readers and writers.
* </ol>
*
* @author Rossen Stoyanchev
* @since 5.0
*/
public interface CodecConfigurer {
/**
* Provides a way to customize or replace HTTP message readers and writers
* registered by default.
* @see #registerDefaults(boolean)
*/
DefaultCodecs defaultCodecs();
/**
* Register custom HTTP message readers or writers in addition to the ones
* registered by default.
*/
CustomCodecs customCodecs();
/**
* Provides a way to completely turn off registration of default HTTP message
* readers and writers, and instead rely only on the ones provided via
* {@link #customCodecs()}.
* <p>By default this is set to {@code "true"} in which case default
* registrations are made; setting this to {@code false} disables default
* registrations.
*/
void registerDefaults(boolean registerDefaults);
/**
* Obtain the configured HTTP message readers.
*/
List<HttpMessageReader<?>> getReaders();
/**
* Obtain the configured HTTP message writers.
*/
List<HttpMessageWriter<?>> getWriters();
/**
* Create a copy of this {@link CodecConfigurer}. The returned clone has its
* own lists of default and custom codecs and generally can be configured
* independently. Keep in mind however that codec instances (if any are
* configured) are themselves not cloned.
* @since 5.1.12
*/
CodecConfigurer clone();
/**
* Customize or replace the HTTP message readers and writers registered by
* default. The options are further extended by
* {@link ClientCodecConfigurer.ClientDefaultCodecs ClientDefaultCodecs} and
* {@link ServerCodecConfigurer.ServerDefaultCodecs ServerDefaultCodecs}.
*/
interface DefaultCodecs {
/**
* Override the default Jackson JSON {@code Decoder}.
* <p>Note that {@link #maxInMemorySize(int)}, if configured, will be
* applied to the given decoder.
* @param decoder the decoder instance to use
* @see org.springframework.http.codec.json.Jackson2JsonDecoder
*/
void jackson2JsonDecoder(Decoder<?> decoder);
/**
* Override the default Jackson JSON {@code Encoder}.
* @param encoder the encoder instance to use
* @see org.springframework.http.codec.json.Jackson2JsonEncoder
*/
void jackson2JsonEncoder(Encoder<?> encoder);
/**
* Override the default Protobuf {@code Decoder}.
* <p>Note that {@link #maxInMemorySize(int)}, if configured, will be
* applied to the given decoder.
* @param decoder the decoder instance to use
* @since 5.1
* @see org.springframework.http.codec.protobuf.ProtobufDecoder
*/
void protobufDecoder(Decoder<?> decoder);
/**
* Override the default Protobuf {@code Encoder}.
* @param encoder the encoder instance to use
* @since 5.1
* @see org.springframework.http.codec.protobuf.ProtobufEncoder
* @see org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter
*/
void protobufEncoder(Encoder<?> encoder);
/**
* Override the default JAXB2 {@code Decoder}.
* <p>Note that {@link #maxInMemorySize(int)}, if configured, will be
* applied to the given decoder.
* @param decoder the decoder instance to use
* @since 5.1.3
* @see org.springframework.http.codec.xml.Jaxb2XmlDecoder
*/
void jaxb2Decoder(Decoder<?> decoder);
/**
* Override the default JABX2 {@code Encoder}.
* @param encoder the encoder instance to use
* @since 5.1.3
* @see org.springframework.http.codec.xml.Jaxb2XmlEncoder
*/
void jaxb2Encoder(Encoder<?> encoder);
/**
* Configure a limit on the number of bytes that can be buffered whenever
* the input stream needs to be aggregated. This can be a result of
* decoding to a single {@code DataBuffer},
* {@link java.nio.ByteBuffer ByteBuffer}, {@code byte[]},
* {@link org.springframework.core.io.Resource Resource}, {@code String}, etc.
* It can also occur when splitting the input stream, e.g. delimited text,
* in which case the limit applies to data buffered between delimiters.
* <p>By default this is not set, in which case individual codec defaults
* apply. In 5.1 most codecs are not limited except {@code FormHttpMessageReader}
* which is limited to 256K. In 5.2 all codecs are limited to 256K by default.
* @param byteCount the max number of bytes to buffer, or -1 for unlimited
* @since 5.1.11
*/
void maxInMemorySize(int byteCount);
/**
* Whether to log form data at DEBUG level, and headers at TRACE level.
* Both may contain sensitive information.
* <p>By default set to {@code false} so that request details are not shown.
* @param enable whether to enable or not
* @since 5.1
*/
void enableLoggingRequestDetails(boolean enable);
}
/**
* Registry for custom HTTP message readers and writers.
*/
interface CustomCodecs {
/**
* Add a custom {@code Decoder} internally wrapped with
* {@link DecoderHttpMessageReader}).
* @param decoder the decoder to add
*/
void decoder(Decoder<?> decoder);
/**
* Add a custom {@code Encoder}, internally wrapped with
* {@link EncoderHttpMessageWriter}.
* @param encoder the encoder to add
*/
void encoder(Encoder<?> encoder);
/**
* Add a custom {@link HttpMessageReader}. For readers of type
* {@link DecoderHttpMessageReader} consider using the shortcut
* {@link #decoder(Decoder)} instead.
* @param reader the reader to add
*/
void reader(HttpMessageReader<?> reader);
/**
* Add a custom {@link HttpMessageWriter}. For writers of type
* {@link EncoderHttpMessageWriter} consider using the shortcut
* {@link #encoder(Encoder)} instead.
* @param writer the writer to add
*/
void writer(HttpMessageWriter<?> writer);
/**
* Register a callback for the {@link DefaultCodecConfig configuration}
* applied to default codecs. This allows custom codecs to follow general
* guidelines applied to default ones, such as logging details and limiting
* the amount of buffered data.
* @param codecsConfigConsumer the default codecs configuration callback
* @since 5.1.12
*/
void withDefaultCodecConfig(Consumer<DefaultCodecConfig> codecsConfigConsumer);
}
/**
* Common options applied to default codecs and passed in a callback to custom codecs
* so they get a chance to align their behavior on the default ones.
* @since 5.1.12
*/
interface DefaultCodecConfig {
/**
* Get the configured limit on the number of bytes that can be buffered whenever
* the input stream needs to be aggregated.
*/
@Nullable
Integer maxInMemorySize();
/**
* Whether to log form data at DEBUG level, and headers at TRACE level.
* Both may contain sensitive information.
*/
@Nullable
Boolean isEnableLoggingRequestDetails();
}
}