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.assets;
016
017import org.apache.tapestry5.commons.Resource;
018import org.apache.tapestry5.internal.services.ResourceStreamer;
019
020import java.io.IOException;
021
022/**
023 * Utility used by {@link org.apache.tapestry5.services.assets.AssetRequestHandler} implementations
024 * where the first folder in the extra path is actually a computed checksum of the resource's content.
025 *
026 * @since 5.4
027 */
028public class ChecksumPath
029{
030    static final String NON_EXISTING_RESOURCE = "_________________________";
031
032    public final String checksum;
033
034    public final String resourcePath;
035
036    private final ResourceStreamer streamer;
037
038    public ChecksumPath(ResourceStreamer streamer, String baseFolder, String extraPath)
039    {
040        this.streamer = streamer;
041        int slashx = extraPath.indexOf('/');
042
043        if (slashx == -1) {
044            checksum = "";
045            resourcePath = NON_EXISTING_RESOURCE;
046            return;
047        }
048
049        checksum = extraPath.substring(0, slashx);
050
051        String morePath = extraPath.substring(slashx + 1);
052        
053        // Slashes at the end of the path should be dropped because
054        // they don't make sense. TAP5-2663
055        while (morePath.endsWith("/")) 
056        {
057            morePath = morePath.substring(0, morePath.length() - 1);
058        }
059
060        if (!isBlank(morePath)) 
061        {
062            resourcePath = baseFolder == null
063                    ? morePath
064                    : baseFolder + "/" + morePath;
065        }
066        else {
067            // When we only have something which looks like a checksum but no actual path.
068            // For example, /assets/META-INF/
069            resourcePath = NON_EXISTING_RESOURCE;
070        }
071    }
072
073    /**
074     * If the resource exists and the checksum is correct, stream it to the client and return true. Otherwise,
075     * return false.
076     *
077     * @param resource
078     *         to stream
079     * @return true if streamed, false otherwise
080     * @throws IOException
081     */
082    public boolean stream(Resource resource) throws IOException
083    {
084        if (resource == null || !resource.exists())
085        {
086            return false;
087        }
088
089
090        return streamer.streamResource(resource, checksum, ResourceStreamer.DEFAULT_OPTIONS);
091    }
092    
093    /**
094     * Copied from StringUtils since it's the only method we want from it.
095     */
096    private static boolean isBlank(final CharSequence cs) {
097        int strLen;
098        if (cs == null || (strLen = cs.length()) == 0) {
099            return true;
100        }
101        for (int i = 0; i < strLen; i++) {
102            if (!Character.isWhitespace(cs.charAt(i))) {
103                return false;
104            }
105        }
106        return true;
107    }
108
109}