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 String _path;
035
036 public void setPath(String path)
037 {
038 _path = path;
039 }
040
041 public void encode(ServiceEncoding encoding)
042 {
043 if (!encoding.getParameterValue(ServiceConstants.SERVICE).equals(Tapestry.ASSET_SERVICE))
044 return;
045
046 String path = encoding.getParameterValue(AssetService.PATH);
047 String digest = encoding.getParameterValue(AssetService.DIGEST);
048
049 // _path ends with a slash, path starts with one.
050
051 String fullPath = _path + ((digest != null) ? "/" + digest : "/" + DIGEST_STATIC) + path;
052
053 encoding.setServletPath(fullPath);
054 encoding.setParameterValue(AssetService.PATH, null);
055 encoding.setParameterValue(AssetService.DIGEST, null);
056 encoding.setParameterValue(ServiceConstants.SERVICE, null);
057 }
058
059 public void decode(ServiceEncoding encoding)
060 {
061 if (!encoding.getServletPath().equals(_path))
062 return;
063
064 String pathInfo = encoding.getPathInfo();
065
066 // The lead character is a slash, so find the next slash (the divider between the
067 // digest and the path).
068 int slashx = pathInfo.indexOf('/', 1);
069
070 encoding.setParameterValue(ServiceConstants.SERVICE, Tapestry.ASSET_SERVICE);
071 encoding.setParameterValue(AssetService.DIGEST, pathInfo.substring(1, slashx));
072 encoding.setParameterValue(AssetService.PATH, pathInfo.substring(slashx));
073 }
074
075 }