001// Copyright 2010-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.assets;
016
017import org.apache.tapestry5.SymbolConstants;
018import org.apache.tapestry5.http.services.BaseURLSource;
019import org.apache.tapestry5.http.services.Request;
020import org.apache.tapestry5.ioc.annotations.Symbol;
021import org.apache.tapestry5.ioc.internal.util.InternalUtils;
022import org.apache.tapestry5.services.AssetPathConverter;
023import org.apache.tapestry5.services.PathConstructor;
024import org.apache.tapestry5.services.assets.AssetPathConstructor;
025import org.apache.tapestry5.services.assets.CompressionStatus;
026import org.apache.tapestry5.services.assets.StreamableResource;
027
028import java.io.IOException;
029
030public class AssetPathConstructorImpl implements AssetPathConstructor
031{
032    private final Request request;
033
034    private final String prefix;
035
036    private final BaseURLSource baseURLSource;
037
038    private final AssetPathConverter pathConverter;
039
040    private final boolean fullyQualified;
041
042    public AssetPathConstructorImpl(Request request,
043                                    BaseURLSource baseURLSource,
044
045                                    @Symbol(SymbolConstants.ASSET_URL_FULL_QUALIFIED)
046                                    boolean fullyQualified,
047
048                                    @Symbol(SymbolConstants.ASSET_PATH_PREFIX)
049                                    String uncompressedAssetPrefix,
050
051                                    PathConstructor pathConstructor,
052
053                                    AssetPathConverter pathConverter)
054    {
055        this.request = request;
056        this.baseURLSource = baseURLSource;
057
058        this.fullyQualified = fullyQualified;
059        this.pathConverter = pathConverter;
060
061        prefix = pathConstructor.constructClientPath(uncompressedAssetPrefix, "");
062    }
063
064    public String constructAssetPath(String virtualFolder, String path, StreamableResource resource) throws IOException
065    {
066        assert InternalUtils.isNonBlank(path);
067
068        assert InternalUtils.isNonBlank(virtualFolder);
069
070        StringBuilder builder = new StringBuilder();
071
072        if (fullyQualified)
073        {
074            builder.append(baseURLSource.getBaseURL(request.isSecure()));
075        }
076
077        builder.append(prefix);
078        builder.append(virtualFolder);
079        builder.append('/');
080
081        // The 'z' prefix indicates a compressed resource.
082
083        if (resource.getCompression() == CompressionStatus.COMPRESSED)
084        {
085            builder.append('z');
086        }
087
088        builder.append(resource.getChecksum());
089        builder.append('/');
090        builder.append(path);
091
092        return pathConverter.convertAssetPath(builder.toString());
093    }
094
095}