Browse Source

Nullability refinements

pull/23891/head
Juergen Hoeller 5 years ago
parent
commit
046380988b
  1. 6
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java
  2. 41
      spring-web/src/main/java/org/springframework/http/codec/multipart/SynchronossPartHttpMessageReader.java
  3. 6
      spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

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

@ -27,6 +27,7 @@ import java.nio.charset.Charset; @@ -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 { @@ -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();

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

@ -162,7 +162,6 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem @@ -162,7 +162,6 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem
(mediaType == null || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType));
}
@Override
public Flux<Part> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
return Flux.create(new SynchronossPartGenerator(message))
@ -176,13 +175,9 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem @@ -176,13 +175,9 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem
});
}
@Override
public Mono<Part> readMono(
ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
return Mono.error(new UnsupportedOperationException(
"Cannot read multipart request body into single Part"));
public Mono<Part> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
return Mono.error(new UnsupportedOperationException("Cannot read multipart request body into single Part"));
}
@ -196,16 +191,16 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem @@ -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<Part> sink) {
HttpHeaders headers = this.inputMessage.getHeaders();
@ -228,10 +223,13 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem @@ -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 @@ -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 @@ -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 @@ -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 @@ -338,7 +343,6 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem
private final AtomicInteger terminated = new AtomicInteger(0);
FluxSinkAdapterListener(
FluxSink<Part> sink, MultipartContext context, LimitedPartBodyStreamStorageFactory factory) {
@ -347,7 +351,6 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem @@ -347,7 +351,6 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem
this.storageFactory = factory;
}
@Override
public void onPartFinished(StreamStorage storage, Map<String, List<String>> headers) {
HttpHeaders httpHeaders = new HttpHeaders();

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

@ -27,6 +27,7 @@ import java.nio.charset.Charset; @@ -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 { @@ -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();

Loading…
Cancel
Save