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(boolean post, Object parameter)
044        {
045            Defense.isAssignable(parameter, String.class, "parameter");
046    
047            Map parameters = new HashMap();
048    
049            parameters.put(ServiceConstants.PAGE, parameter);
050    
051            return _linkFactory.constructLink(this, post, parameters, true);
052    
053        }
054    
055        public void service(IRequestCycle cycle) throws IOException
056        {
057            String pageName = cycle.getParameter(ServiceConstants.PAGE);
058    
059            // At one time, the page service required a session, but that is no longer necessary.
060            // Users can now bookmark pages within a Tapestry application. Pages
061            // can implement validate() and throw a PageRedirectException if they don't
062            // want to be accessed this way. For example, most applications have a concept
063            // of a "login" and have a few pages that don't require the user to be logged in,
064            // and other pages that do. The protected pages should redirect to a login page.
065    
066            cycle.activate(pageName);
067    
068            _responseRenderer.renderResponse(cycle);
069        }
070    
071        public String getName()
072        {
073            return Tapestry.PAGE_SERVICE;
074        }
075    
076        /** @since 4.0 */
077        public void setResponseRenderer(ResponseRenderer responseRenderer)
078        {
079            _responseRenderer = responseRenderer;
080        }
081    
082        /** @since 4.0 */
083        public void setLinkFactory(LinkFactory linkFactory)
084        {
085            _linkFactory = linkFactory;
086        }
087    }