1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.workflow.web;
18
19
20 import java.io.IOException;
21 import java.util.EmptyStackException;
22 import javax.servlet.RequestDispatcher;
23 import javax.servlet.ServletException;
24 import javax.servlet.ServletRequest;
25 import javax.servlet.ServletResponse;
26 import org.apache.commons.workflow.Context;
27 import org.apache.commons.workflow.StepException;
28 import org.apache.commons.workflow.base.BaseStep;
29 import org.apache.commons.workflow.util.WorkflowUtils;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class ForwardStep extends BaseStep {
49
50
51
52
53
54
55
56
57 public ForwardStep() {
58
59 super();
60
61 }
62
63
64
65
66
67
68
69 public ForwardStep(String id) {
70
71 super();
72 setId(id);
73
74 }
75
76
77
78
79
80
81
82
83 public ForwardStep(String id, String page) {
84
85 super();
86 setId(id);
87 setPage(page);
88
89 }
90
91
92
93
94
95
96
97
98
99 protected String page = null;
100
101 public String getPage() {
102 return (this.page);
103 }
104
105 public void setPage(String page) {
106 this.page = page;
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121 public void execute(Context context) throws StepException {
122
123
124 if (!(context instanceof WebContext))
125 throw new StepException("Execution context is not a WebContext",
126 this);
127 WebContext webContext = (WebContext) context;
128
129
130 String resource = page;
131 if (resource == null) {
132 try {
133 resource = (String) webContext.pop();
134 } catch (EmptyStackException e) {
135 throw new StepException("Evaluation stack is empty", this);
136 }
137 }
138
139
140 RequestDispatcher rd =
141 webContext.getServletContext().getRequestDispatcher(resource);
142 if (rd == null)
143 throw new StepException("No request dispatcher for '" +
144 resource + "'", this);
145 ServletRequest request = webContext.getServletRequest();
146 ServletResponse response = webContext.getServletResponse();
147
148
149 try {
150 rd.forward(request, response);
151 } catch (IOException e) {
152 throw new StepException("IOException forwarding to '" +
153 resource + "'", e, this);
154 } catch (ServletException e) {
155 throw new StepException("ServletException forwarding to '" +
156 resource + "'", e, this);
157 }
158
159
160 context.setSuspend(true);
161
162 }
163
164
165
166
167
168 public String toString() {
169
170 StringBuffer sb = new StringBuffer("<web:forward");
171 if (getId() != null) {
172 sb.append(" id=\"");
173 sb.append(getId());
174 sb.append("\"");
175 }
176 sb.append(" page=\"");
177 sb.append(getPage());
178 sb.append("\"/>");
179 return (sb.toString());
180
181 }
182
183
184 }