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