    001package org.apache.turbine.util;
    002
    003
    004/*
    005 * Licensed to the Apache Software Foundation (ASF) under one
    006 * or more contributor license agreements.  See the NOTICE file
    007 * distributed with this work for additional information
    008 * regarding copyright ownership.  The ASF licenses this file
    009 * to you under the Apache License, Version 2.0 (the
    010 * "License"); you may not use this file except in compliance
    011 * with the License.  You may obtain a copy of the License at
    012 *
    013 *   http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing,
    016 * software distributed under the License is distributed on an
    017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    018 * KIND, either express or implied.  See the License for the
    019 * specific language governing permissions and limitations
    020 * under the License.
    021 */
    022
    023
    024import java.util.StringTokenizer;
    025
    026import javax.servlet.ServletConfig;
    027import javax.servlet.ServletContext;
    028
    029import org.apache.commons.lang.StringUtils;
    030import org.apache.turbine.Turbine;
    031
    032/**
    033 * This is where common Servlet manipulation routines should go.
    034 *
    035 * @author <a href="mailto:gonzalo.diethelm@sonda.com">Gonzalo Diethelm</a>
    036 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    037 * @version $Id: ServletUtils.java 1709648 2015-10-20 17:08:10Z tv $
    038 */
    039public class ServletUtils
    040{
    041    /**
    042     * Expands a string that points to a relative path or path list,
    043     * leaving it as an absolute path based on the servlet context.
    044     * It will return null if the text is empty or the config object
    045     * is null.
    046     *
    047     * @param config The ServletConfig.
    048     * @param text The String containing a path or path list.
    049     * @return A String with the expanded path or path list.
    050     */
    051    public static String expandRelative(ServletConfig config,
    052                                        String text)
    053    {
    054        if (StringUtils.isEmpty(text))
    055        {
    056            return text;
    057        }
    058
    059        if (config == null)
    060        {
    061            return null;
    062        }
    063
    064        // attempt to make it relative
    065        if (!text.startsWith("/") && !text.startsWith("./")
    066                && !text.startsWith("\\") && !text.startsWith(".\\"))
    067        {
    068            StringBuilder sb = new StringBuilder();
    069            sb.append("./");
    070            sb.append(text);
    071            text = sb.toString();
    072        }
    073
    074        ServletContext context = config.getServletContext();
    075        String base = context.getRealPath("/");
    076
    077        base = (StringUtils.isEmpty(base))
    078            ? config.getInitParameter(Turbine.BASEDIR_KEY)
    079            : base;
    080
    081        if (StringUtils.isEmpty(base))
    082        {
    083            return text;
    084        }
    085
    086        String separator = System.getProperty("path.separator");
    087
    088        StringTokenizer tokenizer = new StringTokenizer(text,
    089                separator);
    090        StringBuilder buffer = new StringBuilder();
    091        while (tokenizer.hasMoreTokens())
    092        {
    093            buffer.append(base).append(tokenizer.nextToken());
    094            if (tokenizer.hasMoreTokens())
    095            {
    096                buffer.append(separator);
    097            }
    098        }
    099        return buffer.toString();
    100    }
    101}