KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > ssi > SSIServletRequestUtil


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation. Licensed under the
3  * Apache License, Version 2.0 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of the License
5  * at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
6  * law or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */

11 package org.apache.catalina.ssi;
12
13
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import org.apache.catalina.util.RequestUtil;
16 public class SSIServletRequestUtil {
17     /**
18      * Return the relative path associated with this servlet. Taken from
19      * DefaultServlet.java. Perhaps this should be put in
20      * org.apache.catalina.util somewhere? Seems like it would be widely used.
21      *
22      * @param request
23      * The servlet request we are processing
24      */

25     public static String JavaDoc getRelativePath(HttpServletRequest JavaDoc request) {
26         // Are we being processed by a RequestDispatcher.include()?
27
if (request.getAttribute("javax.servlet.include.request_uri") != null) {
28             String JavaDoc result = (String JavaDoc)request
29                     .getAttribute("javax.servlet.include.path_info");
30             if (result == null)
31                 result = (String JavaDoc)request
32                         .getAttribute("javax.servlet.include.servlet_path");
33             if ((result == null) || (result.equals(""))) result = "/";
34             return (result);
35         }
36         // No, extract the desired path directly from the request
37
String JavaDoc result = request.getPathInfo();
38         if (result == null) {
39             result = request.getServletPath();
40         }
41         if ((result == null) || (result.equals(""))) {
42             result = "/";
43         }
44         return normalize(result);
45     }
46
47
48     /**
49      * Return a context-relative path, beginning with a "/", that represents
50      * the canonical version of the specified path after ".." and "." elements
51      * are resolved out. If the specified path attempts to go outside the
52      * boundaries of the current context (i.e. too many ".." path elements are
53      * present), return <code>null</code> instead. This normalize should be
54      * the same as DefaultServlet.normalize, which is almost the same ( see
55      * source code below ) as RequestUtil.normalize. Do we need all this
56      * duplication?
57      *
58      * @param path
59      * Path to be normalized
60      */

61     public static String JavaDoc normalize(String JavaDoc path) {
62         if (path == null) return null;
63         String JavaDoc normalized = path;
64         //Why doesn't RequestUtil do this??
65
// Normalize the slashes and add leading slash if necessary
66
if (normalized.indexOf('\\') >= 0)
67             normalized = normalized.replace('\\', '/');
68         normalized = RequestUtil.normalize(path);
69         return normalized;
70     }
71 }
Popular Tags