001 // Copyright 2010 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 015 package org.apache.tapestry5.internal.services.assets; 016 017 import java.io.IOException; 018 019 import org.apache.tapestry5.internal.services.AssetResourceLocator; 020 import org.apache.tapestry5.internal.services.ResourceStreamer; 021 import org.apache.tapestry5.ioc.Resource; 022 import org.apache.tapestry5.services.ClasspathAssetAliasManager; 023 import org.apache.tapestry5.services.Request; 024 import org.apache.tapestry5.services.Response; 025 import org.apache.tapestry5.services.assets.AssetRequestHandler; 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 */ 033 public class ClasspathAssetRequestHandler implements AssetRequestHandler 034 { 035 private final ResourceStreamer streamer; 036 037 private final AssetResourceLocator assetResourceLocator; 038 039 private final String baseFolder; 040 041 public ClasspathAssetRequestHandler(ResourceStreamer streamer, AssetResourceLocator assetResourceLocator, 042 String baseFolder) 043 { 044 this.streamer = streamer; 045 this.assetResourceLocator = assetResourceLocator; 046 this.baseFolder = baseFolder; 047 } 048 049 public boolean handleAssetRequest(Request request, Response response, String extraPath) throws IOException 050 { 051 String assetPath = baseFolder + "/" + extraPath; 052 053 Resource resource = assetResourceLocator.findClasspathResourceForPath(assetPath); 054 055 if (resource == null) 056 return false; 057 058 streamer.streamResource(resource); 059 060 return true; 061 } 062 }