01: /*
02: * Copyright 1999,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.catalina.ssi;
17:
18: import javax.servlet.http.HttpServletRequest;
19: import org.apache.catalina.util.RequestUtil;
20:
21: public class SSIServletRequestUtil {
22: /**
23: * Return the relative path associated with this servlet.
24: *
25: * Taken from DefaultServlet.java. Perhaps this should be put in
26: * org.apache.catalina.util somewhere? Seems like it would be widely used.
27: *
28: * @param request The servlet request we are processing
29: */
30: public static String getRelativePath(HttpServletRequest request) {
31:
32: // Are we being processed by a RequestDispatcher.include()?
33: if (request.getAttribute("javax.servlet.include.request_uri") != null) {
34: String result = (String) request
35: .getAttribute("javax.servlet.include.path_info");
36: if (result == null)
37: result = (String) request
38: .getAttribute("javax.servlet.include.servlet_path");
39: if ((result == null) || (result.equals("")))
40: result = "/";
41: return (result);
42: }
43:
44: // No, extract the desired path directly from the request
45: String result = request.getPathInfo();
46: if (result == null) {
47: result = request.getServletPath();
48: }
49: if ((result == null) || (result.equals(""))) {
50: result = "/";
51: }
52: return normalize(result);
53: }
54:
55: /**
56: * Return a context-relative path, beginning with a "/", that represents
57: * the canonical version of the specified path after ".." and "." elements
58: * are resolved out. If the specified path attempts to go outside the
59: * boundaries of the current context (i.e. too many ".." path elements
60: * are present), return <code>null</code> instead.
61: *
62: * This normalize should be the same as DefaultServlet.normalize, which is almost the same
63: * ( see source code below ) as RequestUtil.normalize. Do we need all this duplication?
64: *
65: * @param path Path to be normalized
66: */
67: public static String normalize(String path) {
68: if (path == null)
69: return null;
70:
71: String normalized = path;
72:
73: //Why doesn't RequestUtil do this??
74:
75: // Normalize the slashes and add leading slash if necessary
76: if (normalized.indexOf('\\') >= 0)
77: normalized = normalized.replace('\\', '/');
78:
79: normalized = RequestUtil.normalize(path);
80: return normalized;
81: }
82: }
|