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.internal.services.ResourceStreamer;
018import org.apache.tapestry5.ioc.Resource;
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        checksum = extraPath.substring(0, slashx);
044
045        String morePath = extraPath.substring(slashx + 1);
046        
047        // Slashes at the end of the path should be dropped because
048        // they don't make sense. TAP5-2663
049        while (morePath.endsWith("/")) 
050        {
051            morePath = morePath.substring(0, morePath.length() - 1);
052        }
053
054        if (!isBlank(morePath)) 
055        {
056            resourcePath = baseFolder == null
057                    ? morePath
058                    : baseFolder + "/" + morePath;
059        }
060        else {
061            // When we only have something which looks like a checksum but no actual path.
062            // For example, /assets/META-INF/
063            resourcePath = NON_EXISTING_RESOURCE;
064        }
065    }
066
067    /**
068     * If the resource exists and the checksum is correct, stream it to the client and return true. Otherwise,
069     * return false.
070     *
071     * @param resource
072     *         to stream
073     * @return true if streamed, false otherwise
074     * @throws IOException
075     */
076    public boolean stream(Resource resource) throws IOException
077    {
078        if (resource == null || !resource.exists())
079        {
080            return false;
081        }
082
083
084        return streamer.streamResource(resource, checksum, ResourceStreamer.DEFAULT_OPTIONS);
085    }
086    
087    /**
088     * Copied from StringUtils since it's the only method we want from it.
089     */
090    private static boolean isBlank(final CharSequence cs) {
091        int strLen;
092        if (cs == null || (strLen = cs.length()) == 0) {
093            return true;
094        }
095        for (int i = 0; i < strLen; i++) {
096            if (!Character.isWhitespace(cs.charAt(i))) {
097                return false;
098            }
099        }
100        return true;
101    }
102
103}