public void include(ServletRequest topRequest, ServletResponse topResponse)
throws IOException, ServletException
{
HttpServletRequest parentReq;
ServletRequestWrapper reqWrapper = null;
if (topRequest instanceof ServletRequestWrapper) {
ServletRequest request = topRequest;
while (request instanceof ServletRequestWrapper) {
reqWrapper = (ServletRequestWrapper) request;
request = ((ServletRequestWrapper) request).getRequest();
}
parentReq = (HttpServletRequest) request;
} else if (topRequest instanceof HttpServletRequest) {
parentReq = (HttpServletRequest) topRequest;
} else {
throw new IllegalStateException(L.l(
"expected instance of ServletRequestWrapper at `{0}'", topResponse));
}
HttpServletResponse parentRes;
ServletResponseWrapper resWrapper = null;
if (topResponse instanceof ServletResponseWrapper) {
ServletResponse response = topResponse;
while (response instanceof ServletResponseWrapper) {
resWrapper = (ServletResponseWrapper) response;
response = ((ServletResponseWrapper) response).getResponse();
}
parentRes = (HttpServletResponse) response;
} else if (topResponse instanceof HttpServletResponse) {
parentRes = (HttpServletResponse) topResponse;
} else {
throw new IllegalStateException(L.l(
"expected instance of ServletResponse at `{0}'", topResponse));
}
RequestAdapter subRequest = RequestAdapter.create();
subRequest.init(parentReq, parentRes, _webApp);
DispatchResponse subResponse = new DispatchResponse();
subResponse.init(parentRes);
if (reqWrapper != null) {
reqWrapper.setRequest(subRequest);
} else {
topRequest = subRequest;
}
if (resWrapper != null) {
resWrapper.setResponse(subResponse);
} else {
topResponse = subResponse;
}
try {
_includeFilterChain.doFilter(topRequest, topResponse);
} finally {
subResponse.finish();
RequestAdapter.free(subRequest);
if (reqWrapper != null)
reqWrapper.setRequest(parentReq);
if (resWrapper != null)
resWrapper.setResponse(parentRes);
}