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.engine.ServiceEncoder;
018    import org.apache.tapestry.engine.ServiceEncoding;
019    import org.apache.tapestry.services.ServiceConstants;
020    
021    /**
022     * Encodes the service name, typical output is "/page.svc" where "page" is a
023     * service name. This is useful for the home and restart services, for example.
024     * This encoder should be prioritized very low so that it doesn't prevent other
025     * encoders from doing their work.
026     * 
027     * @author Howard M. Lewis Ship
028     * @since 4.0
029     */
030    public class ServiceExtensionEncoder implements ServiceEncoder
031    {
032    
033        private String _extension;
034    
035        public void setExtension(String extension)
036        {
037            _extension = extension;
038        }
039    
040        public void encode(ServiceEncoding encoding)
041        {
042            String service = encoding.getParameterValue(ServiceConstants.SERVICE);
043    
044            // Can assume this is never null.
045    
046            encoding.setServletPath("/" + service + "." + _extension);
047            encoding.setParameterValue(ServiceConstants.SERVICE, null);
048        }
049    
050        public void decode(ServiceEncoding encoding)
051        {
052            String servletPath = encoding.getServletPath();
053    
054            int dotx = servletPath.lastIndexOf('.');
055    
056            String extension = servletPath.substring(dotx + 1);
057    
058            if (!extension.equals(_extension)) return;
059    
060            // The first character should be a slash, then the service name, then
061            // the dot.
062    
063            String service = servletPath.substring(1, dotx);
064    
065            encoding.setParameterValue(ServiceConstants.SERVICE, service);
066        }
067    
068    }