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