-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCygnusServices.m
executable file
·200 lines (149 loc) · 4.82 KB
/
CygnusServices.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
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
//
// CygnusServices.m
//
// Created by Robert Bohn on 6/24/10.
// Copyright 2010 Barzutti, Co. All rights reserved.
//
#import "CygnusServices.h"
static CygnusServices *sharedCygnusServices;
@implementation CygnusServices
#pragma mark -
#pragma mark Initialization
- (id)init
{
[super init];
host = [[NSString alloc] initWithString:@"https://ceGateway/servlet/gateway"];
connectionTimeoutInterval = 10.0;
requestTimeoutInterval = 40.0;
return self;
}
#pragma mark -
#pragma mark Singleton Implementation
+ (CygnusServices *)sharedCygnusServices;
{
if (!sharedCygnusServices) {
sharedCygnusServices = [[CygnusServices alloc] init];
}
return sharedCygnusServices;
}
+ (id)allocwWithZone:(NSZone *)zone
{
if (!sharedCygnusServices) {
sharedCygnusServices = [super allocWithZone:zone];
return sharedCygnusServices;
} else {
return nil;
}
}
- (id)copyWithZode:(NSZone *)zone
{
return self;
}
- (void)release
{
// No Op
}
#pragma mark -
#pragma mark Instance Methods
// Method returning an NSMutableArray of NSDictionary elements from the response XML for a requested tag
- (NSMutableArray *)getElements:(NSString *)tag
{
[tagName release];
tagName = [[NSString alloc] initWithString:tag];
[elements release];
elements = [[NSMutableArray alloc] init];
// Parse the XML from the response data
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
[parser setDelegate:self];
[parser parse];
[parser release];
return elements;
}
// Method returning the Cycnus Status
- (NSString *)status
{
if (!xmlData)
return @"No response data found.";
NSMutableArray *serviceElement = [self getElements:@"SERVICE"];
if ([serviceElement count] < 1)
return @"SERVICE emelent not found.";
NSDictionary *dict = [serviceElement objectAtIndex:0];
NSString *serviceStatus = [dict valueForKey:@"status"];
NSString *serviceError = [dict valueForKey:@"error_msg"];
if (!serviceStatus)
return @"STATUS not found in SERVICE element.";
if ([serviceStatus compare:@"OK"] == 0)
return nil;
if (!serviceError)
return serviceStatus;
return serviceError;
}
// Method to send a Cygnus Service Request
- (void)requestService:(NSString *)requestString delegate:(id)caller
{
delegate = caller;
// Build the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:host] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:connectionTimeoutInterval];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[[NSString stringWithFormat:@"%@", requestString] dataUsingEncoding:NSUTF8StringEncoding]];
// Instanciate the object to hold the response
[xmlData release];
xmlData = [[NSMutableData alloc] init];
// Create a timer to catch request timeout errors
timer = [NSTimer scheduledTimerWithTimeInterval:requestTimeoutInterval target:self selector:@selector(timeout:) userInfo:nil repeats:NO];
// Create and initiate the connection
[connection cancel];
[connection release];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
// Was there an error starting the connection?
if (!connection) {
[timer invalidate];
[xmlData release], xmlData = nil;
if (delegate && [delegate respondsToSelector:@selector(requestCompleted:)])
[delegate requestCompleted:@"Unable to create the connection."];
}
}
#pragma mark -
#pragma mark Connection Delegates
// Delegate to assemble data returned from the connection
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[xmlData appendData:data];
}
// Delegate to handle the completion of the request
- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
[timer invalidate];
if (delegate && [delegate respondsToSelector:@selector(requestCompleted:)])
[delegate requestCompleted:[self status]];
}
// Delegate to catch connection errors
- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
{
[timer invalidate];
[xmlData release], xmlData = nil;
// Notify delegate of the error
if (delegate && [delegate respondsToSelector:@selector(requestCompleted:)])
[delegate requestCompleted:[NSString stringWithFormat:@"Connection Error: %@", [error localizedDescription]]];
}
// Delegate to catch timeout errors
- (void)timeout:(NSTimer*)theTimer
{
[connection cancel];
if (delegate && [delegate respondsToSelector:@selector(requestCompleted:)])
[delegate requestCompleted:@"Timeout Error"];
}
#pragma mark -
#pragma mark Parser Delegates
// Delegate to examine each XML element as it is encountered
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqual:tagName]) {
[elements addObject:[attributeDict mutableCopy]];
}
}
@end