org.vraptor.url.DefaultURLManager Maven / Gradle / Ivy
package org.vraptor.url;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
/**
* The default url manager translates url data.
*
* @author Guilherme Silveira
*/
public class DefaultURLManager implements URLManager {
private static final Logger LOG = Logger.getLogger(DefaultURLManager.class);
private static final String INCLUDE_REQUEST_URI = "javax.servlet.include.request_uri";
public RequestInfo extractInfoFromURL(HttpServletRequest req) throws InvalidURLException {
String uri = extractURI(req);
// TODO: trace
if (LOG.isDebugEnabled()) {
LOG.debug("Requested url (request wrapped): " + uri);
}
uri = uri.substring(uri.lastIndexOf('/') + 1);
if (LOG.isDebugEnabled()) {
LOG.debug("requested uri: " + uri);
}
int firstPosition = uri.indexOf('.');
if (firstPosition == -1)
throw new InvalidURLException("Unable to deal with desired url: " + uri);
int secondPosition = uri.indexOf('.', firstPosition + 1);
if (secondPosition == -1)
throw new InvalidURLException("Unable to deal with desired url: " + uri);
int thirdPosition = uri.indexOf('.', secondPosition + 1);
String viewType = null;
if (thirdPosition != -1) {
viewType = uri.substring(secondPosition + 1, thirdPosition);
}
String componentName = uri.substring(0, firstPosition);
String actionName = uri.substring(firstPosition + 1, secondPosition);
RequestInfo info = new DefaultRequestInfo(componentName, actionName, viewType);
if (LOG.isDebugEnabled()) {
LOG.debug("request info extracted: " + info);
}
return info;
}
private String extractURI(HttpServletRequest req) {
String uri;
// details in : http://www.caucho.com/resin-3.0/webapp/faq.xtp#forward-path
// and http://www.javaworld.com/javaworld/jw-03-2003/jw-0328-servlet.html?page=4
if (req.getAttribute(INCLUDE_REQUEST_URI) != null) {
uri = (String) req.getAttribute(INCLUDE_REQUEST_URI);
} else {
uri = req.getRequestURI();
}
return uri;
}
}