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.ResetEventHub;
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 ResetEventHub _resetEventHub;
047    
048        /** @since 4.0 */
049        private boolean _enabled;
050    
051        /** @since 4.0 */
052    
053        private LinkFactory _linkFactory;
054    
055        /** @since 4.0 */
056        private IRequestCycle _requestCycle;
057    
058        public ILink getLink(boolean post, Object parameter)
059        {
060            if (parameter != null)
061                throw new IllegalArgumentException(EngineMessages.serviceNoParameter(this));
062    
063            Map parameters = new HashMap();
064    
065            parameters.put(ServiceConstants.PAGE, _requestCycle.getPage().getPageName());
066    
067            return _linkFactory.constructLink(this, post, parameters, true);
068        }
069    
070        public String getName()
071        {
072            return Tapestry.RESET_SERVICE;
073        }
074    
075        public void service(IRequestCycle cycle) throws IOException
076        {
077            String pageName = cycle.getParameter(ServiceConstants.PAGE);
078    
079            if (_enabled)
080                _resetEventHub.fireResetEvent();
081    
082            cycle.activate(pageName);
083    
084            // Render the same page (that contained the reset link).
085    
086            _responseRenderer.renderResponse(cycle);
087        }
088    
089        /** @since 4.0 */
090        public void setResponseRenderer(ResponseRenderer responseRenderer)
091        {
092            _responseRenderer = responseRenderer;
093        }
094    
095        /** @since 4.0 */
096    
097        public void setResetEventHub(ResetEventHub resetEventHub)
098        {
099            _resetEventHub = resetEventHub;
100        }
101    
102        /** @since 4.0 */
103    
104        public void setEnabled(boolean enabled)
105        {
106            _enabled = enabled;
107        }
108    
109        /** @since 4.0 */
110        public void setLinkFactory(LinkFactory linkFactory)
111        {
112            _linkFactory = linkFactory;
113        }
114    
115        /** @since 4.0 */
116        public void setRequestCycle(IRequestCycle requestCycle)
117        {
118            _requestCycle = requestCycle;
119        }
120    }