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;
022
023import org.jgrapes.core.Channel;
024import org.jgrapes.core.Component;
025import org.jgrapes.core.annotation.Handler;
026import org.jgrapes.http.Session;
027import org.jgrapes.io.IOSubchannel;
028import org.jgrapes.portal.base.events.PortalConfigured;
029import org.jgrapes.portal.base.events.RenderPortlet;
030
031/**
032 * 
033 */
034public class NewPortalSessionPolicy extends Component {
035
036    private final String renderedFlagName = getClass().getName() + ".rendered";
037
038    /**
039     * Creates a new component with its channel set to itself.
040     */
041    public NewPortalSessionPolicy() {
042        // Everything done by super.
043    }
044
045    /**
046     * Creates a new component with its channel set to the given channel.
047     * 
048     * @param componentChannel the component channel
049     */
050    public NewPortalSessionPolicy(Channel componentChannel) {
051        super(componentChannel);
052    }
053
054    /**
055     * Handle render portlet requests.
056     *
057     * @param event the event
058     * @param channel the channel
059     */
060    @Handler
061    public void onRenderPortlet(RenderPortlet event, IOSubchannel channel) {
062        channel.associated(Session.class)
063            .ifPresent(session -> session.put(renderedFlagName, true));
064    }
065
066    /**
067     * Check layout.
068     *
069     * @param event the event
070     * @param channel the channel
071     * @throws InterruptedException the interrupted exception
072     */
073    @Handler
074    @SuppressWarnings("PMD.CollapsibleIfStatements")
075    public void onPortalConfigured(PortalConfigured event, 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}