File: SoSelectorInvocation.m

package info (click to toggle)
sope 5.12.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,588 kB
  • sloc: objc: 169,223; sh: 3,823; ansic: 3,409; javascript: 446; python: 318; makefile: 185
file content (434 lines) | stat: -rw-r--r-- 12,044 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
  Copyright (C) 2002-2005 SKYRIX Software AG

  This file is part of SOPE.

  SOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with SOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#include "SoSelectorInvocation.h"
#include "SoClassSecurityInfo.h"
#include "NSException+HTTP.h"
#include "WOContext+SoObjects.h"
#include <NGObjWeb/WOResponse.h>
#include <NGObjWeb/WOContext.h>
#include <DOM/EDOM.h>
#include "common.h"

@implementation SoSelectorInvocation

static BOOL debugOn = NO;

+ (void)initialize {
  NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
  static BOOL didInit = NO;
  if (didInit) return;
  didInit = YES;

  debugOn = [ud boolForKey:@"SoSelectorInvocationDebugEnabled"];
  if (debugOn) NSLog(@"Note: SOPE selector invocation debug is enabled.");
  
  /* per default selector invocations are public */
  [[self soClassSecurityInfo] declareObjectPublic];
}

- (id)init {
  if ((self = [super init]) != nil) {
    [self setDoesAddContextParameter:YES];
  }
  return self;
}

- (id)initWithSelectorNamed:(NSString *)_sel addContextParameter:(BOOL)_wc {
  if ((self = [self init])) {
    [self addSelectorNamed:_sel];
    [self setDoesAddContextParameter:_wc];
  }
  return self;
}

- (void)dealloc {
  [self->argumentSpecifications release];
  [self->object release];
  [super dealloc];
}

/* containment */

- (void)detachFromContainer {
}
- (id)container {
  return nil;
}
- (NSString *)nameInContainer {
  return nil;
}

/* configuration */

- (void)addSelectorNamed:(NSString *)_name {
  unsigned len;
  
  if ((len = [_name length]) == 0) 
    return;
  if (self->sel != NULL) {
    [self logWithFormat:@"not yet ready for operator overloading (%@).",
	    _name];
    return;
  }
  
  /* count arguments (do we have something like this as a string method?) */
  
  self->argCount = 0;
  while (len > 0) {
    len--;
    
    if ([_name characterAtIndex:len] == ':')
      self->argCount++;
  }
  
  if ((self->sel = NSSelectorFromString(_name)) == NULL) {
    /* this can happen if the product bundle is not yet loaded ... */
#if GNU_RUNTIME
    const char *sname = [_name cString];
    self->sel = sel_registerName(sname);
#else
    /* TODO: not tested against this ObjC runtime */
    [self warnWithFormat:@"(%s): not tested against this ObjC runtime, "
            @"product bundle loading may be broken.", __PRETTY_FUNCTION__];
#endif
  }
  if (self->sel == NULL)
    [self warnWithFormat:@"did not find selector: %@", _name];
}

- (void)setDoesAddContextParameter:(BOOL)_flag {
  self->flags.addContextParameter = _flag ? 1 : 0;
}
- (BOOL)doesAddContextParameter {
  return self->flags.addContextParameter ? YES : NO;
}

- (void)setArgumentSpecifications:(NSDictionary *)_specs {
  ASSIGNCOPY(self->argumentSpecifications, _specs);
}
- (NSDictionary *)argumentSpecifications {
  return self->argumentSpecifications;
}

/* error objects */

- (NSException *)unsupportedSelectorError:(SEL)_sel {
  return [NSException exceptionWithHTTPStatus:500 /* Server Error */
		      reason:@"tried to call unsupported selector"];
}

- (NSException *)noSelectorForArgumentCountError:(unsigned)_callArgCount {
  NSString *s;

  s = [NSString stringWithFormat:
		  @"incorrect argument count for SOPE method: "
		  @"required %i for %@, got %i", 
		  self->argCount, NSStringFromSelector(self->sel), 
		  _callArgCount];
  return [NSException exceptionWithHTTPStatus:400 /* Bad Request */
		      reason:s];
}

/* arguments */

