View Javadoc

1   /*
2    * $Id: ServletActionContext.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2;
23  
24  import java.util.Map;
25  
26  import javax.servlet.ServletContext;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.jsp.PageContext;
30  
31  import org.apache.struts2.dispatcher.mapper.ActionMapping;
32  
33  import com.opensymphony.xwork2.ActionContext;
34  import com.opensymphony.xwork2.util.ValueStack;
35  
36  
37  /***
38   * Web-specific context information for actions. This class subclasses <tt>ActionContext</tt> which
39   * provides access to things like the action name, value stack, etc. This class adds access to
40   * web objects like servlet parameters, request attributes and things like the HTTP session.
41   */
42  public class ServletActionContext extends ActionContext implements StrutsStatics {
43  
44      private static final long serialVersionUID = -666854718275106687L;
45  
46      public static final String STRUTS_VALUESTACK_KEY = "struts.valueStack";
47      public static final String ACTION_MAPPING = "struts.actionMapping";
48  
49      @SuppressWarnings("unused")
50      private ServletActionContext(Map context) {
51          super(context);
52      }
53  
54      /***
55       * Gets the current action context
56       *
57       * @param req The request
58       * @return The current action context
59       */
60      public static ActionContext getActionContext(HttpServletRequest req) {
61          ValueStack vs = getValueStack(req);
62          if (vs != null) {
63              return new ActionContext(vs.getContext());
64          } else {
65              return null;
66          }
67      }
68  
69      /***
70       * Gets the current value stack for this request
71       *
72       * @param req The request
73       * @return The value stack
74       */
75      public static ValueStack getValueStack(HttpServletRequest req) {
76          return (ValueStack) req.getAttribute(STRUTS_VALUESTACK_KEY);
77      }
78  
79      /***
80       * Gets the action mapping for this context
81       *
82       * @return The action mapping
83       */
84      public static ActionMapping getActionMapping() {
85          return (ActionMapping) ActionContext.getContext().get(ACTION_MAPPING);
86      }
87  
88      /***
89       * Returns the HTTP page context.
90       *
91       * @return the HTTP page context.
92       */
93      public static PageContext getPageContext() {
94          return (PageContext) ActionContext.getContext().get(PAGE_CONTEXT);
95      }
96  
97      /***
98       * Sets the HTTP servlet request object.
99       *
100      * @param request the HTTP servlet request object.
101      */
102     public static void setRequest(HttpServletRequest request) {
103         ActionContext.getContext().put(HTTP_REQUEST, request);
104     }
105 
106     /***
107      * Gets the HTTP servlet request object.
108      *
109      * @return the HTTP servlet request object.
110      */
111     public static HttpServletRequest getRequest() {
112         return (HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST);
113     }
114 
115     /***
116      * Sets the HTTP servlet response object.
117      *
118      * @param response the HTTP servlet response object.
119      */
120     public static void setResponse(HttpServletResponse response) {
121         ActionContext.getContext().put(HTTP_RESPONSE, response);
122     }
123 
124     /***
125      * Gets the HTTP servlet response object.
126      *
127      * @return the HTTP servlet response object.
128      */
129     public static HttpServletResponse getResponse() {
130         return (HttpServletResponse) ActionContext.getContext().get(HTTP_RESPONSE);
131     }
132 
133     /***
134      * Gets the servlet context.
135      *
136      * @return the servlet context.
137      */
138     public static ServletContext getServletContext() {
139         return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT);
140     }
141 
142     /***
143      * Sets the current servlet context object
144      *
145      * @param servletContext The servlet context to use
146      */
147     public static void setServletContext(ServletContext servletContext) {
148         ActionContext.getContext().put(SERVLET_CONTEXT, servletContext);
149     }
150 }