-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServiceCallingUtility.m
executable file
·299 lines (247 loc) · 12.9 KB
/
ServiceCallingUtility.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
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
//
// ServiceCallingUtility.m
// test
//
// Created by Minesh Purohit on 31/07/14.
// Copyright (c) 2014 MinuMaster. All rights reserved.
//
#import "ServiceCallingUtility.h"
@implementation ServiceCallingUtility
@synthesize delegate, isEnableDebugMode, sendDirectHTTPBody;
- (void) doWebserviceCall:(NSString *)action withPostVars:(NSArray *)post_vars withGetVars:(NSArray *)get_vars andNotificationName:(NSString *) notificationName
{
notification = notificationName;
responseData = [[NSMutableData alloc] init];
// Add Method Action in URL.
NSString *url_string = action;
//adding get vars in url.
if ( (get_vars != nil) && ([get_vars count] > 0) )
{
url_string = [NSString stringWithFormat:@"%@?",url_string];
for (id param in get_vars)
{
if (![param isKindOfClass:[ServiceParameter class]]) {
NSLog(@"Web Service Only Accept \"ServiceParameter\" Objects.");
return;
}
if ([(ServiceParameter *)param valueType] != SCTypeText) {
NSLog(@"Get Parameters Only Accept \"SCTypeText\" type parameters.");
return;
}
url_string = [NSString stringWithFormat:@"%@%@=%@&", url_string, ((ServiceParameter *)param).key, (NSString *)((ServiceParameter *)param).value];
}//end while
url_string = [url_string substringToIndex:url_string.length-(url_string.length>0)];
}//end if
if (isEnableDebugMode) {
NSLog(@"Get URL String:%@",url_string);
}
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[url_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
// change type to POST (default is GET)
[postRequest setHTTPMethod:@"POST"];
//adding get vars in url.
if ( (post_vars != nil) && ([post_vars count] > 0) )
{
if (sendDirectHTTPBody)
{
if ([post_vars count] > 1) {
NSLog(@"Direct HTTP Post body only accept One Service Parameter object.");
return;
}
if ([post_vars count] > 0)
{
id param = [post_vars objectAtIndex:0];
if (![((ServiceParameter *)param).value isKindOfClass:[NSData class]])
{
NSLog(@"Direct HTTP Post body only accept NSData object.");
return;
}
if (((ServiceParameter *)param).contentType == nil)
{
NSLog(@"Direct HTTP Post must have content type value.");
return;
}
if ([((ServiceParameter *)param).contentType length] <= 0)
{
NSLog(@"Direct HTTP Post must have content type value.");
return;
}
if (((ServiceParameter *)param).timeoutTime <= 0) {
((ServiceParameter *)param).timeoutTime = 15;
}
[postRequest setHTTPBody:((ServiceParameter *)param).value];
[postRequest addValue:((ServiceParameter *)param).contentType forHTTPHeaderField:@"Content-Type"];
[postRequest setTimeoutInterval:((ServiceParameter *)param).timeoutTime];
_intRequestDataSize = [(NSData *)((ServiceParameter *)param).value length];
}
}
else
{
// just some random text that will never occur in the body
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
// set header
[postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
// create data
NSMutableData *postBody = [NSMutableData data];
for (id param in post_vars)
{
if (![param isKindOfClass:[ServiceParameter class]]) {
NSLog(@"Web Service Only Accept \"ServiceParameter\" Objects.");
return;
}
if ([(ServiceParameter *)param valueType] == SCTypeText)
{
// Send Text Value in Key
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",((ServiceParameter *)param).key] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[((ServiceParameter *)param).value dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
else if ([(ServiceParameter *)param valueType] == SCTypeImage)
{
// Send Image Value in Key with Name
if ([((ServiceParameter *)param).value isKindOfClass:[UIImage class]])
{
NSString * fileNameValue = [ServiceParameter getTextFromString:((ServiceParameter *)param).fileName];
if ([fileNameValue length] > 0)
{
NSMutableArray * fileNameObject = [NSMutableArray arrayWithArray:[fileNameValue componentsSeparatedByString:@"."]];
[fileNameObject removeLastObject];
NSString * newFileName = [NSString stringWithFormat:@"%@.jpg",[fileNameObject componentsJoinedByString:@"."]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", ((ServiceParameter *)param).key, newFileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSData * imageData = UIImageJPEGRepresentation(((ServiceParameter *)param).value, .9);
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
else
{
NSLog(@"File Name required for type \"SCTypeImage\".");
return;
}
}
else
{
NSLog(@"Service Parameter Type \"SCTypeImage\" value only accept UIImage object.");
return;
}
}
else if ([(ServiceParameter *)param valueType] == SCTypeFile)
{
if ([((ServiceParameter *)param).value isKindOfClass:[NSData class]])
{
// File Data Part
NSString * fileNameValue = [ServiceParameter getTextFromString:((ServiceParameter *)param).fileName];
if ([fileNameValue length] > 0)
{
NSString * contentType = [ServiceParameter getContentTypeForExtension:[[fileNameValue componentsSeparatedByString:@"."] lastObject]];
if ([contentType length] > 0)
{
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", ((ServiceParameter *)param).key,((ServiceParameter *)param).fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n",contentType] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:((ServiceParameter *)param).value];
[postBody appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
else
{
NSLog(@"Invalid File Name for type \"SCTypeFile\".");
return;
}
}
else
{
NSLog(@"File Name required for type \"SCTypeFile\".");
return;
}
}
else
{
NSLog(@"Service Parameter Type \"SCTypeFile\" value only accept NSData object.");
return;
}
}
else
{
NSLog(@"Service Parameter Invalid valueType.");
return;
}
}//end while
// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//NSLog(@"%@",postBody);
// add body to post
[postRequest setHTTPBody:postBody];
_intRequestDataSize = [postBody length];
}
}//end if
// Asynch request
NSURLConnection *conn = [NSURLConnection connectionWithRequest:postRequest delegate:self];
if (!conn)
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"SC Connection" message:@"Connection Failed. Please try again." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
}
else
{
[conn start];
}
}
#pragma mark - NSURLConnection delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if ([delegate respondsToSelector:@selector(didFailedRequest:withError:)])
{
[delegate didFailedRequest:notification withError:error];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if ([delegate respondsToSelector:@selector(didSuccessRequest:withResponse:)])
{
[delegate didSuccessRequest:notification withResponse:responseString];
}
}
#pragma mark - NURLConnection Progress Methods
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
if ([delegate respondsToSelector:@selector(didSendRequest:progressPercentage:)])
{
// Calculate Completion Percentage
CGFloat fltComplete = 0;
if (totalBytesExpectedToWrite > 0) {
fltComplete = (1.0 * totalBytesWritten) / (1.0 * totalBytesExpectedToWrite);
} else if (_intRequestDataSize > 0) {
fltComplete = (1.0 * totalBytesWritten) / (1.0 * _intRequestDataSize);
}
[delegate didSendRequest:notification progressPercentage:fltComplete];
}
}
#pragma mark - NURLConnection Authentication Methods
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
#pragma mark -
@end