%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.util.ServletUtils |
|
|
1 | package org.apache.turbine.util; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.util.StringTokenizer; |
|
20 | import javax.servlet.ServletConfig; |
|
21 | import javax.servlet.ServletContext; |
|
22 | import javax.servlet.http.HttpServletRequest; |
|
23 | ||
24 | import org.apache.commons.lang.StringUtils; |
|
25 | ||
26 | import org.apache.turbine.Turbine; |
|
27 | import org.apache.turbine.util.uri.URIConstants; |
|
28 | ||
29 | /** |
|
30 | * This is where common Servlet manipulation routines should go. |
|
31 | * |
|
32 | * @author <a href="mailto:gonzalo.diethelm@sonda.com">Gonzalo Diethelm</a> |
|
33 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> |
|
34 | * @version $Id: ServletUtils.java 264148 2005-08-29 14:21:04Z henning $ |
|
35 | */ |
|
36 | 0 | public class ServletUtils |
37 | { |
|
38 | /** |
|
39 | * The default HTTP port number. |
|
40 | * @deprecated use URIConstants.HTTP_PORT |
|
41 | */ |
|
42 | public static final int HTTP_PORT = URIConstants.HTTP_PORT; |
|
43 | ||
44 | /** |
|
45 | * The default HTTPS port number. |
|
46 | * @deprecated use URIConstants.HTTPS_PORT |
|
47 | */ |
|
48 | public static final int HTTPS_PORT = URIConstants.HTTPS_PORT; |
|
49 | ||
50 | /** |
|
51 | * The default FTP port number. |
|
52 | * @deprecated use URIConstants.FTP_PORT |
|
53 | */ |
|
54 | public static final int FTP_PORT = URIConstants.FTP_PORT; |
|
55 | ||
56 | /** |
|
57 | * The part of the URI which separates the protocol indicator (i.e. the |
|
58 | * scheme) from the rest of the URI. |
|
59 | * @deprecated use URIConstants.URI_SCHEME_SEPARATOR; |
|
60 | */ |
|
61 | public static final String URI_SCHEME_SEPARATOR = URIConstants.URI_SCHEME_SEPARATOR; |
|
62 | ||
63 | /** |
|
64 | * Expands a string that points to a relative path or path list, |
|
65 | * leaving it as an absolute path based on the servlet context. |
|
66 | * It will return null if the text is empty or the config object |
|
67 | * is null. |
|
68 | * |
|
69 | * @param config The ServletConfig. |
|
70 | * @param text The String containing a path or path list. |
|
71 | * @return A String with the expanded path or path list. |
|
72 | */ |
|
73 | public static String expandRelative(ServletConfig config, |
|
74 | String text) |
|
75 | { |
|
76 | 0 | if (StringUtils.isEmpty(text)) |
77 | { |
|
78 | 0 | return text; |
79 | } |
|
80 | ||
81 | 0 | if (config == null) |
82 | { |
|
83 | 0 | return null; |
84 | } |
|
85 | ||
86 | // attempt to make it relative |
|
87 | 0 | if (!text.startsWith("/") && !text.startsWith("./") |
88 | && !text.startsWith("\\") && !text.startsWith(".\\")) |
|
89 | { |
|
90 | 0 | StringBuffer sb = new StringBuffer(); |
91 | 0 | sb.append("./"); |
92 | 0 | sb.append(text); |
93 | 0 | text = sb.toString(); |
94 | } |
|
95 | ||
96 | 0 | ServletContext context = config.getServletContext(); |
97 | 0 | String base = context.getRealPath("/"); |
98 | ||
99 | 0 | base = (StringUtils.isEmpty(base)) |
100 | ? config.getInitParameter(Turbine.BASEDIR_KEY) |
|
101 | : base; |
|
102 | ||
103 | 0 | if (StringUtils.isEmpty(base)) |
104 | { |
|
105 | 0 | return text; |
106 | } |
|
107 | ||
108 | 0 | String separator = System.getProperty("path.separator"); |
109 | ||
110 | 0 | StringTokenizer tokenizer = new StringTokenizer(text, |
111 | separator); |
|
112 | 0 | StringBuffer buffer = new StringBuffer(); |
113 | 0 | while (tokenizer.hasMoreTokens()) |
114 | { |
|
115 | 0 | buffer.append(base).append(tokenizer.nextToken()); |
116 | 0 | if (tokenizer.hasMoreTokens()) |
117 | { |
|
118 | 0 | buffer.append(separator); |
119 | } |
|
120 | } |
|
121 | 0 | return buffer.toString(); |
122 | } |
|
123 | ||
124 | /** |
|
125 | * Defaults to the scheme used in the supplied request. |
|
126 | * |
|
127 | * @see #hostURL(HttpServletRequest req, String proto) |
|
128 | * @deprecated Use ServerData(req).getHostUrl() |
|
129 | */ |
|
130 | public static StringBuffer hostURL(HttpServletRequest req) |
|
131 | { |
|
132 | 0 | return hostURL(req, null); |
133 | } |
|
134 | ||
135 | /** |
|
136 | * Returns a URL fragment derived from the provided HTTP request, |
|
137 | * including the protocol used to address the server (if non-standard |
|
138 | * for HTTP/HTTPS). Returns the fragment as a buffer |
|
139 | * |
|
140 | * @param req The request to extract information from. |
|
141 | * @param scheme The protocol indicator to prefix the host name with, or |
|
142 | * the protocol used to address the server with if <code>null</code>. |
|
143 | * @return The desired URL fragment. |
|
144 | * @deprecated Use ServerData(req).getHostUrl() |
|
145 | */ |
|
146 | public static StringBuffer hostURL(HttpServletRequest req, String scheme) |
|
147 | { |
|
148 | 0 | ServerData serverData = new ServerData(req); |
149 | ||
150 | 0 | if (StringUtils.isNotEmpty(scheme)) |
151 | { |
|
152 | 0 | serverData.setServerScheme(scheme); |
153 | } |
|
154 | ||
155 | 0 | StringBuffer sb = new StringBuffer(); |
156 | ||
157 | 0 | serverData.getHostUrl(sb); |
158 | ||
159 | 0 | return sb; |
160 | } |
|
161 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |