01: package org.vraptor.url;
02:
03: import javax.servlet.http.HttpServletRequest;
04:
05: import org.apache.log4j.Logger;
06: import org.vraptor.component.ComponentManager;
07: import org.vraptor.component.ComponentNotFoundException;
08: import org.vraptor.component.ComponentType;
09: import org.vraptor.component.LogicMethod;
10: import org.vraptor.component.LogicNotFoundException;
11:
12: /**
13: * The default url manager translates url data.
14: *
15: * @author Guilherme Silveira
16: */
17: public class DefaultLogicLocator implements LogicLocator {
18:
19: private static final Logger LOG = Logger
20: .getLogger(DefaultLogicLocator.class);
21:
22: private static final String INCLUDE_REQUEST_URI = "javax.servlet.include.request_uri";
23:
24: private final ComponentManager manager;
25:
26: public DefaultLogicLocator(ComponentManager manager) {
27: this .manager = manager;
28: }
29:
30: public LogicMethod locate(HttpServletRequest req)
31: throws InvalidURLException, LogicNotFoundException,
32: ComponentNotFoundException {
33: String uri = extractLogicInfoFromRequestURI(req);
34:
35: int firstPosition = uri.indexOf('.');
36: if (firstPosition == -1)
37: throw new InvalidURLException(
38: "Unable to deal with desired url: " + uri);
39:
40: int secondPosition = uri.indexOf('.', firstPosition + 1);
41: if (secondPosition == -1)
42: throw new InvalidURLException(
43: "Unable to deal with desired url: " + uri);
44:
45: String componentName = uri.substring(0, firstPosition);
46: String actionName = uri.substring(firstPosition + 1,
47: secondPosition);
48:
49: ComponentType component = manager.getComponent(componentName,
50: actionName);
51: LogicMethod method = component.getLogic(actionName);
52:
53: if (LOG.isDebugEnabled()) {
54: LOG.debug("logic method extracted: " + method);
55: }
56: return method;
57:
58: }
59:
60: String extractLogicInfoFromRequestURI(HttpServletRequest req) {
61: String uri = extractURI(req);
62: if (LOG.isDebugEnabled()) {
63: LOG.debug("Requested url (request wrapped): " + uri);
64: }
65:
66: String contextPath = req.getContextPath();
67: int startPosition = uri.indexOf(contextPath)
68: + contextPath.length() + 1;
69:
70: uri = uri.substring(startPosition);
71: if (LOG.isDebugEnabled()) {
72: LOG.debug("requested uri: " + uri);
73: }
74: return uri;
75: }
76:
77: private String extractURI(HttpServletRequest req) {
78: String uri;
79: // details in : http://www.caucho.com/resin-3.0/webapp/faq.xtp#forward-path
80: // and http://www.javaworld.com/javaworld/jw-03-2003/jw-0328-servlet.html?page=4
81: if (req.getAttribute(INCLUDE_REQUEST_URI) != null) {
82: uri = (String) req.getAttribute(INCLUDE_REQUEST_URI);
83: } else {
84: uri = req.getRequestURI();
85: }
86:
87: return uri;
88: }
89:
90: }
|