View Javadoc

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