/
WebService.m
239 lines (178 loc) · 6.67 KB
/
WebService.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
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
//
// WebService.m
// SOAP
//
// Created by Oliver on 16.10.09.
// Copyright 2009 Drobnik.com. All rights reserved.
//
#import "WebService.h"
#import "XMLdocument.h"
#import "NSString+Helpers.h"
#import "NSDataAdditions.h"
@implementation WebService
#pragma mark Conversions
- (BOOL) isBoolStringYES:(NSString *)string
{
if ([[string lowercaseString] isEqualToString:@"false"] ||
[[string lowercaseString] isEqualToString:@"0"])
{
return NO;
}
else
{
return YES;
}
}
#pragma mark Requests
- (NSURLRequest *) makeGETRequestWithLocation:(NSString *)url Parameters:(NSDictionary *)parameters
{
NSMutableString *query = [NSMutableString string];
for (NSString *oneKey in [parameters allKeys])
{
if ([query length])
{
[query appendString:@"&"];
}
else
{
[query appendString:@"?"];
}
[query appendFormat:@"%@=%@", oneKey, [[parameters objectForKey:oneKey] stringByUrlEncoding]];
}
url = [url stringByAppendingString:query];
return [[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]] autorelease];
}
- (NSURLRequest *) makePOSTRequestWithLocation:(NSString *)url Parameters:(NSDictionary *)parameters
{
NSMutableString *query = [NSMutableString string];
for (NSString *oneKey in [parameters allKeys])
{
if ([query length])
{
[query appendString:@"&"];
}
[query appendFormat:@"%@=%@", oneKey, [[parameters objectForKey:oneKey] stringByUrlEncoding]];
}
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]] autorelease];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
// make body
NSData *postBody = [NSData dataWithData:[query dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
return request;
}
- (NSURLRequest *) makeSOAPRequestWithLocation:(NSString *)url Parameters:(NSArray *)parameters Operation:(NSString *)operation Namespace:(NSString *)namespace Action:(NSString *)action SOAPVersion:(SOAPVersion)soapVersion;
{
NSMutableString *envelope = [NSMutableString string];
[envelope appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"];
switch (soapVersion) {
case SOAPVersion1_0:
[envelope appendString:@"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"];
break;
case SOAPVersion1_2:
[envelope appendString:@"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">\n"];
break;
}
[envelope appendString:@"<soap:Body>\n"];
[envelope appendFormat:@"<%@ xmlns=\"%@\">\n", operation, namespace];
for (NSDictionary *oneParameter in parameters)
{
NSObject *value = [oneParameter objectForKey:@"value"];
NSString *parameterName = [[oneParameter objectForKey:@"name"] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"_"]];
[envelope appendFormat:@"<%@>%@</%@>\n", parameterName, [[value description] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding], parameterName];
}
[envelope appendFormat:@"</%@>\n", operation];
[envelope appendString:@"</soap:Body>\n"];
[envelope appendString:@"</soap:Envelope>\n"];
//NSLog(@"%@", parameters);
//NSLog(@"%@", envelope);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]] autorelease];
[request setHTTPMethod:@"POST"];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:action forHTTPHeaderField:@"SOAPAction"];
// make body
NSData *postBody = [NSData dataWithData:[envelope dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
return request;
}
- (NSString *) returnValueFromSOAPResponse:(XMLdocument *)envelope
{
XMLelement *body = [envelope.documentRoot getNamedChild:@"Body"];
XMLelement *response = [body.children lastObject]; // there should be only one
if (response.children)
{
XMLelement *retChild = [response.children lastObject];
return retChild.text;
}
else
{
return nil;
}
}
- (id) returnComplexTypeFromSOAPResponse:(XMLdocument *)envelope asClass:(Class)retClass
{
// create a new instance of expected class
id newObject = [[[retClass alloc] init] autorelease];
XMLelement *body = [envelope.documentRoot getNamedChild:@"Body"];
XMLelement *response = [body.children lastObject]; // there should be only one
XMLelement *result = [response.children lastObject]; // there should be only one
for (XMLelement *oneChild in result.children)
{
// this seems to work for scalars as well as strings without problem
[newObject setValue:oneChild.text forKey:oneChild.name];
}
return newObject;
}
- (NSMutableDictionary*) flattenSOAPResponse:(XMLelement *)element withPath:(NSString*)path
{
// poor man's XPath
NSArray *steps = [path componentsSeparatedByString: @"/"];
NSMutableDictionary *dict = [[[NSMutableDictionary alloc] init] autorelease];
XMLelement *el = element;
for(id step in steps) {
el = [el getNamedChild: step];
if(el.children) {
for (XMLelement *oneChild in el.children)
{
[dict setObject:oneChild.text forKey:oneChild.name];
}
}
}
return dict;
}
- (NSArray *) returnArrayFromSOAPResponse:(XMLdocument *)envelope withClass:(Class)retClass
{
NSMutableArray *tmpArray = [NSMutableArray array];
XMLelement *body = [envelope.documentRoot getNamedChild:@"Body"];
XMLelement *response = [body.children lastObject]; // there should be only one
XMLelement *result = [response.children lastObject]; // there should be only one
for (XMLelement *oneThing in result.children)
{
id newObject = [[[retClass alloc] init] autorelease];
for (XMLelement *oneChild in oneThing.children)
{
// this seems to work for scalars as well as strings without problem
[newObject setValue:oneChild.text forKey:oneChild.name];
}
[tmpArray addObject:newObject];
}
if ([tmpArray count])
{
return [NSArray arrayWithArray:tmpArray];
}
else
{
return nil;
}
}
- (XMLdocument *) returnXMLDocumentFromSOAPResponse:(XMLdocument *)envelope
{
// create a new instance of expected class
//id newObject = [[[retClass alloc] init] autorelease];
XMLelement *body = [envelope.documentRoot getNamedChild:@"Body"];
XMLelement *response = [body.children lastObject]; // there should be only one
XMLelement *result = [response.children lastObject]; // there should be only one
XMLelement *oneMore = [result.children lastObject]; // there should be only one
return [XMLdocument documentWithString:[oneMore description]];
}
@end