📄 applicationdispatcher.java
字号:
}
private void doForward(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
// Reset any output that has been buffered, but keep headers/cookies
if (response.isCommitted()) {
if ( log.isDebugEnabled() )
log.debug(" Forward on committed response --> ISE");
throw new IllegalStateException
(sm.getString("applicationDispatcher.forward.ise"));
}
try {
response.resetBuffer();
} catch (IllegalStateException e) {
if ( log.isDebugEnabled() )
log.debug(" Forward resetBuffer() returned ISE: " + e);
throw e;
}
// Set up to handle the specified request and response
setup(request, response, false);
// Identify the HTTP-specific request and response objects (if any)
HttpServletRequest hrequest = null;
if (request instanceof HttpServletRequest)
hrequest = (HttpServletRequest) request;
HttpServletResponse hresponse = null;
if (response instanceof HttpServletResponse)
hresponse = (HttpServletResponse) response;
// Handle a non-HTTP forward by passing the existing request/response
if ((hrequest == null) || (hresponse == null)) {
if ( log.isDebugEnabled() )
log.debug(" Non-HTTP Forward");
processRequest(hrequest,hresponse);
}
// Handle an HTTP named dispatcher forward
else if ((servletPath == null) && (pathInfo == null)) {
if ( log.isDebugEnabled() )
log.debug(" Named Dispatcher Forward");
processRequest(request,response);
}
// Handle an HTTP path-based forward
else {
if ( log.isDebugEnabled() )
log.debug(" Path Based Forward");
ApplicationHttpRequest wrequest =
(ApplicationHttpRequest) wrapRequest();
String contextPath = context.getPath();
wrequest.setContextPath(contextPath);
wrequest.setRequestURI(requestURI);
wrequest.setServletPath(servletPath);
wrequest.setPathInfo(pathInfo);
if (hrequest.getAttribute(Globals.FORWARD_REQUEST_URI_ATTR) == null) {
wrequest.setAttribute(Globals.FORWARD_REQUEST_URI_ATTR,
hrequest.getRequestURI());
wrequest.setAttribute(Globals.FORWARD_CONTEXT_PATH_ATTR,
hrequest.getContextPath());
wrequest.setAttribute(Globals.FORWARD_SERVLET_PATH_ATTR,
hrequest.getServletPath());
wrequest.setAttribute(Globals.FORWARD_PATH_INFO_ATTR,
hrequest.getPathInfo());
wrequest.setAttribute(Globals.FORWARD_QUERY_STRING_ATTR,
hrequest.getQueryString());
}
if (queryString != null) {
wrequest.setQueryString(queryString);
wrequest.setQueryParams(queryString);
}
processRequest(request,response);
wrequest.recycle();
unwrapRequest();
}
// This is not a real close in order to support error processing
if ( log.isDebugEnabled() )
log.debug(" Disabling the response for futher output");
if (response instanceof ResponseFacade) {
((ResponseFacade) response).finish();
} else {
// Servlet SRV.6.2.2. The Resquest/Response may have been wrapped
// and may no longer be instance of RequestFacade
if (log.isDebugEnabled()){
log.debug( " The Response is vehiculed using a wrapper: "
+ response.getClass().getName() );
}
// Close anyway
try {
PrintWriter writer = response.getWriter();
writer.close();
} catch (IllegalStateException e) {
try {
ServletOutputStream stream = response.getOutputStream();
stream.close();
} catch (IllegalStateException f) {
;
} catch (IOException f) {
;
}
} catch (IOException e) {
;
}
}
}
/**
* Prepare the request based on the filter configuration.
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
private void processRequest(ServletRequest request,
ServletResponse response)
throws IOException, ServletException {
Integer disInt = (Integer) request.getAttribute
(ApplicationFilterFactory.DISPATCHER_TYPE_ATTR);
if (disInt != null) {
if (disInt.intValue() != ApplicationFilterFactory.ERROR) {
outerRequest.setAttribute
(ApplicationFilterFactory.DISPATCHER_REQUEST_PATH_ATTR,
origServletPath);
outerRequest.setAttribute
(ApplicationFilterFactory.DISPATCHER_TYPE_ATTR,
new Integer(ApplicationFilterFactory.FORWARD));
invoke(outerRequest, response);
} else {
invoke(outerRequest, response);
}
}
}
/**
* Include the response from another resource in the current response.
* Any runtime exception, IOException, or ServletException thrown by the
* called servlet will be propogated to the caller.
*
* @param request The servlet request that is including this one
* @param response The servlet response to be appended to
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public void include(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
if (System.getSecurityManager() != null) {
try {
PrivilegedInclude dp = new PrivilegedInclude(request,response);
AccessController.doPrivileged(dp);
} catch (PrivilegedActionException pe) {
Exception e = pe.getException();
if (e instanceof ServletException)
throw (ServletException) e;
throw (IOException) e;
}
} else {
doInclude(request,response);
}
}
private void doInclude(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
// Set up to handle the specified request and response
setup(request, response, true);
// Create a wrapped response to use for this request
// ServletResponse wresponse = null;
ServletResponse wresponse = wrapResponse();
// Handle a non-HTTP include
if (!(request instanceof HttpServletRequest) ||
!(response instanceof HttpServletResponse)) {
if ( log.isDebugEnabled() )
log.debug(" Non-HTTP Include");
request.setAttribute(ApplicationFilterFactory.DISPATCHER_TYPE_ATTR,
new Integer(ApplicationFilterFactory.INCLUDE));
request.setAttribute(ApplicationFilterFactory.DISPATCHER_REQUEST_PATH_ATTR, origServletPath);
invoke(request, outerResponse);
unwrapResponse();
}
// Handle an HTTP named dispatcher include
else if (name != null) {
if ( log.isDebugEnabled() )
log.debug(" Named Dispatcher Include");
ApplicationHttpRequest wrequest =
(ApplicationHttpRequest) wrapRequest();
wrequest.setAttribute(Globals.NAMED_DISPATCHER_ATTR, name);
if (servletPath != null)
wrequest.setServletPath(servletPath);
wrequest.setAttribute(ApplicationFilterFactory.DISPATCHER_TYPE_ATTR,
new Integer(ApplicationFilterFactory.INCLUDE));
wrequest.setAttribute(ApplicationFilterFactory.DISPATCHER_REQUEST_PATH_ATTR, origServletPath);
invoke(outerRequest, outerResponse);
wrequest.recycle();
unwrapRequest();
unwrapResponse();
}
// Handle an HTTP path based include
else {
if ( log.isDebugEnabled() )
log.debug(" Path Based Include");
ApplicationHttpRequest wrequest =
(ApplicationHttpRequest) wrapRequest();
String contextPath = context.getPath();
/*
StringBuffer sb = new StringBuffer();
if (contextPath != null)
sb.append(contextPath);
if (servletPath != null)
sb.append(servletPath);
if (pathInfo != null)
sb.append(pathInfo);
if (sb.length() > 0)
*/
if (requestURI != null)
wrequest.setAttribute(Globals.INCLUDE_REQUEST_URI_ATTR,
requestURI);
if (contextPath != null)
wrequest.setAttribute(Globals.INCLUDE_CONTEXT_PATH_ATTR,
contextPath);
if (servletPath != null)
wrequest.setAttribute(Globals.INCLUDE_SERVLET_PATH_ATTR,
servletPath);
if (pathInfo != null)
wrequest.setAttribute(Globals.INCLUDE_PATH_INFO_ATTR,
pathInfo);
if (queryString != null) {
wrequest.setAttribute(Globals.INCLUDE_QUERY_STRING_ATTR,
queryString);
wrequest.setQueryParams(queryString);
}
wrequest.setAttribute(ApplicationFilterFactory.DISPATCHER_TYPE_ATTR,
new Integer(ApplicationFilterFactory.INCLUDE));
wrequest.setAttribute(ApplicationFilterFactory.DISPATCHER_REQUEST_PATH_ATTR, origServletPath);
invoke(outerRequest, outerResponse);
wrequest.recycle();
unwrapRequest();
unwrapResponse();
}
}
// -------------------------------------------------------- Private Methods
/**
* Ask the resource represented by this RequestDispatcher to process
* the associated request, and create (or append to) the associated
* response.
* <p>
* <strong>IMPLEMENTATION NOTE</strong>: This implementation assumes
* that no filters are applied to a forwarded or included resource,
* because they were already done for the original request.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
private void invoke(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
// Checking to see if the context classloader is the current context
// classloader. If it's not, we're saving it, and setting the context
// classloader to the Context classloader
ClassLoader oldCCL = Thread.currentThread().getContextClassLoader();
ClassLoader contextClassLoader = context.getLoader().getClassLoader();
if (oldCCL != contextClassLoader) {
Thread.currentThread().setContextClassLoader(contextClassLoader);
} else {
oldCCL = null;
}
// Initialize local variables we may need
HttpServletRequest hrequest = null;
if (request instanceof HttpServletRequest)
hrequest = (HttpServletRequest) request;
HttpServletResponse hresponse = null;
if (response instanceof HttpServletResponse)
hresponse = (HttpServletResponse) response;
Servlet servlet = null;
IOException ioException = null;
ServletException servletException = null;
RuntimeException runtimeException = null;
boolean unavailable = false;
// Check for the servlet being marked unavailable
if (wrapper.isUnavailable()) {
log(sm.getString("applicationDispatcher.isUnavailable",
wrapper.getName()));
if (hresponse == null) {
; // NOTE - Not much we can do generically
} else {
long available = wrapper.getAvailable();
if ((available > 0L) && (available < Long.MAX_VALUE))
hresponse.setDateHeader("Retry-After", available);
hresponse.sendError
(HttpServletResponse.SC_SERVICE_UNAVAILABLE,
sm.getString("applicationDispatcher.isUnavailable",
wrapper.getName()));
}
unavailable = true;
}
// Allocate a servlet instance to process this request
try {
if (!unavailable) {
// if (debug >= 2)
// log(" Allocating servlet instance");
servlet = wrapper.allocate();
// if ((debug >= 2) && (servlet == null))
// log(" No servlet instance returned!");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -