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 <NGObjWeb/WORequestHandler.h>
24 @interface WOPageRequestHandler : WORequestHandler
27 //#include "WOPageRequestHandler.h"
28 #include "WORequestHandler+private.h"
29 #include "WOContext+private.h"
30 #include <NGObjWeb/WOApplication.h>
31 #include <NGObjWeb/WOComponent.h>
32 #include <NGObjWeb/WODirectAction.h>
33 #include <NGObjWeb/WORequest.h>
34 #include <NGObjWeb/WOResponse.h>
35 #include <NGObjWeb/WOSession.h>
36 #include <NGObjWeb/WOComponent.h>
37 #include <NGObjWeb/WOSessionStore.h>
38 #include <NGObjWeb/WOStatisticsStore.h>
41 static BOOL perflog = NO;
42 static Class NSDateClass = Nil;
43 static BOOL debugUnknownActions = NO;
44 static BOOL debugOn = NO;
46 @interface WOComponent(Privates)
47 - (void)_awakeWithContext:(WOContext *)_ctx;
48 - (id<WOActionResults>)performActionNamed:(NSString *)_actionName;
51 @implementation WOPageRequestHandler
54 return [super version] + 0 /* 2 */;
58 NSAssert2([super version] == 2,
59 @"invalid superclass (%@) version %i !",
60 NSStringFromClass([self superclass]), [super version]);
62 NSDateClass = [NSDate class];
63 ud = [NSUserDefaults standardUserDefaults];
64 perflog = [ud boolForKey:@"WOProfilePageRequestHandler"];
65 debugOn = [ud boolForKey:@"WOPageRequestHandlerDebugEnabled"];
70 - (NSString *)loggingPrefix {
71 return @"[pg-handler]";
73 - (BOOL)isDebuggingEnabled {
78 The request handler part of a direct action URI looks like this:
80 [actionClass/]actionName[?key=value&key=value&...]
83 - (WOResponse *)handleRequest:(WORequest *)_request
84 inContext:(WOContext *)context
85 session:(WOSession *)session
86 application:(WOApplication *)app
90 id<WOActionResults> result = nil;
99 if ((pageName = [_request headerForKey:@"x-httpd-pagename"]) == nil) {
100 NSArray *handlerPath;
102 handlerPath = [_request requestHandlerPathArray];
103 switch ([handlerPath count]) {
106 actionName = @"default";
109 pageName = [handlerPath objectAtIndex:0];
110 actionName = @"default";
113 pageName = [handlerPath objectAtIndex:0];
114 actionName = [handlerPath objectAtIndex:1];
119 [self debugWithFormat:@"path: %@", handlerPath];
120 [self debugWithFormat:@"page: %@", pageName];
121 [self debugWithFormat:@"action: %@", actionName];
126 [self debugWithFormat:@"using httpd provided pagename: %@", pageName];
132 if ((page = [app pageWithName:pageName inContext:context]) == nil) {
133 [self errorWithFormat:
134 @"could not create page object with name %@", pageName];
138 [self debugWithFormat:@"created page: %@", page];
140 /* setup page context */
141 [page _awakeWithContext:context];
142 [context setPage:page];
144 /* take values phase */
146 [app takeValuesFromRequest:_request inContext:context];
148 /* perform a direct action like action */
150 result = [page performActionNamed:actionName];
152 /* generate response */
154 if (result != page && result != nil) {
155 if ([(id)result isKindOfClass:[WOComponent class]]) {
156 [(WOComponent *)result _awakeWithContext:context];
157 [context setPage:(WOComponent *)result];
159 response = [self generateResponseForComponent:(WOComponent *)result
164 response = [result generateResponse];
168 response = [self generateResponseForComponent:page
173 if ([context hasSession]) {
174 if ([context savePageRequired])
175 [[context session] savePage:(WOComponent *)result];
178 /* check whether a session was created */
179 if ((session == nil) && [context hasSession]) {
180 session = [[[context session] retain] autorelease];
184 /* add session cookies to response */
185 [self addCookiesForSession:session
189 /* store session if one was active */
190 [self saveSession:session
192 withResponse:response
198 @end /* WOPageRequestHandler */
200 @implementation WOComponent(DirectActionExtensions)
202 /* taking form values */
204 - (void)takeFormValuesForKeyArray:(NSArray *)_keys {
209 rq = [[self context] request];
210 keys = [_keys objectEnumerator];
212 while ((key = [keys nextObject]))
213 [self takeValue:[rq formValueForKey:key] forKey:key];
215 - (void)takeFormValuesForKeys:(NSString *)_key1,... {
220 rq = [[self context] request];
222 for (key = _key1; key != nil; key = va_arg(va, NSString *))
223 [self takeValue:[rq formValueForKey:key] forKey:key];
227 /* perform actions */
229 - (id<WOActionResults>)defaultAction {
233 - (id<WOActionResults>)performActionNamed:(NSString *)_actionName {
237 /* discard everything after a point in the URL */
238 rng = [_actionName rangeOfString:@"."];
240 _actionName = [_actionName substringToIndex:rng.location];
242 _actionName = [_actionName stringByAppendingString:@"Action"];
244 if ((actionSel = NSSelectorFromString(_actionName)) == NULL) {
245 [self debugWithFormat:@"did not find selector for action: %@",
247 return [self defaultAction];
250 if ([self respondsToSelector:actionSel])
251 return [self performSelector:actionSel];
253 if (debugUnknownActions) {
254 [self logWithFormat:@"Page class %@ cannot handle action %@",
255 NSStringFromClass([self class]), _actionName];
257 return [self defaultAction];
260 @end /* WOComponent(DirectActionExtensions) */