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;
027import java.util.stream.Stream;
028
029import javax.servlet.ServletConfig;
030import javax.servlet.ServletContext;
031
032import org.apache.commons.lang3.StringUtils;
033import org.apache.turbine.Turbine;
034
035/**
036 * This is where common Servlet manipulation routines should go.
037 *
038 * @author <a href="mailto:gonzalo.diethelm@sonda.com">Gonzalo Diethelm</a>
039 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
040 * @version $Id$
041 */
042public class ServletUtils
043{
044    /**
045     * Expands a string that points to a relative path or path list,
046     * leaving it as an absolute path based on the servlet context.
047     * It will return null if the text is empty or the config object
048     * is null.
049     *
050     * @param config The ServletConfig.
051     * @param text The String containing a path or path list.
052     * @return A String with the expanded path or path list.
053     */
054    public static String expandRelative(ServletConfig config,
055                                        String text)
056    {
057        if (StringUtils.isEmpty(text))
058        {
059            return text;
060        }
061
062        if (config == null)
063        {
064            return null;
065        }
066
067        final String expandedText;
068
069        // attempt to make it relative
070        if (Stream.of("/", "./", "\\", ".\\").noneMatch(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}