in app/src/main/java/org/apache/roller/weblogger/ui/rendering/WeblogRequestMapper.java [89:243]
public boolean handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// kinda silly, but we need to keep track of whether or not the url had
// a trailing slash so that we can act accordingly
boolean trailingSlash = false;
String weblogHandle = null;
String weblogLocale = null;
String weblogRequestContext = null;
String weblogRequestData = null;
log.debug("evaluating ["+request.getRequestURI()+"]");
// figure out potential weblog handle
String servlet = request.getRequestURI();
String pathInfo = null;
if(servlet != null && servlet.trim().length() > 1) {
if(request.getContextPath() != null) {
servlet = servlet.substring(request.getContextPath().length());
}
// strip off the leading slash
servlet = servlet.substring(1);
// strip off trailing slash if needed
if(servlet.endsWith("/")) {
servlet = servlet.substring(0, servlet.length() - 1);
trailingSlash = true;
}
if(servlet.indexOf('/') != -1) {
weblogHandle = servlet.substring(0, servlet.indexOf('/'));
pathInfo = servlet.substring(servlet.indexOf('/')+1);
} else {
weblogHandle = servlet;
}
}
log.debug("potential weblog handle = "+weblogHandle);
// check if it's a valid weblog handle
if(restricted.contains(weblogHandle) || !this.isWeblog(weblogHandle)) {
log.debug("SKIPPED "+weblogHandle);
return false;
}
// is there a special hostname for the specified hostname?
String multiHostNameURL = WebloggerConfig.getProperty("weblog.absoluteurl." + weblogHandle);
if ( multiHostNameURL != null ) {
// there is, so check that configured hostname matches the one in the request
URL weblogAbsoluteURL = new URL( multiHostNameURL );
String headerHost = request.getHeader("Host");
String configHost = weblogAbsoluteURL.getHost();
if (headerHost != null && configHost != null && !headerHost.equals(configHost)) {
log.debug("SKIPPED " + weblogHandle);
return false;
}
}
log.debug("WEBLOG_URL "+request.getServletPath());
// parse the rest of the url and build forward url
if(pathInfo != null) {
// parse the next portion of the url
// we expect [locale/]<context>/<extra>/<info>
String[] urlPath = pathInfo.split("/", 3);
// if we have a locale, deal with it
if(this.isLocale(urlPath[0])) {
weblogLocale = urlPath[0];
// no extra path info specified
if(urlPath.length == 2) {
weblogRequestContext = urlPath[1];
weblogRequestData = null;
// request contains extra path info
} else if(urlPath.length == 3) {
weblogRequestContext = urlPath[1];
weblogRequestData = urlPath[2];
}
// otherwise locale is empty
} else {
weblogLocale = null;
weblogRequestContext = urlPath[0];
// last part of request is extra path info
if(urlPath.length == 2) {
weblogRequestData = urlPath[1];
// if we didn't have a locale then we have split too much
// so we reassemble the last 2 path elements together
} else if(urlPath.length == 3) {
weblogRequestData = urlPath[1] + "/" + urlPath[2];
}
}
}
// special handling for trailing slash issue
// we need this because by http standards the urls /foo and /foo/ are
// supposed to be considered different, so we must enforce that
if(weblogRequestContext == null && !trailingSlash) {
// this means someone referred to a weblog index page with the
// shortest form of url /<weblog> or /<weblog>/<locale> and we need
// to do a redirect to /<weblog>/ or /<weblog>/<locale>/
String redirectUrl = "/" + weblogHandle + "/";
if(request.getQueryString() != null) {
redirectUrl += "?"+request.getQueryString();
}
response.sendRedirect(redirectUrl);
return true;
} else if(weblogRequestContext != null &&
"tags".equals(weblogRequestContext)) {
// tags section can have an index page at /<weblog>/tags/ and
// a tags query at /<weblog>/tags/tag1+tag2, buth that's it
if((weblogRequestData == null && !trailingSlash) ||
(weblogRequestData != null && trailingSlash)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return true;
}
} else if(weblogRequestContext != null && trailingSlash) {
// this means that someone has accessed a weblog url and included
// a trailing slash, like /<weblog>/entry/<anchor>/ which is not
// supported, so we need to offer up a 404 Not Found
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return true;
}
// calculate forward url
String forwardUrl = calculateForwardUrl(request, weblogHandle, weblogLocale,
weblogRequestContext, weblogRequestData);
// if we don't have a forward url then the request was invalid somehow
if(forwardUrl == null) {
return false;
}
// dispatch to forward url
log.debug("forwarding to "+forwardUrl);
RequestDispatcher dispatch = request.getRequestDispatcher(forwardUrl);
dispatch.forward(request, response);
// we dealt with this request ourselves, so return "true"
return true;
}