001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2016, 2018  Michael N. Lipp
004 *
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.osgi.demo.upnp;
020
021import java.util.Optional;
022import org.jgrapes.core.Channel;
023import org.jgrapes.core.Component;
024import org.jgrapes.core.annotation.Handler;
025import org.jgrapes.http.Session;
026import org.jgrapes.io.IOSubchannel;
027import org.jgrapes.webconsole.base.events.ConsoleConfigured;
028import org.jgrapes.webconsole.base.events.RenderConlet;
029
030/**
031 * 
032 */
033public class NewConsoleSessionPolicy extends Component {
034
035    private final String renderedFlagName = getClass().getName() + ".rendered";
036
037    /**
038     * Creates a new component with its channel set to itself.
039     */
040    public NewConsoleSessionPolicy() {
041        // Everything done by super.
042    }
043
044    /**
045     * Creates a new component with its channel set to the given channel.
046     * 
047     * @param componentChannel the component channel
048     */
049    public NewConsoleSessionPolicy(Channel componentChannel) {
050        super(componentChannel);
051    }
052
053    /**
054     * Handle render portlet requests.
055     *
056     * @param event the event
057     * @param channel the channel
058     */
059    @Handler
060    public void onRenderPortlet(RenderConlet event, IOSubchannel channel) {
061        channel.associated(Session.class)
062            .ifPresent(session -> session.put(renderedFlagName, true));
063    }
064
065    /**
066     * Check layout.
067     *
068     * @param event the event
069     * @param channel the channel
070     * @throws InterruptedException the interrupted exception
071     */
072    @Handler
073    @SuppressWarnings("PMD.CollapsibleIfStatements")
074    public void onPortalConfigured(ConsoleConfigured event,
075            IOSubchannel channel)
076            throws InterruptedException {
077        Optional<Session> optSession = channel.associated(Session.class);
078        if (optSession.isPresent()) {
079            if ((Boolean) optSession.get().getOrDefault(
080                renderedFlagName, false)) {
081                return;
082            }
083        }
084    }
085
086}