01: package org.vraptor.url;
02:
03: import javax.servlet.http.HttpServletRequest;
04:
05: import org.apache.log4j.Logger;
06: import org.vraptor.LogicException;
07: import org.vraptor.annotations.Remotable;
08: import org.vraptor.component.LogicMethod;
09: import org.vraptor.view.RemoteView;
10: import org.vraptor.view.RemoteViewManager;
11: import org.vraptor.view.ViewManager;
12:
13: public class DefaultViewLocator implements ViewLocator {
14:
15: private static final Logger LOG = Logger
16: .getLogger(DefaultViewLocator.class);
17:
18: private static final String INCLUDE_REQUEST_URI = "javax.servlet.include.request_uri";
19:
20: public ViewManager locate(HttpServletRequest req,
21: LogicMethod method, ViewManager defaultViewManager)
22: throws InvalidURLException, LogicException {
23: String uri = extractURI(req);
24:
25: if (LOG.isDebugEnabled()) {
26: LOG.debug("Requested url (request wrapped): " + uri);
27: }
28:
29: uri = uri.substring(uri.lastIndexOf('/') + 1);
30: if (LOG.isDebugEnabled()) {
31: LOG.debug("requested uri: " + uri);
32: }
33:
34: int firstPosition = uri.indexOf('.');
35: if (firstPosition == -1)
36: throw new InvalidURLException(
37: "Unable to deal with desired url: " + uri);
38:
39: int secondPosition = uri.indexOf('.', firstPosition + 1);
40: if (secondPosition == -1)
41: throw new InvalidURLException(
42: "Unable to deal with desired url: " + uri);
43:
44: int thirdPosition = uri.indexOf('.', secondPosition + 1);
45: String viewType = null;
46: if (thirdPosition != -1) {
47: viewType = uri.substring(secondPosition + 1, thirdPosition);
48: }
49:
50: //TODO allow view managers to be expanded by registering your own managers...
51: // this code should look go for each one as in the converter finder
52: if (("xml".equals(viewType) || "ajax".equals(viewType))) {
53: if (!method.getMetadata().isAnnotationPresent(
54: Remotable.class)) {
55: throw new LogicException(
56: "logic method is not @Remotable");
57: }
58: return new RemoteViewManager(defaultViewManager, RemoteView
59: .valueOf(viewType.toUpperCase()));
60: }
61:
62: return defaultViewManager;
63: }
64:
65: private String extractURI(HttpServletRequest req) {
66: String uri;
67: // details in : http://www.caucho.com/resin-3.0/webapp/faq.xtp#forward-path
68: // and http://www.javaworld.com/javaworld/jw-03-2003/jw-0328-servlet.html?page=4
69: if (req.getAttribute(INCLUDE_REQUEST_URI) != null) {
70: uri = (String) req.getAttribute(INCLUDE_REQUEST_URI);
71: } else {
72: uri = req.getRequestURI();
73: }
74:
75: return uri;
76: }
77:
78: }
|