@Override
public void include(final ServletRequest request, final ServletResponse response) throws ServletException, IOException {
HttpServletRequestImpl requestImpl = HttpServletRequestImpl.getRequestImpl(request);
final HttpServletResponseImpl responseImpl = HttpServletResponseImpl.getResponseImpl(response);
final HttpServerExchange exchange = requestImpl.getExchange();
final ServletRequest oldRequest = exchange.getAttachment(HttpServletRequestImpl.ATTACHMENT_KEY);
final ServletResponse oldResponse = exchange.getAttachment(HttpServletResponseImpl.ATTACHMENT_KEY);
Object requestUri = null;
Object contextPath = null;
Object servletPath = null;
Object pathInfo = null;
Object queryString = null;
Map<String, Deque<String>> queryParameters = requestImpl.getQueryParameters();
if (!named) {
requestUri = request.getAttribute(INCLUDE_REQUEST_URI);
contextPath = request.getAttribute(INCLUDE_CONTEXT_PATH);
servletPath = request.getAttribute(INCLUDE_SERVLET_PATH);
pathInfo = request.getAttribute(INCLUDE_PATH_INFO);
queryString = request.getAttribute(INCLUDE_QUERY_STRING);
String newQueryString = "";
int qsPos = path.indexOf("?");
String newServletPath = path;
if (qsPos != -1) {
newQueryString = newServletPath.substring(qsPos + 1);
newServletPath = newServletPath.substring(0, qsPos);
}
String newRequestUri = servletContext.getContextPath() + newServletPath;
Map<String, Deque<String>> newQueryParameters = createNewQueryParameters(queryParameters, newQueryString);
requestImpl.setQueryParameters(newQueryParameters);
request.setAttribute(INCLUDE_REQUEST_URI, newRequestUri);
request.setAttribute(INCLUDE_CONTEXT_PATH, servletContext.getContextPath());
request.setAttribute(INCLUDE_SERVLET_PATH, pathMatch.getMatched());
request.setAttribute(INCLUDE_PATH_INFO, pathMatch.getRemaining());
request.setAttribute(INCLUDE_QUERY_STRING, newQueryString);
}
boolean inInclude = responseImpl.isInsideInclude();
responseImpl.setInsideInclude(true);
ServletContextImpl oldContext = requestImpl.getServletContext();
try {
requestImpl.setServletContext(servletContext);
responseImpl.setServletContext(servletContext);
try {
exchange.putAttachment(HttpServletRequestImpl.ATTACHMENT_KEY, request);
exchange.putAttachment(HttpServletResponseImpl.ATTACHMENT_KEY, response);
servletContext.getDeployment().getServletDispatcher().dispatchToServlet(exchange, chain, DispatcherType.INCLUDE);
} catch (ServletException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
} finally {
responseImpl.setInsideInclude(inInclude);
requestImpl.setServletContext(oldContext);
responseImpl.setServletContext(oldContext);
exchange.putAttachment(HttpServletRequestImpl.ATTACHMENT_KEY, oldRequest);
exchange.putAttachment(HttpServletResponseImpl.ATTACHMENT_KEY, oldResponse);
if (!named) {
request.setAttribute(INCLUDE_REQUEST_URI, requestUri);
request.setAttribute(INCLUDE_CONTEXT_PATH, contextPath);
request.setAttribute(INCLUDE_SERVLET_PATH, servletPath);
request.setAttribute(INCLUDE_PATH_INFO, pathInfo);