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