- (NSArray *)extractSOAPArgumentsFromContext:(id)_ctx specification:(id)_spec {
  /* spec is supposed to be an array of DOM query pathes ... */
  NSMutableArray *args;
  unsigned       i, count;
  id             soapEnvelope;
  
  // TODO: I guess that should be improved a bit in the dispatcher
  if ((soapEnvelope = [_ctx valueForKey:@"SOAPEnvelope"]) == nil) {
    // TODO: generate some kind of fault? (NSException?)
    [self errorWithFormat:@"no SOAP envelope available in context!"];
    return nil;
  }
  
  /* for each positional selector argument we have a query path */
  count = [_spec count];
  args  = [NSMutableArray arrayWithCapacity:count];
  for (i = 0; i < count; i++) {
    NSString *qppath;
    id value;
    
    qppath = [_spec objectAtIndex:i];
    value  = [qppath isNotNull] ? [soapEnvelope lookupQueryPath:qppath] : nil;
    
    [args addObject:(value != nil ? value : (id)[NSNull null])];
  }
  return args;
}

- (NSArray *)extractArgumentsFromContext:(id)_ctx
  forRequestType:(NSString *)_type
  specification:(id)_spec 
{
  if ([_type isEqualToString:@"SOAP"])
    return [self extractSOAPArgumentsFromContext:_ctx specification:_spec];
  
  [self errorWithFormat:
          @"cannot extract parameters for request type: '%@'", _type];
  return nil;
}

/* invocation */

- (BOOL)isCallable {
  return YES;
}
- (id)clientObject {
  return self->object;
}

- (id)primaryCallSelector:(SEL)_sel withArguments:(NSArray *)_args {
  unsigned     i, callArgCount;
  NSInvocation *inv;
  id result;
  
  if (self->object == nil || _sel == NULL)
    return nil;
  
  if (![self->object respondsToSelector:_sel]) {
    [self logWithFormat:
	    @"Object does not support selector %@, probably broken "
	    @"product.plist file.",
	    NSStringFromSelector(_sel)];
    return [self unsupportedSelectorError:_sel];
  }
  
  callArgCount = [_args count];
  
  /* use primitives if possible */
  
  if (callArgCount == 0)
    return [self->object performSelector:_sel];
  if (callArgCount == 1) {
    return [self->object performSelector:_sel
		withObject:[_args objectAtIndex:0]];
  }
  if (callArgCount == 2) {
    return [self->object performSelector:_sel 
		withObject:[_args objectAtIndex:0] 
		withObject:[_args objectAtIndex:1]];
  }
  
  /* construct NSInvocation */

  // TODO: do security audit, can this lead to "issues"? (should not)
  
  inv = [NSInvocation invocationWithMethodSignature:
			[self->object methodSignatureForSelector:_sel]];
  [inv setSelector:_sel];
  [inv setTarget:self->object];
  
  for (i = 0; i < callArgCount; i++) {
    id arg;
    
    arg = [_args objectAtIndex:i];
    [inv setArgument:&arg atIndex:(i + 2)];
  }
  
  NS_DURING {
    [inv invoke];
    [inv getReturnValue:&result];
  }
  NS_HANDLER
    result = [[localException retain] autorelease];
  NS_ENDHANDLER;
  return result;
}

- (SEL)selectorForNumberOfArguments:(unsigned)_argcount {
#if 1
  // for now, we require an exact match
  return self->argCount == _argcount ? self->sel : NULL;
#else
  // we fill up missing args with nil/NSNull (TODO: prior validation!)
  return self->argCount >= _argcount ? self->sel : NULL;
#endif
}

- (id)callOnObject:(id)_client inContext:(id)_ctx {
  /* call method for keyword arguments or other stuff */
  NSArray      *args;
  NSDictionary *argspec;
  SEL          selector;
  
  /* do not rebind, this breaks if client!=lookup */
  if (self->object == nil) {
    /* bind on demand */
    return [[self bindToObject:_client inContext:_ctx]
	          callOnObject:_client inContext:_ctx];
  }

  /* process arguments */
  
  args    = nil;
  argspec = [self->argumentSpecifications objectForKey:[_ctx soRequestType]];
  if (argspec == nil) {
    /* no argument extractors defined */
    if (debugOn) {
      [self debugWithFormat:@"no arg type spec for type: '%@'",
	      [_ctx soRequestType]];
    }
    if ([self doesAddContextParameter])
      args = [NSArray arrayWithObject:(_ctx ? _ctx : (id)[NSNull null])];
  }
  else {
    args = [self extractArgumentsFromContext:_ctx
		 forRequestType:[_ctx soRequestType]
		 specification:argspec];
    if (debugOn) [self debugWithFormat:@"extracted args %@", args];
    if ([self doesAddContextParameter]) {
      if (args != nil)
	args = [args arrayByAddingObject:(_ctx != nil?_ctx:(id)[NSNull null])];
      else
	args = [NSArray arrayWithObject: (_ctx != nil?_ctx:(id)[NSNull null])];
    }
  }
  
  /* find selector */
  
  selector = [self selectorForNumberOfArguments:[args count]];
  if (selector == NULL) {
    [self warnWithFormat:@"missing selector for invocation!"];
    return [self noSelectorForArgumentCountError:[args count]];
  }
  
  // TODO: check whether this is really a "context invocation"
  // TODO: check number of arguments

  /* invoke */
  
  /* if prebound, call on the bound object, not on the client ! */
  return [self primaryCallSelector:self->sel withArguments:args];
}

