View Javadoc

1   /*
2    * $Id: WebActionContext.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.chain.contexts;
19  
20  import org.apache.commons.chain.web.WebContext;
21  import org.apache.struts.Globals;
22  import org.apache.struts.config.ModuleConfig;
23  
24  import java.util.Map;
25  
26  /***
27   * <p> Provide a Subclass of ActionContextBase which is understood to be
28   * wrapping an instance of <code>org.apache.commons.chain.web.WebContext</code>.
29   * </p>
30   */
31  public class WebActionContext extends ActionContextBase {
32      /***
33       * Instantiate this composite by wrapping an instance of WebContext.
34       *
35       * @param context The WebContext to wrap
36       */
37      public WebActionContext(WebContext context) {
38          super(context);
39      }
40  
41      /***
42       * Provide the wrapped WebContext for this composite.
43       *
44       * @return The wrapped WebContext
45       */
46      protected WebContext webContext() {
47          return (WebContext) this.getBaseContext();
48      }
49  
50      public void release() {
51          super.release();
52      }
53  
54      // -------------------------------
55      // WebContext property wrappers
56      // -------------------------------
57  
58      /***
59       * <p> Return an immutable Map that maps header names to the first (or
60       * only) header value (as a String). </p>
61       *
62       * @return A immutable Map of web request header names
63       */
64      public Map getHeader() {
65          return webContext().getHeader();
66      }
67  
68      /***
69       * <p> Return an immutable Map that maps header names to the set of all
70       * values specified in the request (as a String array). Header names must
71       * be matched in a case-insensitive manner. </p>
72       *
73       * @return An immutable Map of web request header values
74       */
75      public Map getHeaderValues() {
76          return webContext().getHeaderValues();
77      }
78  
79      /***
80       * <p> Return an immutable Map that maps context application
81       * initialization parameters to their values. </p>
82       *
83       * @return An immutable Map of web context initialization parameters
84       */
85      public Map getInitParam() {
86          return webContext().getInitParam();
87      }
88  
89      /***
90       * <p> Return a map whose keys are <code>String</code> request parameter
91       * names and whose values are <code>String</code> values. </p> <p> For
92       * parameters which were submitted with more than one value, only one
93       * value will be returned, as if one called
94       * <code>ServletRequest.getParameter(String)</code>
95       * </p>
96       *
97       * @return A map of web request parameters
98       */
99      public Map getParam() {
100         return webContext().getParam();
101     }
102 
103     /***
104      * <p> Return a map whose keys are <code>String</code> request parameter
105      * names and whose values are <code>String[]</code> values. </p>
106      *
107      * @return A map of web request parameter values (as an array)
108      */
109     public Map getParamValues() {
110         return webContext().getParamValues();
111     }
112 
113     public Map getApplicationScope() {
114         return webContext().getApplicationScope();
115     }
116 
117     public Map getRequestScope() {
118         return webContext().getRequestScope();
119     }
120 
121     public Map getParameterMap() {
122         return getParamValues();
123     }
124 
125     public Map getSessionScope() {
126         return webContext().getSessionScope();
127     }
128 
129     // ISSUE: AbstractSelectModule set the precedent of doing this at the
130     // "web context" level instead of the ServletWebContext level.
131     // Consider whether that's how we want to do it universally for other
132     // manipulations of the RequestScope or not...
133     public void setModuleConfig(ModuleConfig moduleConfig) {
134         super.setModuleConfig(moduleConfig);
135         this.getRequestScope().put(Globals.MODULE_KEY, moduleConfig);
136     }
137 
138     /***
139      * @see org.apache.struts.chain.contexts.ActionContext#getModuleConfig()
140      */
141     public ModuleConfig getModuleConfig() {
142         ModuleConfig mc = super.getModuleConfig();
143 
144         if (mc == null) {
145             mc = (ModuleConfig) this.getRequestScope().get(Globals.MODULE_KEY);
146         }
147 
148         return mc;
149     }
150 
151     // ISSUE:  AbstractSelectModule set the precedent of doing this at the
152     // "web context" level instead of the ServletWebContext level.  Consider
153     // whether that's how we want to do it universally for other manipulations
154     // of the RequestScope or not...
155     public void setCancelled(Boolean cancelled) {
156         super.setCancelled(cancelled);
157 
158         // historic semantics of "isCancelled" are to consider any non-null
159         // value in the request under Globals.CANCEL_KEY as "yes, this was
160         // cancelled."
161         if ((cancelled != null) && cancelled.booleanValue()) {
162             this.getRequestScope().put(Globals.CANCEL_KEY, cancelled);
163         } else {
164             this.getRequestScope().remove(Globals.CANCEL_KEY);
165         }
166     }
167 
168     public Boolean getCancelled() {
169         Boolean cancelled = super.getCancelled();
170 
171         if (cancelled == null) {
172             cancelled =
173                 (Boolean) this.getRequestScope().get(Globals.CANCEL_KEY);
174         }
175 
176         return cancelled;
177     }
178 }