Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
olegz committed Jul 6, 2023
1 parent f53b49e commit 28bedce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,17 @@ public void service(HttpServletRequest request, HttpServletResponse response, Co
AsyncContext asyncContext = request.getAsyncContext();
if (asyncContext != null) {
filterChain = new ProxyFilterChain(this.dispatcher);
((ProxyAsyncContext) asyncContext).addDispatchHandler(() -> {
try {
new ProxyFilterChain(this.dispatcher).doFilter(request, response);
asyncContext.complete();
}
catch (Exception e) {
throw new IllegalStateException(e);
}
});
if (asyncContext instanceof ProxyAsyncContext proxyAsyncContext) {
proxyAsyncContext.addDispatchHandler(() -> {
try {
new ProxyFilterChain(this.dispatcher).doFilter(request, response);
asyncContext.complete();
}
catch (Exception e) {
throw new IllegalStateException(e);
}
});
}
}

if (latch != null) {
Expand Down Expand Up @@ -262,20 +264,12 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
else {
this.delegateServlet.service(request, response);
if (((HttpServletResponse) response).getStatus() != HttpStatus.OK.value() && request instanceof ProxyHttpServletRequest) {
((HttpServletRequest) request).setAttribute(RequestDispatcher.ERROR_STATUS_CODE, ((HttpServletResponse) response).getStatus());
this.setErrorMessageAttribute((ProxyHttpServletRequest) request, (ProxyHttpServletResponse) response, null);
((HttpServletRequest) request).setAttribute(RequestDispatcher.ERROR_REQUEST_URI, ((HttpServletRequest) request).getRequestURI());

((ProxyHttpServletRequest) request).setRequestURI("/error");
this.delegateServlet.service(request, response);
}
}
}
catch (Exception e) {
if (request instanceof ProxyHttpServletRequest) {
((HttpServletRequest) request).setAttribute(RequestDispatcher.ERROR_STATUS_CODE, HttpStatus.INTERNAL_SERVER_ERROR);
this.setErrorMessageAttribute((ProxyHttpServletRequest) request, (ProxyHttpServletResponse) response, e);
((HttpServletRequest) request).setAttribute(RequestDispatcher.ERROR_STATUS_CODE, HttpStatus.INTERNAL_SERVER_ERROR.value());
this.setErrorMessageAttribute((HttpServletRequest) request, (HttpServletResponse) response, e);
((HttpServletRequest) request).setAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE, e);
((HttpServletRequest) request).setAttribute(RequestDispatcher.ERROR_REQUEST_URI, ((HttpServletRequest) request).getRequestURI());
((ProxyHttpServletRequest) request).setRequestURI("/error");
Expand All @@ -287,12 +281,12 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
}

private void setErrorMessageAttribute(ProxyHttpServletRequest request, ProxyHttpServletResponse response, Exception exception) {
private void setErrorMessageAttribute(HttpServletRequest request, HttpServletResponse response, Exception exception) {
if (exception != null && StringUtils.hasText(exception.getMessage())) {
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, exception.getMessage());
}
else if (StringUtils.hasText(response.getErrorMessage())) {
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, response.getErrorMessage());
else if (response instanceof ProxyHttpServletResponse proxyResponse && StringUtils.hasText(proxyResponse.getErrorMessage())) {
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, proxyResponse.getErrorMessage());
}
else {
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, HttpStatus.valueOf(response.getStatus()).getReasonPhrase());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public URL getResource(String path) throws MalformedURLException {

@Override
public InputStream getResourceAsStream(String path) {
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
return null;
}

@Override
Expand Down

0 comments on commit 28bedce

Please sign in to comment.