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.core.request.handler;
018
019import org.apache.wicket.Page;
020import org.apache.wicket.request.IRequestHandler;
021import org.apache.wicket.request.IRequestHandlerDelegate;
022import org.apache.wicket.request.component.IRequestablePage;
023
024/**
025 * Request handler that works with a page instance.
026 *
027 * @author Matej Knopp
028 */
029public interface IPageRequestHandler extends IPageClassRequestHandler
030{
031        /**
032         * Returns the page. Be aware that the page can be instantiated if this wasn't the case already.
033         *
034         * @return page instance
035         */
036        IRequestablePage getPage();
037
038        /**
039         * Returns the page id.
040         *
041         * @return page id
042         */
043        Integer getPageId();
044
045        /**
046         * Checks if the page instance is already created or if it will be created when
047         * {@link #getPage()} is called
048         * 
049         * @return {@code true} iff page instance is already created
050         */
051        boolean isPageInstanceCreated();
052
053        /**
054         * Returns the number of times this page has been rendered.
055         *
056         * @return the number of times this page has been rendered.
057         * @see IRequestablePage#getRenderCount()
058         */
059        Integer getRenderCount();
060        
061        /**
062         * Resolves a page instance from the request handler iff the page instance is already created
063         * 
064         * @param handler The request handler
065         * @return page or {@code null} if none
066         */
067        static Page getPage(IRequestHandler handler)
068        {
069                while (handler instanceof IRequestHandlerDelegate)
070                {
071                        handler = ((IRequestHandlerDelegate) handler).getDelegateHandler();
072                }
073
074                if (handler instanceof IPageRequestHandler)
075                {
076                        IPageRequestHandler pageHandler = (IPageRequestHandler) handler;
077                        if (pageHandler.isPageInstanceCreated())
078                        {
079                                return (Page) pageHandler.getPage();
080                        }
081                }
082                return null;
083        }
084}