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