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;
016    
017    import java.io.IOException;
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.apache.hivemind.util.Defense;
022    import org.apache.tapestry.IRequestCycle;
023    import org.apache.tapestry.Tapestry;
024    import org.apache.tapestry.services.LinkFactory;
025    import org.apache.tapestry.services.ResponseRenderer;
026    import org.apache.tapestry.services.ServiceConstants;
027    
028    /**
029     * Basic server for creating a link to another page in the application.
030     * 
031     * @author Howard Lewis Ship
032     * @since 1.0.9
033     */
034    
035    public class PageService implements IEngineService
036    {
037        /** @since 4.0 */
038        private ResponseRenderer _responseRenderer;
039    
040        /** @since 4.0 */
041        private LinkFactory _linkFactory;
042    
043        public ILink getLink(IRequestCycle cycle, boolean post, Object parameter)
044        {
045            Defense.isAssignable(parameter, String.class, "parameter");
046    
047            Map parameters = new HashMap();
048    
049            parameters.put(ServiceConstants.SERVICE, getName());
050            parameters.put(ServiceConstants.PAGE, parameter);
051    
052            return _linkFactory.constructLink(cycle, post, parameters, true);
053    
054        }
055    
056        public void service(IRequestCycle cycle) throws IOException
057        {
058            String pageName = cycle.getParameter(ServiceConstants.PAGE);
059    
060            // At one time, the page service required a session, but that is no longer necessary.
061            // Users can now bookmark pages within a Tapestry application. Pages
062            // can implement validate() and throw a PageRedirectException if they don't
063            // want to be accessed this way. For example, most applications have a concept
064            // of a "login" and have a few pages that don't require the user to be logged in,
065            // and other pages that do. The protected pages should redirect to a login page.
066    
067            cycle.activate(pageName);
068    
069            _responseRenderer.renderResponse(cycle);
070        }
071    
072        public String getName()
073        {
074            return Tapestry.PAGE_SERVICE;
075        }
076    
077        /** @since 4.0 */
078        public void setResponseRenderer(ResponseRenderer responseRenderer)
079        {
080            _responseRenderer = responseRenderer;
081        }
082    
083        /** @since 4.0 */
084        public void setLinkFactory(LinkFactory linkFactory)
085        {
086            _linkFactory = linkFactory;
087        }
088    }