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