Skip to content

Commit a989ea0

Browse files
committed
Polish Synchronoss message reader
Issue: SPR-16639
1 parent 729d0d2 commit a989ea0

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

spring-web/src/main/java/org/springframework/http/codec/multipart/SynchronossPartHttpMessageReader.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,27 @@ public Mono<Part> readMono(ResolvableType elementType, ReactiveHttpInputMessage
9898
Map<String, Object> hints) {
9999

100100
return Mono.error(new UnsupportedOperationException(
101-
"This reader does not support reading a single element."));
101+
"Can't read a multipart request body into a single Part."));
102102
}
103103

104104

105105
/**
106-
* Consume and feed input to the Synchronoss parser, then adapt parser
107-
* output events to {@code Flux<Sink<Part>>}.
106+
* Consume and feed input to the Synchronoss parser, then listen for parser
107+
* output events and adapt to {@code Flux<Sink<Part>>}.
108108
*/
109109
private static class SynchronossPartGenerator implements Consumer<FluxSink<Part>> {
110110

111111
private final ReactiveHttpInputMessage inputMessage;
112112

113113
private final DataBufferFactory bufferFactory;
114114

115+
115116
SynchronossPartGenerator(ReactiveHttpInputMessage inputMessage, DataBufferFactory factory) {
116117
this.inputMessage = inputMessage;
117118
this.bufferFactory = factory;
118119
}
119120

121+
120122
@Override
121123
public void accept(FluxSink<Part> emitter) {
122124
HttpHeaders headers = this.inputMessage.getHeaders();
@@ -176,12 +178,14 @@ private static class FluxSinkAdapterListener implements NioMultipartParserListen
176178

177179
private final AtomicInteger terminated = new AtomicInteger(0);
178180

179-
FluxSinkAdapterListener(FluxSink<Part> sink, DataBufferFactory bufferFactory, MultipartContext context) {
181+
182+
FluxSinkAdapterListener(FluxSink<Part> sink, DataBufferFactory factory, MultipartContext context) {
180183
this.sink = sink;
181-
this.bufferFactory = bufferFactory;
184+
this.bufferFactory = factory;
182185
this.context = context;
183186
}
184187

188+
185189
@Override
186190
public void onPartFinished(StreamStorage storage, Map<String, List<String>> headers) {
187191
HttpHeaders httpHeaders = new HttpHeaders();
@@ -192,14 +196,14 @@ public void onPartFinished(StreamStorage storage, Map<String, List<String>> head
192196
private Part createPart(StreamStorage storage, HttpHeaders httpHeaders) {
193197
String filename = MultipartUtils.getFileName(httpHeaders);
194198
if (filename != null) {
195-
return new SynchronossFilePart(httpHeaders, storage, this.bufferFactory, filename);
199+
return new SynchronossFilePart(httpHeaders, filename, storage, this.bufferFactory);
196200
}
197201
else if (MultipartUtils.isFormField(httpHeaders, this.context)) {
198202
String value = MultipartUtils.readFormParameterValue(storage, httpHeaders);
199203
return new SynchronossFormFieldPart(httpHeaders, this.bufferFactory, value);
200204
}
201205
else {
202-
return new DefaultSynchronossPart(httpHeaders, storage, this.bufferFactory);
206+
return new SynchronossPart(httpHeaders, storage, this.bufferFactory);
203207
}
204208
}
205209

@@ -229,47 +233,53 @@ public void onNestedPartFinished() {
229233

230234
private abstract static class AbstractSynchronossPart implements Part {
231235

236+
private final String name;
237+
232238
private final HttpHeaders headers;
233239

234240
private final DataBufferFactory bufferFactory;
235241

242+
236243
AbstractSynchronossPart(HttpHeaders headers, DataBufferFactory bufferFactory) {
237244
Assert.notNull(headers, "HttpHeaders is required");
238245
Assert.notNull(bufferFactory, "'bufferFactory' is required");
246+
this.name = MultipartUtils.getFieldName(headers);
239247
this.headers = headers;
240248
this.bufferFactory = bufferFactory;
241249
}
242250

251+
243252
@Override
244253
public String name() {
245-
return MultipartUtils.getFieldName(this.headers);
254+
return this.name;
246255
}
247256

248257
@Override
249258
public HttpHeaders headers() {
250259
return this.headers;
251260
}
252261

253-
protected DataBufferFactory getBufferFactory() {
262+
DataBufferFactory getBufferFactory() {
254263
return this.bufferFactory;
255264
}
256265
}
257266

258267

259-
private static class DefaultSynchronossPart extends AbstractSynchronossPart {
268+
private static class SynchronossPart extends AbstractSynchronossPart {
260269

261270
private final StreamStorage storage;
262271

263-
DefaultSynchronossPart(HttpHeaders headers, StreamStorage storage, DataBufferFactory factory) {
272+
273+
SynchronossPart(HttpHeaders headers, StreamStorage storage, DataBufferFactory factory) {
264274
super(headers, factory);
265275
Assert.notNull(storage, "'storage' is required");
266276
this.storage = storage;
267277
}
268278

279+
269280
@Override
270281
public Flux<DataBuffer> content() {
271-
return DataBufferUtils.readInputStream(this.storage::getInputStream, getBufferFactory(),
272-
4096);
282+
return DataBufferUtils.readInputStream(getStorage()::getInputStream, getBufferFactory(), 4096);
273283
}
274284

275285
protected StreamStorage getStorage() {
@@ -278,20 +288,23 @@ protected StreamStorage getStorage() {
278288
}
279289

280290

281-
private static class SynchronossFilePart extends DefaultSynchronossPart implements FilePart {
291+
private static class SynchronossFilePart extends SynchronossPart implements FilePart {
282292

283293
private static final OpenOption[] FILE_CHANNEL_OPTIONS = {
284294
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE };
285295

296+
286297
private final String filename;
287298

288-
public SynchronossFilePart(
289-
HttpHeaders headers, StreamStorage storage, DataBufferFactory factory, String filename) {
299+
300+
SynchronossFilePart(HttpHeaders headers, String filename, StreamStorage storage,
301+
DataBufferFactory factory) {
290302

291303
super(headers, storage, factory);
292304
this.filename = filename;
293305
}
294306

307+
295308
@Override
296309
public String filename() {
297310
return this.filename;
@@ -342,11 +355,13 @@ private static class SynchronossFormFieldPart extends AbstractSynchronossPart im
342355

343356
private final String content;
344357

358+
345359
SynchronossFormFieldPart(HttpHeaders headers, DataBufferFactory bufferFactory, String content) {
346360
super(headers, bufferFactory);
347361
this.content = content;
348362
}
349363

364+
350365
@Override
351366
public String value() {
352367
return this.content;
@@ -361,8 +376,8 @@ public Flux<DataBuffer> content() {
361376
}
362377

363378
private Charset getCharset() {
364-
return Optional.ofNullable(MultipartUtils.getCharEncoding(headers()))
365-
.map(Charset::forName).orElse(StandardCharsets.UTF_8);
379+
String name = MultipartUtils.getCharEncoding(headers());
380+
return (name != null ? Charset.forName(name) : StandardCharsets.UTF_8);
366381
}
367382
}
368383

0 commit comments

Comments
 (0)