KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > urls > ServletURL


1 package org.jahia.urls;
2
3 import java.net.MalformedURLException JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5
6 /**
7  * <p>Title: </p>
8  * <p>Description: </p>
9  * <p>Copyright: Copyright (c) 2002</p>
10  * <p>Company: Jahia Ltd</p>
11  * @author Serge Huber
12  * @version 1.0
13  */

14
15 public class ServletURL extends QueryMapURL {
16
17     private String JavaDoc contextPath;
18     private String JavaDoc servletPath;
19     private String JavaDoc pathInfo;
20
21     public ServletURL () {
22         super();
23     }
24
25     public ServletURL (HttpServletRequest JavaDoc request)
26         throws MalformedURLException JavaDoc {
27         this(request, true);
28     }
29
30     public ServletURL (HttpServletRequest JavaDoc request, boolean relative)
31         throws MalformedURLException JavaDoc {
32         this(request.getScheme(), request.getServerName(),
33              request.getServerPort(), request.getContextPath(),
34              request.getServletPath(), request.getPathInfo(),
35              request.getQueryString(), relative);
36     }
37
38     public ServletURL (String JavaDoc contextPath, String JavaDoc servletPath, String JavaDoc pathInfo,
39                        String JavaDoc queryString) throws MalformedURLException JavaDoc {
40         this(null, null, -1, contextPath, servletPath, pathInfo, queryString, true);
41     }
42
43     public ServletURL (String JavaDoc scheme, String JavaDoc serverName, int serverPort,
44                        String JavaDoc contextPath, String JavaDoc servletPath, String JavaDoc pathInfo,
45                        String JavaDoc queryString, boolean relative)
46         throws MalformedURLException JavaDoc {
47
48         this();
49         setScheme(scheme);
50         setHostName(serverName);
51         setPort(serverPort);
52
53         this.contextPath = contextPath;
54         this.servletPath = servletPath;
55         this.pathInfo = pathInfo;
56         setPath(contextPath, servletPath, pathInfo);
57         setQueryString(queryString);
58
59         setURIStartingAtPath(relative);
60
61     }
62
63     /**
64      * Servlet API pattern matching resolution. This static method can be
65      * used to test the validity of a servlet path against a Servlet API
66      * mapping pattern.
67      * @param pattern the pattern to be matched against, according to
68      * Servlet API 2.3 Section 11.2 p71
69      * @param servletPath the servlet path to test, to see if it matches
70      * the pattern passed.
71      * @return true if the servlet path matches the pattern, false otherwise.
72      */

73     static public boolean matchesServletPattern(String JavaDoc pattern, String JavaDoc servletPath) {
74         if ((pattern == null) || (servletPath == null)) {
75             return false;
76         }
77         // first we must check what type of mapping the pattern uses.
78
if ((pattern.startsWith("/") && pattern.endsWith("/*"))) {
79             // path mapping detected.
80
String JavaDoc pathToMatch = pattern.substring(0, pattern.length()-2);
81             if (servletPath.startsWith(pathToMatch)) {
82                 return true;
83             } else {
84                 return false;
85             }
86         } else if (pattern.startsWith("*.")) {
87             // extension mapping detected.
88
int lastPointPos = servletPath.lastIndexOf(".");
89             if (lastPointPos == -1) {
90                 return false;
91             }
92             String JavaDoc extension = servletPath.substring(lastPointPos + 1);
93             String JavaDoc patternExt = pattern.substring(2);
94             if (patternExt.equals(extension)) {
95                 return true;
96             } else {
97                 return false;
98             }
99         } else if (!pattern.equals("/")) {
100             // exact mapping detected.
101
if (pattern.equals(servletPath)) {
102                 return true;
103             } else {
104                 return false;
105             }
106         } else {
107             // default servlet mapping "/"
108
return true;
109         }
110     }
111
112     public String JavaDoc getContextPath () {
113         return contextPath;
114     }
115
116     public void setContextPath(String JavaDoc contextPath) {
117         this.contextPath = contextPath;
118         setPath(getContextPath(), contextPath, getPathInfo());
119     }
120
121     public String JavaDoc getServletPath () {
122         return servletPath;
123     }
124
125     public void setServletPath(String JavaDoc servletPath) {
126         this.servletPath = servletPath;
127         setPath(getContextPath(), servletPath, getPathInfo());
128     }
129
130     public String JavaDoc getPathInfo () {
131         return pathInfo;
132     }
133
134     public void setPathInfo(String JavaDoc pathInfo) {
135         this.pathInfo = pathInfo;
136         setPath(getContextPath(), getServletPath(), pathInfo);
137     }
138
139     public String JavaDoc getRequestURI () {
140         return getPath();
141     }
142
143     private void setPath(String JavaDoc contextPath, String JavaDoc servletPath, String JavaDoc pathInfo) {
144         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
145         result.append(contextPath);
146         if (servletPath != null) {
147             result.append(servletPath);
148         }
149         if (pathInfo != null) {
150             result.append(pathInfo);
151         }
152         super.setPath(result.toString());
153     }
154
155
156 }
Popular Tags