Thread.currentThread().setContextClassLoader(ucl);
if (req.isSecure())
fillSecureAttrs(req);
final HttpServletRequest hreq = (HttpServletRequest) req;
if (this.requestListeners != null) {
ServletRequestEvent e = new ServletRequestEvent(this, hreq);
for (ServletRequestListener rlistener : requestListeners)
rlistener.requestInitialized(e);
}
try {
String path = hreq.getPathInfo();
// TODO: wrap request to implement methods like
// getRequestDispatcher()
// which supports relative path, no leading / means relative to
// currently called
if (_DEBUG)
System.err
.printf("Full req:%s, ContextPath: %s, ServletPath:%s, pathInfo:%s\n",
hreq.getRequestURI(), hreq.getContextPath(),
hreq.getServletPath(), path);
SimpleFilterChain sfc = new SimpleFilterChain();
if (path != null) {
// note a limitation, servlet name can't start with /WEB-INF
if (path.regionMatches(true, 0, "/WEB-INF", 0,
"/WEB-INF".length())
|| path.regionMatches(true, 0, "/META-INF", 0,
"/META-INF".length())) {
((HttpServletResponse) res)
.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
for (FilterAccessDescriptor fad : filters)
if (fad.matchDispatcher(DispatcherType.REQUEST)
&& fad.matchPath(path) >= 0)
sfc.add(fad);
for (ServletAccessDescr sad : servlets) {
if (_DEBUG)
System.err.println("Trying matching " + path + " to "
+ Arrays.toString(sad.mapping) + " = "
+ sad.matchPath(path));
int patIndex;
if ((patIndex = sad.matchPath(path)) >= 0) {
if (sad.instance == null) {
if (sad.loadOnStart < 0)
synchronized (sad) {
if (sad.instance == null)
sad.newInstance();
}
if (sad.instance == null) {
sad.loadOnStart = Integer.MAX_VALUE; // mark
// unsuccessful
// instantiation
// and
// ban
// the
// servlet?
((HttpServletResponse) res)
.sendError(
HttpServletResponse.SC_GONE,
"Servlet "
+ sad.name
+ " hasn't been instantiated successfully or has been unloaded.");
return;
}
} else {
if (sad.timeToReactivate > 0) {
if (sad.timeToReactivate > System
.currentTimeMillis()) {
((HttpServletResponse) res)
.setIntHeader(
"Retry-After",
(int) (sad.timeToReactivate - System
.currentTimeMillis()) / 1000 + 1);
// ((HttpServletResponse)
// res).setDateHeader("Retry-After", new
// Date(sad.timeToReactivate));
((HttpServletResponse) res)
.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
return;
} else
sad.timeToReactivate = 0;
}
}
for (FilterAccessDescriptor fad : filters)
if (fad.matchDispatcher(DispatcherType.REQUEST)
&& fad.matchServlet(sad.name) >= 0)
sfc.add(fad);
// sfc.add(fad.filterInstance);
// System.err.println("used:"+
// sad.servPath+", wanted:"+((WebAppServlet)
// sad.getServletContext()).contextPath);
sfc.setFilter(new WebAppContextFilter(
sad.mapping[patIndex].servPath));
// add servlet in chain
sfc.setServlet(sad);
sfc.reset();
sfc.doFilter(req, res);
return;
}
}
} else {
((HttpServletResponse) res).sendRedirect(hreq.getRequestURI()
+ "/");
return;
}
// no matching, process as file
sfc.setFilter(new WebAppContextFilter());
sfc.setServlet(new HttpServlet() {
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
String path = ((HttpServletRequest) req)
.getPathTranslated();
returnFileContent(path, (HttpServletRequest) req,
(HttpServletResponse) res);
}
});
sfc.reset();
sfc.doFilter(req, res);
} finally {
if (this.requestListeners != null) {
ServletRequestEvent e = new ServletRequestEvent(this, hreq);
for (ServletRequestListener rlistener : requestListeners)
rlistener.requestDestroyed(e);
}
}
}