/*
* WebFlow Navigation Manager: webflow definiton, server side navigation history and automatic session cleaning.
* Distributed under LGPL license at web site http://wfnm.sourceforge.net .
*/
package net.sf.wfnm.web;
import net.sf.wfnm.AttributeContainer;
import net.sf.wfnm.Config;
import net.sf.wfnm.ConfigException;
import net.sf.wfnm.LogHelper;
import net.sf.wfnm.NavigationContext;
import net.sf.wfnm.NavigationContextFactory;
import net.sf.wfnm.NavigationManager;
import net.sf.wfnm.NavigationManagerFactory;
import net.sf.wfnm.StatsFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* A factory that notify to the navigation context that a page has been reached.
*
* @author <a href="mailto:malbari@users.sourceforge.net">Maurizio Albari</a>
* @version 1.0.6
*/
public class NotifyFactory {
/**
* The log
*/
private static final Log log = LogFactory.getLog(NotifyFactory.class);
/**
* The unique instance of this factory
*/
private static NotifyFactory instance = new NotifyFactory();
/**
* The url calculator instance
*/
private UrlCalculator urlCalculator;
/**
* Creates a new NotifyFactory object.
*/
private NotifyFactory() {
Class calcClass = Config.getInstance().getUrlCalculator();
if (!UrlCalculator.class.isAssignableFrom(calcClass)) {
throw new ConfigException("Class '" + calcClass + "' cannot be assigned to interface '" +
UrlCalculator.class.getName() + "'");
}
try {
urlCalculator = (UrlCalculator) calcClass.newInstance();
} catch (Exception e) {
throw new ConfigException("Unable to instantiate the class '" + calcClass.getName() + "'", e);
}
}
/**
* Returns the unique instance of the NotifyFactory.
*
* @return the unique instance of the NotifyFactory
*/
public static NotifyFactory getInstance() {
return instance;
}
/**
* Notify that a page has been reached.
*
* @param request the http request
*/
public static void notifyPage(HttpServletRequest request) {
getInstance().notifyReachedPage(request);
}
/**
* Reset the WFNM framework.
*
* @param session the http session
*/
public static void resetFramework(HttpSession session) {
if (Config.getInstance().isEnabled()) {
AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(session);
NavigationManagerFactory.resetInstance(container);
}
}
/**
* Simulates the notification of a specified previous page.
*
* @param session the http session
* @param url the url of the page
*/
public static void simulateNotifyPreviousPage(HttpSession session, String url) {
if ((url != null) && Config.getInstance().isEnabled()) {
AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(session);
NavigationManager navigationManager = NavigationManagerFactory.getInstance(container);
if (navigationManager.isPageVisited(url)) {
navigationManager.notifyPage(container, url, null, null);
}
}
}
/**
* Simulates the notification of the previous page.
*
* @param session the http session
*/
public static void simulateNotifyPreviousPage(HttpSession session) {
if (Config.getInstance().isEnabled()) {
AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(session);
NavigationManager navigationManager = NavigationManagerFactory.getInstance(container);
String url = navigationManager.getPreviousPage();
if ((url != null) && navigationManager.isPageVisited(url)) {
navigationManager.notifyPage(container, url, null, null);
}
}
}
/**
* Simulates the notification of a previous webflow.
*
* @param session the http session
*/
public static void simulateNotifyPreviousWebflow(HttpSession session) {
if (Config.getInstance().isEnabled()) {
AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(session);
NavigationManager navigationManager = NavigationManagerFactory.getInstance(container);
String url = navigationManager.getPreviousWebflow();
if (url != null) {
navigationManager.notifyPage(container, url, null, null);
}
}
}
/**
* Simulates the notification of a specified previous webflow.
*
* @param session the http session
* @param webflowName the previous webflow
*/
public static void simulateNotifyPreviousWebflow(HttpSession session, String webflowName) {
if (Config.getInstance().isEnabled()) {
AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(session);
NavigationManager navigationManager = NavigationManagerFactory.getInstance(container);
String url = navigationManager.getPreviousWebflow(webflowName);
if (url != null) {
navigationManager.notifyPage(container, url, null, null);
}
}
}
/**
* Notify to the WFNM framework that a page has been reached
*
* @param request the HttpServletRequest
*/
protected void notifyReachedPage(HttpServletRequest request) {
NavigationContext navigationContext = NavigationContextFactory.getInstance();
try {
if (!navigationContext.isIgnorePage()) {
AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(request.getSession());
if (Config.getInstance().isEnabled()) {
if (navigationContext.isFrameworkToReset()) {
NavigationManagerFactory.resetInstance(container);
} else {
navigationContext.notifyPage(container, urlCalculator.calculateUrl(request));
}
}
if (Config.getInstance().isStatsEnabled()) {
StatsFactory.getInstance().logStatistic(container);
}
LogHelper.logWebflowStack(container);
}
} finally {
NavigationContextFactory.restoreInstance();
}
}
}