@Test public void include_appendsNewHeader_cannotAppendNewHeaders() throws InvalidRequestEventException, IOException { final String firstPart = "first"; final String secondPart = "second"; final String headerKey = "X-Custom-Header"; AwsProxyRequest proxyRequest = new AwsProxyRequestBuilder("/hello", "GET").build(); AwsProxyResponse resp = mockLambdaHandler((AwsProxyHttpServletRequest req, AwsHttpServletResponse res)-> { if (req.getAttribute("cnt") == null) { res.getOutputStream().write(firstPart.getBytes()); req.setAttribute("cnt", 1); req.getRequestDispatcher("/includer").include(req, res); res.setStatus(200); res.flushBuffer(); } else { res.getOutputStream().write(secondPart.getBytes()); res.addHeader(headerKey, "value"); } }).proxy(proxyRequest, new MockLambdaContext()); assertEquals(firstPart + secondPart, resp.getBody()); assertFalse(resp.getMultiValueHeaders().containsKey(headerKey)); }
@Override @SuppressWarnings("unchecked") @SuppressFBWarnings("SERVLET_QUERY_STRING") public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { if (lambdaContainerHandler == null) { throw new IllegalStateException("Null container handler in dispatcher"); } if (servletResponse.isCommitted()) { throw new IllegalStateException("Cannot forward request with committed response"); } servletRequest.setAttribute(DISPATCHER_TYPE_ATTRIBUTE, DispatcherType.INCLUDE); if (!isNamedDispatcher) { servletRequest.setAttribute("javax.servlet.include.request_uri", ((HttpServletRequest)servletRequest).getRequestURI()); servletRequest.setAttribute("javax.servlet.include.context_path", ((HttpServletRequest) servletRequest).getContextPath()); servletRequest.setAttribute("javax.servlet.include.servlet_path", ((HttpServletRequest) servletRequest).getServletPath()); servletRequest.setAttribute("javax.servlet.include.path_info", ((HttpServletRequest) servletRequest).getPathInfo()); servletRequest.setAttribute("javax.servlet.include.query_string", SecurityUtils.encode(SecurityUtils.crlf(((HttpServletRequest) servletRequest).getQueryString()))); setRequestPath(servletRequest, dispatchTo); } lambdaContainerHandler.doFilter((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse, getServlet((HttpServletRequest)servletRequest)); }
AwsProxyRequestDispatcher implements RequestDispatcher { @Override @SuppressWarnings("unchecked") @SuppressFBWarnings("SERVLET_QUERY_STRING") public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { if (lambdaContainerHandler == null) { throw new IllegalStateException("Null container handler in dispatcher"); } if (servletResponse.isCommitted()) { throw new IllegalStateException("Cannot forward request with committed response"); } servletRequest.setAttribute(DISPATCHER_TYPE_ATTRIBUTE, DispatcherType.INCLUDE); if (!isNamedDispatcher) { servletRequest.setAttribute("javax.servlet.include.request_uri", ((HttpServletRequest)servletRequest).getRequestURI()); servletRequest.setAttribute("javax.servlet.include.context_path", ((HttpServletRequest) servletRequest).getContextPath()); servletRequest.setAttribute("javax.servlet.include.servlet_path", ((HttpServletRequest) servletRequest).getServletPath()); servletRequest.setAttribute("javax.servlet.include.path_info", ((HttpServletRequest) servletRequest).getPathInfo()); servletRequest.setAttribute("javax.servlet.include.query_string", SecurityUtils.encode(SecurityUtils.crlf(((HttpServletRequest) servletRequest).getQueryString()))); setRequestPath(servletRequest, dispatchTo); } lambdaContainerHandler.doFilter((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse, getServlet((HttpServletRequest)servletRequest)); } }
AwsProxyRequestDispatcher implements RequestDispatcher { @Override @SuppressWarnings("unchecked") @SuppressFBWarnings("SERVLET_QUERY_STRING") public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { if (lambdaContainerHandler == null) { throw new IllegalStateException("Null container handler in dispatcher"); } if (servletResponse.isCommitted()) { throw new IllegalStateException("Cannot forward request with committed response"); } servletRequest.setAttribute(DISPATCHER_TYPE_ATTRIBUTE, DispatcherType.INCLUDE); if (!isNamedDispatcher) { servletRequest.setAttribute("javax.servlet.include.request_uri", ((HttpServletRequest)servletRequest).getRequestURI()); servletRequest.setAttribute("javax.servlet.include.context_path", ((HttpServletRequest) servletRequest).getContextPath()); servletRequest.setAttribute("javax.servlet.include.servlet_path", ((HttpServletRequest) servletRequest).getServletPath()); servletRequest.setAttribute("javax.servlet.include.path_info", ((HttpServletRequest) servletRequest).getPathInfo()); servletRequest.setAttribute("javax.servlet.include.query_string", SecurityUtils.encode(SecurityUtils.crlf(((HttpServletRequest) servletRequest).getQueryString()))); setRequestPath(servletRequest, dispatchTo); } lambdaContainerHandler.doFilter((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse, getServlet((HttpServletRequest)servletRequest)); } AwsProxyRequestDispatcher(final String target, final boolean namedDispatcher, final AwsLambdaServletContainerHandler handler); }
AwsProxyRequestDispatcher implements RequestDispatcher { @Override @SuppressWarnings("unchecked") @SuppressFBWarnings("SERVLET_QUERY_STRING") public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { if (lambdaContainerHandler == null) { throw new IllegalStateException("Null container handler in dispatcher"); } if (servletResponse.isCommitted()) { throw new IllegalStateException("Cannot forward request with committed response"); } servletRequest.setAttribute(DISPATCHER_TYPE_ATTRIBUTE, DispatcherType.INCLUDE); if (!isNamedDispatcher) { servletRequest.setAttribute("javax.servlet.include.request_uri", ((HttpServletRequest)servletRequest).getRequestURI()); servletRequest.setAttribute("javax.servlet.include.context_path", ((HttpServletRequest) servletRequest).getContextPath()); servletRequest.setAttribute("javax.servlet.include.servlet_path", ((HttpServletRequest) servletRequest).getServletPath()); servletRequest.setAttribute("javax.servlet.include.path_info", ((HttpServletRequest) servletRequest).getPathInfo()); servletRequest.setAttribute("javax.servlet.include.query_string", SecurityUtils.encode(SecurityUtils.crlf(((HttpServletRequest) servletRequest).getQueryString()))); setRequestPath(servletRequest, dispatchTo); } lambdaContainerHandler.doFilter((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse, getServlet((HttpServletRequest)servletRequest)); } AwsProxyRequestDispatcher(final String target, final boolean namedDispatcher, final AwsLambdaServletContainerHandler handler); @Override @SuppressWarnings("unchecked") void forward(ServletRequest servletRequest, ServletResponse servletResponse); @Override @SuppressWarnings("unchecked") @SuppressFBWarnings("SERVLET_QUERY_STRING") void include(ServletRequest servletRequest, ServletResponse servletResponse); }
AwsProxyRequestDispatcher implements RequestDispatcher { @Override @SuppressWarnings("unchecked") @SuppressFBWarnings("SERVLET_QUERY_STRING") public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { if (lambdaContainerHandler == null) { throw new IllegalStateException("Null container handler in dispatcher"); } if (servletResponse.isCommitted()) { throw new IllegalStateException("Cannot forward request with committed response"); } servletRequest.setAttribute(DISPATCHER_TYPE_ATTRIBUTE, DispatcherType.INCLUDE); if (!isNamedDispatcher) { servletRequest.setAttribute("javax.servlet.include.request_uri", ((HttpServletRequest)servletRequest).getRequestURI()); servletRequest.setAttribute("javax.servlet.include.context_path", ((HttpServletRequest) servletRequest).getContextPath()); servletRequest.setAttribute("javax.servlet.include.servlet_path", ((HttpServletRequest) servletRequest).getServletPath()); servletRequest.setAttribute("javax.servlet.include.path_info", ((HttpServletRequest) servletRequest).getPathInfo()); servletRequest.setAttribute("javax.servlet.include.query_string", SecurityUtils.encode(SecurityUtils.crlf(((HttpServletRequest) servletRequest).getQueryString()))); setRequestPath(servletRequest, dispatchTo); } lambdaContainerHandler.doFilter((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse, getServlet((HttpServletRequest)servletRequest)); } AwsProxyRequestDispatcher(final String target, final boolean namedDispatcher, final AwsLambdaServletContainerHandler handler); @Override @SuppressWarnings("unchecked") void forward(ServletRequest servletRequest, ServletResponse servletResponse); @Override @SuppressWarnings("unchecked") @SuppressFBWarnings("SERVLET_QUERY_STRING") void include(ServletRequest servletRequest, ServletResponse servletResponse);  }