001    /*
002     * Copyright 1999-2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */ 
016    
017    package org.apache.commons.workflow.web;
018    
019    
020    import java.util.EmptyStackException;
021    import javax.servlet.ServletContext;
022    import javax.servlet.ServletRequest;
023    import javax.servlet.ServletResponse;
024    import javax.servlet.http.HttpSession;
025    import org.apache.commons.collections.ArrayStack;
026    import org.apache.commons.workflow.Context;
027    import org.apache.commons.workflow.Scope;
028    import org.apache.commons.workflow.base.BaseContext;
029    
030    
031    /**
032     * <p><strong>WebContext</strong> is a specialized <code>Context</code>
033     * implementation appropriate for web applications that run on top of a
034     * Servlet 2.2 (or later) container.  It exposes the attributes associated
035     * with requests, sessions, and the servlet context as <code>Scopes</code>
036     * within the workflow management system.</p>
037     *
038     * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
039     * @author Craig R. McClanahan
040     */
041    
042    public class WebContext extends BaseContext {
043    
044    
045        // ----------------------------------------------------- Manifest Constants
046    
047    
048        /**
049         * The scope identifier for the scope associated with the current
050         * servlet request.
051         */
052        public static final int REQUEST_SCOPE = 2;
053    
054    
055        /**
056         * The scope name for the scope associated with the current
057         * servlet request.
058         */
059        public static final String REQUEST_SCOPE_NAME = "request";
060    
061    
062        /**
063         * The scope identifier for the scope associated with the current
064         * HTTP session.
065         */
066        public static final int SESSION_SCOPE = 4;
067    
068    
069        /**
070         * The scope name for the scope associated with the current
071         * HTTP session.
072         */
073        public static final String SESSION_SCOPE_NAME = "session";
074    
075    
076        /**
077         * The scope identifier for the scope associated with the current
078         * servlet context.
079         */
080        public static final int APPLICATION_SCOPE = 6;
081    
082    
083        /**
084         * The scope name for the scope associated with the current
085         * servlet context.
086         */
087        public static final String APPLICATION_SCOPE_NAME = "application";
088    
089    
090        // ------------------------------------------------------------- Properties
091    
092    
093        /**
094         * The HttpSession that provides our associated "session" scope.
095         */
096        protected HttpSession httpSession = null;
097    
098        public HttpSession getHttpSession() {
099    
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    }