Skip to content

Commit 2d94b43

Browse files
committed
Polishing
1 parent f7f7343 commit 2d94b43

File tree

5 files changed

+40
-52
lines changed

5 files changed

+40
-52
lines changed

spring-web/src/main/java/org/springframework/http/ContentDisposition.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ public int hashCode() {
233233
result = 31 * result + ObjectUtils.nullSafeHashCode(this.filename);
234234
result = 31 * result + ObjectUtils.nullSafeHashCode(this.charset);
235235
result = 31 * result + ObjectUtils.nullSafeHashCode(this.size);
236-
result = 31 * result + (this.creationDate != null ? this.creationDate.hashCode() : 0);
237-
result = 31 * result + (this.modificationDate != null ? this.modificationDate.hashCode() : 0);
238-
result = 31 * result + (this.readDate != null ? this.readDate.hashCode() : 0);
236+
result = 31 * result + ObjectUtils.nullSafeHashCode(this.creationDate);
237+
result = 31 * result + ObjectUtils.nullSafeHashCode(this.modificationDate);
238+
result = 31 * result + ObjectUtils.nullSafeHashCode(this.readDate);
239239
return result;
240240
}
241241

@@ -360,7 +360,7 @@ else if (attribute.equals("filename*") ) {
360360
if (idx1 != -1 && idx2 != -1) {
361361
charset = Charset.forName(value.substring(0, idx1).trim());
362362
Assert.isTrue(UTF_8.equals(charset) || ISO_8859_1.equals(charset),
363-
"Charset should be UTF-8 or ISO-8859-1");
363+
"Charset must be UTF-8 or ISO-8859-1");
364364
filename = decodeFilename(value.substring(idx2 + 1), charset);
365365
}
366366
else {
@@ -532,10 +532,11 @@ private static String escapeQuotationsInFilename(String filename) {
532532
* @see <a href="https://tools.ietf.org/html/rfc5987">RFC 5987</a>
533533
*/
534534
private static String encodeFilename(String input, Charset charset) {
535-
Assert.notNull(input, "'input' is required");
536-
Assert.notNull(charset, "'charset' is required");
535+
Assert.notNull(input, "'input' must not be null");
536+
Assert.notNull(charset, "'charset' must not be null");
537537
Assert.isTrue(!StandardCharsets.US_ASCII.equals(charset), "ASCII does not require encoding");
538538
Assert.isTrue(UTF_8.equals(charset) || ISO_8859_1.equals(charset), "Only UTF-8 and ISO-8859-1 are supported");
539+
539540
byte[] source = input.getBytes(charset);
540541
int len = source.length;
541542
StringBuilder sb = new StringBuilder(len << 1);

spring-web/src/main/java/org/springframework/http/server/reactive/JettyHttpHandlerAdapter.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,12 +65,12 @@ public JettyHttpHandlerAdapter(HttpHandler httpHandler) {
6565
protected ServletServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context)
6666
throws IOException, URISyntaxException {
6767

68-
// TODO: need to compile against Jetty 10 to use HttpFields (class->interface)
6968
if (jetty10Present) {
69+
// No HttpFields optimization on Jetty 10 due to binary incompatibility
7070
return super.createRequest(request, context);
7171
}
7272

73-
Assert.notNull(getServletPath(), "Servlet path is not initialized");
73+
Assert.state(getServletPath() != null, "Servlet path is not initialized");
7474
return new JettyServerHttpRequest(
7575
request, context, getServletPath(), getDataBufferFactory(), getBufferSize());
7676
}
@@ -79,15 +79,14 @@ protected ServletServerHttpRequest createRequest(HttpServletRequest request, Asy
7979
protected ServletServerHttpResponse createResponse(HttpServletResponse response,
8080
AsyncContext context, ServletServerHttpRequest request) throws IOException {
8181

82-
// TODO: need to compile against Jetty 10 to use HttpFields (class->interface)
8382
if (jetty10Present) {
83+
// No HttpFields optimization on Jetty 10 due to binary incompatibility
8484
return new BaseJettyServerHttpResponse(
8585
response, context, getDataBufferFactory(), getBufferSize(), request);
8686
}
87-
else {
88-
return new JettyServerHttpResponse(
89-
response, context, getDataBufferFactory(), getBufferSize(), request);
90-
}
87+
88+
return new JettyServerHttpResponse(
89+
response, context, getDataBufferFactory(), getBufferSize(), request);
9190
}
9291

9392

@@ -120,8 +119,6 @@ else if (request instanceof HttpServletRequestWrapper) {
120119
"] to org.eclipse.jetty.server.Request");
121120
}
122121
}
123-
124-
125122
}
126123

127124

spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -170,7 +170,7 @@ public void service(ServletRequest request, ServletResponse response) throws Ser
170170
AsyncListener requestListener;
171171
String logPrefix;
172172
try {
173-
httpRequest = createRequest(((HttpServletRequest) request), asyncContext);
173+
httpRequest = createRequest((HttpServletRequest) request, asyncContext);
174174
requestListener = httpRequest.getAsyncListener();
175175
logPrefix = httpRequest.getLogPrefix();
176176
}
@@ -183,8 +183,10 @@ public void service(ServletRequest request, ServletResponse response) throws Ser
183183
return;
184184
}
185185

