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.pageStore;
018
019import java.io.Serializable;
020import java.util.function.Supplier;
021
022import org.apache.wicket.MetaDataKey;
023import org.apache.wicket.Session;
024import org.apache.wicket.page.IManageablePage;
025import org.apache.wicket.request.cycle.RequestCycle;
026
027/**
028 * Context of a {@link IManageablePage} when it is stored in an {@link IPageStore}, decoupling it
029 * from request cycle and session.
030 * 
031 * @author Matej Knopp
032 * @author svenmeier
033 */
034public interface IPageContext
035{
036        /**
037         * Get data from the current request cycle.
038         * 
039         * @param key
040         *            key
041         * @param defaultValue
042         *            default value to use if not present
043         * 
044         * @see RequestCycle#getMetaData(MetaDataKey)
045         */
046        <T> T getRequestData(MetaDataKey<T> key, Supplier<T> defaultValue);
047
048        /**
049         * Get an attribute from the session. <br>
050         * Binds the session if not already set <em>and</em> supplier is not <code>null</code>.
051         * Sets the session attribute if supplier is not <code>null</code>.
052         * 
053         * @param key
054         *            key
055         * @param defaultValue
056         *            default value to use if not present
057         * @return value
058         * 
059         * @see Session#getAttribute(String)
060         */
061        <T extends Serializable> T getSessionAttribute(String key, Supplier<T> defaultValue);
062
063        /**
064         * Get metadata from the session. <br>
065         * Binds the session if not already set <em>and</em> supplier is not <code>null</code>.
066         * Sets the session attribute if supplier is not <code>null</code>.
067         *
068         * @param key
069         *            key
070         * @param defaultValue
071         *            optional supplier of a default value to use if not present
072         * @return value
073         * 
074         * @see Session#getMetaData(MetaDataKey)
075         */
076        <T extends Serializable> T getSessionData(MetaDataKey<T> key, Supplier<T> defaultValue);
077
078        /**
079         * Get the identifier of the session.
080         * 
081         * @param bind
082         *            should the session be bound
083         * @return session id, might be <code>null</code> if not bound yet
084         */
085        String getSessionId(boolean bind);
086}