From 046380988b9f98bed43fad1943c8f10f6a1429de Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 30 Oct 2019 00:26:11 +0100 Subject: [PATCH] Nullability refinements --- .../mock/web/MockHttpServletResponse.java | 6 ++- .../SynchronossPartHttpMessageReader.java | 41 ++++++++++--------- .../web/test/MockHttpServletResponse.java | 6 ++- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 7f0f826d7d..a1d5beefcd 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -27,6 +27,7 @@ import java.nio.charset.Charset; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collection; @@ -375,8 +376,9 @@ public class MockHttpServletResponse implements HttpServletResponse { if (maxAge >= 0) { buf.append("; Max-Age=").append(maxAge); buf.append("; Expires="); - if (cookie instanceof MockCookie && ((MockCookie) cookie).getExpires() != null) { - buf.append(((MockCookie) cookie).getExpires().format(DateTimeFormatter.RFC_1123_DATE_TIME)); + ZonedDateTime expires = (cookie instanceof MockCookie ? ((MockCookie) cookie).getExpires() : null); + if (expires != null) { + buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME)); } else { HttpHeaders headers = new HttpHeaders(); diff --git a/spring-web/src/main/java/org/springframework/http/codec/multipart/SynchronossPartHttpMessageReader.java b/spring-web/src/main/java/org/springframework/http/codec/multipart/SynchronossPartHttpMessageReader.java index 86f3db2571..d613dd6a94 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/multipart/SynchronossPartHttpMessageReader.java +++ b/spring-web/src/main/java/org/springframework/http/codec/multipart/SynchronossPartHttpMessageReader.java @@ -162,7 +162,6 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem (mediaType == null || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType)); } - @Override public Flux read(ResolvableType elementType, ReactiveHttpInputMessage message, Map hints) { return Flux.create(new SynchronossPartGenerator(message)) @@ -176,13 +175,9 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem }); } - @Override - public Mono readMono( - ResolvableType elementType, ReactiveHttpInputMessage message, Map hints) { - - return Mono.error(new UnsupportedOperationException( - "Cannot read multipart request body into single Part")); + public Mono readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map hints) { + return Mono.error(new UnsupportedOperationException("Cannot read multipart request body into single Part")); } @@ -196,16 +191,16 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem private final LimitedPartBodyStreamStorageFactory storageFactory = new LimitedPartBodyStreamStorageFactory(); + @Nullable private NioMultipartParserListener listener; + @Nullable private NioMultipartParser parser; - public SynchronossPartGenerator(ReactiveHttpInputMessage inputMessage) { this.inputMessage = inputMessage; } - @Override public void accept(FluxSink sink) { HttpHeaders headers = this.inputMessage.getHeaders(); @@ -228,10 +223,13 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem @Override protected void hookOnNext(DataBuffer buffer) { + Assert.state(this.parser != null && this.listener != null, "Not initialized yet"); + int size = buffer.readableByteCount(); this.storageFactory.increaseByteCount(size); byte[] resultBytes = new byte[size]; buffer.read(resultBytes); + try { this.parser.write(resultBytes); } @@ -248,24 +246,32 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem @Override protected void hookOnError(Throwable ex) { try { - this.parser.close(); + if (this.parser != null) { + this.parser.close(); + } } catch (IOException ex2) { // ignore } finally { - int index = this.storageFactory.getCurrentPartIndex(); - this.listener.onError("Failure while parsing part[" + index + "]", ex); + if (this.listener != null) { + int index = this.storageFactory.getCurrentPartIndex(); + this.listener.onError("Failure while parsing part[" + index + "]", ex); + } } } @Override protected void hookFinally(SignalType type) { try { - this.parser.close(); + if (this.parser != null) { + this.parser.close(); + } } catch (IOException ex) { - this.listener.onError("Error while closing parser", ex); + if (this.listener != null) { + this.listener.onError("Error while closing parser", ex); + } } } @@ -279,9 +285,9 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem private class LimitedPartBodyStreamStorageFactory implements PartBodyStreamStorageFactory { - private final PartBodyStreamStorageFactory storageFactory = maxInMemorySize > 0 ? + private final PartBodyStreamStorageFactory storageFactory = (maxInMemorySize > 0 ? new DefaultPartBodyStreamStorageFactory(maxInMemorySize) : - new DefaultPartBodyStreamStorageFactory(); + new DefaultPartBodyStreamStorageFactory()); private int index = 1; @@ -289,7 +295,6 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem private long partSize; - public int getCurrentPartIndex() { return this.index; } @@ -338,7 +343,6 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem private final AtomicInteger terminated = new AtomicInteger(0); - FluxSinkAdapterListener( FluxSink sink, MultipartContext context, LimitedPartBodyStreamStorageFactory factory) { @@ -347,7 +351,6 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem this.storageFactory = factory; } - @Override public void onPartFinished(StreamStorage storage, Map> headers) { HttpHeaders httpHeaders = new HttpHeaders(); diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java index 397a0606e1..21e24eb0eb 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java @@ -27,6 +27,7 @@ import java.nio.charset.Charset; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collection; @@ -375,8 +376,9 @@ public class MockHttpServletResponse implements HttpServletResponse { if (maxAge >= 0) { buf.append("; Max-Age=").append(maxAge); buf.append("; Expires="); - if (cookie instanceof MockCookie && ((MockCookie) cookie).getExpires() != null) { - buf.append(((MockCookie) cookie).getExpires().format(DateTimeFormatter.RFC_1123_DATE_TIME)); + ZonedDateTime expires = (cookie instanceof MockCookie ? ((MockCookie) cookie).getExpires() : null); + if (expires != null) { + buf.append(expires.format(DateTimeFormatter.RFC_1123_DATE_TIME)); } else { HttpHeaders headers = new HttpHeaders();