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.ioc.annotations.Symbol; 019import org.apache.tapestry5.services.PathConstructor; 020 021public class PathConstructorImpl implements PathConstructor 022{ 023 private final String clientPrefix, dispatchPrefix; 024 025 public PathConstructorImpl( 026 @Symbol(SymbolConstants.CONTEXT_PATH) String contextPath, 027 @Symbol(SymbolConstants.APPLICATION_FOLDER) String applicationFolder) 028 { 029 StringBuilder b = new StringBuilder("/"); 030 031 if (applicationFolder.length() > 0) 032 { 033 b.append(applicationFolder); 034 b.append('/'); 035 } 036 037 dispatchPrefix = b.toString(); 038 039 // If you mis-configure embedded Tomcat, you can get a contextPath of "/" rather than "". 040 // To make things fool proof, we handle that case. 041 clientPrefix = (contextPath.equals("/") ? "" : contextPath) + dispatchPrefix; 042 } 043 044 public String constructClientPath(String... terms) 045 { 046 return build(clientPrefix, terms); 047 } 048 049 public String constructDispatchPath(String... terms) 050 { 051 return build(dispatchPrefix, terms); 052 } 053 054 private String build(String prefix, String... terms) 055 { 056 StringBuilder b = new StringBuilder(prefix); 057 String sep = ""; 058 059 for (String term : terms) 060 { 061 b.append(sep); 062 b.append(term); 063 064 sep = "/"; 065 } 066 067 068 return b.toString(); 069 } 070}