Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Mar 1, 2024
1 parent 6432b13 commit 3b7c435
Showing 1 changed file with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public StandardServletAsyncWebRequest(HttpServletRequest request, HttpServletRes
}

//noinspection DataFlowIssue
((LifecycleHttpServletResponse) getResponse()).setParent(this);
((LifecycleHttpServletResponse) getResponse()).setAsyncWebRequest(this);
}


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

@Nullable
private StandardServletAsyncWebRequest parent;
private StandardServletAsyncWebRequest asyncWebRequest;

@Nullable
private ServletOutputStream outputStream;

public LifecycleHttpServletResponse(HttpServletResponse response) {
super(response);
}

public void setParent(StandardServletAsyncWebRequest parent) {
this.parent = parent;
public void setAsyncWebRequest(StandardServletAsyncWebRequest asyncWebRequest) {
this.asyncWebRequest = asyncWebRequest;
}

@Override
public ServletOutputStream getOutputStream() {
if (this.outputStream == null) {
Assert.notNull(this.parent, "Not initialized");
this.outputStream = new LifecycleServletOutputStream((HttpServletResponse) getResponse(), this.parent);
Assert.notNull(this.asyncWebRequest, "Not initialized");
this.outputStream = new LifecycleServletOutputStream(
(HttpServletResponse) getResponse(), this.asyncWebRequest);
}
return this.outputStream;
}
Expand All @@ -236,13 +238,15 @@ public ServletOutputStream getOutputStream() {
*/
private static final class LifecycleServletOutputStream extends ServletOutputStream {

private final HttpServletResponse response;
private final HttpServletResponse delegate;

private final StandardServletAsyncWebRequest asyncWebRequest;

private final StandardServletAsyncWebRequest parent;
private LifecycleServletOutputStream(
HttpServletResponse delegate, StandardServletAsyncWebRequest asyncWebRequest) {

private LifecycleServletOutputStream(HttpServletResponse response, StandardServletAsyncWebRequest parent) {
this.response = response;
this.parent = parent;
this.delegate = delegate;
this.asyncWebRequest = asyncWebRequest;
}

@Override
Expand All @@ -258,7 +262,7 @@ public void setWriteListener(WriteListener writeListener) {
public void write(int b) throws IOException {
checkState();
try {
this.response.getOutputStream().write(b);
this.delegate.getOutputStream().write(b);
}
catch (IOException ex) {
handleIOException(ex, "ServletOutputStream failed to write");
Expand All @@ -268,7 +272,7 @@ public void write(int b) throws IOException {
public void write(byte[] buf, int offset, int len) throws IOException {
checkState();
try {
this.response.getOutputStream().write(buf, offset, len);
this.delegate.getOutputStream().write(buf, offset, len);
}
catch (IOException ex) {
handleIOException(ex, "ServletOutputStream failed to write");
Expand All @@ -279,7 +283,7 @@ public void write(byte[] buf, int offset, int len) throws IOException {
public void flush() throws IOException {
checkState();
try {
this.response.getOutputStream().flush();
this.delegate.getOutputStream().flush();
}
catch (IOException ex) {
handleIOException(ex, "ServletOutputStream failed to flush");
Expand All @@ -290,23 +294,23 @@ public void flush() throws IOException {
public void close() throws IOException {
checkState();
try {
this.response.getOutputStream().close();
this.delegate.getOutputStream().close();
}
catch (IOException ex) {
handleIOException(ex, "ServletOutputStream failed to close");
}
}

private void checkState() throws AsyncRequestNotUsableException {
if (this.parent.state.get() != State.ACTIVE) {
String reason = this.parent.state.get() == State.COMPLETED ?
"async request completion" : "Servlet container onError notification";
throw new AsyncRequestNotUsableException("Response not usable after " + reason + ".");
if (this.asyncWebRequest.state.get() != State.ACTIVE) {
throw new AsyncRequestNotUsableException("Response not usable after " +
(this.asyncWebRequest.state.get() == State.COMPLETED ?
"async request completion" : "onError notification") + ".");
}
}

private void handleIOException(IOException ex, String msg) throws AsyncRequestNotUsableException {
this.parent.transitionToErrorState();
this.asyncWebRequest.transitionToErrorState();
throw new AsyncRequestNotUsableException(msg, ex);
}

Expand Down

0 comments on commit 3b7c435

Please sign in to comment.