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.InterceptorStackFactory;
025: import org.jboss.portal.core.controller.command.mapper.CommandFactory;
026: import org.jboss.portal.core.controller.command.mapper.URLFactory;
027: import org.jboss.portal.core.controller.command.response.ErrorResponse;
028: import org.jboss.portal.core.controller.handler.AjaxResponse;
029: import org.jboss.portal.core.controller.handler.CommandForward;
030: import org.jboss.portal.core.controller.handler.HTTPResponse;
031: import org.jboss.portal.core.controller.handler.HandlerResponse;
032: import org.jboss.portal.core.controller.handler.ResponseForward;
033: import org.jboss.portal.core.controller.handler.ResponseHandler;
034: import org.jboss.portal.core.controller.handler.ResponseHandlerException;
035: import org.jboss.portal.core.model.CustomizationManager;
036: import org.jboss.portal.core.model.instance.InstanceContainer;
037: import org.jboss.portal.core.model.portal.PortalObjectContainer;
038: import org.jboss.portal.core.model.portal.content.ContentRendererRegistry;
039: import org.jboss.portal.core.model.portal.control.page.PageControlPolicy;
040: import org.jboss.portal.jems.as.system.AbstractJBossService;
041: import org.jboss.portal.security.spi.auth.PortalAuthorizationManagerFactory;
042: import org.jboss.portal.server.RequestController;
043: import org.jboss.portal.server.ServerException;
044: import org.jboss.portal.server.ServerInvocation;
045: import org.jboss.portal.theme.PageService;
046:
047: import javax.servlet.ServletException;
048: import java.io.IOException;
049:
050: /**
051: * @author <a href="mailto:mholzner@novell.com">Martin Holzner</a>
052: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
053: * @version $Revision: 8786 $
054: */
055: public class Controller extends AbstractJBossService implements
056: RequestController {
057:
058: /** . */
059: protected PageService pageService;
060:
061: /** . */
062: protected CommandFactory commandFactory;
063:
064: /** . */
065: protected URLFactory urlFactory;
066:
067: /** . */
068: protected InterceptorStackFactory stackFactory;
069:
070: /** . */
071: protected PortalObjectContainer portalObjectContainer;
072:
073: /** . */
074: protected InstanceContainer instanceContainer;
075:
076: /** . */
077: protected PortalAuthorizationManagerFactory portalAuthorizationManagerFactory;
078:
079: /** . */
080: protected CustomizationManager customizationManager;
081:
082: /** . */
083: protected ContentRendererRegistry contentRendererRegistry;
084:
085: /** . */
086: protected ResponseHandler responseHandler;
087:
088: /** . */
089: protected PageControlPolicy pageControlPolicy;
090:
091: public ContentRendererRegistry getContentRendererRegistry() {
092: return contentRendererRegistry;
093: }
094:
095: public void setContentRendererRegistry(
096: ContentRendererRegistry contentRendererRegistry) {
097: this .contentRendererRegistry = contentRendererRegistry;
098: }
099:
100: public CustomizationManager getCustomizationManager() {
101: return customizationManager;
102: }
103:
104: public void setCustomizationManager(
105: CustomizationManager customizationManager) {
106: this .customizationManager = customizationManager;
107: }
108:
109: public PortalAuthorizationManagerFactory getPortalAuthorizationManagerFactory() {
110: return portalAuthorizationManagerFactory;
111: }
112:
113: public void setPortalAuthorizationManagerFactory(
114: PortalAuthorizationManagerFactory portalAuthorizationManagerFactory) {
115: this .portalAuthorizationManagerFactory = portalAuthorizationManagerFactory;
116: }
117:
118: public InstanceContainer getInstanceContainer() {
119: return instanceContainer;
120: }
121:
122: public void setInstanceContainer(InstanceContainer instanceContainer) {
123: this .instanceContainer = instanceContainer;
124: }
125:
126: public PortalObjectContainer getPortalObjectContainer() {
127: return portalObjectContainer;
128: }
129:
130: public void setPortalObjectContainer(
131: PortalObjectContainer portalObjectContainer) {
132: this .portalObjectContainer = portalObjectContainer;
133: }
134:
135: public URLFactory getURLFactory() {
136: return urlFactory;
137: }
138:
139: public void setURLFactory(URLFactory urlFactory) {
140: this .urlFactory = urlFactory;
141: }
142:
143: public CommandFactory getCommandFactory() {
144: return commandFactory;
145: }
146:
147: public void setCommandFactory(CommandFactory commandFactory) {
148: this .commandFactory = commandFactory;
149: }
150:
151: public PageService getPageService() {
152: return pageService;
153: }
154:
155: public void setPageService(PageService pageService) {
156: this .pageService = pageService;
157: }
158:
159: public InterceptorStackFactory getStackFactory() {
160: return stackFactory;
161: }
162:
163: public void setStackFactory(InterceptorStackFactory stackFactory) {
164: this .stackFactory = stackFactory;
165: }
166:
167: public ResponseHandler getResponseHandler() {
168: return responseHandler;
169: }
170:
171: public void setResponseHandler(ResponseHandler responseHandler) {
172: this .responseHandler = responseHandler;
173: }
174:
175: public PageControlPolicy getPageControlPolicy() {
176: return pageControlPolicy;
177: }
178:
179: public void setPageControlPolicy(PageControlPolicy pageControlPolicy) {
180: this .pageControlPolicy = pageControlPolicy;
181: }
182:
183: public final void handle(ServerInvocation invocation)
184: throws ServerException {
185: // Create controller context
186: ControllerContext controllerContext = new ControllerContext(
187: invocation, this );
188:
189: // Invoke the chain that creates the initial command
190: ControllerCommand cmd = commandFactory.doMapping(
191: controllerContext, invocation, invocation
192: .getServerContext().getPortalHost(), invocation
193: .getServerContext().getPortalContextPath(),
194: invocation.getServerContext().getPortalRequestPath());
195:
196: // Handle that case
197: if (cmd == null) {
198: throw new ServerException(
199: "No command was produced by the command factory");
200: }
201:
202: // Handle the command created
203: processCommand(controllerContext, cmd);
204: }
205:
206: /**
207: * Handle a command which means it executes the command and reacts upon the response created by the command.
208: *
209: * @param controllerContext the controller context
210: * @param command the command
211: * @throws org.jboss.portal.server.ServerException
212: *
213: */
214: protected void processCommand(ControllerContext controllerContext,
215: ControllerCommand command) throws ServerException {
216: ControllerResponse response;
217:
218: //
219: try {
220: response = controllerContext.execute(command);
221: } catch (CommandRedirectionException e) {
222: processHandlerResponse(controllerContext, command,
223: new CommandForward(e.getRedirection(), null));
224:
225: // We are done
226: return;
227: } catch (ControllerException e) {
228: response = new ErrorResponse(e, true);
229: }
230:
231: //
232: if (response == null) {
233: response = new ErrorResponse(
234: "No response was provided by the invocation of "
235: + command, true);
236: }
237:
238: //
239: processCommandResponse(controllerContext, command, response);
240: }
241:
242: protected void processCommandResponse(
243: ControllerContext controllerContext,
244: ControllerCommand command, ControllerResponse response)
245: throws ServerException {
246: // Handle the result
247: HandlerResponse handlerResponse;
248: try {
249: handlerResponse = responseHandler.processCommandResponse(
250: controllerContext, command, response);
251: } catch (ResponseHandlerException e) {
252: throw new ServerException(e);
253: }
254:
255: // Might be null if no handling done
256: if (handlerResponse == null) {
257: return;
258: }
259:
260: //
261: processHandlerResponse(controllerContext, command,
262: handlerResponse);
263: }
264:
265: protected void processHandlerResponse(
266: ControllerContext controllerContext,
267: ControllerCommand command, HandlerResponse handlerResponse)
268: throws ServerException {
269: // Find out if we can execute in the same server invocation
270: if (handlerResponse instanceof CommandForward) {
271: CommandForward forward = (CommandForward) handlerResponse;
272: processCommand(controllerContext, forward.getCommand());
273: } else if (handlerResponse instanceof ResponseForward) {
274: ResponseForward forward = (ResponseForward) handlerResponse;
275: ControllerResponse response = forward.getResponse();
276: processCommandResponse(controllerContext, command, response);
277: } else if (handlerResponse instanceof HTTPResponse) {
278: HTTPResponse hr = (HTTPResponse) handlerResponse;
279: sendResponse(controllerContext, hr);
280: } else if (handlerResponse instanceof AjaxResponse) {
281: AjaxResponse ar = (AjaxResponse) handlerResponse;
282: sendResponse(controllerContext, ar);
283: }
284: }
285:
286: /** All http responses in the stack should be handled here. */
287: protected void sendResponse(ControllerContext ctx, HTTPResponse resp) {
288: try {
289: resp.sendResponse(ctx.getServerInvocation()
290: .getServerContext());
291: } catch (IOException e) {
292: log.error("Cound not send http response", e);
293: } catch (ServletException e) {
294: log.error("Cound not send http response", e);
295: }
296: }
297:
298: /** All http responses in the stack should be handled here. */
299: protected void sendResponse(ControllerContext ctx, AjaxResponse resp) {
300: try {
301: resp.sendResponse(ctx.getServerInvocation()
302: .getServerContext());
303: } catch (IOException e) {
304: log.error("Cound not send http response", e);
305: } catch (ServletException e) {
306: log.error("Cound not send http response", e);
307: }
308: }
309: }
|