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.tapestry.IRequestCycle;
022    import org.apache.tapestry.Tapestry;
023    import org.apache.tapestry.services.LinkFactory;
024    import org.apache.tapestry.services.ResetEventCoordinator;
025    import org.apache.tapestry.services.ResponseRenderer;
026    import org.apache.tapestry.services.ServiceConstants;
027    
028    /**
029     * ServiceLink used to discard all cached data (templates, specifications, et cetera). This is
030     * primarily used during development. It could be a weakness of a Tapestry application, making it
031     * susceptible to denial of service attacks, which is why it is disabled by default. The link
032     * generated by the ResetService redisplays the current page after discarding all data.
033     * 
034     * @author Howard Lewis Ship
035     * @since 1.0.9
036     */
037    
038    public class ResetService implements IEngineService
039    {
040        /** @since 4.0 */
041    
042        private ResponseRenderer _responseRenderer;
043    
044        /** @since 4.0 */
045    
046        private ResetEventCoordinator _resetEventCoordinator;
047    
048        /** @since 4.0 */
049        private boolean _enabled;
050    
051        /** @since 4.0 */
052    
053        private LinkFactory _linkFactory;
054    
055        public ILink getLink(IRequestCycle cycle, boolean post, Object parameter)
056        {
057            if (parameter != null)
058                throw new IllegalArgumentException(EngineMessages.serviceNoParameter(this));
059    
060            Map parameters = new HashMap();
061    
062            parameters.put(ServiceConstants.SERVICE, getName());
063            parameters.put(ServiceConstants.PAGE, cycle.getPage().getPageName());
064    
065            return _linkFactory.constructLink(cycle, post, parameters, true);
066        }
067    
068        public String getName()
069        {
070            return Tapestry.RESET_SERVICE;
071        }
072    
073        public void service(IRequestCycle cycle) throws IOException
074        {
075            String pageName = cycle.getParameter(ServiceConstants.PAGE);
076    
077            if (_enabled)
078                _resetEventCoordinator.fireResetEvent();
079    
080            cycle.activate(pageName);
081    
082            // Render the same page (that contained the reset link).
083    
084            _responseRenderer.renderResponse(cycle);
085        }
086    
087        /** @since 4.0 */
088        public void setResponseRenderer(ResponseRenderer responseRenderer)
089        {
090            _responseRenderer = responseRenderer;
091        }
092    
093        /** @since 4.0 */
094    
095        public void setResetEventCoordinator(ResetEventCoordinator resetEventCoordinator)
096        {
097            _resetEventCoordinator = resetEventCoordinator;
098        }
099    
100        /** @since 4.0 */
101    
102        public void setEnabled(boolean enabled)
103        {
104            _enabled = enabled;
105        }
106    
107        /** @since 4.0 */
108        public void setLinkFactory(LinkFactory linkFactory)
109        {
110            _linkFactory = linkFactory;
111        }
112    }