Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* perform dispatch */
- - (id)dispatchInContext:(WOContext *)_ctx {
- NSAutoreleasePool *pool;
- WORequest *rq;
- NSString *httpMethod;
- id clientObject;
- id methodObject;
- id resultObject;
- if (debugOn) {
- [self debugWithFormat:@"dispatchInContext: START"];
- [self debugWithFormat:@" context = %@", _ctx];
- }
- pool = [[NSAutoreleasePool alloc] init];
- rq = [_ctx request];
- if (debugOn) {
- [self debugWithFormat:@" request = %@", rq];
- [self debugWithFormat:@" request method = %@", [rq method]];
- }
- /* find client object */
- if ((clientObject = [_ctx clientObject]) != nil) {
- if (debugOn)
- [self debugWithFormat:@"client object set in ctx: %@", clientObject];
- }
- else if ((clientObject = [self->object clientObject]) != nil) {
- if (debugOn)
- [self debugWithFormat:@"setting client object: %@", clientObject];
- [_ctx setClientObject:clientObject];
- }
- else if (debugOn) {
- [self debugWithFormat:@"no client object found"];
- }
- /* check whether client object is a response ... */
- if ([clientObject isKindOfClass:[WOResponse class]]) {
- [self debugWithFormat:@"clientObject is a WOResponse, returning that: %@",
- clientObject];
- resultObject = [clientObject retain];
- [pool release];
- return [resultObject autorelease];
- }
- /*
- This X- field is used by Google which uses POST to trigger REST methods,
- don't ask me why ... ;-/
- */
- if (![(httpMethod = [rq headerForKey:@"x-http-method-override"]) isNotEmpty]) {
- httpMethod = [rq method];
- if (debugOn)
- [self debugWithFormat:@"using HTTP method from request: %@", httpMethod];
- }
- else if (debugOn) {
- [self debugWithFormat:@"using overridden HTTP method: %@", httpMethod];
- }
- /* find callable (method) object */
- if ([self->object isCallable]) {
- if (debugOn)
- [self debugWithFormat:@"traversed object is callable: %@", self->object];
- methodObject = self->object;
- }
- else if ([[self->object soClass] hasKey:httpMethod inContext:_ctx]) {
- if (debugOn)
- [self debugWithFormat:
- @"class has method for HTTP verb '%@', looking it up", httpMethod];
- methodObject = [self->object lookupName:[rq method]
- inContext:_ctx
- acquire:NO];
- if (debugOn)
- [self debugWithFormat:@"lookup returned methodObject: %@", methodObject];
- }
- else if (useRedirectsForDefaultMethods) {
- NSString *defaultName;
- if (debugOn)
- [self debugWithFormat:@"checking redirect to default method"];
- methodObject = nil;
- defaultName = [self->object defaultMethodNameInContext:_ctx];
- if ([defaultName isNotEmpty]) {
- WOResponse *r;
- NSString *url;
- url = [self->object baseURLInContext:_ctx];
- if (![url hasSuffix:@"/"]) url = [url stringByAppendingString:@"/"];
- url = [url stringByAppendingString:defaultName];
- [self debugWithFormat:@"redirect to default method %@ of %@: %@",
- defaultName, self->object, url];
- r = [[_ctx response] retain];
- [r setStatus:302 /* moved */];
- [r setHeader:url forKey:@"location"];
- [pool release];
- return [r autorelease];
- }
- else if (debugOn) {
- [self debugWithFormat:@"no default method name for redirect"];
- }
- }
- else {
- methodObject = [self->object lookupDefaultMethod];
- if (debugOn)
- [self debugWithFormat:@"using default method: %@", methodObject];
- }
- /* apply arguments */
- if ([methodObject respondsToSelector:
- @selector(takeValuesFromRequest:inContext:)]) {
- if (debugOn)
- [self debugWithFormat:@"applying values from request to %@",
- methodObject];
- [methodObject takeValuesFromRequest:rq inContext:_ctx];
- }
- else if (debugOn && methodObject != nil) {
- [self debugWithFormat:
- @"methodObject does not take request values: %@", methodObject];
- }
- /* perform call */
- if (methodObject == nil || ![methodObject isCallable]) {
- resultObject = self->object;
- if (debugOn) {
- if (methodObject == nil) {
- [self debugWithFormat:
- @"no methodObject, using object as result: %@", resultObject];
- }
- else {
- [self debugWithFormat:
- @"methodObject not callable %@, using object as result: %@",
- methodObject, resultObject];
- }
- }
- }
- else {
- if (debugOn)
- [self debugWithFormat:@"calling %@ on client %@",
- methodObject, [_ctx clientObject]];
- resultObject = [methodObject callOnObject:[_ctx clientObject]
- inContext:_ctx];
- if (debugOn) {
- if ([resultObject isKindOfClass:[WOResponse class]]) {
- [self debugWithFormat:
- @"call produced response: 0x%p (code=%i)",
- resultObject, [(WOResponse *)resultObject status]];
- }
- else {
- [self debugWithFormat:
- @"call produced result: %@", resultObject];
- }
- }
- }
- resultObject = [resultObject retain];
- [pool release];
- if (debugOn)
- [self debugWithFormat:@"dispatchInContext: END -> %@", resultObject];
- /* deliver result */
- return [resultObject autorelease];
- }
- /* page construction */
- - (id)instantiateMethodInContext:(id)_ctx {
- // Careful: this must return a RETAINED object!
- Class clazz;
- id lMethod;
- if (debugOn){
- [self debugWithFormat:@"instantiateMethodInContext: START"];
- [self debugWithFormat:@" methodObject = %@", self->methodObject];
- [self debugWithFormat:@" context = %@", _ctx];
- }
- if (_ctx == nil) {
- [self debugWithFormat:
- @"Note: got no explicit context for method instantiation, using application context."];
- _ctx = [[WOApplication application] context];
- if (debugOn) {
- [self debugWithFormat:@" resolved context = %@", _ctx];
- }
- }
- /* find class */
- if (debugOn) {
- [self debugWithFormat:@"Resolving action class name: %@",
- [self actionClassName]];
- }
- if ((clazz = NSClassFromString([self actionClassName])) == Nil) {
- [self errorWithFormat:@"did not find action class: %@",
- [self actionClassName]];
- return nil;
- }
- if (debugOn) {
- [self debugWithFormat:@"Resolved action class: %@", clazz];
- }
- /* instantiate */
- if ([clazz instancesRespondToSelector:@selector(initWithContext:)]) {
- if (debugOn) {
- [self debugWithFormat:
- @"Instantiating using -initWithContext:"];
- }
- lMethod = [[clazz alloc] initWithContext:_ctx];
- }
- else if ([clazz instancesRespondToSelector:@selector(initWithRequest:)]) {
- if (debugOn) {
- [self debugWithFormat:
- @"Instantiating using -initWithRequest:"];
- [self debugWithFormat:
- @" request = %@",
- [(id<WOPageGenerationContext>)_ctx request]];
- }
- lMethod = [[clazz alloc] initWithRequest:
- [(id<WOPageGenerationContext>)_ctx request]];
- }
- else {
- if (debugOn) {
- [self debugWithFormat:
- @"Instantiating using plain -init"];
- }
- lMethod = [[clazz alloc] init];
- }
- if (debugOn) {
- [self debugWithFormat:@"Instantiation result: %@", lMethod];
- [self debugWithFormat:@"instantiateMethodInContext: END"];
- }
- return lMethod;
- }
Advertisement
Add Comment
Please, Sign In to add comment