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.include()</code> operation on the
34 * specified context relative path, and push the response data (as a String
35 * onto the evaluation stack.</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 retrieved, or omitted to pop a
41 * computed String value from the top of the evaluation stack.</li>
42 * </ul>
43 *
44 * <p><strong>WARNING</strong> - This implementation requires a Servlet 2.3
45 * based container, because it uses the new response wrapper facilities.</p>
46 *
47 * <p><strong>DESIGN QUESTION - What about binary content?</strong></p>
48 *
49 * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
50 * @author Craig R. McClanahan
51 */
52
53 public class IncludeStep23 extends BaseStep {
54
55
56 // ----------------------------------------------------------= Constructors
57
58
59 /**
60 * Construct a default instance of this Step.
61 */
62 public IncludeStep23() {
63
64 super();
65
66 }
67
68
69 /**
70 * Construct an instance of this Step with the specified identifier.
71 *
72 * @param id Step identifier
73 */
74 public IncludeStep23(String id) {
75
76 super();
77 setId(id);
78
79 }
80
81
82 /**
83 * Construct a fully configured instance of this Step.
84 *
85 * @param id Step identifier
86 * @param page Context-relative url
87 */
88 public IncludeStep23(String id, String page) {
89
90 super();
91 setId(id);
92 setPage(page);
93
94 }
95
96
97 // ------------------------------------------------------------- Properties
98
99
100 /**
101 * The context-relative URL (starting with '/') of the resource to be
102 * retrieved.
103 */
104 protected String page = null;
105
106 public String getPage() {
107 return (this.page);
108 }
109
110 public void setPage(String page) {
111 this.page = page;
112 }
113
114
115 // --------------------------------------------------------- Public Methods
116
117
118 /**
119 * Perform the executable actions related to this Step, in the context of
120 * the specified Context.
121 *
122 * @param context The Context that is tracking our execution state
123 *
124 * @exception StepException if a processing error has occurred
125 */
126 public void execute(Context context) throws StepException {
127
128 // Make sure our executing Context is a WebContext
129 if (!(context instanceof WebContext))
130 throw new StepException("Execution context is not a WebContext",
131 this);
132 WebContext webContext = (WebContext) context;
133
134 // Get the actual resource reference we will be using
135 String resource = page;
136 if (resource == null) {
137 try {
138 resource = (String) webContext.pop();
139 } catch (EmptyStackException e) {
140 throw new StepException("Evaluation stack is empty", this);
141 }
142 }
143
144 // Create a request dispatcher and response wrapper for this resource
145 RequestDispatcher rd =
146 webContext.getServletContext().getRequestDispatcher(resource);
147 if (rd == null)
148 throw new StepException("No request dispatcher for '" +
149 resource + "'", this);
150 ServletRequest request = webContext.getServletRequest();
151 ServletResponse response =
152 new IncludeResponse23(webContext.getServletResponse());
153
154 // Request the included resource
155 String content = null;
156 try {
157 rd.include(request, response);
158 content = ((IncludeResponse23) response).getContent();
159 } catch (IOException e) {
160 throw new StepException("IOException including '" +
161 resource + "'", e, this);
162 } catch (ServletException e) {
163 throw new StepException("ServletException including '" +
164 resource + "'", e, this);
165 }
166
167 // Push the resulting String onto the evaluation stack
168 webContext.push(content);
169
170 }
171
172
173 /**
174 * Render a string representation of this Step.
175 */
176 public String toString() {
177
178 StringBuffer sb = new StringBuffer("<web:include");
179 if (getId() != null) {
180 sb.append(" id=\"");
181 sb.append(getId());
182 sb.append("\"");
183 }
184 sb.append(" page=\"");
185 sb.append(getPage());
186 sb.append("\"/>");
187 return (sb.toString());
188
189 }
190
191
192 }