01: /*
02: * Created on Aug 5, 2005
03: */
04: package uk.org.ponder.rsf.viewstate;
05:
06: import org.springframework.util.StringUtils;
07:
08: import uk.org.ponder.stringutil.URLUtil;
09: import uk.org.ponder.util.Logger;
10:
11: /**
12: * @author Antranig Basman (antranig@caret.cam.ac.uk) A URL rewriter for
13: * "resource" URLs (i.e. those which lead outside the RSF system).
14: */
15: public class URLRewriter {
16: /**
17: * URLs with this prefix will be rewritten to be relative to the "context
18: * resource root" for this application. In a Webapp environment e.g., this
19: * will be the directory one level above WEB-INF/
20: */
21: public static final String CONTEXT_PREFIX = "$context/";
22:
23: private ViewStateHandler viewstatehandler;
24:
25: public void setViewStateHandler(ViewStateHandler viewstatehandler) {
26: this .viewstatehandler = viewstatehandler;
27: }
28:
29: public static boolean isContextURL(String path) {
30: return path.startsWith(CONTEXT_PREFIX);
31: }
32:
33: public String rewriteContextURL(String path) {
34: return viewstatehandler.encodeResourceURL(path
35: .substring(CONTEXT_PREFIX.length()));
36: }
37:
38: /**
39: * relpath has leading slash removed.
40: */
41: public String rewriteResourceURL(String path, String resourcebase) {
42: String resourceURL = null;
43: if (!URLUtil.isAbsolute(path)
44: && (path.length() == 0 || path.charAt(0) != '/')) {
45: if (isContextURL(path)) {
46: resourceURL = rewriteContextURL(path);
47: } else {
48: resourceURL = viewstatehandler
49: .encodeResourceURL(resourcebase + path);
50: }
51: resourceURL = StringUtils.cleanPath(resourceURL);
52: }
53: if (Logger.log.isDebugEnabled()) {
54: Logger.log.debug("getResourceURL returning " + resourceURL
55: + " for path " + path);
56: }
57: return resourceURL;
58: }
59: }
|