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