1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.webflow;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.abstracthorizon.danube.connection.Connection;
19 import org.abstracthorizon.danube.connection.ConnectionException;
20 import org.abstracthorizon.danube.http.HTTPConnection;
21 import org.abstracthorizon.danube.http.session.HTTPSessionManager;
22 import org.abstracthorizon.danube.http.session.SimpleSessionManager;
23 import org.abstracthorizon.danube.mvc.Controller;
24 import org.abstracthorizon.danube.mvc.ModelAndView;
25 import org.springframework.webflow.context.ExternalContext;
26 import org.springframework.webflow.execution.support.ApplicationView;
27 import org.springframework.webflow.executor.FlowExecutor;
28 import org.springframework.webflow.executor.ResponseInstruction;
29 import org.springframework.webflow.executor.mvc.FlowController;
30 import org.springframework.webflow.executor.support.FlowExecutorArgumentExtractor;
31 import org.springframework.webflow.executor.support.FlowRequestHandler;
32
33
34
35
36
37
38
39
40
41
42
43 public class DanubeFlowController implements Controller {
44
45
46 private FlowExecutor flowExecutor;
47
48
49 private FlowExecutorArgumentExtractor argumentExtractor = null;
50
51
52 private HTTPSessionManager sessionManager;
53
54
55 private Map<String, Object> attributes;
56
57
58
59
60 public DanubeFlowController() {
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public FlowExecutor getFlowExecutor() {
79 return flowExecutor;
80 }
81
82
83
84
85
86
87 public void setFlowExecutor(FlowExecutor flowExecutor) {
88 this.flowExecutor = flowExecutor;
89 }
90
91
92
93
94
95 public FlowExecutorArgumentExtractor getArgumentExtractor() {
96 if (argumentExtractor == null) {
97
98 }
99 return argumentExtractor;
100 }
101
102
103
104
105
106
107 public void setArgumentExtractor(FlowExecutorArgumentExtractor parameterExtractor) {
108 this.argumentExtractor = parameterExtractor;
109 }
110
111
112
113
114
115
116 public void setDefaultFlowId(String defaultFlowId) {
117
118 }
119
120
121
122
123
124
125
126 public ModelAndView handleRequest(Connection connection) throws ConnectionException {
127 try {
128 HTTPConnection httpConnection = (HTTPConnection) connection;
129
130 DanubeExternalContext context = new DanubeExternalContext(this, httpConnection);
131
132 FlowRequestHandler flowRequestHandler = new FlowRequestHandler(getFlowExecutor(), getArgumentExtractor());
133 ResponseInstruction responseInstruction = flowRequestHandler.handleFlowRequest(context);
134
135 return toModelAndView(responseInstruction, context);
136 } catch (ConnectionException e) {
137 throw e;
138 } catch (Exception e) {
139 throw new ConnectionException(e);
140 }
141 }
142
143
144
145
146
147
148
149 @SuppressWarnings("unchecked")
150 protected ModelAndView toModelAndView(ResponseInstruction response, ExternalContext context) {
151 if (response.isApplicationView()) {
152 ApplicationView view = (ApplicationView)response.getViewSelection();
153 Map<String, Object> model = new HashMap<String, Object>(view.getModel());
154
155
156
157 return new ModelAndView(view.getViewName(), model);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 } else if (response.isNull()) {
174 return null;
175 } else {
176 throw new IllegalArgumentException("Don't know how to handle response instruction " + response);
177 }
178 }
179
180
181
182
183
184 public HTTPSessionManager getSessionManager() {
185 if (sessionManager == null) {
186 sessionManager = new SimpleSessionManager();
187 }
188 return sessionManager;
189 }
190
191
192
193
194
195 public void setSessionManager(HTTPSessionManager sessionManager) {
196 this.sessionManager = sessionManager;
197 }
198
199
200
201
202
203 public Map<String, Object> getAttributes() {
204 if (attributes == null) {
205 attributes = new HashMap<String, Object>();
206 }
207 return attributes;
208 }
209
210
211
212
213
214 public void setAttributes(Map<String, Object> attributes) {
215 if (attributes == null) {
216 getAttributes();
217 }
218 this.attributes = attributes;
219 }
220 }