View Javadoc

1   /*
2    *  Copyright 1999-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  package org.apache.struts.flow;
17  
18  import org.apache.struts.flow.core.*;
19  import org.apache.struts.flow.core.javascript.fom.FOM_Flow;
20  import org.apache.struts.action.ActionMapping;
21  import org.apache.struts.action.ActionErrors;
22  import org.apache.struts.action.ActionMessages;
23  import javax.servlet.*;
24  import org.apache.commons.chain.web.WebContext;
25  import org.apache.commons.chain.web.servlet.ServletWebContext;
26  import org.apache.struts.util.MessageResources;
27  
28  import java.util.Map;
29  
30  /***
31   *  Access to Struts and Servlet resources
32   *
33   * @jsname struts
34   */
35  public class Struts {
36      
37      protected FOM_Flow flow = null;
38      protected static final Logger logger = Factory.getLogger();
39  
40      public Struts() {
41          throw new IllegalStateException("Cannot create new Struts object in a flow script");
42      }
43              
44  
45      /***  Constructor for the JSLog object */
46      public Struts(FOM_Flow flow) {
47          this.flow = flow;
48      }
49      
50      private ServletWebContext getContext() {
51          WebContext ctx = flow.getWebContext();
52          if (ctx instanceof ServletWebContext) {
53              return (ServletWebContext)ctx;
54          } else {
55              throw new IllegalStateException("The web context must be the ServletWebContext");
56          }
57      }
58      
59      /***
60       *  Gets a map of request parameters as Strings
61       */
62      public Map getParam() {
63          return getContext().getParam();
64      }
65      
66      /***
67       *  Gets a map of request parameters as String arrays
68       */
69      public Map getParamValues() {
70          return getContext().getParamValues();
71      }
72      
73      /***
74       *  Gets a map of request attributes
75       */
76      public Map getRequestScope() {
77          return getContext().getRequestScope();
78      }
79      
80      /***
81       *  Gets a map of session attributes
82       */
83      public Map getSessionScope() {
84          return getContext().getSessionScope();
85      }
86      
87      /***
88       *  Gets a map of application attributes
89       */
90      public Map getApplicationScope() {
91          return getContext().getApplicationScope();
92      }
93      
94      /***
95       *  Gets the servlet request
96       */
97      public ServletRequest getRequest() {
98          return getContext().getRequest();
99      }
100     
101     /***
102      *  Gets the servlet context
103      */
104     public ServletContext getServletContext() {
105         return getContext().getContext();
106     }
107 
108     /***
109      *  Gets an application resources message
110      *
111      * @param key The message key
112      */
113     public String getMessage(String key) {
114         MessageResources res = (MessageResources)getContext().get(Constants.MESSAGE_RESOURCES_KEY);
115         return res.getMessage(key);
116     }
117     
118     /***
119      *  Gets the action mapping
120      */
121     public ActionMapping getMapping() {
122         return (ActionMapping)getContext().get(Constants.ACTION_CONFIG_KEY);
123     }
124     
125     /***
126      * Gets if the action has been canceled
127      */
128     public boolean isCanceled() {
129         FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
130         return action.isCancelled(getContext().getRequest());
131     }
132     
133     /***
134      *  Gets if the current token is valid
135      */
136     public boolean isTokenValid() {
137         FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
138         return action.isTokenValid(getContext().getRequest());
139     }
140     
141     /***
142      *  Resets the current transation token
143      */
144     public void resetToken() {
145         FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
146         action.resetToken(getContext().getRequest());
147     }
148     
149     /***
150      *  Saves the action errors in the request
151      *
152      * @param errors The action errors
153      */
154     public void saveErrors(ActionErrors errors) {
155         FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
156         action.saveErrors(getContext().getRequest(), errors);
157     }
158     
159     /*** 
160      *   Saves the action messages in the request
161      *
162      * @param msgs The action messages
163      */
164     public void saveMessages(ActionMessages msgs) {
165         FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
166         action.saveMessages(getContext().getRequest(), msgs);
167     }
168     
169     /***
170      *  Saves a transaction token in the request
171      */
172     public void saveToken() {
173         FlowAction action = (FlowAction)getContext().get(Constants.ACTION_KEY);
174         action.saveToken(getContext().getRequest());
175     }
176 }
177