Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ForwardStep |
|
| 2.7142857142857144;2.714 |
1 | /* | |
2 | * Copyright 1999-2001,2004 The Apache Software Foundation. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
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 | * <p>Perform a <code>RequestDispatcher.forward()</code> operation on the | |
34 | * specified context relative path, and tell our <code>Context</code> to | |
35 | * suspend execution until control is returned.</p> | |
36 | * | |
37 | * <p>Supported Attributes:</p> | |
38 | * <ul> | |
39 | * <li><strong>page</strong> - Context relative URL (starting with a slash) | |
40 | * of the application resource to be forwarded to, or omitted to pop a | |
41 | * computed String value from the top of the evaluation stack.</li> | |
42 | * </ul> | |
43 | * | |
44 | * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $ | |
45 | * @author Craig R. McClanahan | |
46 | */ | |
47 | ||
48 | public class ForwardStep extends BaseStep { | |
49 | ||
50 | ||
51 | // ----------------------------------------------------------= Constructors | |
52 | ||
53 | ||
54 | /** | |
55 | * Construct a default instance of this Step. | |
56 | */ | |
57 | public ForwardStep() { | |
58 | ||
59 | 0 | super(); |
60 | ||
61 | 0 | } |
62 | ||
63 | ||
64 | /** | |
65 | * Construct an instance of this Step with the specified identifier. | |
66 | * | |
67 | * @param id Step identifier | |
68 | */ | |
69 | public ForwardStep(String id) { | |
70 | ||
71 | 0 | super(); |
72 | 0 | setId(id); |
73 | ||
74 | 0 | } |
75 | ||
76 | ||
77 | /** | |
78 | * Construct a fully configured instance of this Step. | |
79 | * | |
80 | * @param id Step identifier | |
81 | * @param page Context-relative url | |
82 | */ | |
83 | public ForwardStep(String id, String page) { | |
84 | ||
85 | 0 | super(); |
86 | 0 | setId(id); |
87 | 0 | setPage(page); |
88 | ||
89 | 0 | } |
90 | ||
91 | ||
92 | // ------------------------------------------------------------- Properties | |
93 | ||
94 | ||
95 | /** | |
96 | * The context-relative URL (starting with '/') of the resource to be | |
97 | * forwarded to. | |
98 | */ | |
99 | 0 | protected String page = null; |
100 | ||
101 | public String getPage() { | |
102 | 0 | return (this.page); |
103 | } | |
104 | ||
105 | public void setPage(String page) { | |
106 | 0 | this.page = page; |
107 | 0 | } |
108 | ||
109 | ||
110 | // --------------------------------------------------------- Public Methods | |
111 | ||
112 | ||
113 | /** | |
114 | * Perform the executable actions related to this Step, in the context of | |
115 | * the specified Context. | |
116 | * | |
117 | * @param context The Context that is tracking our execution state | |
118 | * | |
119 | * @exception StepException if a processing error has occurred | |
120 | */ | |
121 | public void execute(Context context) throws StepException { | |
122 | ||
123 | // Make sure our executing Context is a WebContext | |
124 | 0 | if (!(context instanceof WebContext)) |
125 | 0 | throw new StepException("Execution context is not a WebContext", |
126 | this); | |
127 | 0 | WebContext webContext = (WebContext) context; |
128 | ||
129 | // Get the actual resource reference we will be using | |
130 | 0 | String resource = page; |
131 | 0 | if (resource == null) { |
132 | try { | |
133 | 0 | resource = (String) webContext.pop(); |
134 | 0 | } catch (EmptyStackException e) { |
135 | 0 | throw new StepException("Evaluation stack is empty", this); |
136 | 0 | } |
137 | } | |
138 | ||
139 | // Create a request dispatcher for this resource | |
140 | 0 | RequestDispatcher rd = |
141 | webContext.getServletContext().getRequestDispatcher(resource); | |
142 | 0 | if (rd == null) |
143 | 0 | throw new StepException("No request dispatcher for '" + |
144 | resource + "'", this); | |
145 | 0 | ServletRequest request = webContext.getServletRequest(); |
146 | 0 | ServletResponse response = webContext.getServletResponse(); |
147 | ||
148 | // Forward to the requested resource | |
149 | try { | |
150 | 0 | rd.forward(request, response); |
151 | 0 | } catch (IOException e) { |
152 | 0 | throw new StepException("IOException forwarding to '" + |
153 | resource + "'", e, this); | |
154 | 0 | } catch (ServletException e) { |
155 | 0 | throw new StepException("ServletException forwarding to '" + |
156 | resource + "'", e, this); | |
157 | 0 | } |
158 | ||
159 | // Signal our Context to suspend execution | |
160 | 0 | context.setSuspend(true); |
161 | ||
162 | 0 | } |
163 | ||
164 | ||
165 | /** | |
166 | * Render a string representation of this Step. | |
167 | */ | |
168 | public String toString() { | |
169 | ||
170 | 0 | StringBuffer sb = new StringBuffer("<web:forward"); |
171 | 0 | if (getId() != null) { |
172 | 0 | sb.append(" id=\""); |
173 | 0 | sb.append(getId()); |
174 | 0 | sb.append("\""); |
175 | } | |
176 | 0 | sb.append(" page=\""); |
177 | 0 | sb.append(getPage()); |
178 | 0 | sb.append("\"/>"); |
179 | 0 | return (sb.toString()); |
180 | ||
181 | } | |
182 | ||
183 | ||
184 | } |