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  import java.util.stream.Stream;
28  
29  import javax.servlet.ServletConfig;
30  import javax.servlet.ServletContext;
31  
32  import org.apache.commons.lang3.StringUtils;
33  import org.apache.turbine.Turbine;
34  
35  /**
36   * This is where common Servlet manipulation routines should go.
37   *
38   * @author <a href="mailto:gonzalo.diethelm@sonda.com">Gonzalo Diethelm</a>
39   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40   * @version $Id$
41   */
42  public class ServletUtils
43  {
44      /**
45       * Expands a string that points to a relative path or path list,
46       * leaving it as an absolute path based on the servlet context.
47       * It will return null if the text is empty or the config object
48       * is null.
49       *
50       * @param config The ServletConfig.
51       * @param text The String containing a path or path list.
52       * @return A String with the expanded path or path list.
53       */
54      public static String expandRelative(ServletConfig config,
55                                          String text)
56      {
57          if (StringUtils.isEmpty(text))
58          {
59              return text;
60          }
61  
62          if (config == null)
63          {
64              return null;
65          }
66  
67          final String expandedText;
68  
69          // attempt to make it relative
70          if (Stream.of("/", "./", "\\", ".\\").noneMatch(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 }