From 28bedce46ede99e38e5916750fbf8fdc93902638 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 28 Jun 2023 18:24:26 +0200 Subject: [PATCH] Cleanup --- .../function/serverless/web/ProxyMvc.java | 38 ++++++++----------- .../serverless/web/ProxyServletContext.java | 2 +- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ProxyMvc.java b/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ProxyMvc.java index 293263ee8..397daaced 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ProxyMvc.java +++ b/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ProxyMvc.java @@ -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) { @@ -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"); @@ -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()); diff --git a/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ProxyServletContext.java b/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ProxyServletContext.java index 6567df9e8..14faea7a5 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ProxyServletContext.java +++ b/spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ProxyServletContext.java @@ -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