001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.controller;
023:
024: import org.jboss.portal.common.invocation.AbstractInvocationContext;
025: import org.jboss.portal.common.invocation.InterceptorStack;
026: import org.jboss.portal.common.invocation.InvocationException;
027: import org.jboss.portal.common.servlet.BufferingRequestWrapper;
028: import org.jboss.portal.common.servlet.BufferingResponseWrapper;
029: import org.jboss.portal.core.aspects.server.UserInterceptor;
030: import org.jboss.portal.core.model.portal.navstate.PortalObjectNavigationalStateContext;
031: import org.jboss.portal.identity.User;
032: import org.jboss.portal.server.ServerInvocation;
033: import org.jboss.portal.server.ServerInvocationContext;
034: import org.jboss.portal.server.ServerURL;
035: import org.jboss.portal.server.request.URLContext;
036: import org.jboss.portal.server.request.URLFormat;
037: import org.jboss.portal.web.ServletContainer;
038:
039: import javax.servlet.RequestDispatcher;
040: import javax.servlet.ServletContext;
041: import javax.servlet.http.HttpServletRequest;
042: import java.util.Map;
043:
044: /**
045: * The context of the controller.
046: *
047: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
048: * @version $Revision: 8786 $
049: */
050: public class ControllerContext extends AbstractInvocationContext {
051:
052: /** . */
053: public static final int CLASSIC_TYPE = 0;
054:
055: /** . */
056: public static final int AJAX_TYPE = 1;
057:
058: /** The server invocation. */
059: private final ServerInvocation serverInvocation;
060:
061: /** The controller. */
062: private final Controller controller;
063:
064: /** . */
065: private final int type;
066:
067: /** The depth of the call stack. */
068: private int depth;
069:
070: public ControllerContext(ServerInvocation serverInvocation,
071: Controller controller) {
072: if (serverInvocation == null) {
073: throw new IllegalArgumentException();
074: }
075: if (controller == null) {
076: throw new IllegalArgumentException();
077: }
078:
079: //
080: this .serverInvocation = serverInvocation;
081: this .controller = controller;
082: this .depth = 0;
083:
084: //
085: HttpServletRequest req = serverInvocation.getServerContext()
086: .getClientRequest();
087: String value = req.getHeader("ajax");
088: if ("true".equalsIgnoreCase(value)) {
089: type = AJAX_TYPE;
090: } else {
091: type = CLASSIC_TYPE;
092: }
093:
094: //
095: addResolver(ControllerCommand.REQUEST_SCOPE, serverInvocation
096: .getContext());
097: addResolver(ControllerCommand.SESSION_SCOPE, serverInvocation
098: .getContext());
099: addResolver(
100: ControllerCommand.NAVIGATIONAL_STATE_SCOPE,
101: new PortalObjectNavigationalStateContext(
102: serverInvocation
103: .getContext()
104: .getAttributeResolver(
105: ControllerCommand.PRINCIPAL_SCOPE)));
106: addResolver(ControllerCommand.PRINCIPAL_SCOPE, serverInvocation
107: .getContext());
108: }
109:
110: public ServletContainer getServletContainer() {
111: return serverInvocation.getRequest().getServer()
112: .getServletContainer();
113: }
114:
115: public ControllerResponse execute(ControllerCommand command)
116: throws ControllerException, InvocationException {
117: if (command == null) {
118: throw new IllegalArgumentException();
119: }
120:
121: // Contextualize
122: command.createContext(this );
123:
124: //
125: int oldDepth = depth;
126:
127: //
128: try {
129: depth++;
130:
131: // Execute
132: InterceptorStack commandStack = controller
133: .getStackFactory().getInterceptorStack();
134:
135: //
136: return (ControllerResponse) command.invoke(commandStack);
137: } catch (Exception e) {
138: ControllerCommand.rethrow(e);
139: throw new Error("Should not happen");
140: } finally {
141: //
142: depth = oldDepth;
143:
144: // Call destroy
145: command.destroyContext();
146: }
147: }
148:
149: public int getType() {
150: return type;
151: }
152:
153: public int getDepth() {
154: return depth;
155: }
156:
157: /**
158: * Render the command as an URL or return null if it is not possible.
159: *
160: * @param cmd the command to render
161: * @param urlContext the url context
162: * @param urlFormat the url format
163: * @return the URL as a string or null
164: */
165: public String renderURL(ControllerCommand cmd,
166: URLContext urlContext, URLFormat urlFormat) {
167: ServerURL serverURL = controller.getURLFactory().doMapping(
168: this , serverInvocation, cmd);
169:
170: //
171: if (serverURL == null) {
172: return null;
173: } else {
174: return serverInvocation.getResponse().renderURL(serverURL,
175: urlContext, urlFormat);
176: }
177: }
178:
179: public ServerInvocation getServerInvocation() {
180: return serverInvocation;
181: }
182:
183: public Controller getController() {
184: return controller;
185: }
186:
187: public User getUser() {
188: return (User) getAttribute(ServerInvocation.PRINCIPAL_SCOPE,
189: UserInterceptor.USER_KEY);
190: }
191:
192: public Map getUserProfile() {
193: return (Map) getAttribute(ServerInvocation.PRINCIPAL_SCOPE,
194: UserInterceptor.PROFILE_KEY);
195: }
196:
197: public ControllerRequestDispatcher getRequestDispatcher(
198: String contextPath, String path) {
199: ServerInvocationContext serverContext = serverInvocation
200: .getServerContext();
201: ServletContext servletContext = serverContext
202: .getClientRequest().getSession().getServletContext()
203: .getContext(contextPath);
204: RequestDispatcher rd = servletContext
205: .getRequestDispatcher(path);
206:
207: //
208: if (rd != null) {
209: BufferingRequestWrapper bufferReq = new BufferingRequestWrapper(
210: serverContext.getClientRequest(), contextPath,
211: serverInvocation.getRequest().getLocales());
212: BufferingResponseWrapper bufferResp = new BufferingResponseWrapper(
213: serverContext.getClientResponse());
214: return new ControllerRequestDispatcher(rd, bufferReq,
215: bufferResp);
216: }
217:
218: //
219: return null;
220: }
221: }
|