]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WOPageRequestHandler.m
Improved WORepetition's implementation to be more convenient in regards to the 'list...
[sope] / sope-appserver / NGObjWeb / WOPageRequestHandler.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include <NGObjWeb/WORequestHandler.h>
23
24 @interface WOPageRequestHandler : WORequestHandler
25 @end
26
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>
39 #include "common.h"
40
41 static BOOL  perflog             = NO;
42 static Class NSDateClass         = Nil;
43 static BOOL  debugUnknownActions = NO;
44 static BOOL  debugOn             = NO;
45
46 @interface WOComponent(Privates)
47 - (void)_awakeWithContext:(WOContext *)_ctx;
48 - (id<WOActionResults>)performActionNamed:(NSString *)_actionName;
49 @end
50
51 @implementation WOPageRequestHandler
52
53 + (int)version {
54   return [super version] + 0 /* 2 */;
55 }
56 + (void)initialize {
57   NSUserDefaults *ud;
58   NSAssert2([super version] == 2,
59             @"invalid superclass (%@) version %i !",
60             NSStringFromClass([self superclass]), [super version]);
61
62   NSDateClass = [NSDate class];
63   ud = [NSUserDefaults standardUserDefaults];
64   perflog = [ud boolForKey:@"WOProfilePageRequestHandler"];
65   debugOn = [ud boolForKey:@"WOPageRequestHandlerDebugEnabled"];
66 }
67
68 /* debugging */
69
70 - (NSString *)loggingPrefix {
71   return @"[pg-handler]";
72 }
73 - (BOOL)isDebuggingEnabled {
74   return debugOn;
75 }
76
77 /*
78   The request handler part of a direct action URI looks like this:
79
80     [actionClass/]actionName[?key=value&key=value&...]
81 */
82
83 - (WOResponse *)handleRequest:(WORequest *)_request
84   inContext:(WOContext *)context
85   session:(WOSession *)session
86   application:(WOApplication *)app
87 {
88   NSString      *actionName;
89   WOResponse    *response;
90   id<WOActionResults> result = nil;
91   NSString      *pageName;
92   WOComponent   *page;
93   
94   *(&result) = nil;
95   *(&response)        = nil;
96   *(&actionName)      = nil;
97   
98   /* process path */
99   if ((pageName = [_request headerForKey:@"x-httpd-pagename"]) == nil) {
100     NSArray *handlerPath;
101     
102     handlerPath = [_request requestHandlerPathArray];
103     switch ([handlerPath count]) {
104       case 0:
105         pageName   = @"Main";
106         actionName = @"default";
107         break;
108       case 1:
109         pageName   = [handlerPath objectAtIndex:0];
110         actionName = @"default";
111         break;
112       default:
113         pageName   = [handlerPath objectAtIndex:0];
114         actionName = [handlerPath objectAtIndex:1];
115         break;
116     }
117     
118     if (debugOn) {
119       [self debugWithFormat:@"path:   %@",   handlerPath];
120       [self debugWithFormat:@"page:   %@",   pageName];
121       [self debugWithFormat:@"action: %@", actionName];
122     }
123   }
124   else {
125     if (debugOn)
126       [self debugWithFormat:@"using httpd provided pagename: %@", pageName];
127   }
128   
129   if (pageName == nil)
130     pageName = @"Main";
131   
132   if ((page = [app pageWithName:pageName inContext:context]) == nil) {
133     [self errorWithFormat:
134             @"could not create page object with name %@", pageName];
135     return nil;
136   }
137   
138   [self debugWithFormat:@"created page: %@", page];
139   
140   /* setup page context */
141   [page _awakeWithContext:context];
142   [context setPage:page];
143   
144   /* take values phase */
145   
146   [app takeValuesFromRequest:_request inContext:context];
147   
148   /* perform a direct action like action */
149   
150   result = [page performActionNamed:actionName];
151
152   /* generate response */
153   
154   if (result != page && result != nil) {
155     if ([(id)result isKindOfClass:[WOComponent class]]) {
156       [(WOComponent *)result _awakeWithContext:context];
157       [context setPage:(WOComponent *)result];
158       
159       response = [self generateResponseForComponent:(WOComponent *)result
160                        inContext:context
161                        application:app];
162     }
163     else
164       response = [result generateResponse];
165   }
166   else {
167     result = page;
168     response = [self generateResponseForComponent:page
169                      inContext:context
170                      application:app];
171   }
172   
173   if ([context hasSession]) {
174     if ([context savePageRequired])
175       [[context session] savePage:(WOComponent *)result];
176   }
177   
178   /* check whether a session was created */
179   if ((session == nil) && [context hasSession]) {
180     session = [[[context session] retain] autorelease];
181     [session lock];
182   }
183   
184   /* add session cookies to response */
185   [self addCookiesForSession:session
186         toResponse:response
187         inContext:context];
188     
189   /* store session if one was active */
190   [self saveSession:session
191         inContext:context
192         withResponse:response
193         application:app];
194   
195   return response;
196 }
197
198 @end /* WOPageRequestHandler */
199
200 @implementation WOComponent(DirectActionExtensions)
201
202 /* taking form values */
203
204 - (void)takeFormValuesForKeyArray:(NSArray *)_keys {
205   NSEnumerator *keys;
206   NSString     *key;
207   WORequest    *rq;
208
209   rq   = [[self context] request];
210   keys = [_keys objectEnumerator];
211
212   while ((key = [keys nextObject]))
213     [self takeValue:[rq formValueForKey:key] forKey:key];
214 }
215 - (void)takeFormValuesForKeys:(NSString *)_key1,... {
216   va_list   va;
217   NSString  *key;
218   WORequest *rq;
219   
220   rq = [[self context] request];
221   va_start(va, _key1);
222   for (key = _key1; key != nil; key = va_arg(va, NSString *))
223     [self takeValue:[rq formValueForKey:key] forKey:key];
224   va_end(va);
225 }
226
227 /* perform actions */
228
229 - (id<WOActionResults>)defaultAction {
230   return self;
231 }
232
233 - (id<WOActionResults>)performActionNamed:(NSString *)_actionName {
234   SEL actionSel;
235   NSRange rng;
236   
237   /* discard everything after a point in the URL */
238   rng = [_actionName rangeOfString:@"."];
239   if (rng.length > 0)
240     _actionName = [_actionName substringToIndex:rng.location];
241   
242   _actionName = [_actionName stringByAppendingString:@"Action"];
243   
244   if ((actionSel = NSSelectorFromString(_actionName)) == NULL) {
245     [self debugWithFormat:@"did not find selector for action: %@", 
246             _actionName];
247     return [self defaultAction];
248   }
249   
250   if ([self respondsToSelector:actionSel]) 
251     return [self performSelector:actionSel];
252
253   if (debugUnknownActions) {
254     [self logWithFormat:@"Page class %@ cannot handle action %@",
255             NSStringFromClass([self class]), _actionName];
256   }
257   return [self defaultAction];
258 }
259
260 @end /* WOComponent(DirectActionExtensions) */