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 org.apache.commons.chain.web.jakarta.servlet.ServletWebContext;
24  import org.apache.struts.Globals;
25  import org.apache.struts.action.ActionMessages;
26  import org.apache.struts.action.ActionServlet;
27  import org.apache.struts.chain.Constants;
28  import org.apache.struts.config.ActionConfig;
29  import org.apache.struts.util.MessageResources;
30  
31  import jakarta.servlet.ServletContext;
32  import jakarta.servlet.http.HttpServletRequest;
33  import jakarta.servlet.http.HttpServletResponse;
34  
35  /**
36   * Implement ActionContext interface while making Servlet API-specific values
37   * available.
38   */
39  public class ServletActionContext extends WebActionContext {
40  
41      /**
42       * Instantiate this composite by wrapping a ServletWebContext.
43       *
44       * @param context The ServletWebContext to wrap
45       */
46      public ServletActionContext(ServletWebContext context) {
47          super(context);
48      }
49  
50      /**
51       * Instantiate this Context for a given {@code ServletContext},
52       * {@code HttpServletRequest}, and {@code HttpServletResponse}.
53       *
54       * @param context  The instant ServletContext
55       * @param request  The instant HttpServletRequest
56       * @param response The instant HttpServletResponse
57       */
58      public ServletActionContext(ServletContext context,
59          HttpServletRequest request, HttpServletResponse response) {
60          this(new ServletWebContext(context, request, response));
61      }
62  
63      /**
64       * Provide the {@code ServletWebContext} for this composite.
65       *
66       * @return Our ServletWebContext
67       */
68      protected ServletWebContext servletWebContext() {
69          return (ServletWebContext) this.getBaseContext();
70      }
71  
72      public void release() {
73          this.servletWebContext().release();
74          super.release();
75      }
76  
77      // -------------------------------
78      // Servlet specific properties
79      // -------------------------------
80  
81      /**
82       * Return the {@code ServletContext} for this context.
83       *
84       * @return Our ServletContext
85       */
86      public ServletContext getContext() {
87          return servletWebContext().getContext();
88      }
89  
90      /**
91       * Return the {@code HttpServletRequest} for this context.
92       *
93       * @return Our HttpServletRequest
94       */
95      public HttpServletRequest getRequest() {
96          return servletWebContext().getRequest();
97      }
98  
99      /**
100      * Return the {@code HttpServletResponse} for this context.
101      *
102      * @return Our HttpServletResponse
103      */
104     public HttpServletResponse getResponse() {
105         return servletWebContext().getResponse();
106     }
107 
108     /**
109      * Return the {@code ActionServlet} for this context.
110      *
111      * @return Our ActionServlet
112      */
113     public ActionServlet getActionServlet() {
114         return (ActionServlet) this.get(Constants.ACTION_SERVLET_KEY);
115     }
116 
117     /**
118      * Set the {@code ActionServlet} instance for this context.
119      *
120      * @param servlet Our ActionServlet instance
121      */
122     public void setActionServlet(ActionServlet servlet) {
123         this.put(Constants.ACTION_SERVLET_KEY, servlet);
124     }
125 
126     // -------------------------------
127     // Servlet specific modifications to base properties.
128     // -------------------------------
129     public void setActionConfig(ActionConfig actionConfig) {
130         super.setActionConfig(actionConfig);
131         this.getRequestScope().put(Globals.MAPPING_KEY, actionConfig);
132 
133         // ISSUE: Should we check this call to put?
134     }
135 
136     public MessageResources getMessageResources() {
137         return ((MessageResources) getRequest().getAttribute(Globals.MESSAGES_KEY));
138     }
139 
140     // ISSUE: This method would probably be better handled by a "Struts"
141     // object which encapsulated the servler (Application) scope.
142     public MessageResources getMessageResources(String key) {
143         // Identify the current module
144         ServletContext context = getActionServlet().getServletContext();
145 
146         // Return the requested message resources instance
147         return (MessageResources) context.getAttribute(key
148             + getModuleConfig().getPrefix());
149     }
150 
151     public void setMessageResources(MessageResources resources) {
152         super.setMessageResources(resources);
153         this.getRequest().setAttribute(Globals.MESSAGES_KEY, resources);
154     }
155 
156     /**
157      * Store the message resources for the current module under the given
158      * request attribute key.
159      *
160      * @param key       Request attribute key
161      * @param resources Message resources to store
162      */
163     public void setMessageResources(String key, MessageResources resources) {
164         this.getRequest().setAttribute(key + getModuleConfig().getPrefix(),
165             resources);
166     }
167 
168     // -------------------------------
169     // ActionMessage Processing
170     // -------------------------------
171     public void saveErrors(ActionMessages errors) {
172         // Remove any error messages attribute if none are required
173         if ((errors == null) || errors.isEmpty()) {
174             getRequest().removeAttribute(Globals.ERROR_KEY);
175 
176             return;
177         }
178 
179         // Save the error messages we need
180         getRequest().setAttribute(Globals.ERROR_KEY, errors);
181     }
182 
183     public void saveMessages(ActionMessages messages) {
184         if ((messages == null) || messages.isEmpty()) {
185             getRequest().removeAttribute(Globals.MESSAGE_KEY);
186 
187             return;
188         }
189 
190         getRequest().setAttribute(Globals.MESSAGE_KEY, messages);
191     }
192 
193     public void addMessages(ActionMessages messages) {
194         if (messages == null) {
195             return;
196         }
197 
198         ActionMessages requestMessages = getMessages();
199 
200         if (requestMessages == null) {
201             requestMessages = new ActionMessages();
202         }
203 
204         requestMessages.add(messages);
205         saveMessages(requestMessages);
206     }
207 
208     public void addErrors(ActionMessages errors) {
209         if (errors == null) {
210             return;
211         }
212 
213         ActionMessages requestErrors = getErrors();
214 
215         if (requestErrors == null) {
216             requestErrors = new ActionMessages();
217         }
218 
219         requestErrors.add(errors);
220         saveErrors(requestErrors);
221     }
222 
223     public ActionMessages getErrors() {
224         return (ActionMessages) this.getRequest().getAttribute(Globals.ERROR_KEY);
225     }
226 
227     public ActionMessages getMessages() {
228         return (ActionMessages) this.getRequest().getAttribute(Globals.MESSAGE_KEY);
229     }
230 
231     // -------------------------------
232     // Token Processing
233     // Implementing the servlet-aware versions by using the
234     // TokenProcessor class
235     // directly should ensure greater compatibility.
236     // -------------------------------
237     public void saveToken() {
238         token.saveToken(getRequest());
239     }
240 
241     public String generateToken() {
242         return token.generateToken(getRequest());
243     }
244 
245     public boolean isTokenValid(boolean reset) {
246         return token.isTokenValid(getRequest(), reset);
247     }
248 
249     public void resetToken() {
250         token.resetToken(getRequest());
251     }
252 }