001    // Copyright 2004, 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.INamespace;
018    import org.apache.tapestry.engine.ServiceEncoder;
019    import org.apache.tapestry.engine.ServiceEncoding;
020    import org.apache.tapestry.services.ServiceConstants;
021    
022    /**
023     * The canonical implementation of {@link org.apache.tapestry.engine.ServiceEncoder}, it encodes
024     * page name and a service name. The page name becomes the servlet path, prefixed with "/" and
025     * suffixed with a dot and a particular extension. In this way, "/app?service=page&page=Home"
026     * becomes simply "Home.html". This is most suitable for the "page" and "external" services.
027     * 
028     * @author Howard M. Lewis Ship
029     * @since 4.0
030     */
031    public class PageServiceEncoder implements ServiceEncoder
032    {
033        private String _extension;
034    
035        private String _serviceName;
036    
037        public void encode(ServiceEncoding encoding)
038        {
039            String service = encoding.getParameterValue(ServiceConstants.SERVICE);
040    
041            if (!service.equals(_serviceName))
042                return;
043    
044            String pageName = encoding.getParameterValue(ServiceConstants.PAGE);
045    
046            // Only handle pages in the application namespace (not from a library).
047    
048            if (pageName.indexOf(INamespace.SEPARATOR) >= 0)
049                return;
050    
051            StringBuffer buffer = new StringBuffer("/");
052            buffer.append(pageName);
053            buffer.append('.');
054            buffer.append(_extension);
055    
056            encoding.setServletPath(buffer.toString());
057    
058            encoding.setParameterValue(ServiceConstants.SERVICE, null);
059            encoding.setParameterValue(ServiceConstants.PAGE, null);
060        }
061    
062        public void decode(ServiceEncoding encoding)
063        {
064            String servletPath = encoding.getServletPath();
065    
066            int dotx = servletPath.lastIndexOf('.');
067            if (dotx < 0)
068                return;
069    
070            String extension = servletPath.substring(dotx + 1);
071    
072            if (!extension.equals(_extension))
073                return;
074    
075            // Skip the slash and the dot.
076    
077            String page = servletPath.substring(1, dotx);
078    
079            encoding.setParameterValue(ServiceConstants.SERVICE, _serviceName);
080            encoding.setParameterValue(ServiceConstants.PAGE, page);
081        }
082    
083        public void setExtension(String extension)
084        {
085            _extension = extension;
086        }
087    
088        public void setServiceName(String serviceName)
089        {
090            _serviceName = serviceName;
091        }
092    }