2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Pulic License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE 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 SOPE; 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 @interface WORequestHandler(URI)
38 - (BOOL)doesRejectFavicon;
41 @implementation WOComponentRequestHandler
44 return [super version] + 0 /* 2 */;
47 NSAssert2([super version] == 2,
48 @"invalid superclass (%@) version %i !",
49 NSStringFromClass([self superclass]), [super version]);
52 - (WOResponse *)restoreSessionWithID:(NSString *)_sid
53 inContext:(WOContext *)_ctx
58 app = [WOApplication application];
60 // invalid session ID (or no session-ID ?!, which is no error ?) */
61 return [app handleSessionRestorationErrorInContext:_ctx];
64 if ((session = [app restoreSessionWithID:_sid inContext:_ctx]) != nil) {
65 /* awake restored session */
66 [_ctx setSession:session];
67 [session _awakeWithContext:_ctx];
73 return [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 // TODO: this should be integrated into the WORequestHandler default
85 NSString *sessionID = nil;
86 WOApplication *application = nil;
88 WOResponse *response = nil;
89 WOSession *session = nil;
90 WOComponent *component = nil;
92 NSString *handlerPath = nil;
94 if (_request == nil) return nil;
96 if ([self doesRejectFavicon] && [[_request uri] isNotNull]) {
97 // TODO: code copied from WORequestHandler ...
98 if ([@"/favicon.ico" isEqualToString:[_request uri]]) {
99 response = [WOResponse responseWithRequest:_request];
100 [response setStatus:404 /* not found */];
101 [self debugWithFormat:@"rejected favicon request: %@", [_request uri]];
106 application = [WOApplication application];
107 handlerPath = [_request requestHandlerPath];
110 [self logWithFormat:@"[component request handler] path: '%@'", handlerPath];
113 if (![application allowsConcurrentRequestHandling]) {
114 [application lockRequestHandling];
118 context = [WOContext contextWithRequest:_request];
119 [application _setCurrentContext:context];
122 parse handler path (URL)
126 session/context.element-id
128 if ([handlerPath isNotEmpty]) {
129 NSArray *spath = [_request requestHandlerPathArray];
131 if ([spath count] > 1)
132 [context setRequestSenderID:[spath objectAtIndex:1]];
133 if ([spath isNotEmpty])
134 sessionID = [spath objectAtIndex:0];
137 if (![sessionID isNotEmpty])
138 sessionID = [application sessionIDFromRequest:_request];
141 [self logWithFormat:@"%s: made context %@ (cid=%@, sn=%@) ..",
142 __PRETTY_FUNCTION__, context, [context contextID], sessionID];
147 /* restore or create session */
148 if ([sessionID isNotEmpty]) {
149 if ((response = [self restoreSessionWithID:sessionID inContext:context]))
153 Note: this creates a _new_ session if the restoration handler did not
154 return a response! We check that below by comparing the session
157 session = [context session];
160 [self debugWithFormat:@"restored session (id=%@): %@", sessionID, session];
162 if (session && (![sessionID isEqualToString:[session sessionID]])) {
163 [self errorWithFormat:@"session-ids do not match (%@ vs %@)",
164 sessionID, [session sessionID]];
167 if ([session isNotNull]) {
171 only try to restore a page if we still have the same session and if
172 the request contains an element-id (eg if we reconnect to the main
173 URL we do not have an element-id
175 eid = [context currentElementID];
176 if ([sessionID isEqualToString:[session sessionID]] && eid != nil) {
177 /* awake stored page from "old" session */
178 component = [session restorePageForContextID:eid];
180 if (component == nil) {
181 [self logWithFormat:@"could not restore component from session: %@",
183 response = [application handlePageRestorationErrorInContext:context];
186 else /* a new session was created (but no restore-error response ret.) */
187 component = [application pageWithName:nil inContext:context];
189 else if (response == nil) {
190 [[WOApplication application] warnWithFormat:
191 @"got no session restoration error, "
192 @"but missing session!"];
196 /* create new session */
197 session = [application _initializeSessionInContext:context];
198 if ([session isNotNull]) {
199 /* awake created session */
201 component = [application pageWithName:nil inContext:context];
204 response = [application handleSessionCreationErrorInContext:context];
207 if ((session != nil) && (component != nil) && (response == nil)) {
208 WOComponent *newPage = nil;
210 [[session retain] autorelease];
213 NSAssert(application, @"missing application object ..");
214 NSAssert(session, @"missing session object ..");
217 /* set request page in context */
218 [context setPage:component];
220 /* run take-values phase */
221 [application takeValuesFromRequest:_request inContext:context];
223 /* run invoke-action phase */
224 newPage = [application invokeActionForRequest:_request inContext:context];
226 /* process resulting page */
227 if (newPage == nil) {
228 if ((newPage = [context page]) == nil) {
229 newPage = [application pageWithName:nil inContext:context];
230 [context setPage:newPage];
233 else if ([newPage isKindOfClass:[WOComponent class]])
234 [context setPage:newPage];
236 [self debugWithFormat:@"%s: new page: %@", __PRETTY_FUNCTION__, newPage];
238 /* generate response */
240 response = [self generateResponseForComponent:[context page]
242 application:application];
245 [self warnWithFormat:@"%s: did not enter request/response transaction ...",
246 __PRETTY_FUNCTION__];
252 [context sleepComponents];
257 if (session != nil) {
258 if ([context savePageRequired])
259 [session savePage:[context page]];
261 [self debugWithFormat:@"saving session %@", [session sessionID]];
263 if ([session storesIDsInCookies]) {
264 [self debugWithFormat:@"add cookie to session: %@", session];
265 [self addCookiesForSession:session
270 #if 1 // TODO: explain that
271 [application saveSessionForContext:context];
273 [self saveSession:session
275 withResponse:response
276 application:application];
285 [application unlockRequestHandling];
289 [application _setCurrentContext:nil];
293 @end /* WOComponentRequestHandler */