Made handling of client error more robust, avoiding

IllegalStateExceptions when trying to complete the response.
This commit is contained in:
Simone Bordet 2014-05-05 16:18:10 +02:00
parent 943fb64176
commit a9134a9b94
2 changed files with 17 additions and 26 deletions

View File

@ -46,7 +46,7 @@ public class AsyncProxyServlet extends ProxyServlet
{
ServletInputStream input = request.getInputStream();
DeferredContentProvider provider = new DeferredContentProvider();
input.setReadListener(new StreamReader(request, provider));
input.setReadListener(new StreamReader(proxyRequest, request, provider));
return provider;
}
@ -104,11 +104,13 @@ public class AsyncProxyServlet extends ProxyServlet
private class StreamReader implements ReadListener, Callback
{
private final byte[] buffer = new byte[getHttpClient().getRequestBufferSize()];
private final Request proxyRequest;
private final HttpServletRequest request;
private final DeferredContentProvider provider;
public StreamReader(HttpServletRequest request, DeferredContentProvider provider)
public StreamReader(Request proxyRequest, HttpServletRequest request, DeferredContentProvider provider)
{
this.proxyRequest = proxyRequest;
this.request = request;
this.provider = provider;
}
@ -168,7 +170,7 @@ public class AsyncProxyServlet extends ProxyServlet
@Override
public void failed(Throwable x)
{
onClientRequestFailure(request, x);
onClientRequestFailure(proxyRequest, request, x);
}
}

View File

@ -480,7 +480,7 @@ public class ProxyServlet extends HttpServlet
proxyRequest.send(new ProxyResponseListener(request, response));
}
protected ContentProvider proxyRequestContent(Request proxyRequest, final HttpServletRequest request) throws IOException
protected ContentProvider proxyRequestContent(final Request proxyRequest, final HttpServletRequest request) throws IOException
{
return new InputStreamContentProvider(request.getInputStream())
{
@ -500,18 +500,15 @@ public class ProxyServlet extends HttpServlet
@Override
protected void onReadFailure(Throwable failure)
{
onClientRequestFailure(request, failure);
onClientRequestFailure(proxyRequest, request, failure);
}
};
}
protected void onClientRequestFailure(HttpServletRequest request, Throwable failure)
protected void onClientRequestFailure(Request proxyRequest, HttpServletRequest request, Throwable failure)
{
AsyncContext asyncContext = request.getAsyncContext();
HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();
response.setStatus(500);
response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
asyncContext.complete();
_log.debug(getRequestId(request) + " client request failure", failure);
proxyRequest.abort(failure);
}
protected void onRewriteFailed(HttpServletRequest request, HttpServletResponse response) throws IOException
@ -569,9 +566,9 @@ public class ProxyServlet extends HttpServlet
protected void onResponseSuccess(HttpServletRequest request, HttpServletResponse response, Response proxyResponse)
{
_log.debug("{} proxying successful", getRequestId(request));
AsyncContext asyncContext = request.getAsyncContext();
asyncContext.complete();
_log.debug("{} proxying successful", getRequestId(request));
}
protected void onResponseFailure(HttpServletRequest request, HttpServletResponse response, Response proxyResponse, Throwable failure)
@ -584,9 +581,9 @@ public class ProxyServlet extends HttpServlet
else
response.setStatus(HttpServletResponse.SC_BAD_GATEWAY);
response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
AsyncContext asyncContext = request.getAsyncContext();
asyncContext.complete();
}
AsyncContext asyncContext = request.getAsyncContext();
asyncContext.complete();
}
protected int getRequestId(HttpServletRequest request)
@ -806,21 +803,13 @@ public class ProxyServlet extends HttpServlet
});
}
@Override
public void onSuccess(Response proxyResponse)
{
onResponseSuccess(request, response, proxyResponse);
}
@Override
public void onFailure(Response proxyResponse, Throwable failure)
{
onResponseFailure(request, response, proxyResponse, failure);
}
@Override
public void onComplete(Result result)
{
if (result.isSucceeded())
onResponseSuccess(request, response, result.getResponse());
else
onResponseFailure(request, response, result.getResponse(), result.getFailure());
_log.debug("{} proxying complete", getRequestId(request));
}
}