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 Public 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
55 WOApplication *app = [WOApplication application];
56 WOResponse *response = nil;
59 // invalid session ID (or no session-ID ?!, which is no error ?) */
60 response = [app handleSessionRestorationErrorInContext:_ctx];
63 WOSession *session = nil;
65 if ((session = [app restoreSessionWithID:_sid inContext:_ctx])) {
66 // awake restored session
67 [_ctx setSession:session];
68 [session _awakeWithContext:_ctx];
74 response = [app handleSessionRestorationErrorInContext:_ctx];
81 The request handler path of a component URI looks like this:
83 sessionID/componentName/contextID/elementID/instance/server
86 - (WOResponse *)handleRequest:(WORequest *)_request {
87 // TODO: this should be integrated into the WORequestHandler default
89 NSString *sessionID = nil;
90 WOApplication *application = nil;
92 WOResponse *response = nil;
93 WOSession *session = nil;
94 WOComponent *component = nil;
96 NSString *handlerPath = nil;
98 if (_request == nil) return nil;
100 if ([self doesRejectFavicon] && [[_request uri] isNotNull]) {
101 // TODO: code copied from WORequestHandler ...
102 if ([@"/favicon.ico" isEqualToString:[_request uri]]) {
103 response = [WOResponse responseWithRequest:_request];
104 [response setStatus:404 /* not found */];
105 [self debugWithFormat:@"rejected favicon request: %@", [_request uri]];
110 application = [WOApplication application];
111 handlerPath = [_request requestHandlerPath];
114 [self logWithFormat:@"[component request handler] path: '%@'", handlerPath];
117 if (![application allowsConcurrentRequestHandling]) {
118 [application lockRequestHandling];
122 context = [WOContext contextWithRequest:_request];
123 [application _setCurrentContext:context];
126 parse handler path (URL)
130 session/context.element-id
132 if ([handlerPath length] > 0) {
133 NSArray *spath = [_request requestHandlerPathArray];
135 if ([spath count] > 1)
136 [context setRequestSenderID:[spath objectAtIndex:1]];
137 if ([spath count] > 0)
138 sessionID = [spath objectAtIndex:0];
141 if ([sessionID length] == 0)
142 sessionID = [application sessionIDFromRequest:_request];
145 [self logWithFormat:@"%s: made context %@ (cid=%@, sn=%@) ..",
146 __PRETTY_FUNCTION__, context, [context contextID], sessionID];
151 /* restore or create session */
152 if ([sessionID isNotNull]) {
153 response = [self restoreSessionWithID:sessionID inContext:context];
154 session = response ? nil : [context session];
156 //[self logWithFormat:@"restored session (id=%@): %@", sessionID, session];
158 if ([session isNotNull]) {
159 /* awake stored page */
160 component = [session restorePageForContextID:[context currentElementID]];
162 if (component == nil)
163 response = [application handlePageRestorationErrorInContext:context];
166 [self logWithFormat:@"%s: restored request component: %@",
167 __PRETTY_FUNCTION__, component];
171 else if (response == nil) {
172 [[WOApplication application] warnWithFormat:
173 @"got no session restoration error, "
174 @"but missing session !"];
178 /* create new session */
179 session = [application _initializeSessionInContext:context];
180 if ([session isNotNull]) {
181 /* awake created session */
183 component = [application pageWithName:nil inContext:context];
186 response = [application handleSessionCreationErrorInContext:context];
189 if ((session != nil) && (component != nil) && (response == nil)) {
190 WOComponent *newPage = nil;
192 [[session retain] autorelease];
195 NSAssert(application, @"missing application object ..");
196 NSAssert(session, @"missing session object ..");
199 /* set request page in context */
200 [context setPage:component];
202 /* run take-values phase */
203 [application takeValuesFromRequest:_request inContext:context];
205 /* run invoke-action phase */
206 newPage = [application invokeActionForRequest:_request inContext:context];
208 /* process resulting page */
209 if (newPage == nil) {
210 if ((newPage = [context page]) == nil) {
211 newPage = [application pageWithName:nil inContext:context];
212 [context setPage:newPage];
215 else if ([newPage isKindOfClass:[WOComponent class]])
216 [context setPage:newPage];
218 [self debugWithFormat:@"%s: new page: %@", __PRETTY_FUNCTION__, newPage];
220 /* generate response */
222 response = [self generateResponseForComponent:[context page]
224 application:application];
227 [self warnWithFormat:@"%s: did not enter request/response transaction ...",
228 __PRETTY_FUNCTION__];
234 [context sleepComponents];
239 if (session != nil) {
240 if ([context savePageRequired])
241 [session savePage:[context page]];
243 [self logWithFormat:@"saving session %@", [session sessionID]];
245 if ([session storesIDsInCookies]) {
246 [self logWithFormat:@"add cookie to session: %@", session];
247 [self addCookiesForSession:session
253 [application saveSessionForContext:context];
255 [self saveSession:session
257 withResponse:response
258 application:application];
267 [application unlockRequestHandling];
271 [application _setCurrentContext:nil];
275 @end /* WOComponentRequestHandler */