Coverage Report - org.apache.tapestry5.internal.services.AssetResourceLocatorImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AssetResourceLocatorImpl
100%
28/28
100%
10/10
0
 
 1  
 // Copyright 2009 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.internal.services;
 16  
 
 17  
 import org.apache.tapestry5.SymbolConstants;
 18  
 import org.apache.tapestry5.ioc.Resource;
 19  
 import org.apache.tapestry5.ioc.annotations.Inject;
 20  
 import org.apache.tapestry5.ioc.annotations.Symbol;
 21  
 import org.apache.tapestry5.ioc.internal.util.ClasspathResource;
 22  
 import org.apache.tapestry5.services.AssetFactory;
 23  
 import org.apache.tapestry5.services.ClasspathAssetAliasManager;
 24  
 import org.apache.tapestry5.services.ContextProvider;
 25  
 import org.apache.tapestry5.services.Response;
 26  
 
 27  
 import javax.servlet.http.HttpServletResponse;
 28  
 import java.io.IOException;
 29  
 
 30  
 public class AssetResourceLocatorImpl implements AssetResourceLocator
 31  
 {
 32  
 
 33  
     private final ClasspathAssetAliasManager aliasManager;
 34  
 
 35  
     private final ResourceCache resourceCache;
 36  
 
 37  
     private final AssetFactory contextAssetFactory;
 38  
 
 39  
     private final Response response;
 40  
 
 41  
     private final String applicationAssetPrefix;
 42  
 
 43  
     public AssetResourceLocatorImpl(ClasspathAssetAliasManager aliasManager,
 44  
 
 45  
                                     ResourceCache resourceCache,
 46  
 
 47  
                                     @Inject @Symbol(SymbolConstants.APPLICATION_VERSION)
 48  
                                     String applicationVersion,
 49  
 
 50  
                                     @ContextProvider
 51  
                                     AssetFactory contextAssetFactory,
 52  
 
 53  
                                     Response response)
 54  
 
 55  26
     {
 56  26
         this.aliasManager = aliasManager;
 57  26
         this.resourceCache = resourceCache;
 58  26
         this.contextAssetFactory = contextAssetFactory;
 59  26
         this.response = response;
 60  
 
 61  26
         applicationAssetPrefix = RequestConstants.ASSET_PATH_PREFIX + RequestConstants.CONTEXT_FOLDER + applicationVersion + "/";
 62  26
     }
 63  
 
 64  
     public Resource findResourceForPath(String path) throws IOException
 65  
     {
 66  252
         if (path.startsWith(applicationAssetPrefix))
 67  24
             return findContextResource(path.substring(applicationAssetPrefix.length()));
 68  
 
 69  228
         String resourcePath = aliasManager.toResourcePath(path);
 70  
 
 71  228
         Resource resource = new ClasspathResource(resourcePath);
 72  
 
 73  228
         if (!resourceCache.requiresDigest(resource)) return resource;
 74  
 
 75  8
         String file = resource.getFile();
 76  
 
 77  
         // Somehow this code got real ugly, but it's all about preventing NPEs when a resource
 78  
         // that should have a digest doesn't.
 79  
 
 80  8
         boolean valid = false;
 81  8
         Resource result = resource;
 82  
 
 83  8
         int lastdotx = file.lastIndexOf('.');
 84  
 
 85  8
         if (lastdotx > 0)
 86  
         {
 87  6
             int prevdotx = file.lastIndexOf('.', lastdotx - 1);
 88  
 
 89  6
             if (prevdotx > 0)
 90  
             {
 91  
 
 92  4
                 String requestDigest = file.substring(prevdotx + 1, lastdotx);
 93  
 
 94  
                 // Strip the digest out of the file name.
 95  
 
 96  4
                 String realFile = file.substring(0, prevdotx) + file.substring(lastdotx);
 97  
 
 98  4
                 result = resource.forFile(realFile);
 99  
 
 100  4
                 String actualDigest = resourceCache.getDigest(result);
 101  
 
 102  4
                 valid = requestDigest.equals(actualDigest);
 103  
             }
 104  
         }
 105  
 
 106  8
         if (valid) return result;
 107  
 
 108  
         // TODO: Perhaps we should send an exception here, so that the caller can decide
 109  
         // to send the error. I'm not happy with this.
 110  
         
 111  6
         response.sendError(HttpServletResponse.SC_FORBIDDEN,
 112  
                            ServicesMessages.wrongAssetDigest(result));
 113  
 
 114  6
         return null;
 115  
     }
 116  
 
 117  
     private Resource findContextResource(String contextPath)
 118  
     {
 119  24
         return contextAssetFactory.getRootResource().forFile(contextPath);
 120  
     }
 121  
 }