View Javadoc

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.Step;
28  import org.apache.commons.workflow.StepException;
29  import org.apache.commons.workflow.base.BaseStep;
30  
31  
32  /**
33   * <p>Unconditionally transfer control to the step that is identified by
34   * a request parameter with the specified name.</p>
35   *
36   * <p>Supported Attributes:</p>
37   * <ul>
38   * <li><strong>step</strong> - Name of a request parameter, included on the
39   *     current request, that contains the identifier of the Step (within the
40   *     current Activity) to which control should be transferred.  If not
41   *     specified, a request parameter named <code>step</code> is used.</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 GotoStep extends BaseStep {
49  
50  
51      // ----------------------------------------------------------= Constructors
52  
53  
54      /**
55       * Construct a default instance of this Step.
56       */
57      public GotoStep() {
58  
59          super();
60  
61      }
62  
63  
64      /**
65       * Construct an instance of this Step with the specified identifier.
66       *
67       * @param id Step identifier
68       */
69      public GotoStep(String id) {
70  
71          super();
72          setId(id);
73  
74      }
75  
76  
77      /**
78       * Construct a fully configured instance of this Step.
79       *
80       * @param id Step identifier
81       * @param step Request parameter name containing our step identifier
82       */
83      public GotoStep(String id, String step) {
84  
85          super();
86          setId(id);
87          setStep(step);
88  
89      }
90  
91  
92      // ------------------------------------------------------------- Properties
93  
94  
95      /**
96       * The request parameter containing the identifier of the Step to which
97       * control should be transferred.
98       */
99      protected String step = "step";
100 
101     public String getStep() {
102         return (this.step);
103     }
104 
105     public void setStep(String step) {
106         this.step = step;
107     }
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         if (!(context instanceof WebContext))
125             throw new StepException("Execution context is not a WebContext",
126                                     this);
127         WebContext webContext = (WebContext) context;
128 
129         // Locate the step identifier to which we will transfer control
130         ServletRequest request = webContext.getServletRequest();
131         String id = request.getParameter(step);
132         if (id == null)
133             throw new StepException("No request parameter '" + step + "'",
134                                     this);
135 
136 
137         // Locate the step to which we will transfer control
138         Step next = getOwner().findStep(id);
139         if (next == null)
140             throw new StepException("Cannot find step '" + id + "'", this);
141 
142         // Tell our Context to transfer control
143         context.setNextStep(next);
144 
145     }
146 
147 
148     /**
149      * Render a string representation of this Step.
150      */
151     public String toString() {
152 
153         StringBuffer sb = new StringBuffer("<web:goto");
154         if (getId() != null) {
155             sb.append(" id=\"");
156             sb.append(getId());
157             sb.append("\"");
158         }
159         sb.append(" step=\"");
160         sb.append(getStep());
161         sb.append("\"/>");
162         return (sb.toString());
163 
164     }
165 
166 
167 }