    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.internal.services.ResourceStreamer;
    018import org.apache.tapestry5.ioc.Resource;
    019import org.apache.tapestry5.services.AssetSource;
    020import org.apache.tapestry5.services.ClasspathAssetAliasManager;
    021import org.apache.tapestry5.services.Request;
    022import org.apache.tapestry5.services.Response;
    023import org.apache.tapestry5.services.assets.AssetRequestHandler;
    024
    025import java.io.IOException;
    026
    027/**
    028 * A handler for asset requests for classpath assets (within a specific folder).
    029 * Each mapping of the {@link ClasspathAssetAliasManager} gets one of these.
    030 *
    031 * @since 5.2.0
    032 */
    033public class ClasspathAssetRequestHandler implements AssetRequestHandler
    034{
    035    private final ResourceStreamer streamer;
    036
    037    private final AssetSource assetSource;
    038
    039    private final String baseFolder;
    040
    041    public ClasspathAssetRequestHandler(ResourceStreamer streamer,
    042                                        AssetSource assetSource, String baseFolder)
    043    {
    044        this.streamer = streamer;
    045        this.assetSource = assetSource;
    046        this.baseFolder = baseFolder;
    047    }
    048
    049    public boolean handleAssetRequest(Request request, Response response, String extraPath) throws IOException
    050    {
    051        ChecksumPath path = new ChecksumPath(streamer, baseFolder, extraPath);
    052
    053        Resource resource = assetSource.resourceForPath(path.resourcePath);
    054
    055        return path.stream(resource);
    056    }
    057}