    001// Copyright 2013 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.internal.services;
    016
    017import org.apache.tapestry5.SymbolConstants;
    018import org.apache.tapestry5.http.TapestryHttpSymbolConstants;
    019import org.apache.tapestry5.ioc.annotations.Symbol;
    020import org.apache.tapestry5.services.PathConstructor;
    021
    022public class PathConstructorImpl implements PathConstructor
    023{
    024    private final String clientPrefix, dispatchPrefix;
    025
    026    public PathConstructorImpl(
    027            @Symbol(TapestryHttpSymbolConstants.CONTEXT_PATH) String contextPath,
    028            @Symbol(SymbolConstants.APPLICATION_FOLDER) String applicationFolder)
    029    {
    030        StringBuilder b = new StringBuilder("/");
    031
    032        if (applicationFolder.length() > 0)
    033        {
    034            b.append(applicationFolder);
    035            b.append('/');
    036        }
    037
    038        dispatchPrefix = b.toString();
    039
    040        // If you mis-configure embedded Tomcat, you can get a contextPath of "/" rather than "".
    041        // To make things fool proof, we handle that case.
    042        clientPrefix = (contextPath.equals("/") ? "" : contextPath) + dispatchPrefix;
    043    }
    044
    045    public String constructClientPath(String... terms)
    046    {
    047        return build(clientPrefix, terms);
    048    }
    049
    050    public String constructDispatchPath(String... terms)
    051    {
    052        return build(dispatchPrefix, terms);
    053    }
    054
    055    private String build(String prefix, String... terms)
    056    {
    057        StringBuilder b = new StringBuilder(prefix);
    058        String sep = "";
    059
    060        for (String term : terms)
    061        {
    062            b.append(sep);
    063            b.append(term);
    064
    065            sep = "/";
    066        }
    067
    068
    069        return b.toString();
    070    }
    071}