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;
018
019import org.apache.wicket.request.component.IRequestablePage;
020import org.apache.wicket.request.flow.ResetResponseException;
021import org.apache.wicket.core.request.handler.IPageProvider;
022import org.apache.wicket.core.request.handler.PageProvider;
023import org.apache.wicket.core.request.handler.RenderPageRequestHandler;
024import org.apache.wicket.core.request.handler.RenderPageRequestHandler.RedirectPolicy;
025import org.apache.wicket.request.mapper.parameter.PageParameters;
026
027/**
028 * Causes Wicket to interrupt current request processing and immediately respond with the specified
029 * page.
030 * 
031 * @author Igor Vaynberg (ivaynberg)
032 * @see NonResettingRestartException
033 */
034public class RestartResponseException extends ResetResponseException
035{
036        private static final long serialVersionUID = 1L;
037
038        /**
039         * Redirects to the specified bookmarkable page
040         * 
041         * @param <C>
042         *            The page type
043         * 
044         * @param pageClass
045         *            class of bookmarkable page
046         */
047        public <C extends Page> RestartResponseException(final Class<C> pageClass)
048        {
049                this(pageClass, null);
050        }
051
052        /**
053         * Redirects to the specified bookmarkable page with the given page parameters
054         * 
055         * @param <C>
056         *            The page type
057         * 
058         * @param pageClass
059         *            class of bookmarkable page
060         * @param params
061         *            bookmarkable page parameters
062         */
063        public <C extends Page> RestartResponseException(final Class<C> pageClass,
064                final PageParameters params)
065        {
066                this(new PageProvider(pageClass, params), RedirectPolicy.ALWAYS_REDIRECT);
067        }
068
069        /**
070         * Redirects to the specified page
071         * 
072         * @param page
073         *            redirect page
074         */
075        public RestartResponseException(final IRequestablePage page)
076        {
077                this(new PageProvider(makeStateful(page)), RedirectPolicy.AUTO_REDIRECT);
078        }
079
080        /**
081         * Redirects to the page provided by the passed {@code pageProvider} using the explicit
082         * {@code redirectPolicy}
083         * 
084         * @param pageProvider
085         *            the provider for the page
086         * @param redirectPolicy
087         *            the redirect policy to use
088         */
089        public RestartResponseException(final IPageProvider pageProvider,
090                final RedirectPolicy redirectPolicy)
091        {
092                super(new RenderPageRequestHandler(pageProvider, redirectPolicy));
093        }
094
095        /**
096         * Marks the page as stateful so that it is still available after a redirect.
097         * See WICKET-5078 and WICKET-3965
098         *
099         * @param page
100         *      The page to mark stateful
101         * @return The passed page.
102         *
103         */
104        private static IRequestablePage makeStateful(IRequestablePage page)
105        {
106                if (page instanceof Page)
107                {
108                        ((Page)page).setStatelessHint(false);
109                }
110                return page;
111        }
112}