View Javadoc

1   /*
2    * Copyright 1999-2001,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  
17  package org.apache.commons.workflow.web;
18  
19  
20  import java.util.EmptyStackException;
21  import javax.servlet.ServletContext;
22  import javax.servlet.ServletRequest;
23  import javax.servlet.ServletResponse;
24  import javax.servlet.http.HttpSession;
25  import org.apache.commons.collections.ArrayStack;
26  import org.apache.commons.workflow.Context;
27  import org.apache.commons.workflow.Scope;
28  import org.apache.commons.workflow.base.BaseContext;
29  
30  
31  /**
32   * <p><strong>WebContext</strong> is a specialized <code>Context</code>
33   * implementation appropriate for web applications that run on top of a
34   * Servlet 2.2 (or later) container.  It exposes the attributes associated
35   * with requests, sessions, and the servlet context as <code>Scopes</code>
36   * within the workflow management system.</p>
37   *
38   * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
39   * @author Craig R. McClanahan
40   */
41  
42  public class WebContext extends BaseContext {
43  
44  
45      // ----------------------------------------------------- Manifest Constants
46  
47  
48      /**
49       * The scope identifier for the scope associated with the current
50       * servlet request.
51       */
52      public static final int REQUEST_SCOPE = 2;
53  
54  
55      /**
56       * The scope name for the scope associated with the current
57       * servlet request.
58       */
59      public static final String REQUEST_SCOPE_NAME = "request";
60  
61  
62      /**
63       * The scope identifier for the scope associated with the current
64       * HTTP session.
65       */
66      public static final int SESSION_SCOPE = 4;
67  
68  
69      /**
70       * The scope name for the scope associated with the current
71       * HTTP session.
72       */
73      public static final String SESSION_SCOPE_NAME = "session";
74  
75  
76      /**
77       * The scope identifier for the scope associated with the current
78       * servlet context.
79       */
80      public static final int APPLICATION_SCOPE = 6;
81  
82  
83      /**
84       * The scope name for the scope associated with the current
85       * servlet context.
86       */
87      public static final String APPLICATION_SCOPE_NAME = "application";
88  
89  
90      // ------------------------------------------------------------- Properties
91  
92  
93      /**
94       * The HttpSession that provides our associated "session" scope.
95       */
96      protected HttpSession httpSession = null;
97  
98      public HttpSession getHttpSession() {
99  
100         return (this.httpSession);
101 
102     }
103 
104     public void setHttpSession(HttpSession httpSession) {
105 
106         this.httpSession = httpSession;
107         Scope oldScope = getScope(SESSION_SCOPE);
108         if ((oldScope != null) && (oldScope instanceof HttpSessionScope))
109             ((HttpSessionScope) oldScope).setHttpSession(httpSession);
110         else
111             addScope(SESSION_SCOPE, SESSION_SCOPE_NAME,
112                      new HttpSessionScope(httpSession));
113 
114     }
115 
116 
117     /**
118      * The ServletContext that provides our associated "application" scope.
119      */
120     protected ServletContext servletContext = null;
121 
122     public ServletContext getServletContext() {
123 
124         return (this.servletContext);
125 
126     }
127 
128     public void setServletContext(ServletContext servletContext) {
129 
130         this.servletContext = servletContext;
131         Scope oldScope = getScope(APPLICATION_SCOPE);
132         if ((oldScope != null) && (oldScope instanceof ServletContextScope))
133             ((ServletContextScope) oldScope).setServletContext(servletContext);
134         else
135             addScope(APPLICATION_SCOPE, APPLICATION_SCOPE_NAME,
136                      new ServletContextScope(servletContext));
137 
138     }
139 
140 
141     /**
142      * The ServletRequest that provides our associated "request" scope.
143      */
144     protected ServletRequest servletRequest = null;
145 
146     public ServletRequest getServletRequest() {
147 
148         return (this.servletRequest);
149 
150     }
151 
152     public void setServletRequest(ServletRequest servletRequest) {
153 
154         this.servletRequest = servletRequest;
155         Scope oldScope = getScope(REQUEST_SCOPE);
156         if ((oldScope != null) && (oldScope instanceof ServletRequestScope))
157             ((ServletRequestScope) oldScope).setServletRequest(servletRequest);
158         else
159             addScope(REQUEST_SCOPE, REQUEST_SCOPE_NAME,
160                      new ServletRequestScope(servletRequest));
161 
162         // FIXME - Consider exposing the cookies and headers from
163         // a request as "read only" scopes
164 
165     }
166 
167 
168     /**
169      * The ServletResponse we should pass on to any request dispatcher
170      * delegations.
171      */
172     protected ServletResponse servletResponse = null;
173 
174     public ServletResponse getServletResponse() {
175 
176         return (this.servletResponse);
177 
178     }
179 
180     public void setServletResponse(ServletResponse servletResponse) {
181 
182         this.servletResponse = servletResponse;
183 
184     }
185 
186 
187 }