}
@Override
public void include(final ServletRequest request, final ServletResponse response) throws ServletException, IOException {
final ServletRequestContext servletRequestContext = SecurityActions.requireCurrentServletRequestContext();
final HttpServletRequestImpl requestImpl = servletRequestContext.getOriginalRequest();
final HttpServletResponseImpl responseImpl = servletRequestContext.getOriginalResponse();
ThreadSetupAction.Handle handle = null;
ServletContextImpl oldServletContext = null;
HttpSessionImpl oldSession = null;
if (servletRequestContext.getCurrentServletContext() != this.servletContext) {
//cross context request, we need to run the thread setup actions
oldServletContext = servletRequestContext.getCurrentServletContext();
oldSession = servletRequestContext.getSession();
servletRequestContext.setSession(null);
handle = this.servletContext.getDeployment().getThreadSetupAction().setup(servletRequestContext.getExchange());
servletRequestContext.setCurrentServletContext(this.servletContext);
}
try {
if (!servletContext.getDeployment().getDeploymentInfo().isAllowNonStandardWrappers()) {
if (servletRequestContext.getOriginalRequest() != request) {
if (!(request instanceof ServletRequestWrapper)) {
throw UndertowServletMessages.MESSAGES.requestWasNotOriginalOrWrapper(request);
}
}
if (servletRequestContext.getOriginalResponse() != response) {
if (!(response instanceof ServletResponseWrapper)) {
throw UndertowServletMessages.MESSAGES.responseWasNotOriginalOrWrapper(response);
}
}
}
final ServletRequest oldRequest = servletRequestContext.getServletRequest();
final ServletResponse oldResponse = servletRequestContext.getServletResponse();
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);
int qsPos = path.indexOf("?");
String newServletPath = path;
if (qsPos != -1) {
String newQueryString = newServletPath.substring(qsPos + 1);
newServletPath = newServletPath.substring(0, qsPos);
Map<String, Deque<String>> newQueryParameters = QueryParameterUtils.mergeQueryParametersWithNewQueryString(queryParameters, newQueryString);
requestImpl.setQueryParameters(newQueryParameters);
requestImpl.setAttribute(INCLUDE_QUERY_STRING, newQueryString);
} else {
requestImpl.setAttribute(INCLUDE_QUERY_STRING, "");
}
String newRequestUri = servletContext.getContextPath() + newServletPath;
requestImpl.setAttribute(INCLUDE_REQUEST_URI, newRequestUri);
requestImpl.setAttribute(INCLUDE_CONTEXT_PATH, servletContext.getContextPath());
requestImpl.setAttribute(INCLUDE_SERVLET_PATH, pathMatch.getMatched());
requestImpl.setAttribute(INCLUDE_PATH_INFO, pathMatch.getRemaining());
}
boolean inInclude = responseImpl.isInsideInclude();
responseImpl.setInsideInclude(true);
DispatcherType oldDispatcherType = servletRequestContext.getDispatcherType();
ServletContextImpl oldContext = requestImpl.getServletContext();
try {
requestImpl.setServletContext(servletContext);
responseImpl.setServletContext(servletContext);
try {
servletRequestContext.setServletRequest(request);
servletRequestContext.setServletResponse(response);
servletContext.getDeployment().getServletDispatcher().dispatchToServlet(requestImpl.getExchange(), 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);
servletRequestContext.setServletRequest(oldRequest);
servletRequestContext.setServletResponse(oldResponse);
servletRequestContext.setDispatcherType(oldDispatcherType);
if (!named) {
requestImpl.setAttribute(INCLUDE_REQUEST_URI, requestUri);
requestImpl.setAttribute(INCLUDE_CONTEXT_PATH, contextPath);
requestImpl.setAttribute(INCLUDE_SERVLET_PATH, servletPath);
requestImpl.setAttribute(INCLUDE_PATH_INFO, pathInfo);
requestImpl.setAttribute(INCLUDE_QUERY_STRING, queryString);
requestImpl.setQueryParameters(queryParameters);
}
}
} finally {
if(handle != null) {
servletRequestContext.setSession(oldSession);
servletRequestContext.setCurrentServletContext(oldServletContext);
handle.tearDown();
}
}
}