Coverage Report - org.apache.tapestry.engine.encoders.AssetEncoder
 
Classes in this File Line Coverage Branch Coverage Complexity
AssetEncoder
0%
0/28
0%
0/12
3.667
 
 1  
 // Copyright 2005 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.tapestry.engine.encoders;
 16  
 
 17  
 import org.apache.tapestry.Tapestry;
 18  
 import org.apache.tapestry.asset.AssetService;
 19  
 import org.apache.tapestry.engine.ServiceEncoder;
 20  
 import org.apache.tapestry.engine.ServiceEncoding;
 21  
 import org.apache.tapestry.services.ServiceConstants;
 22  
 
 23  
 /**
 24  
  * Encoder for the {@link org.apache.tapestry.asset.AssetService} that uses servlet path info
 25  
  * to store the resource digest and the path to the resource.
 26  
  * 
 27  
  * @author Howard M. Lewis Ship
 28  
  * @since 4.0
 29  
  */
 30  0
 public class AssetEncoder implements ServiceEncoder
 31  
 {
 32  
     public static final String DIGEST_STATIC = "static";
 33  
     
 34  
     private static final String PATH_SEPARATOR = "/";
 35  
     
 36  
     private String _path;
 37  
     
 38  
     public void setPath(String path)
 39  
     {
 40  0
         _path = path;
 41  0
     }
 42  
     
 43  
     public void encode(ServiceEncoding encoding)
 44  
     {
 45  0
         if (!encoding.getParameterValue(ServiceConstants.SERVICE).equals(Tapestry.ASSET_SERVICE))
 46  0
             return;
 47  
         
 48  0
         String path = encoding.getParameterValue(AssetService.PATH);
 49  0
         String digest = encoding.getParameterValue(AssetService.DIGEST);
 50  
         
 51  
         // fix broken path if doesn't start with / 
 52  
          
 53  0
         if (!path.startsWith(PATH_SEPARATOR))
 54  0
             path = PATH_SEPARATOR + path;
 55  
         
 56  
         // _path ends with a slash, path starts with one.
 57  
         
 58  0
         String fullPath = _path + ((digest != null) ? "/" + digest : "/" + DIGEST_STATIC) + path;
 59  
         
 60  0
         encoding.setServletPath(fullPath);
 61  0
         encoding.setParameterValue(AssetService.PATH, null);
 62  0
         encoding.setParameterValue(AssetService.DIGEST, null);
 63  0
         encoding.setParameterValue(ServiceConstants.SERVICE, null);
 64  0
     }
 65  
 
 66  
     public void decode(ServiceEncoding encoding)
 67  
     {
 68  0
         if (!encoding.getServletPath().equals(_path))
 69  0
             return;
 70  0
         encoding.setParameterValue(ServiceConstants.SERVICE, Tapestry.ASSET_SERVICE);
 71  0
         String pathInfo = encoding.getPathInfo();
 72  0
         if (pathInfo == null)
 73  0
             pathInfo = "/";
 74  
         
 75  
         // The lead character is a slash, so find the next slash (the divider between the
 76  
         // digest and the path).
 77  0
         int slashx = pathInfo.indexOf('/', 1);
 78  0
         if (slashx < 0) 
 79  
         {
 80  0
             encoding.setParameterValue(AssetService.DIGEST, "");
 81  0
             encoding.setParameterValue(AssetService.PATH, "");
 82  
         }
 83  
         else
 84  
         {
 85  0
             encoding.setParameterValue(AssetService.DIGEST, pathInfo.substring(1, slashx));
 86  0
             encoding.setParameterValue(AssetService.PATH, pathInfo.substring(slashx));
 87  
         }
 88  0
     }
 89  
     
 90  
 }