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 ; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 26 import java.util.Map ; 27 import java.util.Stack ; 28 29 30 52 public class CocoonContext extends ContextImpl implements XMLContext, Consts, Input, Output { 53 public static final String ENV_SCOPE = "env"; 54 public static final String SESSION_SCOPE = "session"; 55 public static final String REQUEST_SCOPE = "request"; 56 public static final String RESPONSE_SCOPE = "response"; 57 public static final String REQ_PARAM_SCOPE = "params"; 58 public static final String VIEW_SCOPE = "view"; 59 public static final String MODEL_KEY = "Model"; 60 public static final String CONTEXT_PATH_KEY = "ContextPath"; 61 public static final String CONTEXT_PATH_KEY2 = "contextPath"; 62 private ContentHandler _handler; 63 64 public CocoonContext(Env env, ContentHandler handler, Map 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 84 public ContentHandler getContentHandler() { 85 return _handler; 86 } 87 88 91 public void setContentHandler(ContentHandler handler) { 92 _handler = handler; 93 } 94 95 98 public Map getViewParams() { 99 return (MapScope) super.getScope(VIEW_SCOPE); 100 } 101 102 105 public void disableOutput() { 106 _handler = null; 107 } 108 109 112 public Request getRequest() { 113 return ((RequestScope) super.getScope(REQUEST_SCOPE)).getRequest(); 114 } 115 116 119 public Response getResponse() { 120 return ((ResponseScope) super.getScope(RESPONSE_SCOPE)).getResponse(); 121 } 122 123 126 public InputStream getInputStream() throws IOException { 127 return ((HttpRequest) getRequest()).getInputStream(); 128 } 129 130 133 public OutputStream getOutputStream() throws IOException { 134 return ((HttpResponse) getResponse()).getOutputStream(); 135 } 136 137 void executeCall(String baseUri, String targetState, String targetModule, 138 String returnState, String returnModule) 139 throws IOException , 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 uriStr = req.getRequestURI(); 144 String 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 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 stack; 164 165 if ((stack = (Stack ) sess.getAttribute( 166 CocoonStateMachineService.CONTINUATION_KEY)) == null) { 167 stack = new Stack (); 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 context) { 189 super._stack = context; 190 } 191 192 } 193 | Popular Tags |