-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathFrankCommandRoute.m
95 lines (71 loc) · 1.96 KB
/
FrankCommandRoute.m
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
//
// FrankCommandRoute.m
// Frank
//
// Created by phodgson on 5/30/10.
// Copyright 2010 ThoughtWorks. See NOTICE file for details.
//
#import "HTTPDataResponse.h"
#import "FrankCommandRoute.h"
#import "RoutingHTTPConnection.h"
#if TARGET_OS_IPHONE
#import "UIImage+Frank.h"
#else
#import "NSImage+Frank.h"
#endif
extern BOOL frankLogEnabled;
@implementation FrankCommandRoute
#pragma mark singleton implementation
static FrankCommandRoute *s_singleton;
+ (void)initialize
{
static BOOL initialized = NO;
if(!initialized)
{
initialized = YES;
s_singleton = [[FrankCommandRoute alloc] init];
}
}
+ (FrankCommandRoute *) singleton{
return s_singleton;
}
- (id) init
{
self = [super init];
if (self != nil) {
_commandDict = [[NSMutableDictionary alloc]init];
}
return self;
}
- (void) dealloc
{
[_commandDict release];
[super dealloc];
}
-(void) registerCommand: (id<FrankCommand>)command withName:(NSString *)commandName {
[_commandDict setObject:command forKey:commandName];
}
#pragma mark Route implementation
- (id<FrankCommand>) commandForPath: (NSArray *)path{
if( 1 != [path count] )
return nil;
return [_commandDict objectForKey:[path objectAtIndex:0]];
}
- (NSObject<HTTPResponse> *) handleRequestForPath: (NSArray *)path withConnection:(RoutingHTTPConnection *)connection{
if(frankLogEnabled) {
NSLog( @"received request with path %@\nPOST DATA:\n%@", path, connection.postDataAsString );
}
id<FrankCommand> command = [self commandForPath:path];
if( nil == command )
return nil;
NSString *response = [command handleCommandWithRequestBody:connection.postDataAsString];
if(frankLogEnabled) {
NSLog( @"returning:\n%@", response );
}
NSData *browseData = [response dataUsingEncoding:NSUTF8StringEncoding];
return [[[HTTPDataResponse alloc] initWithData:browseData] autorelease];
}
- (BOOL) canHandlePostForPath: (NSArray *)path{
return( nil != [self commandForPath:path] );
}
@end