View Javadoc
1   package org.apache.turbine.util;
2   
3   
4   import java.io.File;
5   
6   /*
7    * Licensed to the Apache Software Foundation (ASF) under one
8    * or more contributor license agreements.  See the NOTICE file
9    * distributed with this work for additional information
10   * regarding copyright ownership.  The ASF licenses this file
11   * to you under the Apache License, Version 2.0 (the
12   * "License"); you may not use this file except in compliance
13   * with the License.  You may obtain a copy of the License at
14   *
15   *   http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing,
18   * software distributed under the License is distributed on an
19   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20   * KIND, either express or implied.  See the License for the
21   * specific language governing permissions and limitations
22   * under the License.
23   */
24  
25  
26  import java.util.StringTokenizer;
27  
28  import javax.servlet.ServletConfig;
29  import javax.servlet.ServletContext;
30  
31  import org.apache.commons.lang3.StringUtils;
32  import org.apache.turbine.Turbine;
33  
34  /**
35   * This is where common Servlet manipulation routines should go.
36   *
37   * @author <a href="mailto:gonzalo.diethelm@sonda.com">Gonzalo Diethelm</a>
38   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
39   * @version $Id$
40   */
41  public class ServletUtils
42  {
43      /**
44       * Expands a string that points to a relative path or path list,
45       * leaving it as an absolute path based on the servlet context.
46       * It will return null if the text is empty or the config object
47       * is null.
48       *
49       * @param config The ServletConfig.
50       * @param text The String containing a path or path list.
51       * @return A String with the expanded path or path list.
52       */
53      public static String expandRelative(ServletConfig config,
54                                          String text)
55      {
56          if (StringUtils.isEmpty(text))
57          {
58              return text;
59          }
60  
61          if (config == null)
62          {
63              return null;
64          }
65  
66          final String expandedText;
67  
68          // attempt to make it relative
69          if (!text.startsWith("/") && !text.startsWith("./")
70                  && !text.startsWith("\\") && !text.startsWith(".\\"))
71          {
72              StringBuilder sb = new StringBuilder();
73              sb.append("./");
74              sb.append(text);
75              expandedText = sb.toString();
76          }
77          else
78          {
79              expandedText = text;
80          }
81  
82          ServletContext context = config.getServletContext();
83          String base = StringUtils.defaultIfEmpty(context.getRealPath("/"),
84                  Turbine.getApplicationRoot());
85  
86          if (StringUtils.isEmpty(base))
87          {
88              return expandedText;
89          }
90  
91          StringTokenizer tokenizer = new StringTokenizer(expandedText, File.pathSeparator);
92          StringBuilder buffer = new StringBuilder();
93          while (tokenizer.hasMoreTokens())
94          {
95              buffer.append(base).append(tokenizer.nextToken());
96              if (tokenizer.hasMoreTokens())
97              {
98                  buffer.append(File.pathSeparator);
99              }
100         }
101         return buffer.toString();
102     }
103 }