KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > cocoon > CocoonContext


1 package org.sapia.soto.state.cocoon;
2
3 import org.apache.cocoon.environment.ObjectModelHelper;
4 import org.apache.cocoon.environment.Request;
5 import org.apache.cocoon.environment.Response;
6 import org.apache.cocoon.environment.Session;
7 import org.apache.cocoon.environment.http.HttpRequest;
8 import org.apache.cocoon.environment.http.HttpResponse;
9
10 import org.sapia.soto.Debug;
11 import org.sapia.soto.Env;
12 import org.sapia.soto.state.ContextImpl;
13 import org.sapia.soto.state.Input;
14 import org.sapia.soto.state.MapScope;
15 import org.sapia.soto.state.Output;
16 import org.sapia.soto.state.xml.XMLContext;
17
18 import org.sapia.ubik.net.UriSyntaxException;
19
20 import org.xml.sax.ContentHandler JavaDoc;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import java.util.Map JavaDoc;
27 import java.util.Stack JavaDoc;
28
29
30 /**
31  * Implements the <code>Context</code> interface on top of Cocoon-related objects.
32  * An instance of this class contains the following scopes:
33  * <ul>
34  * <li>env : corresponds to the Soto container; used to acquire service instances.
35  * <li>session: corresponds to the Cocoon session (allows to set/get session values).
36  * <li>request: corresponds to the Cocoon request (allows to set/get request attributes).
37  * <li>params : corresponds to the Cocoon request parameters (allows to retrieve parameters).
38  * <li>view : corresponds to the eventual view that is to be generated - the values bound
39  * to the view are exported in to the target templating/generation engine's execution variables.
40  * </ul>
41  * <p>
42  * An instance of this class in addition holds the <code>ContentHandler</code> implementation
43  * that will eventually receive SAX events from the eventual view.
44  *
45  * @author Yanick Duchesne
46  * <dl>
47  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
48  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
49  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
50  * </dl>
51  */

52 public class CocoonContext extends ContextImpl implements XMLContext, Consts, Input, Output {
53   public static final String JavaDoc ENV_SCOPE = "env";
54   public static final String JavaDoc SESSION_SCOPE = "session";
55   public static final String JavaDoc REQUEST_SCOPE = "request";
56   public static final String JavaDoc RESPONSE_SCOPE = "response";
57   public static final String JavaDoc REQ_PARAM_SCOPE = "params";
58   public static final String JavaDoc VIEW_SCOPE = "view";
59   public static final String JavaDoc MODEL_KEY = "Model";
60   public static final String JavaDoc CONTEXT_PATH_KEY = "ContextPath";
61     public static final String JavaDoc CONTEXT_PATH_KEY2 = "contextPath";
62   private ContentHandler JavaDoc _handler;
63   
64   public CocoonContext(Env env, ContentHandler JavaDoc handler, Map JavaDoc objectModel) {
65     _handler = handler;
66
67     Request req = ObjectModelHelper.getRequest(objectModel);
68     Response res = ObjectModelHelper.getResponse(objectModel);
69     super.addScope(SESSION_SCOPE, new SessionScope(req));
70     super.addScope(REQUEST_SCOPE, new RequestScope(req));
71     super.addScope(RESPONSE_SCOPE, new ResponseScope(res));
72     super.addScope(REQ_PARAM_SCOPE, new ReqParamScope(req));
73     super.addScope(ENV_SCOPE, new EnvScope(env));
74
75     MapScope view = new MapScope();
76     view.put(CONTEXT_PATH_KEY, req.getContextPath());
77         view.put(CONTEXT_PATH_KEY2, req.getContextPath());
78     super.addScope(VIEW_SCOPE, view);
79   }
80     
81     /**
82      * @see org.sapia.soto.state.xml.XMLContext#getContentHandler()
83      */

84   public ContentHandler JavaDoc getContentHandler() {
85     return _handler;
86   }
87   
88     /**
89      * @see org.sapia.soto.state.xml.XMLContext#setContentHandler(org.xml.sax.ContentHandler)
90      */

91     public void setContentHandler(ContentHandler JavaDoc handler) {
92         _handler = handler;
93     }
94
95   /**
96    * @return the <code>Map</code> of values corresponding to the <code>view</code> scope.
97    */

98   public Map JavaDoc getViewParams() {
99     return (MapScope) super.getScope(VIEW_SCOPE);
100   }
101
102   /**
103    * Sets the <code>ContentHandler</code> that this instance holds to <code>null</code>.
104    */

105   public void disableOutput() {
106     _handler = null;
107   }
108
109   /**
110    * @return the <code>Request</code> that this instance holds.
111    */

112   public Request getRequest() {
113     return ((RequestScope) super.getScope(REQUEST_SCOPE)).getRequest();
114   }
115
116   /**
117    * @return the <code>Response</code> that this instance holds.
118    */

119   public Response getResponse() {
120     return ((ResponseScope) super.getScope(RESPONSE_SCOPE)).getResponse();
121   }
122
123   /**
124    * @see org.sapia.soto.state.Input#getInputStream()
125    */

126   public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
127     return ((HttpRequest) getRequest()).getInputStream();
128   }
129
130   /**
131    * @see org.sapia.soto.state.Output#getOutputStream()
132    */

133   public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
134     return ((HttpResponse) getResponse()).getOutputStream();
135   }
136
137   void executeCall(String JavaDoc baseUri, String JavaDoc targetState, String JavaDoc targetModule,
138     String JavaDoc returnState, String JavaDoc returnModule)
139     throws IOException JavaDoc, UriSyntaxException {
140     Session sess = ((SessionScope) super.getScope(SESSION_SCOPE)).createSession();
141     HttpRequest req = (HttpRequest) ((RequestScope) super.getScope(REQUEST_SCOPE)).getRequest();
142     HttpResponse res = (HttpResponse) ((ResponseScope) super.getScope(RESPONSE_SCOPE)).getResponse();
143     String JavaDoc uriStr = req.getRequestURI();
144     String JavaDoc contextPath = req.getContextPath();
145
146     if (Debug.DEBUG) {
147       Debug.debug("request URI: " + uriStr);
148       Debug.debug("server name: " + req.getServerName());
149       Debug.debug("server port: " + req.getServerPort());
150       Debug.debug("context path: " + contextPath);
151     }
152
153     String JavaDoc target;
154
155     if (baseUri != null) {
156       target = baseUri;
157     } else {
158       target = "";
159     }
160
161     target = target + targetState + "?" + CONTINUATION_KEY + "=true";
162
163     Stack JavaDoc stack;
164
165     if ((stack = (Stack JavaDoc) sess.getAttribute(
166               CocoonStateMachineService.CONTINUATION_KEY)) == null) {
167       stack = new Stack JavaDoc();
168     }
169
170     Call call = new Call(targetState, targetModule, returnState, returnModule,
171         _stack);
172
173     if (targetState == null) {
174       call.setExecuted();
175     }
176
177     stack.push(call);
178     sess.setAttribute(CocoonStateMachineService.CONTINUATION_KEY, stack);
179
180     if (Debug.DEBUG) {
181       Debug.debug("Redirecting to: " + target);
182     }
183
184     res.sendRedirect(target);
185     disableOutput();
186   }
187
188   void setStack(Stack JavaDoc context) {
189     super._stack = context;
190   }
191
192 }
193
Popular Tags