View Javadoc

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