View Javadoc

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