KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > ServletUtils


1 package org.apache.turbine.util;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
20 import javax.servlet.ServletConfig JavaDoc;
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
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,v 1.4.2.2 2004/05/20 03:16:38 seade Exp $
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 JavaDoc 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 JavaDoc expandRelative(ServletConfig JavaDoc config,
74                                         String JavaDoc text)
75     {
76         if (StringUtils.isEmpty(text))
77         {
78             return text;
79         }
80
81         if (config == null)
82         {
83             return null;
84         }
85
86         // attempt to make it relative
87
if (!text.startsWith("/") && !text.startsWith("./")
88                 && !text.startsWith("\\") && !text.startsWith(".\\"))
89         {
90             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
91             sb.append("./");
92             sb.append(text);
93             text = sb.toString();
94         }
95         
96         ServletContext JavaDoc context = config.getServletContext();
97         String JavaDoc 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 JavaDoc separator = System.getProperty("path.separator");
109         
110         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(text,
111                 separator);
112         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc hostURL(HttpServletRequest JavaDoc 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 JavaDoc hostURL(HttpServletRequest JavaDoc req, String JavaDoc scheme)
147     {
148         ServerData serverData = new ServerData(req);
149
150         if (StringUtils.isNotEmpty(scheme))
151         {
152             serverData.setServerScheme(scheme);
153         }
154
155         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
156
157         serverData.getHostUrl(sb);
158
159         return sb;
160     }
161 }
162
Popular Tags