1 /*
2 * Copyright (c) 2005-2007 Creative Sphere Limited.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *
10 * Creative Sphere - initial API and implementation
11 *
12 */
13 package org.abstracthorizon.danube.webflow;
14
15 import java.util.Collections;
16 import java.util.Enumeration;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
20
21 import org.abstracthorizon.danube.http.HTTPConnection;
22 import org.abstracthorizon.danube.http.session.Session;
23 import org.springframework.binding.collection.SharedMap;
24 import org.springframework.binding.collection.SharedMapDecorator;
25 import org.springframework.binding.collection.StringKeyedMapAdapter;
26 import org.springframework.webflow.context.ExternalContext;
27 import org.springframework.webflow.core.collection.LocalParameterMap;
28 import org.springframework.webflow.core.collection.LocalSharedAttributeMap;
29 import org.springframework.webflow.core.collection.MutableAttributeMap;
30 import org.springframework.webflow.core.collection.ParameterMap;
31 import org.springframework.webflow.core.collection.SharedAttributeMap;
32
33 //import org.springframework.webflow.AttributeMap;
34 //import org.springframework.webflow.ExternalContext;
35 //import org.springframework.webflow.ParameterMap;
36 //import org.springframework.webflow.SharedAttributeMap;
37 //import org.springframework.webflow.SharedMap;
38 //import org.springframework.webflow.context.ExternalContext;
39 //import org.springframework.webflow.context.SharedMapDecorator;
40 //import org.springframework.webflow.context.StringKeyedMapAdapter;
41
42 /**
43 * Danube Spring WebFLow's external context adapter.
44 *
45 * @author Daniel Sendula
46 */
47 public class DanubeExternalContext implements ExternalContext {
48
49 /** Connection reference */
50 protected HTTPConnection connection;
51
52 /** Reference to the controller */
53 protected DanubeFlowController controller;
54
55 /**
56 * Constructor
57 * @param controller reference to the controller
58 * @param connection reference to the current connection
59 */
60 public DanubeExternalContext(DanubeFlowController controller, HTTPConnection connection) {
61 this.controller = controller;
62 this.connection = connection;
63 }
64
65 /**
66 * Returns connection's context path
67 * @return connection's context path
68 */
69 public String getContextPath() {
70 return connection.getContextPath();
71 }
72
73 /**
74 * Returns connection's component path
75 * @return connection's component path
76 */
77 public String getDispatcherPath() {
78 return connection.getComponentPath();
79 }
80
81 /**
82 * Returns connection's component resource path
83 * @return connection's component resource path
84 */
85 public String getRequestPathInfo() {
86 return connection.getComponentResourcePath();
87 }
88
89 /**
90 * Returns request parameter map adapter from connection's request paramaters
91 * @return request parameter map adapter
92 */
93 public ParameterMap getRequestParameterMap() {
94 return new LocalParameterMap(connection.getRequestParameters().getAsMap());
95 }
96
97 /**
98 * Returns connection's attributes as an attribute map
99 * @return connection's attributes
100 */
101 // public AttributeMap getRequestMap() {
102 // return new LocalAttributeMap(connection.getAttributes());
103 // }
104
105 /**
106 * Returns new {@link SessionMap} instance
107 * @return new {@link SessionMap} instance
108 */
109 // public SharedAttributeMap getSessionMap() {
110 // return new LocalSharedAttributeMap(SharedMap new SessionMap());
111 // }
112
113 /**
114 * Returns controller's attributes properly decorated and adapted
115 * @return controller's attributes
116 */
117 // public SharedAttributeMap getApplicationMap() {
118 // return new SharedAttributeMap(new SharedMapDecorator(controller.getAttributes()));
119 // }
120
121 /**
122 * Session map adapter
123 */
124 public class SessionMap extends StringKeyedMapAdapter implements SharedMap {
125
126 /**
127 * Constructor
128 *
129 */
130 public SessionMap() {
131 }
132
133 /**
134 * Returns controller's session manager's session if there is one. <code>null</code> otherwise.
135 * @return controller's session manager's session
136 */
137 protected Session getSession() {
138 return (Session)controller.getSessionManager().findSession(connection, false);
139 }
140
141 /**
142 * Returns session's attribute if session is available
143 * @return session's attribute or <code>null</code> if session or attribute is not available
144 */
145 protected Object getAttribute(String key) {
146 Session session = getSession();
147 if (session == null) {
148 return null;
149 } else {
150 return session.getAttributes().get(key);
151 }
152 }
153
154 /**
155 * Sets session attribute. If session is not already created it is going to be created now.
156 * @param key attribute key
157 * @param value attribute value
158 */
159 protected void setAttribute(String key, Object value) {
160 Session session = (Session)controller.getSessionManager().findSession(connection, true);
161 session.getAttributes().put(key, value);
162 }
163
164 /**
165 * Removes attribute. This method won't create new session if there isn't one already there
166 * @param key attribute's key
167 */
168 protected void removeAttribute(String key) {
169 Session session = getSession();
170 if (session != null) {
171 session.getAttributes().remove(key);
172 }
173 }
174
175 /**
176 * Returns enumeration of all attribute names. This method won't create new session if there isn't one already there
177 * @return enumeration of all attribute names
178 */
179 protected Iterator<String> getAttributeNames() {
180 Session session = getSession();
181 if (session != null) {
182 return session.getAttributes().keySet().iterator();
183 } else {
184 List<String> list = Collections.emptyList();
185 return list.iterator();
186 }
187 }
188
189 /**
190 * Returns session. If there wasn't a session created already new one will be created when this
191 * method is called.
192 * @return session
193 */
194 public Object getMutex() {
195 Session session = (Session)controller.getSessionManager().findSession(connection, true);
196 return session;
197 }
198 }
199
200 /**
201 * Enumeration adapter over itereator
202 */
203 public static class EnumerationIteratorAdapter<T> implements Enumeration<T> {
204
205 /** Iterator to be adapted */
206 protected Iterator<T> iterator;
207
208 /**
209 * Constructor
210 * @param iterator iterator to be adapted
211 */
212 public EnumerationIteratorAdapter(Iterator<T> iterator) {
213 this.iterator = iterator;
214 }
215
216 /**
217 * Returns {@link Iterator#hasNext()}
218 * @return {@link Iterator#hasNext()}
219 */
220 public boolean hasMoreElements() {
221 return iterator.hasNext();
222 }
223
224 /**
225 * Returns {@link Iterator#next()}
226 * @return {@link Iterator#next()}
227 */
228 public T nextElement() {
229 return iterator.next();
230 }
231 }
232
233 public SharedAttributeMap getApplicationMap() {
234 // TODO Auto-generated method stub
235 return new LocalSharedAttributeMap(new SharedMapDecorator(new HashMap<Object, Object>()));
236 }
237
238 public SharedAttributeMap getGlobalSessionMap() {
239 // TODO Auto-generated method stub
240 return new LocalSharedAttributeMap(new SharedMapDecorator(new HashMap<Object, Object>()));
241 }
242
243 public MutableAttributeMap getRequestMap() {
244 // TODO Auto-generated method stub
245 return new LocalSharedAttributeMap(new SharedMapDecorator(new HashMap<Object, Object>()));
246 }
247
248 public SharedAttributeMap getSessionMap() {
249 // TODO Auto-generated method stub
250 return new LocalSharedAttributeMap(new SharedMapDecorator(new HashMap<Object, Object>()));
251 }
252
253 }