001    // Copyright 2010, 2011 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    
015    package org.apache.tapestry5.internal.services.assets;
016    
017    import org.apache.tapestry5.SymbolConstants;
018    import org.apache.tapestry5.internal.services.RequestConstants;
019    import org.apache.tapestry5.ioc.annotations.Symbol;
020    import org.apache.tapestry5.services.BaseURLSource;
021    import org.apache.tapestry5.services.Request;
022    import org.apache.tapestry5.services.assets.AssetPathConstructor;
023    
024    public class AssetPathConstructorImpl implements AssetPathConstructor
025    {
026        private final Request request;
027    
028        private final String prefix;
029    
030        private final BaseURLSource baseURLSource;
031    
032        private final boolean fullyQualified;
033    
034        public AssetPathConstructorImpl(Request request,
035                                        BaseURLSource baseURLSource,
036    
037                                        @Symbol(SymbolConstants.APPLICATION_VERSION)
038                                        String applicationVersion,
039    
040                                        @Symbol(SymbolConstants.APPLICATION_FOLDER)
041                                        String applicationFolder,
042    
043                                        @Symbol(SymbolConstants.ASSET_URL_FULL_QUALIFIED)
044                                        boolean fullyQualified)
045        {
046            this.request = request;
047            this.baseURLSource = baseURLSource;
048    
049            this.fullyQualified = fullyQualified;
050    
051            String folder = applicationFolder.equals("") ? "" : "/" + applicationFolder;
052    
053            this.prefix = folder + RequestConstants.ASSET_PATH_PREFIX + applicationVersion + "/";
054        }
055    
056        public String constructAssetPath(String virtualFolder, String path)
057        {
058            StringBuilder builder = new StringBuilder();
059    
060            if (fullyQualified)
061                builder.append(baseURLSource.getBaseURL(request.isSecure()));
062    
063            builder.append(request.getContextPath());
064            builder.append(prefix);
065            builder.append(virtualFolder);
066            builder.append('/');
067            builder.append(path);
068    
069            return builder.toString();
070        }
071    }