public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (_config == null)
{
return;
}
HttpServletRequest httpRequest = (HttpServletRequest) request;
String uri = httpRequest.getRequestURI();
// if the uri does not contain a suffix, we consider
// it to represent a directory / context, not a file.
// file has suffix. No need to search for welcome file
if (uri.lastIndexOf('.") > uri.lastIndexOf('/"))
{
chain.doFilter(request, response);
return;
}
String contextPath = httpRequest.getContextPath();
String welcomeFile = null;
StringBuffer sb = new StringBuffer(uri);
if (!uri.endsWith("/"))
{
sb.append('/");
}
String baseURI = sb.delete(
0,
contextPath.length()).toString();
// REVISIT: we probably can check for existence once at startup
// and know the exact welcome file by now. Of course, that
// would not work if the files change at runtime, but does it matter?
for (int i = 0; i < _welcomeFiles.length; i++)
{
sb.setLength(0);
sb.append(baseURI).append(_welcomeFiles[i]);
File file = new File(_context.getRealPath(sb.toString()));
// context.log("Welcome File: " + file.getAbsolutePath());
if (file.exists())
{
// REVISIT: This will force all "welcome" JSPs through MyFaces.
// Shouldn't we allow the user to enter *.jsf and check for *.jsp for existence, instead?
if (_welcomeFiles[i].endsWith(".jsp"))
{
// alter the name of the file we are requesting to
// force it through the MyFacesServlet
sb.replace(
sb.lastIndexOf(".jsp"),
sb.length(),
".jsf");
welcomeFile = sb.toString();
}
// we have discovered a filename;
// stop the loop
break;
}
}
if (welcomeFile == null)
{
sb.setLength(0);
sb.append(baseURI);
sb.append("index.jsf");
welcomeFile = sb.toString();
}
RequestDispatcher rd = httpRequest.getRequestDispatcher(welcomeFile);
rd.forward(request, response);
return;
}
If the URI indicates a context, or a subdirectory within a particular
context, but does not specify a filename, the request is redirected to
one of the default welcome files, assuming they are provided in the web.xml file.
If no welcome files are specified, or if none of the welcome files
actually exists, then the request is redirected to a file named "index.jsp" for
that context or subdirectory with the current context. If the index.jsp file
does not exist, the servlet will return a File Not Found Error 404 message.
A well configured servlet should provide a means of handling this type of
error, along with a link to an appropriate help page.
A URI is thought to represent a context and/or subdirectory(s) if
it lacks a suffix following the pattern .suffix. |