1
2
3
4
5 package net.sf.wfnm.web;
6
7 import net.sf.wfnm.AttributeContainer;
8 import net.sf.wfnm.Config;
9 import net.sf.wfnm.ConfigException;
10 import net.sf.wfnm.LogHelper;
11 import net.sf.wfnm.NavigationContext;
12 import net.sf.wfnm.NavigationContextFactory;
13 import net.sf.wfnm.NavigationManager;
14 import net.sf.wfnm.NavigationManagerFactory;
15 import net.sf.wfnm.StatsFactory;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpSession;
22
23
24 /***
25 * A factory that notify to the navigation context that a page has been reached.
26 *
27 * @author <a href="mailto:malbari@users.sourceforge.net">Maurizio Albari</a>
28 * @version 1.0.6
29 */
30 public class NotifyFactory {
31 /***
32 * The log
33 */
34 private static final Log log = LogFactory.getLog(NotifyFactory.class);
35
36 /***
37 * The unique instance of this factory
38 */
39 private static NotifyFactory instance = new NotifyFactory();
40
41 /***
42 * The url calculator instance
43 */
44 private UrlCalculator urlCalculator;
45
46 /***
47 * Creates a new NotifyFactory object.
48 */
49 private NotifyFactory() {
50 Class calcClass = Config.getInstance().getUrlCalculator();
51
52 if (!UrlCalculator.class.isAssignableFrom(calcClass)) {
53 throw new ConfigException("Class '" + calcClass + "' cannot be assigned to interface '" +
54 UrlCalculator.class.getName() + "'");
55 }
56
57 try {
58 urlCalculator = (UrlCalculator) calcClass.newInstance();
59 } catch (Exception e) {
60 throw new ConfigException("Unable to instantiate the class '" + calcClass.getName() + "'", e);
61 }
62 }
63
64 /***
65 * Returns the unique instance of the NotifyFactory.
66 *
67 * @return the unique instance of the NotifyFactory
68 */
69 public static NotifyFactory getInstance() {
70 return instance;
71 }
72
73 /***
74 * Notify that a page has been reached.
75 *
76 * @param request the http request
77 */
78 public static void notifyPage(HttpServletRequest request) {
79 getInstance().notifyReachedPage(request);
80 }
81
82 /***
83 * Reset the WFNM framework.
84 *
85 * @param session the http session
86 */
87 public static void resetFramework(HttpSession session) {
88 if (Config.getInstance().isEnabled()) {
89 AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(session);
90 NavigationManagerFactory.resetInstance(container);
91 }
92 }
93
94 /***
95 * Simulates the notification of a specified previous page.
96 *
97 * @param session the http session
98 * @param url the url of the page
99 */
100 public static void simulateNotifyPreviousPage(HttpSession session, String url) {
101 if ((url != null) && Config.getInstance().isEnabled()) {
102 AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(session);
103 NavigationManager navigationManager = NavigationManagerFactory.getInstance(container);
104
105 if (navigationManager.isPageVisited(url)) {
106 navigationManager.notifyPage(container, url, null, null);
107 }
108 }
109 }
110
111 /***
112 * Simulates the notification of the previous page.
113 *
114 * @param session the http session
115 */
116 public static void simulateNotifyPreviousPage(HttpSession session) {
117 if (Config.getInstance().isEnabled()) {
118 AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(session);
119 NavigationManager navigationManager = NavigationManagerFactory.getInstance(container);
120 String url = navigationManager.getPreviousPage();
121
122 if ((url != null) && navigationManager.isPageVisited(url)) {
123 navigationManager.notifyPage(container, url, null, null);
124 }
125 }
126 }
127
128 /***
129 * Simulates the notification of a previous webflow.
130 *
131 * @param session the http session
132 */
133 public static void simulateNotifyPreviousWebflow(HttpSession session) {
134 if (Config.getInstance().isEnabled()) {
135 AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(session);
136 NavigationManager navigationManager = NavigationManagerFactory.getInstance(container);
137 String url = navigationManager.getPreviousWebflow();
138
139 if (url != null) {
140 navigationManager.notifyPage(container, url, null, null);
141 }
142 }
143 }
144
145 /***
146 * Simulates the notification of a specified previous webflow.
147 *
148 * @param session the http session
149 * @param webflowName the previous webflow
150 */
151 public static void simulateNotifyPreviousWebflow(HttpSession session, String webflowName) {
152 if (Config.getInstance().isEnabled()) {
153 AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(session);
154 NavigationManager navigationManager = NavigationManagerFactory.getInstance(container);
155 String url = navigationManager.getPreviousWebflow(webflowName);
156
157 if (url != null) {
158 navigationManager.notifyPage(container, url, null, null);
159 }
160 }
161 }
162
163 /***
164 * Notify to the WFNM framework that a page has been reached
165 *
166 * @param request the HttpServletRequest
167 */
168 protected void notifyReachedPage(HttpServletRequest request) {
169 NavigationContext navigationContext = NavigationContextFactory.getInstance();
170
171 try {
172 if (!navigationContext.isIgnorePage()) {
173 AttributeContainer container = (AttributeContainer) HttpSessionWrapper.wrapItIfNecessary(request.getSession());
174
175 if (Config.getInstance().isEnabled()) {
176 if (navigationContext.isFrameworkToReset()) {
177 NavigationManagerFactory.resetInstance(container);
178 } else {
179 navigationContext.notifyPage(container, urlCalculator.calculateUrl(request));
180 }
181 }
182
183 if (Config.getInstance().isStatsEnabled()) {
184 StatsFactory.getInstance().logStatistic(container);
185 }
186
187 LogHelper.logWebflowStack(container);
188 }
189 } finally {
190 NavigationContextFactory.restoreInstance();
191 }
192 }
193 }