001    // Copyright 2005 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.tapestry.engine.encoders;
016    
017    import org.apache.tapestry.Tapestry;
018    import org.apache.tapestry.asset.AssetService;
019    import org.apache.tapestry.engine.ServiceEncoder;
020    import org.apache.tapestry.engine.ServiceEncoding;
021    import org.apache.tapestry.services.ServiceConstants;
022    
023    /**
024     * Encoder for the {@link org.apache.tapestry.asset.AssetService} that uses servlet path info
025     * to store the resource digest and the path to the resource.
026     * 
027     * @author Howard M. Lewis Ship
028     * @since 4.0
029     */
030    public class AssetEncoder implements ServiceEncoder
031    {
032        public static final String DIGEST_STATIC = "static";
033        
034        private static final String PATH_SEPARATOR = "/";
035        
036        private String _path;
037        
038        public void setPath(String path)
039        {
040            _path = path;
041        }
042        
043        public void encode(ServiceEncoding encoding)
044        {
045            if (!encoding.getParameterValue(ServiceConstants.SERVICE).equals(Tapestry.ASSET_SERVICE))
046                return;
047            
048            String path = encoding.getParameterValue(AssetService.PATH);
049            String digest = encoding.getParameterValue(AssetService.DIGEST);
050            
051            // fix broken path if doesn't start with / 
052             
053            if (!path.startsWith(PATH_SEPARATOR))
054                path = PATH_SEPARATOR + path;
055            
056            // _path ends with a slash, path starts with one.
057            
058            String fullPath = _path + ((digest != null) ? "/" + digest : "/" + DIGEST_STATIC) + path;
059            
060            encoding.setServletPath(fullPath);
061            encoding.setParameterValue(AssetService.PATH, null);
062            encoding.setParameterValue(AssetService.DIGEST, null);
063            encoding.setParameterValue(ServiceConstants.SERVICE, null);
064        }
065    
066        public void decode(ServiceEncoding encoding)
067        {
068            if (!encoding.getServletPath().equals(_path))
069                return;
070            
071            String pathInfo = encoding.getPathInfo();
072            
073            // The lead character is a slash, so find the next slash (the divider between the
074            // digest and the path).
075            int slashx = pathInfo.indexOf('/', 1);
076            
077            encoding.setParameterValue(ServiceConstants.SERVICE, Tapestry.ASSET_SERVICE);
078            encoding.setParameterValue(AssetService.DIGEST, pathInfo.substring(1, slashx));
079            encoding.setParameterValue(AssetService.PATH, pathInfo.substring(slashx));
080        }
081        
082    }