2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
21 // $Id: WOPageRequestHandler.m 1 2004-08-20 10:08:27Z znek $
23 #include <NGObjWeb/WORequestHandler.h>
25 @interface WOPageRequestHandler : WORequestHandler
28 //#include "WOPageRequestHandler.h"
29 #include "WORequestHandler+private.h"
30 #include "WOContext+private.h"
31 #include <NGObjWeb/WOApplication.h>
32 #include <NGObjWeb/WOComponent.h>
33 #include <NGObjWeb/WODirectAction.h>
34 #include <NGObjWeb/WORequest.h>
35 #include <NGObjWeb/WOResponse.h>
36 #include <NGObjWeb/WOSession.h>
37 #include <NGObjWeb/WOComponent.h>
38 #include <NGObjWeb/WOSessionStore.h>
39 #include <NGObjWeb/WOStatisticsStore.h>
42 static BOOL perflog = NO;
43 static Class NSDateClass = Nil;
44 static BOOL debugUnknownActions = NO;
45 static BOOL debugOn = NO;
47 @interface WOComponent(Privates)
48 - (void)_awakeWithContext:(WOContext *)_ctx;
49 - (id<WOActionResults>)performActionNamed:(NSString *)_actionName;
52 @implementation WOPageRequestHandler
55 return [super version] + 0 /* 2 */;
59 NSAssert2([super version] == 2,
60 @"invalid superclass (%@) version %i !",
61 NSStringFromClass([self superclass]), [super version]);
63 NSDateClass = [NSDate class];
64 ud = [NSUserDefaults standardUserDefaults];
65 perflog = [ud boolForKey:@"WOProfilePageRequestHandler"];
66 debugOn = [ud boolForKey:@"WOPageRequestHandlerDebugEnabled"];
71 - (NSString *)loggingPrefix {
72 return @"[pg-handler]";
74 - (BOOL)isDebuggingEnabled {
79 The request handler part of a direct action URI looks like this:
81 [actionClass/]actionName[?key=value&key=value&...]
84 - (WOResponse *)handleRequest:(WORequest *)_request
85 inContext:(WOContext *)context
86 session:(WOSession *)session
87 application:(WOApplication *)app
91 id<WOActionResults> result = nil;
100 if ((pageName = [_request headerForKey:@"x-httpd-pagename"]) == nil) {
101 NSArray *handlerPath;
103 handlerPath = [_request requestHandlerPathArray];
104 switch ([handlerPath count]) {
107 actionName = @"default";
110 pageName = [handlerPath objectAtIndex:0];
111 actionName = @"default";
114 pageName = [handlerPath objectAtIndex:0];
115 actionName = [handlerPath objectAtIndex:1];
120 [self debugWithFormat:@"path: %@", handlerPath];
121 [self debugWithFormat:@"page: %@", pageName];
122 [self debugWithFormat:@"action: %@", actionName];
127 [self debugWithFormat:@"using httpd provided pagename: %@", pageName];
133 if ((page = [app pageWithName:pageName inContext:context]) == nil) {
134 [self errorWithFormat:
135 @"could not create page object with name %@", pageName];
139 [self debugWithFormat:@"created page: %@", page];
141 /* setup page context */
142 [page _awakeWithContext:context];
143 [context setPage:page];
145 /* take values phase */
147 [app takeValuesFromRequest:_request inContext:context];
149 /* perform a direct action like action */
151 result = [page performActionNamed:actionName];
153 /* generate response */
155 if (result != page && result != nil) {
156 if ([(id)result isKindOfClass:[WOComponent class]]) {
157 [(WOComponent *)result _awakeWithContext:context];
158 [context setPage:(WOComponent *)result];
160 response = [self generateResponseForComponent:(WOComponent *)result
165 response = [result generateResponse];
169 response = [self generateResponseForComponent:page
174 if ([context hasSession]) {
175 if ([context savePageRequired])
176 [[context session] savePage:(WOComponent *)result];
179 /* check whether a session was created */
180 if ((session == nil) && [context hasSession]) {
181 session = [[[context session] retain] autorelease];
185 /* add session cookies to response */
186 [self addCookiesForSession:session
190 /* store session if one was active */
191 [self saveSession:session
193 withResponse:response
199 @end /* WOPageRequestHandler */
201 @implementation WOComponent(DirectActionExtensions)
203 /* taking form values */
205 - (void)takeFormValuesForKeyArray:(NSArray *)_keys {
210 rq = [[self context] request];
211 keys = [_keys objectEnumerator];
213 while ((key = [keys nextObject]))
214 [self takeValue:[rq formValueForKey:key] forKey:key];
216 - (void)takeFormValuesForKeys:(NSString *)_key1,... {
221 rq = [[self context] request];
223 for (key = _key1; key != nil; key = va_arg(va, NSString *))
224 [self takeValue:[rq formValueForKey:key] forKey:key];
228 /* perform actions */
230 - (id<WOActionResults>)defaultAction {
234 - (id<WOActionResults>)performActionNamed:(NSString *)_actionName {
238 /* discard everything after a point in the URL */
239 rng = [_actionName rangeOfString:@"."];
241 _actionName = [_actionName substringToIndex:rng.location];
243 _actionName = [_actionName stringByAppendingString:@"Action"];
245 if ((actionSel = NSSelectorFromString(_actionName)) == NULL) {
246 [self debugWithFormat:@"did not find selector for action: %@",
248 return [self defaultAction];
251 if ([self respondsToSelector:actionSel])
252 return [self performSelector:actionSel];
254 if (debugUnknownActions) {
255 [self logWithFormat:@"Page class %@ cannot handle action %@",
256 NSStringFromClass([self class]), _actionName];
258 return [self defaultAction];
261 @end /* WOComponent(DirectActionExtensions) */