Skip to content

Commit f16aa4a

Browse files
committed
Nullability refinements
1 parent a3c9e8d commit f16aa4a

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.text.DateFormat;
2727
import java.text.ParseException;
2828
import java.text.SimpleDateFormat;
29+
import java.time.ZonedDateTime;
2930
import java.time.format.DateTimeFormatter;
3031
import java.util.ArrayList;
3132
import java.util.Collection;
@@ -346,8 +347,9 @@ private String getCookieHeader(Cookie cookie) {
346347
if (maxAge >= 0) {
347348
buf.append("; Max-Age=").append(maxAge);
348349
buf.append("; Expires=");
349-
if (cookie instanceof MockCookie && ((MockCookie) cookie).getExpires() != null) {
350-
buf.append(((MockCookie) cookie).getExpires().format(DateTimeFormatter.RFC_1123_DATE_TIME));
350+
ZonedDateTime expires = (cookie instanceof MockCookie ? ((MockCookie) cookie).getExpires() : null);
351+
if (expires != null) {
352+
buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME));
351353
}
352354
else {
353355
HttpHeaders headers = new HttpHeaders();

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType
163163
(mediaType == null || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType));
164164
}
165165

166-
167166
@Override
168167
public Flux<Part> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
169168
return Flux.create(new SynchronossPartGenerator(message))
@@ -177,13 +176,9 @@ public Flux<Part> read(ResolvableType elementType, ReactiveHttpInputMessage mess
177176
});
178177
}
179178

180-
181179
@Override
182-
public Mono<Part> readMono(
183-
ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
184-
185-
return Mono.error(new UnsupportedOperationException(
186-
"Cannot read multipart request body into single Part"));
180+
public Mono<Part> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
181+
return Mono.error(new UnsupportedOperationException("Cannot read multipart request body into single Part"));
187182
}
188183

189184

@@ -197,16 +192,16 @@ private class SynchronossPartGenerator extends BaseSubscriber<DataBuffer> implem
197192

198193
private final LimitedPartBodyStreamStorageFactory storageFactory = new LimitedPartBodyStreamStorageFactory();
199194

195+
@Nullable
200196
private NioMultipartParserListener listener;
201197

198+
@Nullable
202199
private NioMultipartParser parser;
203200

204-
205201
public SynchronossPartGenerator(ReactiveHttpInputMessage inputMessage) {
206202
this.inputMessage = inputMessage;
207203
}
208204

209-
210205
@Override
211206
public void accept(FluxSink<Part> sink) {
212207
HttpHeaders headers = this.inputMessage.getHeaders();
@@ -229,10 +224,13 @@ public void accept(FluxSink<Part> sink) {
229224

230225
@Override
231226
protected void hookOnNext(DataBuffer buffer) {
227+
Assert.state(this.parser != null && this.listener != null, "Not initialized yet");
228+
232229
int size = buffer.readableByteCount();
233230
this.storageFactory.increaseByteCount(size);
234231
byte[] resultBytes = new byte[size];
235232
buffer.read(resultBytes);
233+
236234
try {
237235
this.parser.write(resultBytes);
238236
}
@@ -249,24 +247,32 @@ protected void hookOnNext(DataBuffer buffer) {
249247
@Override
250248
protected void hookOnError(Throwable ex) {
251249
try {
252-
this.parser.close();
250+
if (this.parser != null) {
251+
this.parser.close();
252+
}
253253
}
254254
catch (IOException ex2) {
255255
// ignore
256256
}
257257
finally {
258-
int index = this.storageFactory.getCurrentPartIndex();
259-
this.listener.onError("Failure while parsing part[" + index + "]", ex);
258+
if (this.listener != null) {
259+
int index = this.storageFactory.getCurrentPartIndex();
260+
this.listener.onError("Failure while parsing part[" + index + "]", ex);
261+
}
260262
}
261263
}
262264

263265
@Override
264266
protected void hookFinally(SignalType type) {
265267
try {
266-
this.parser.close();
268+
if (this.parser != null) {
269+
this.parser.close();
270+
}
267271
}
268272
catch (IOException ex) {
269-
this.listener.onError("Error while closing parser", ex);
273+
if (this.listener != null) {
274+
this.listener.onError("Error while closing parser", ex);
275+
}
270276
}
271277
}
272278

@@ -280,17 +286,16 @@ private int getContentLength(HttpHeaders headers) {
280286

281287
private class LimitedPartBodyStreamStorageFactory implements PartBodyStreamStorageFactory {
282288

283-
private final PartBodyStreamStorageFactory storageFactory = maxInMemorySize > 0 ?
289+
private final PartBodyStreamStorageFactory storageFactory = (maxInMemorySize > 0 ?
284290
new DefaultPartBodyStreamStorageFactory(maxInMemorySize) :
285-
new DefaultPartBodyStreamStorageFactory();
291+
new DefaultPartBodyStreamStorageFactory());
286292

287293
private int index = 1;
288294

289295
private boolean isFilePart;
290296

291297
private long partSize;
292298

293-
294299
public int getCurrentPartIndex() {
295300
return this.index;
296301
}
@@ -339,7 +344,6 @@ private static class FluxSinkAdapterListener implements NioMultipartParserListen
339344

340345
private final AtomicInteger terminated = new AtomicInteger(0);
341346

342-
343347
FluxSinkAdapterListener(
344348
FluxSink<Part> sink, MultipartContext context, LimitedPartBodyStreamStorageFactory factory) {
345349

@@ -348,7 +352,6 @@ private static class FluxSinkAdapterListener implements NioMultipartParserListen
348352
this.storageFactory = factory;
349353
}
350354

351-
352355
@Override
353356
public void onPartFinished(StreamStorage storage, Map<String, List<String>> headers) {
354357
HttpHeaders httpHeaders = new HttpHeaders();

spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.text.DateFormat;
2727
import java.text.ParseException;
2828
import java.text.SimpleDateFormat;
29+
import java.time.ZonedDateTime;
2930
import java.time.format.DateTimeFormatter;
3031
import java.util.ArrayList;
3132
import java.util.Collection;
@@ -346,8 +347,9 @@ private String getCookieHeader(Cookie cookie) {
346347
if (maxAge >= 0) {
347348
buf.append("; Max-Age=").append(maxAge);
348349
buf.append("; Expires=");
349-
if (cookie instanceof MockCookie && ((MockCookie) cookie).getExpires() != null) {
350-
buf.append(((MockCookie) cookie).getExpires().format(DateTimeFormatter.RFC_1123_DATE_TIME));
350+
ZonedDateTime expires = (cookie instanceof MockCookie ? ((MockCookie) cookie).getExpires() : null);
351+
if (expires != null) {
352+
buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME));
351353
}
352354
else {
353355
HttpHeaders headers = new HttpHeaders();

0 commit comments

Comments
 (0)