/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2000 Blacksmith, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see http://www.gnu.org */ package net.wotonomy.web; /** * An implementation of WORequestHandler that dispatches Component actions. * * @author michael@mpowers.net * @author $Author: cgruber $ * @version $Revision: 905 $ */ public class WOComponentRequestHandler extends WORequestHandler { public WOResponse handleRequest(WORequest aRequest) { WOApplication application = aRequest.application(); WOContext context = WOContext.contextWithRequest(aRequest); WOResponse response = null; // no concurrent access is allowed to a user's session: // a user/browser/machine with multiple requests will have to wait synchronized (aRequest.request.getSession()) { try { application.awake(); WOSession session = null; /* * // the NeXT way String sessionID = aRequest.sessionID(); if ( sessionID != * null ) { session = application.restoreSessionWithID( sessionID, context ); if * ( session == null ) { response = * application.handleSessionRestorationErrorInContext( context ); } } else { * session = application.createSessionForRequest( aRequest ); if ( session == * null ) { response = application.handleSessionCreationErrorInContext( context * ); } else { session.setContext( context ); } } */ // the servlet way String sessionID = aRequest.sessionID(); if (sessionID != null) { session = application.restoreSessionWithID(sessionID, context); } if (session == null) { session = application.createSessionForRequest(aRequest); if (session == null) { response = application.handleSessionCreationErrorInContext(context); } else { session.setContext(context); } } context.setSession(session); session.awake(); if (response == null) { WOComponent page; String contextID = aRequest.contextID(); if (contextID != null) { page = session.restorePageForContextID(contextID); } else { page = application.pageWithName(aRequest.pageName(), context); } if (page == null) { // FIXME: should we call this a restoration error, or do we // allow a bookmark with an expired context id to resume gracefully? page = application.pageWithName(aRequest.pageName(), context); // !response = application.handlePageRestorationErrorInContext( context ); } // !else { context.pushElement(page); // ? needed? page.ensureAwakeInContext(context); // ? shouldn't this be in WOApplication? // only take values from request if there are values in the request System.out .println("should I takeValuesFromRequest ? " + (aRequest.formValueKeys().count() > 0)); if (aRequest.formValueKeys().count() > 0) { application.takeValuesFromRequest(aRequest, context); } // only invoke action if there is a sender id to invoke WOActionResults result; System.out.println("senderID: " + aRequest.senderID()); if (aRequest.senderID() != null) { result = application.invokeAction(aRequest, context); } else { result = null; } if (result == null || result == page) // same page is returned { result = page; // generate response response = new WOResponse(); application.appendToResponse(response, context); page.sleep(); } else // different page is returned if (result instanceof WOComponent) { page.sleep(); page = (WOComponent) result; page.ensureAwakeInContext(context); context.popElement(); // removes page context.pushElement(page); // generate response response = new WOResponse(); application.appendToResponse(response, context); page.sleep(); } else // WOResponse was returnd { response = (WOResponse) result; } context.popElement(); session.sleep(); session.savePage(page); session.setContext(null); application.saveSessionForContext(context); } } } catch (Throwable t) { response = application.handleException(t, context); } application.sleep(); } return response; } } /* * $Log$ Revision 1.2 2006/02/19 01:44:02 cgruber Add xmlrpc files Remove jclark * and replace with dom4j and javax.xml.sax stuff Re-work dependencies and * imports so it all compiles. * * Revision 1.1 2006/02/16 13:22:22 cgruber Check in all sources in * eclipse-friendly maven-enabled packages. * * Revision 1.8 2003/08/07 00:15:15 chochos general cleanup (mostly removing * unused imports) * * Revision 1.7 2003/01/17 20:34:57 mpowers Better handling for components and * parents in the context's element stack. * * Revision 1.6 2003/01/15 19:50:49 mpowers Fixed issues with WOSession and * Serializable. Can now persist sessions between classloaders (hot swap of * class impls). * * Revision 1.4 2003/01/14 15:51:09 mpowers No longer calling takeValues or * invokeAction if there are no request params * * Revision 1.3 2003/01/13 22:24:34 mpowers Request-response cycle is working * with session and page persistence. * * Revision 1.1 2003/01/09 16:13:59 mpowers Implemented * WOComponentRequestHandler: Bringing the request-response cycle more into * conformance. * * Revision 1.2 2002/12/17 14:57:44 mpowers Minor corrections to WORequests's * parsing, and updated javadocs. * * Revision 1.1.1.1 2000/12/21 15:53:19 mpowers Contributing wotonomy. * * Revision 1.2 2000/12/20 16:25:50 michael Added log to all files. * * */