Coverage Report - org.apache.tapestry5.internal.services.AssetDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
AssetDispatcher
92%
22/24
90%
9/10
6
 
 1  
 // Copyright 2006, 2008, 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.ioc.Resource;
 18  
 import org.apache.tapestry5.services.ClasspathAssetAliasManager;
 19  
 import org.apache.tapestry5.services.Dispatcher;
 20  
 import org.apache.tapestry5.services.Request;
 21  
 import org.apache.tapestry5.services.Response;
 22  
 
 23  
 import javax.servlet.http.HttpServletResponse;
 24  
 import java.io.IOException;
 25  
 
 26  
 /**
 27  
  * Recognizes requests where the path begins with "/asset/" and delivers the content therein as a bytestream. Also
 28  
  * handles requests that are simply polling for a change to the file.
 29  
  *
 30  
  * @see ResourceStreamer
 31  
  * @see ClasspathAssetAliasManager
 32  
  * @see ResourceCache
 33  
  */
 34  
 public class AssetDispatcher implements Dispatcher
 35  
 {
 36  
     private final ResourceStreamer streamer;
 37  
 
 38  
     private final ResourceCache resourceCache;
 39  
 
 40  
     private final AssetResourceLocator assetResourceLocator;
 41  
 
 42  
     static final String IF_MODIFIED_SINCE_HEADER = "If-Modified-Since";
 43  
 
 44  
     public AssetDispatcher(ResourceStreamer streamer,
 45  
 
 46  
                            ResourceCache resourceCache,
 47  
 
 48  
                            AssetResourceLocator assetResourceLocator)
 49  74
     {
 50  74
         this.streamer = streamer;
 51  74
         this.resourceCache = resourceCache;
 52  74
         this.assetResourceLocator = assetResourceLocator;
 53  
 
 54  74
     }
 55  
 
 56  
     public boolean dispatch(Request request, Response response) throws IOException
 57  
     {
 58  1614
         String path = request.getPath();
 59  
 
 60  
         // Remember that the request path does not include the context path, so we can simply start
 61  
         // looking for the asset path prefix right off the bat.
 62  
 
 63  1614
         if (!path.startsWith(RequestConstants.ASSET_PATH_PREFIX)) return false;
 64  
 
 65  
         // ClassLoaders like their paths to start with a leading slash.
 66  
 
 67  110
         Resource resource = assetResourceLocator.findResourceForPath(path);
 68  
 
 69  110
         if (resource == null) return true;
 70  
 
 71  104
         if (!resource.exists())
 72  
         {
 73  0
             response.sendError(HttpServletResponse.SC_NOT_FOUND, ServicesMessages
 74  
                     .assetDoesNotExist(resource));
 75  0
             return true;
 76  
         }
 77  
 
 78  104
         long ifModifiedSince = 0;
 79  
 
 80  
         try
 81  
         {
 82  104
             ifModifiedSince = request.getDateHeader(IF_MODIFIED_SINCE_HEADER);
 83  
         }
 84  2
         catch (IllegalArgumentException ex)
 85  
         {
 86  
             // Simulate the header being missing if it is poorly formatted.
 87  
 
 88  2
             ifModifiedSince = -1;
 89  102
         }
 90  
 
 91  104
         if (ifModifiedSince > 0)
 92  
         {
 93  4
             long modified = resourceCache.getTimeModified(resource);
 94  
 
 95  4
             if (ifModifiedSince >= modified)
 96  
             {
 97  2
                 response.sendError(HttpServletResponse.SC_NOT_MODIFIED, "");
 98  2
                 return true;
 99  
             }
 100  
         }
 101  
 
 102  102
         streamer.streamResource(resource);
 103  
 
 104  102
         return true;
 105  
     }
 106  
 }