1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 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 if (StringUtils.isEmpty(text))
77 {
78 return text;
79 }
80
81 if (config == null)
82 {
83 return null;
84 }
85
86
87 if (!text.startsWith("/") && !text.startsWith("./")
88 && !text.startsWith("//") && !text.startsWith(".//"))
89 {
90 StringBuffer sb = new StringBuffer();
91 sb.append("./");
92 sb.append(text);
93 text = sb.toString();
94 }
95
96 ServletContext context = config.getServletContext();
97 String base = context.getRealPath("/");
98
99 base = (StringUtils.isEmpty(base))
100 ? config.getInitParameter(Turbine.BASEDIR_KEY)
101 : base;
102
103 if (StringUtils.isEmpty(base))
104 {
105 return text;
106 }
107
108 String separator = System.getProperty("path.separator");
109
110 StringTokenizer tokenizer = new StringTokenizer(text,
111 separator);
112 StringBuffer buffer = new StringBuffer();
113 while (tokenizer.hasMoreTokens())
114 {
115 buffer.append(base).append(tokenizer.nextToken());
116 if (tokenizer.hasMoreTokens())
117 {
118 buffer.append(separator);
119 }
120 }
121 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 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 ServerData serverData = new ServerData(req);
149
150 if (StringUtils.isNotEmpty(scheme))
151 {
152 serverData.setServerScheme(scheme);
153 }
154
155 StringBuffer sb = new StringBuffer();
156
157 serverData.getHostUrl(sb);
158
159 return sb;
160 }
161 }