001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.request.flow;
018
019import org.apache.wicket.request.ILogData;
020import org.apache.wicket.request.ILoggableRequestHandler;
021import org.apache.wicket.request.IRequestCycle;
022import org.apache.wicket.request.IRequestHandler;
023import org.apache.wicket.request.IRequestHandlerDelegate;
024import org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException;
025import org.apache.wicket.request.handler.logger.DelegateLogData;
026import org.apache.wicket.request.handler.logger.NoLogData;
027
028/**
029 * An exception that resets the response before executing the specified request handler
030 * 
031 * @author Igor Vaynberg (ivaynberg)
032 * @author Jonathan Locke
033 */
034public abstract class ResetResponseException extends ReplaceHandlerException
035{
036        private static final long serialVersionUID = 1L;
037
038        /**
039         * Construct.
040         * 
041         * @param handler
042         */
043        protected ResetResponseException(final IRequestHandler handler)
044        {
045                super(new ResponseResettingDecorator(handler), true);
046        }
047
048        private static class ResponseResettingDecorator
049                implements
050                        IRequestHandler,
051                        IRequestHandlerDelegate,
052                        ILoggableRequestHandler
053        {
054                private final IRequestHandler delegate;
055
056                private DelegateLogData logData;
057
058                /**
059                 * Construct.
060                 * 
061                 * @param delegate
062                 */
063                public ResponseResettingDecorator(final IRequestHandler delegate)
064                {
065                        this.delegate = delegate;
066                }
067
068                @Override
069                public void detach(final IRequestCycle requestCycle)
070                {
071                        delegate.detach(requestCycle);
072
073                        if (logData == null)
074                        {
075                                ILogData delegateData;
076                                if (delegate instanceof ILoggableRequestHandler)
077                                        delegateData = ((ILoggableRequestHandler)delegate).getLogData();
078                                else
079                                        delegateData = new NoLogData();
080                                logData = new DelegateLogData(delegateData);
081                        }
082                }
083
084                @Override
085                public void respond(final IRequestCycle requestCycle)
086                {
087                        requestCycle.getResponse().reset();
088                        delegate.respond(requestCycle);
089                }
090
091                @Override
092                public IRequestHandler getDelegateHandler()
093                {
094                        return delegate;
095                }
096
097                /** {@inheritDoc} */
098                @Override
099                public DelegateLogData getLogData()
100                {
101                        return logData;
102                }
103        }
104}