{
if (!isStarted())
return;
// Get the base requests
final Request base_request=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest();
final String old_servlet_name=base_request.getServletName();
final String old_servlet_path=base_request.getServletPath();
final String old_path_info=base_request.getPathInfo();
final Map old_role_map=base_request.getRoleMap();
try
{
ServletHolder servlet_holder=null;
FilterChain chain=null;
// find the servlet
if (target.startsWith("/"))
{
// Look for the servlet by path
PathMap.Entry entry=getHolderEntry(target);
if (entry!=null)
{
servlet_holder=(ServletHolder)entry.getValue();
base_request.setServletName(servlet_holder.getName());
base_request.setRoleMap(servlet_holder.getRoleMap());
if(Log.isDebugEnabled())Log.debug("servlet="+servlet_holder);
String servlet_path_spec=(String)entry.getKey();
String servlet_path=entry.getMapped()!=null?entry.getMapped():PathMap.pathMatch(servlet_path_spec,target);
String path_info=PathMap.pathInfo(servlet_path_spec,target);
if (type==INCLUDE)
{
base_request.setAttribute(Dispatcher.__INCLUDE_SERVLET_PATH,servlet_path);
base_request.setAttribute(Dispatcher.__INCLUDE_PATH_INFO, path_info);
}
else
{
base_request.setServletPath(servlet_path);
base_request.setPathInfo(path_info);
}
if (servlet_holder!=null && _filterMappings!=null && _filterMappings.length>0)
chain=getFilterChain(type, target, servlet_holder);
}
}
else
{
// look for a servlet by name!
servlet_holder=(ServletHolder)_servletNameMap.get(target);
if (servlet_holder!=null && _filterMappings!=null && _filterMappings.length>0)
{
base_request.setServletName(servlet_holder.getName());
chain=getFilterChain(type, null,servlet_holder);
}
}
if (Log.isDebugEnabled())
{
Log.debug("chain="+chain);
Log.debug("servlet holder="+servlet_holder);
}
// Do the filter/handling thang
if (servlet_holder!=null)
{
base_request.setHandled(true);
if (chain!=null)
chain.doFilter(request, response);
else
servlet_holder.handle(request,response);
}
else
notFound(request, response);
}
catch(RetryRequest e)
{
base_request.setHandled(false);
throw e;
}
catch(EofException e)
{
throw e;
}
catch(Exception e)
{
if (type!=REQUEST)
{
if (e instanceof IOException)
throw (IOException)e;
if (e instanceof RuntimeException)
throw (RuntimeException)e;
if (e instanceof ServletException)
throw (ServletException)e;
}
Throwable th=e;
if (th instanceof ServletException)
{
Log.debug(th);
Throwable cause=((ServletException)th).getRootCause();
if (cause!=th && cause!=null)
th=cause;
}
if (th instanceof HttpException)
throw (HttpException)th;
else if (th instanceof UnavailableException)
{
Log.warn(request.getRequestURI()+": "+th);
Log.debug(th);
}
else if (Log.isDebugEnabled() || !( th instanceof java.io.IOException))
{
Log.warn(request.getRequestURI(), th);
if(Log.isDebugEnabled())
Log.debug(request.toString());
}
else
{
Log.warn(request.getRequestURI()+": "+th);
}
// TODO httpResponse.getHttpConnection().forceClose();
if (!response.isCommitted())
{
request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION_TYPE,th.getClass());
request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION,th);
if (th instanceof UnavailableException)
{
UnavailableException ue = (UnavailableException)th;
if (ue.isPermanent())
response.sendError(HttpServletResponse.SC_NOT_FOUND,th.getMessage());
else
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,th.getMessage());
}
else
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,th.getMessage());
}
else
if(Log.isDebugEnabled())Log.debug("Response already committed for handling "+th);
}
catch(Error e)
{
if (type!=REQUEST)
throw e;
Log.warn("Error for "+request.getRequestURI(),e);
if(Log.isDebugEnabled())Log.debug(request.toString());
// TODO httpResponse.getHttpConnection().forceClose();
if (!response.isCommitted())
{
request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION_TYPE,e.getClass());
request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION,e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.getMessage());
}
else
if(Log.isDebugEnabled())Log.debug("Response already committed for handling ",e);
}
finally
{
base_request.setServletName(old_servlet_name);
base_request.setRoleMap(old_role_map);
if (type!=INCLUDE)
{
base_request.setServletPath(old_servlet_path);
base_request.setPathInfo(old_path_info);
}
}
return;
}