/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.myfaces.orchestra.conversation.jsf;
import org.apache.myfaces.orchestra.conversation.Conversation;
import org.apache.myfaces.orchestra.conversation.ConversationFlashLifetimeAspect;
import org.apache.myfaces.orchestra.conversation.ConversationManager;
import org.apache.myfaces.orchestra.conversation.FlashScopeManager;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import java.util.Iterator;
import java.util.Set;
/**
* Handle Flash conversations.
* <p>
* At the end of request processing, delete any flash-scope conversations for which no
* bean in that scope has been accessed during the request.
*/
public class FlashScopePhaseListener implements PhaseListener
{
private static final long serialVersionUID = 1L;
public void afterPhase(PhaseEvent event)
{
if (PhaseId.RENDER_RESPONSE.equals(event.getPhaseId()))
{
invalidateFlashConversations(event.getFacesContext().getViewRoot().getViewId());
}
}
public void beforePhase(PhaseEvent event)
{
}
/**
* Invalidates any conversation with aspect {@link ConversationFlashLifetimeAspect}
* which has not been accessed during a http request
*/
protected void invalidateFlashConversations(String viewId)
{
FlashScopeManager flashManager = FlashScopeManager.getInstance();
if (flashManager.isIgnoreRequest())
{
return;
}
if (flashManager.getFlashScopeManagerConfiguration() != null)
{
Set ignoredViewIds = flashManager.getFlashScopeManagerConfiguration().getIgnoreViewIds();
if (ignoredViewIds != null && ignoredViewIds.contains(viewId))
{
// The flash configuration has explicitly stated that no conversations should be
// terminated when processing this specific view, so just return.
//
// Special "ignored views" are useful when dealing with things like nested
// frames within a page that periodically refresh themselves while the "main"
// part of the page remains unsubmitted.
return;
}
}
ConversationManager conversationManager = ConversationManager.getInstance(false);
if (conversationManager == null)
{
return;
}
Iterator iterConversations = conversationManager.iterateConversations();
while (iterConversations.hasNext())
{
Conversation conversation = (Conversation) iterConversations.next();
// This conversation has "flash" scope if it has an attached Aspect
// of type ConversationFlashLifetimeAspect. All other conversations
// are not flash-scoped and should be ignored here.
ConversationFlashLifetimeAspect aspect =
(ConversationFlashLifetimeAspect)
conversation.getAspect(ConversationFlashLifetimeAspect.class);
if (aspect != null && !aspect.isAccessed())
{
conversation.invalidate();
}
}
}
public PhaseId getPhaseId()
{
return PhaseId.RENDER_RESPONSE;
}
}