View Javadoc

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