Skip to content

Commit 3b7c435

Browse files
committed
Polishing
See gh-32342
1 parent 6432b13 commit 3b7c435

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

spring-web/src/main/java/org/springframework/web/context/request/async/StandardServletAsyncWebRequest.java

+24-20
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public StandardServletAsyncWebRequest(HttpServletRequest request, HttpServletRes
9696
}
9797

9898
//noinspection DataFlowIssue
99-
((LifecycleHttpServletResponse) getResponse()).setParent(this);
99+
((LifecycleHttpServletResponse) getResponse()).setAsyncWebRequest(this);
100100
}
101101

102102

@@ -207,23 +207,25 @@ public void onComplete(AsyncEvent event) throws IOException {
207207
private static final class LifecycleHttpServletResponse extends HttpServletResponseWrapper {
208208

209209
@Nullable
210-
private StandardServletAsyncWebRequest parent;
210+
private StandardServletAsyncWebRequest asyncWebRequest;
211211

212+
@Nullable
212213
private ServletOutputStream outputStream;
213214

214215
public LifecycleHttpServletResponse(HttpServletResponse response) {
215216
super(response);
216217
}
217218

218-
public void setParent(StandardServletAsyncWebRequest parent) {
219-
this.parent = parent;
219+
public void setAsyncWebRequest(StandardServletAsyncWebRequest asyncWebRequest) {
220+
this.asyncWebRequest = asyncWebRequest;
220221
}
221222

222223
@Override
223224
public ServletOutputStream getOutputStream() {
224225
if (this.outputStream == null) {
225-
Assert.notNull(this.parent, "Not initialized");
226-
this.outputStream = new LifecycleServletOutputStream((HttpServletResponse) getResponse(), this.parent);
226+
Assert.notNull(this.asyncWebRequest, "Not initialized");
227+
this.outputStream = new LifecycleServletOutputStream(
228+
(HttpServletResponse) getResponse(), this.asyncWebRequest);
227229
}
228230
return this.outputStream;
229231
}
@@ -236,13 +238,15 @@ public ServletOutputStream getOutputStream() {
236238
*/
237239
private static final class LifecycleServletOutputStream extends ServletOutputStream {
238240

239-
private final HttpServletResponse response;
241+
private final HttpServletResponse delegate;
242+
243+
private final StandardServletAsyncWebRequest asyncWebRequest;
240244

241-
private final StandardServletAsyncWebRequest parent;
245+
private LifecycleServletOutputStream(
246+
HttpServletResponse delegate, StandardServletAsyncWebRequest asyncWebRequest) {
242247

243-
private LifecycleServletOutputStream(HttpServletResponse response, StandardServletAsyncWebRequest parent) {
244-
this.response = response;
245-
this.parent = parent;
248+
this.delegate = delegate;
249+
this.asyncWebRequest = asyncWebRequest;
246250
}
247251

248252
@Override
@@ -258,7 +262,7 @@ public void setWriteListener(WriteListener writeListener) {
258262
public void write(int b) throws IOException {
259263
checkState();
260264
try {
261-
this.response.getOutputStream().write(b);
265+
this.delegate.getOutputStream().write(b);
262266
}
263267
catch (IOException ex) {
264268
handleIOException(ex, "ServletOutputStream failed to write");
@@ -268,7 +272,7 @@ public void write(int b) throws IOException {
268272
public void write(byte[] buf, int offset, int len) throws IOException {
269273
checkState();
270274
try {
271-
this.response.getOutputStream().write(buf, offset, len);
275+
this.delegate.getOutputStream().write(buf, offset, len);
272276
}
273277
catch (IOException ex) {
274278
handleIOException(ex, "ServletOutputStream failed to write");
@@ -279,7 +283,7 @@ public void write(byte[] buf, int offset, int len) throws IOException {
279283
public void flush() throws IOException {
280284
checkState();
281285
try {
282-
this.response.getOutputStream().flush();
286+
this.delegate.getOutputStream().flush();
283287
}
284288
catch (IOException ex) {
285289
handleIOException(ex, "ServletOutputStream failed to flush");
@@ -290,23 +294,23 @@ public void flush() throws IOException {
290294
public void close() throws IOException {
291295
checkState();
292296
try {
293-
this.response.getOutputStream().close();
297+
this.delegate.getOutputStream().close();
294298
}
295299
catch (IOException ex) {
296300
handleIOException(ex, "ServletOutputStream failed to close");
297301
}
298302
}
299303

300304
private void checkState() throws AsyncRequestNotUsableException {
301-
if (this.parent.state.get() != State.ACTIVE) {
302-
String reason = this.parent.state.get() == State.COMPLETED ?
303-
"async request completion" : "Servlet container onError notification";
304-
throw new AsyncRequestNotUsableException("Response not usable after " + reason + ".");
305+
if (this.asyncWebRequest.state.get() != State.ACTIVE) {
306+
throw new AsyncRequestNotUsableException("Response not usable after " +
307+
(this.asyncWebRequest.state.get() == State.COMPLETED ?
308+
"async request completion" : "onError notification") + ".");
305309
}
306310
}
307311

308312
private void handleIOException(IOException ex, String msg) throws AsyncRequestNotUsableException {
309-
this.parent.transitionToErrorState();
313+
this.asyncWebRequest.transitionToErrorState();
310314
throw new AsyncRequestNotUsableException(msg, ex);
311315
}
312316

0 commit comments

Comments
 (0)