// Copyright 2010, 2013 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry5.internal.services.assets;

import org.apache.tapestry5.internal.services.ResourceStreamer;
import org.apache.tapestry5.ioc.Resource;
import org.apache.tapestry5.services.AssetSource;
import org.apache.tapestry5.services.ClasspathAssetAliasManager;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Response;
import org.apache.tapestry5.services.assets.AssetRequestHandler;

import java.io.IOException;

/**
 * A handler for asset requests for classpath assets (within a specific folder).
 * Each mapping of the {@link ClasspathAssetAliasManager} gets one of these.
 *
 * @since 5.2.0
 */
public class ClasspathAssetRequestHandler implements AssetRequestHandler
{
    private final ResourceStreamer streamer;

    private final AssetSource assetSource;

    private final String baseFolder;

    public ClasspathAssetRequestHandler(ResourceStreamer streamer,
                                        AssetSource assetSource, String baseFolder)
    {
        this.streamer = streamer;
        this.assetSource = assetSource;
        this.baseFolder = baseFolder;
    }

    public boolean handleAssetRequest(Request request, Response response, String extraPath) throws IOException
    {
        ChecksumPath path = new ChecksumPath(streamer, baseFolder, extraPath);

        Resource resource = assetSource.resourceForPath(path.resourcePath);

        return path.stream(resource);
    }
}

@@ -876,24 +876,91 @@
 iasManager;%0A
+import org.apache.tapestry5.services.ClasspathAssetProtectionRule;%0A
 import org.a
@@ -1484,16 +1484,20 @@
 Source;%0A
+    
 %0A    pri
@@ -1525,16 +1525,98 @@
 eFolder;
+%0A    %0A    private final ClasspathAssetProtectionRule classpathAssetProtectionRule;
 %0A%0A    pu
@@ -1758,16 +1758,115 @@
 seFolder
+,%0A                                        ClasspathAssetProtectionRule classpathAssetProtectionRule
 )%0A    %7B%0A
@@ -1973,24 +1973,98 @@
 baseFolder;%0A
+        this.classpathAssetProtectionRule = classpathAssetProtectionRule;%0A
     %7D%0A%0A    p
@@ -2250,25 +2250,208 @@
 extraPath);%0A
+        
 %0A
+        final boolean handled;%0A        if (classpathAssetProtectionRule.block(path.resourcePath)) %0A        %7B%0A            handled = false;%0A        %7D%0A        else%0A        %7B%0A    
         Reso
@@ -2514,16 +2514,20 @@
 ePath);%0A
+    
 %0A       
@@ -2531,14 +2531,21 @@
     
-return
+    handled =
  pat
@@ -2564,16 +2564,50 @@
 ource);%0A
+        %7D%0A        return handled;%0A
     %7D%0A%7D%0A

Add configurable service to block access to classpath assets