KickJava   Java API By Example, From Geeks To Geeks.

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


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

18
19 public class ContentServletURL extends ServletURL {
20
21     private static org.apache.log4j.Logger logger =
22             org.apache.log4j.Logger.getLogger (ContentServletURL.class);
23
24     InsertionSortedMap pathInfoParameters = new InsertionSortedMap();
25
26     public ContentServletURL (HttpServletRequest JavaDoc request)
27         throws MalformedURLException JavaDoc {
28         super(request);
29     }
30
31     public ContentServletURL (HttpServletRequest JavaDoc request, String JavaDoc url, boolean ignoringAuthorityInTest) throws MalformedURLException JavaDoc {
32         super();
33         if (!ContentServletURL.isContentServletURL(request, url,
34             ignoringAuthorityInTest)) {
35             throw new MalformedURLException JavaDoc("URL " + url +
36                 " is not a valid content servlet URL");
37         }
38
39         /** @todo this code is not optimized and very similar to the
40          * isContentServletURL method ! Find a cleaner way to do this !
41          */

42         ContentServletURL requestURL = new ContentServletURL(request);
43         URI targetURI = new URI(url);
44         QueryMapURL targetURL = null;
45         if (targetURI.isRelative()) {
46             String JavaDoc curRequestURI = requestURL.getRequestURI();
47             int lastSlashPos = curRequestURI.lastIndexOf("/");
48             if (lastSlashPos != -1) {
49                 String JavaDoc newURI = curRequestURI.substring(0, lastSlashPos+1) + url;
50                 targetURL = new QueryMapURL(newURI);
51             } else {
52                 // this shouldn't happen !
53
targetURL = new QueryMapURL(url);
54             }
55         } else {
56             targetURL = new QueryMapURL(url);
57         }
58         setScheme(targetURL.getScheme());
59         setUserInfo(targetURL.getUserInfo());
60         setHostName(targetURL.getHostName());
61         setPort(targetURL.getPort());
62         // now let's compare with the request URL to see if they match. The
63
// detail of this comparison depends on weather we are comparing
64
// in relative mode or in fully qualified mode.
65
String JavaDoc targetPath = targetURL.getPath();
66         String JavaDoc targetServletPath = targetPath.substring(requestURL.
67             getContextPath().length());
68         int queryPos = targetServletPath.indexOf("?");
69         int fragmentPos = targetServletPath.indexOf("#");
70         String JavaDoc targetQueryString = null;
71         String JavaDoc targetFragmentString = null;
72         String JavaDoc targetPathInfo;
73         if (queryPos > 0) {
74             targetPathInfo = targetServletPath.substring(requestURL.
75                 getServletPath().length(), queryPos);
76             if (fragmentPos > 0) {
77                 targetQueryString = targetServletPath.substring(queryPos+1, fragmentPos);
78                 targetFragmentString = targetServletPath.substring(fragmentPos+1);
79             } else {
80                 targetQueryString = targetServletPath.substring(queryPos+1);
81             }
82         } else if (fragmentPos > 0) {
83             targetPathInfo = targetServletPath.substring(requestURL.
84                 getServletPath().length(), fragmentPos);
85             targetFragmentString = targetServletPath.substring(fragmentPos+1);
86         } else {
87             targetPathInfo = targetServletPath.substring(requestURL.
88                 getServletPath().length());
89         }
90         setPathInfo(targetPathInfo);
91         setQueryString(targetQueryString);
92         setFragmentString(targetFragmentString);
93     }
94
95     static public boolean isContentServletURL (HttpServletRequest JavaDoc request,
96                                                String JavaDoc url, boolean ignoringAuthorityInTest)
97         throws MalformedURLException JavaDoc {
98         ContentServletURL requestURL = new ContentServletURL(request);
99         String JavaDoc useURL = url;
100         URI targetURL = new URI(useURL);
101         // now let's compare with the request URL to see if they match. The
102
// detail of this comparison depends on weather we are comparing
103
// in relative mode or in fully qualified mode.
104
if ((!targetURL.isURIStartingAtPath()) &&
105             (!targetURL.isRelative()) &&
106             (!ignoringAuthorityInTest)) {
107             if (!requestURL.getScheme().equals(targetURL.getScheme())) {
108                 return false;
109             }
110             if (!requestURL.getHostName().equals(targetURL.getHostName())) {
111                 return false;
112             }
113             if (requestURL.getPort() != targetURL.getPort()) {
114                 return false;
115             }
116         }
117         if (!targetURL.isRelative()) {
118             String JavaDoc targetPath = targetURL.getPath();
119             if (!targetPath.startsWith(requestURL.getContextPath())) {
120                 return false;
121             }
122             String JavaDoc targetServletPath = targetPath.substring(requestURL.
123                 getContextPath().length());
124             if (!targetServletPath.startsWith(requestURL.getServletPath())) {
125                 return false;
126             }
127         }
128         // if we got this far this means we have successfully matched both the
129
// context and the servlet path.
130

131         // SCSE 14.06.2005: check if the URL contains only JavaScript block
132
if (targetURL.getScheme() != null
133             && "javascript".equals(targetURL.getScheme().toLowerCase())) {
134            return false;
135         }
136         /**
137          * @todo we can add here checks on the path info to make sure the
138          * number of path info parameters is a multiple of two, etc, but for
139          * the time being we will stop here.
140          */

141         return true;
142     }
143
144     public String JavaDoc getPathInfoParameter(String JavaDoc name) {
145         return (String JavaDoc) pathInfoParameters.get(name);
146     }
147
148     public String JavaDoc setPathInfoParameter(String JavaDoc name, String JavaDoc value) {
149         return (String JavaDoc) pathInfoParameters.put(name, value);
150     }
151
152     public String JavaDoc getPathInfo() {
153         if (pathInfoParameters.size() == 0) {
154             return null;
155         }
156         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
157         java.util.Iterator JavaDoc parameterIter = pathInfoParameters.entrySet().iterator();
158         while (parameterIter.hasNext()) {
159             Entry curEntry = (Entry) parameterIter.next();
160             result.append("/");
161             result.append(curEntry.getKey());
162             result.append("/");
163             result.append(curEntry.getValue());
164         }
165         return result.toString();
166
167     }
168
169     public void setPathInfo(String JavaDoc pathInfo) {
170         super.setPathInfo(pathInfo);
171         parsePathInfoParameters(pathInfo);
172     }
173
174     private void parsePathInfoParameters (String JavaDoc pathInfo) {
175         // Parse the PathInfo and build a custom parameter map
176

177         if (pathInfo != null) {
178
179             if (pathInfo.lastIndexOf(";jsessionid=") != -1) {
180                 // let's remove the session ID from the parameters if it was attached.
181
int sessionIDPos = pathInfo.lastIndexOf(";jsessionid=");
182                 pathInfo = pathInfo.substring(0, sessionIDPos);
183                 logger.debug("Removed session ID marker from end of path info");
184             }
185
186             if (pathInfo.lastIndexOf(".") != -1) {
187                 // let's remove false static ending.
188
int lastSlash = pathInfo.lastIndexOf("/");
189                 if (lastSlash != -1) {
190                     String JavaDoc fakeStaticName = pathInfo.substring(lastSlash + 1);
191                     pathInfo = pathInfo.substring(0, lastSlash);
192                     logger.debug("Removed fake static ending. pathInfo=[" +
193                                  pathInfo + "] fakeEnding=[" +
194                                  fakeStaticName + "]");
195                 }
196             }
197
198             try {
199                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pathInfo, "/");
200
201                 while (st.hasMoreTokens()) {
202                     String JavaDoc token = st.nextToken();
203                     pathInfoParameters.put(token, st.nextToken());
204                 }
205
206             } catch (NoSuchElementException JavaDoc nee) {
207                 // stop parsing token
208
}
209         }
210     }
211
212 }
213
Popular Tags