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