View Javadoc

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