2 Copyright (C) 2002-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
23 #include "SoObjectMethodDispatcher.h"
26 #include "SoObjectRequestHandler.h"
27 #include "WOContext+SoObjects.h"
28 #include <NGObjWeb/WORequest.h>
29 #include <NGObjWeb/WOResponse.h>
30 #include <NGObjWeb/WOContext.h>
31 #include <NGObjWeb/WOElement.h>
34 @implementation SoObjectMethodDispatcher
36 static BOOL debugOn = NO;
39 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
40 static BOOL didInit = NO;
44 debugOn = [ud boolForKey:@"SoObjectMethodDispatcherDebugEnabled"];
47 - (id)initWithObject:(id)_object {
48 if ((self = [super init])) {
49 self->object = [_object retain];
54 [self->object release];
58 /* perform dispatch */
60 - (id)dispatchInContext:(WOContext *)_ctx {
61 NSAutoreleasePool *pool;
67 pool = [[NSAutoreleasePool alloc] init];
70 /* find client object */
72 if ((clientObject = [_ctx clientObject])) {
74 [self debugWithFormat:@"client object set in ctx: %@", clientObject];
76 else if ((clientObject = [self->object clientObject])) {
78 [self debugWithFormat:@"setting client object: %@", clientObject];
79 [_ctx setClientObject:clientObject];
82 // TODO: should check XML-RPC !!!
83 // (hm, why ? XML-RPC is handled by other dispatcher ?)
85 /* find callable (method) object */
87 if ([self->object isCallable]) {
89 [self debugWithFormat:@"traversed object is callable: %@", self->object];
90 methodObject = self->object;
92 else if ([[self->object soClass] hasKey:[rq method] inContext:_ctx]) {
93 // TODO: I'm not sure whether this step is correct
94 /* the class has a GET method */
95 methodObject = [self->object lookupName:[rq method]
100 // TODO: should we replace the methodObject with a redirect to the
101 // default method name? This would ensure proper URLs
102 methodObject = [self->object lookupDefaultMethod];
104 [self debugWithFormat:@"using default method: %@", methodObject];
107 /* apply arguments */
109 if ([methodObject respondsToSelector:
110 @selector(takeValuesFromRequest:inContext:)]) {
112 [self debugWithFormat:@"applying values from request ..."];
113 [methodObject takeValuesFromRequest:rq inContext:_ctx];
118 if (methodObject == nil || ![methodObject isCallable]) {
120 The object is neither callable nor does it have a default method,
121 so we just pass it through.
123 resultObject = self->object;
125 [self debugWithFormat:@"got no method, using object as result: %@",
130 resultObject = [methodObject callOnObject:[_ctx clientObject]
133 if ([resultObject isKindOfClass:[WOResponse class]]) {
134 [self debugWithFormat:@"call produced response: 0x%08X (code=%i)",
135 resultObject, [(WOResponse *)resultObject status]];
138 [self debugWithFormat:@"call produced result: %@", resultObject];
142 resultObject = [resultObject retain];
146 return [resultObject autorelease];
151 - (NSString *)loggingPrefix {
152 return @"[obj-mth-dispatch]";
154 - (BOOL)isDebuggingEnabled {
155 return debugOn ? YES : NO;
160 - (NSString *)description {
163 ms = [NSMutableString stringWithCapacity:64];
164 [ms appendFormat:@"<0x%08X[%@]:", self,
165 NSStringFromClass((Class)*(void**)self)];
168 [ms appendFormat:@" object=%@", self->object];
170 [ms appendString:@" <no object>"];
172 [ms appendString:@">"];
176 @end /* SoObjectMethodDispatcher */