    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.commons.Resource;
    018import org.apache.tapestry5.http.services.Request;
    019import org.apache.tapestry5.http.services.Response;
    020import org.apache.tapestry5.internal.services.ResourceStreamer;
    021import org.apache.tapestry5.services.AssetSource;
    022import org.apache.tapestry5.services.ClasspathAssetAliasManager;
    023import org.apache.tapestry5.services.ClasspathAssetProtectionRule;
    024import org.apache.tapestry5.services.assets.AssetRequestHandler;
    025import org.slf4j.Logger;
    026import org.slf4j.LoggerFactory;
    027
    028import java.io.IOException;
    029
    030/**
    031 * A handler for asset requests for classpath assets (within a specific folder).
    032 * Each mapping of the {@link ClasspathAssetAliasManager} gets one of these.
    033 *
    034 * @since 5.2.0
    035 */
    036public class ClasspathAssetRequestHandler implements AssetRequestHandler
    037{
    038
    039    private final static Logger LOGGER = LoggerFactory.getLogger(ClasspathAssetRequestHandler.class);
    040
    041    private final ResourceStreamer streamer;
    042
    043    private final AssetSource assetSource;
    044
    045    private final String baseFolder;
    046
    047    private final ClasspathAssetProtectionRule classpathAssetProtectionRule;
    048
    049    public ClasspathAssetRequestHandler(ResourceStreamer streamer,
    050                                        AssetSource assetSource, String baseFolder,
    051                                        ClasspathAssetProtectionRule classpathAssetProtectionRule)
    052    {
    053        this.streamer = streamer;
    054        this.assetSource = assetSource;
    055        this.baseFolder = baseFolder;
    056        this.classpathAssetProtectionRule = classpathAssetProtectionRule;
    057    }
    058
    059    public boolean handleAssetRequest(Request request, Response response, String extraPath) throws IOException
    060    {
    061        ChecksumPath path = new ChecksumPath(streamer, baseFolder, extraPath);
    062
    063        final boolean handled;
    064        if (classpathAssetProtectionRule.block(path.resourcePath) && !path.resourcePath.equals(ChecksumPath.NON_EXISTING_RESOURCE))
    065        {
    066            if (LOGGER.isWarnEnabled())
    067            {
    068                LOGGER.warn("Blocked request for classpath asset '" + path.resourcePath +
    069                        "'. Contribute a new ClasspathAssetProtectionRule if you need this asset to be publicly accessible.");
    070            }
    071            handled = false;
    072        }
    073        else
    074        {
    075            Resource resource = assetSource.resourceForPath(path.resourcePath);
    076
    077            handled = path.stream(resource);
    078        }
    079        return handled;
    080    }
    081}