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