2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "WOComponentRequestHandler.h"
23 #include "WORequestHandler+private.h"
24 #include "WOContext+private.h"
25 #include <NGObjWeb/WOApplication.h>
26 #include <NGObjWeb/WORequest.h>
27 #include <NGObjWeb/WOResponse.h>
28 #include <NGObjWeb/WOSession.h>
29 #include <NGObjWeb/WOComponent.h>
32 @interface WOApplication(Privates)
33 - (WOSession *)_initializeSessionInContext:(WOContext *)_ctx;
34 - (void)_setCurrentContext:(WOContext *)_ctx;
37 @implementation WOComponentRequestHandler
40 return [super version] + 0 /* 2 */;
43 NSAssert2([super version] == 2,
44 @"invalid superclass (%@) version %i !",
45 NSStringFromClass([self superclass]), [super version]);
48 - (WOResponse *)restoreSessionWithID:(NSString *)_sid
49 inContext:(WOContext *)_ctx
51 WOApplication *app = [WOApplication application];
52 WOResponse *response = nil;
55 // invalid session ID (or no session-ID ?!, which is no error ?) */
56 response = [app handleSessionRestorationErrorInContext:_ctx];
59 WOSession *session = nil;
61 if ((session = [app restoreSessionWithID:_sid inContext:_ctx])) {
62 // awake restored session
63 [_ctx setSession:session];
64 [session _awakeWithContext:_ctx];
70 response = [app handleSessionRestorationErrorInContext:_ctx];
77 The request handler path of a component URI looks like this:
79 sessionID/componentName/contextID/elementID/instance/server
82 - (WOResponse *)handleRequest:(WORequest *)_request {
83 NSString *sessionID = nil;
84 WOApplication *application = nil;
85 WOContext *context = nil;
86 WOResponse *response = nil;
87 WOSession *session = nil;
88 WOComponent *component = nil;
90 NSString *handlerPath = nil;
92 if (_request == nil) return nil;
94 application = [WOApplication application];
95 handlerPath = [_request requestHandlerPath];
98 NSLog(@"[component request handler] path=%@ ..", handlerPath);
101 if (![application allowsConcurrentRequestHandling]) {
102 [application lockRequestHandling];
106 context = [WOContext contextWithRequest:_request];
107 [application _setCurrentContext:context];
110 parse handler path (URL)
114 session/context.element-id
116 if ([handlerPath length] > 0) {
117 NSArray *spath = [_request requestHandlerPathArray];
119 if ([spath count] > 1)
120 [context setRequestSenderID:[spath objectAtIndex:1]];
121 if ([spath count] > 0)
122 sessionID = [spath objectAtIndex:0];
125 if ([sessionID length] == 0)
126 sessionID = [application sessionIDFromRequest:_request];
129 NSLog(@"%s: made context %@ (cid=%@, sn=%@) ..", __PRETTY_FUNCTION__
130 context, [context contextID], sessionID);
135 /* restore or create session */
137 response = [self restoreSessionWithID:sessionID inContext:context];
138 session = response ? nil : [context session];
141 /* awake stored page */
142 component = [session restorePageForContextID:[context currentElementID]];
144 if (component == nil)
145 response = [application handlePageRestorationErrorInContext:context];
148 NSLog(@"%s: restored request component %@", __PRETTY_FUNCTION__,
153 else if (response == nil) {
154 [[WOApplication application] warnWithFormat:
155 @"got no session restoration error, "
156 @"but missing session !"];
160 /* create new session */
161 session = [application _initializeSessionInContext:context];
163 /* awake created session */
165 component = [application pageWithName:nil inContext:context];
168 response = [application handleSessionCreationErrorInContext:context];
171 if ((session != nil) && (component != nil) && (response == nil)) {
172 WOComponent *newPage = nil;
174 [[session retain] autorelease];
177 NSAssert(application, @"missing application object ..");
178 NSAssert(session, @"missing session object ..");
181 /* set request page in context */
182 [context setPage:component];
184 /* run take-values phase */
185 [application takeValuesFromRequest:_request inContext:context];
187 /* run invoke-action phase */
188 newPage = [application invokeActionForRequest:_request inContext:context];
190 /* process resulting page */
191 if (newPage == nil) {
192 if ((newPage = [context page]) == nil) {
193 newPage = [application pageWithName:nil inContext:context];
194 [context setPage:newPage];
197 else if ([newPage isKindOfClass:[WOComponent class]])
198 [context setPage:newPage];
201 [self debugWithFormat:@"%s: new page: %@", __PRETTY_FUNCTION__, newPage];
204 /* generate response */
205 #if 1 /* new code, ensure that _fixupResponse is called */
206 response = [self generateResponseForComponent:[context page]
208 application:application];
210 response = [context response];
211 [application appendToResponse:response inContext:context];
215 [self warnWithFormat:@"%s: did not enter request/response transaction ...",
216 __PRETTY_FUNCTION__];
222 [context sleepComponents];
227 if ([context savePageRequired])
228 [session savePage:[context page]];
230 NSLog(@"saving session %@", [session sessionID]);
231 [application saveSessionForContext:context];
239 [application unlockRequestHandling];
243 [application _setCurrentContext:nil];
247 @end /* WOComponentRequestHandler */