186-
ServerHttpResponse httpResponse = createResponse(((HttpServletResponse) response), asyncContext, httpRequest);
187-
AsyncListener responseListener = ((ServletServerHttpResponse) httpResponse).getAsyncListener();
186+
ServletServerHttpResponse wrappedResponse =
187+
createResponse((HttpServletResponse) response, asyncContext, httpRequest);
188+
ServerHttpResponse httpResponse = wrappedResponse;
189+
AsyncListener responseListener = wrappedResponse.getAsyncListener();
188190
if (httpRequest.getMethod() == HttpMethod.HEAD) {
189191
httpResponse = new HttpHeadResponseDecorator(httpResponse);
190192
}
@@ -201,7 +203,7 @@ public void service(ServletRequest request, ServletResponse response) throws Ser
201203
protected ServletServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context)
202204
throws IOException, URISyntaxException {
203205

204-
Assert.notNull(this.servletPath, "Servlet path is not initialized");
206+
Assert.state(this.servletPath != null, "Servlet path is not initialized");
205207
return new ServletServerHttpRequest(
206208
request, context, this.servletPath, getDataBufferFactory(), getBufferSize());
207209
}
@@ -264,9 +266,7 @@ private static class HttpHandlerAsyncListener implements AsyncListener {
264266

265267
private final String logPrefix;
266268

267-
268-
public HttpHandlerAsyncListener(
269-
AsyncListener requestAsyncListener, AsyncListener responseAsyncListener,
269+
public HttpHandlerAsyncListener(AsyncListener requestAsyncListener, AsyncListener responseAsyncListener,
270270
Runnable handlerDisposeTask, AtomicBoolean completionFlag, String logPrefix) {
271271

272272
this.requestAsyncListener = requestAsyncListener;
@@ -276,7 +276,6 @@ public HttpHandlerAsyncListener(
276276
this.logPrefix = logPrefix;
277277
}
278278

279-
280279
@Override
281280
public void onTimeout(AsyncEvent event) {
282281
// Should never happen since we call asyncContext.setTimeout(-1)
@@ -362,9 +361,7 @@ private static class HandlerResultSubscriber implements Subscriber<Void>, Runnab
362361
@Nullable
363362
private volatile Subscription subscription;
364363

365-
public HandlerResultSubscriber(
366-
AsyncContext asyncContext, AtomicBoolean completionFlag, String logPrefix) {
367-
364+
public HandlerResultSubscriber(AsyncContext asyncContext, AtomicBoolean completionFlag, String logPrefix) {
368365
this.asyncContext = asyncContext;
369366
this.completionFlag = completionFlag;
370367
this.logPrefix = logPrefix;

spring-web/src/main/java/org/springframework/http/server/reactive/TomcatHttpHandlerAdapter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
*/
5959
public class TomcatHttpHandlerAdapter extends ServletHttpHandlerAdapter {
6060

61-
6261
public TomcatHttpHandlerAdapter(HttpHandler httpHandler) {
6362
super(httpHandler);
6463
}
@@ -68,7 +67,7 @@ public TomcatHttpHandlerAdapter(HttpHandler httpHandler) {
6867
protected ServletServerHttpRequest createRequest(HttpServletRequest request, AsyncContext asyncContext)
6968
throws IOException, URISyntaxException {
7069

71-
Assert.notNull(getServletPath(), "Servlet path is not initialized");
70+
Assert.state(getServletPath() != null, "Servlet path is not initialized");
7271
return new TomcatServerHttpRequest(
7372
request, asyncContext, getServletPath(), getDataBufferFactory(), getBufferSize());
7473
}

spring-web/src/test/java/org/springframework/http/ContentDispositionTests.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,20 +25,22 @@
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
2727
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
28+
import static org.springframework.http.ContentDisposition.parse;
2829

2930
/**
30-
* Unit tests for {@link ContentDisposition}
31+
* Unit tests for {@link ContentDisposition}.
32+
*
3133
* @author Sebastien Deleuze
3234
* @author Rossen Stoyanchev
3335
*/
3436
class ContentDispositionTests {
3537

36-
private static DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
38+
private static final DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
3739

3840

39-
@Test
4041
@SuppressWarnings("deprecation")
41-
void parse() {
42+
@Test
43+
void parseFilenameQuoted() {
4244
assertThat(parse("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123"))
4345
.isEqualTo(ContentDisposition.formData()
4446
.name("foo")
@@ -72,21 +74,21 @@ void parseEncodedFilename() {
7274
.build());
7375
}
7476

75-
@Test // gh-24112
77+
@Test // gh-24112
7678
void parseEncodedFilenameWithPaddedCharset() {
7779
assertThat(parse("attachment; filename*= UTF-8''some-file.zip"))
7880
.isEqualTo(ContentDisposition.attachment()
7981
.filename("some-file.zip", StandardCharsets.UTF_8)
8082
.build());
8183
}
8284

83-
@Test // gh-26463
85+
@Test // gh-26463
8486
void parseBase64EncodedFilename() {
8587
String input = "attachment; filename=\"=?UTF-8?B?5pel5pys6KqeLmNzdg==?=\"";
8688
assertThat(parse(input).getFilename()).isEqualTo("日本語.csv");
8789
}
8890

89-
@Test // gh-26463
91+
@Test // gh-26463
9092
void parseBase64EncodedShiftJISFilename() {
9193
String input = "attachment; filename=\"=?SHIFT_JIS?B?k/qWe4zqLmNzdg==?=\"";
9294
assertThat(parse(input).getFilename()).isEqualTo("日本語.csv");
@@ -116,7 +118,7 @@ void parseEncodedFilenameWithInvalidName() {
116118
.isThrownBy(() -> parse("form-data; name=\"name\"; filename*=UTF-8''%A.txt"));
117119
}
118120

119-
@Test // gh-23077
121+
@Test // gh-23077
120122
@SuppressWarnings("deprecation")
121123
void parseWithEscapedQuote() {
122124
BiConsumer<String, String> tester = (description, filename) ->
@@ -137,8 +139,8 @@ void parseWithEscapedQuote() {
137139
"The Twilight Zone \\\\\\\\");
138140
}
139141

140-
@Test
141142
@SuppressWarnings("deprecation")
143+
@Test
142144
void parseWithExtraSemicolons() {
143145
assertThat(parse("form-data; name=\"foo\";; ; filename=\"foo.txt\"; size=123"))
144146
.isEqualTo(ContentDisposition.formData()
@@ -148,8 +150,8 @@ void parseWithExtraSemicolons() {
148150
.build());
149151
}
150152

151-
@Test
152153
@SuppressWarnings("deprecation")
154+
@Test
153155
void parseDates() {
154156
ZonedDateTime creationTime = ZonedDateTime.parse("Mon, 12 Feb 2007 10:15:30 -0500", formatter);
155157
ZonedDateTime modificationTime = ZonedDateTime.parse("Tue, 13 Feb 2007 10:15:30 -0500", formatter);
@@ -167,8 +169,8 @@ void parseDates() {
167169
.build());
168170
}
169171

170-
@Test
171172
@SuppressWarnings("deprecation")
173+
@Test
172174
void parseIgnoresInvalidDates() {
173175
ZonedDateTime readTime = ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter);
174176

@@ -197,13 +199,8 @@ void parseInvalidParameter() {
197199
assertThatIllegalArgumentException().isThrownBy(() -> parse("foo;bar"));
198200
}
199201

200-
private static ContentDisposition parse(String input) {
201-
return ContentDisposition.parse(input);
202-
}
203-
204-
205-
@Test
206202
@SuppressWarnings("deprecation")
203+
@Test
207204
void format() {
208205
assertThat(
209206
ContentDisposition.formData()
@@ -235,14 +232,11 @@ void formatWithEncodedFilenameUsingUsAscii() {
235232
.isEqualTo("form-data; name=\"name\"; filename=\"test.txt\"");
236233
}
237234

238-
@Test // gh-24220
235+
@Test // gh-24220
239236
void formatWithFilenameWithQuotes() {
240-
241237
BiConsumer<String, String> tester = (input, output) -> {
242-
243238
assertThat(ContentDisposition.formData().filename(input).build().toString())
244239
.isEqualTo("form-data; filename=\"" + output + "\"");
245-
246240
assertThat(ContentDisposition.formData().filename(input, StandardCharsets.US_ASCII).build().toString())
247241
.isEqualTo("form-data; filename=\"" + output + "\"");
248242
};

0 commit comments

Comments
 (0)