View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package com.ibatis.struts;
    18
    19  import com.ibatis.struts.httpmap.*;
    20
    21  import javax.servlet.http.HttpServletRequest;
    22  import javax.servlet.http.HttpServletResponse;
    23  import java.util.Map;
    24  import java.util.List;
    25  import java.util.ArrayList;
    26
    27  /***
    28   * The ActionContext class gives simplified, thread-safe access to
    29   * the request and response, as well as form parameters, request
    30   * attributes, session attributes, application attributes.  Much
    31   * of this can be accopmplished without using the Struts or even
    32   * the Servlet API, therefore isolating your application from
    33   * presentation framework details.
    34   * <p/>
    35   * This class also provides facilities for simpler message and error
    36   * message handling.  Although not as powerful as that provided by
    37   * Struts, it is great for simple applications that don't require
    38   * internationalization or the flexibility of resource bundles.
    39   * <p/>
    40   * <i>Note: A more complete error and message handling API will be implemented.</i>
    41   * <p/>
    42   * Date: Mar 9, 2004 9:57:39 PM
    43   *
    44   * @author Clinton Begin
    45   */
    46  public class ActionContext {
    47
    48    private static final ThreadLocal localContext = new ThreadLocal();
    49
    50    private HttpServletRequest request;
    51    private HttpServletResponse response;
    52
    53    private Map cookieMap;
    54    private Map parameterMap;
    55    private Map requestMap;
    56    private Map sessionMap;
    57    private Map applicationMap;
    58
    59    private ActionContext() {
    60    }
    61
    62    protected static void initialize(HttpServletRequest request, HttpServletResponse response) {
    63      ActionContext ctx = getActionContext();
    64      ctx.request = request;
    65      ctx.response = response;
    66      ctx.cookieMap = null;
    67      ctx.parameterMap = null;
    68      ctx.requestMap = null;
    69      ctx.sessionMap = null;
    70      ctx.applicationMap = null;
    71    }
    72
    73    public void setSimpleMessage(String message) {
    74      getRequestMap().put("message", message);
    75    }
    76
    77    public void addSimpleError(String message) {
    78      List errors = (List) getRequestMap().get("errors");
    79      if (errors == null) {
    80        errors = new ArrayList();
    81        getRequestMap().put("errors", errors);
    82      }
    83      errors.add(message);
    84    }
    85
    86    public boolean isSimpleErrorsExist () {
    87      List errors = (List) getRequestMap().get("errors");
    88      return errors != null && errors.size() > 0;
    89    }
    90
    91    public Map getCookieMap() {
    92      if (cookieMap == null) {
    93        cookieMap = new CookieMap(request);
    94      }
    95      return cookieMap;
    96    }
    97
    98    public Map getParameterMap() {
    99      if (parameterMap == null) {
    100       parameterMap = new ParameterMap(request);
    101     }
    102     return parameterMap;
    103   }
    104
    105   public Map getRequestMap() {
    106     if (requestMap == null) {
    107       requestMap = new RequestMap(request);
    108     }
    109     return requestMap;
    110   }
    111
    112   public Map getSessionMap() {
    113     if (sessionMap == null) {
    114       sessionMap = new SessionMap(request);
    115     }
    116     return sessionMap;
    117   }
    118
    119   public Map getApplicationMap() {
    120     if (applicationMap == null) {
    121       applicationMap = new ApplicationMap(request);
    122     }
    123     return applicationMap;
    124   }
    125
    126   public HttpServletRequest getRequest() {
    127     return request;
    128   }
    129
    130   public HttpServletResponse getResponse() {
    131     return response;
    132   }
    133
    134   public static ActionContext getActionContext() {
    135     ActionContext ctx = (ActionContext) localContext.get();
    136     if (ctx == null) {
    137       ctx = new ActionContext();
    138       localContext.set(ctx);
    139     }
    140     return ctx;
    141   }
    142 }