- (id)callOnObject:(id)_client 
  withPositionalParameters:(NSArray *)_args
  inContext:(id)_ctx
{
  /* call method for positional parameters */
  SEL      selector;
  unsigned callArgCount;
  
  if (self->object == nil) {
    /* bind on demand */
    return [[self bindToObject:_client inContext:_ctx]
	          callOnObject:_client 
	          withPositionalParameters:_args inContext:_ctx];
  }
  
  callArgCount = [_args count];
  if ([self doesAddContextParameter]) callArgCount++;
  
  if ((selector = [self selectorForNumberOfArguments:callArgCount]) == NULL) {
    [self warnWithFormat:@"missing selector for invocation (args=%d)!",
            callArgCount];
    return [self noSelectorForArgumentCountError:callArgCount];
  }
  
  // TODO:
  //   step A: fill up missing arguments (support default values?!)
  //   step B: validate arguments!
  
  if ([self doesAddContextParameter])
    _args = [_args arrayByAddingObject:_ctx ? _ctx : (id)[NSNull null]];
  
  if (debugOn) {
    [self debugWithFormat:@"call on %@ with args(%i) %@ context %@",
	    _client, [_args count], _args, _client];
  }
  return [self primaryCallSelector:self->sel withArguments:_args];
}

/* binding */

- (BOOL)isBound {
  return self->object == nil ? NO : YES;
}

- (id)bindToObject:(id)_object inContext:(id)_ctx {
  SoSelectorInvocation *inv;
  
  if (_object == nil) return nil;
  
  inv = [SoSelectorInvocation alloc];
  inv->sel      = self->sel;
  inv->argCount = self->argCount;
  inv->flags    = self->flags;
  inv->object   = [_object retain];
  inv->method   = [_object methodForSelector:inv->sel];
  inv->argumentSpecifications = [self->argumentSpecifications copy];
  return [inv autorelease];
}

/* delivering as content (can happen in DAV !) */

- (void)appendToResponse:(WOResponse *)_r inContext:(WOContext *)_ctx {
  [_r appendContentString:@"Compiled SOPE method: "];
  
  if (self->sel)
    [_r appendContentHTMLString:NSStringFromSelector(self->sel)];
  else
    [_r appendContentHTMLString:@"missing selector!"];

  if (self->object) {
    [_r appendContentHTMLString:@" (method is bound to object of class "];
    [_r appendContentHTMLString:NSStringFromClass([self->object class])];
    [_r appendContentHTMLString:@")"];
  }
}

/* description */

- (NSString *)description {
  NSMutableString *ms;

  ms = [NSMutableString stringWithCapacity:64];
  [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
  
  if (self->sel) {
    [ms appendFormat:@" sel=%@", NSStringFromSelector(self->sel)];
    [ms appendFormat:@"(%i args)", self->argCount];
  }
  if (self->object) {
    [ms appendFormat:@" bound(0x%p,%@)", 
          self->object, NSStringFromClass([self->object class])];
  }

  if ([self doesAddContextParameter])
    [ms appendFormat:@" ctx-arg"];
  
  if ([self->argumentSpecifications count] > 0) {
    id tmp;
    
    tmp = [self->argumentSpecifications allKeys];
    tmp = [tmp componentsJoinedByString:@","];
    [ms appendFormat:@" arg-handlers=%@",tmp];
  }
  
  [ms appendString:@">"];
  return ms;
}

@end /* SoSelectorInvocation */