PageRenderTime 103ms CodeModel.GetById 8ms RepoModel.GetById 2ms app.codeStats 0ms

/WebServiceLocalTimeSoap/LocalTime/LocalTime.m

https://gitlab.com/praveenvelanati/ios-demo
Objective C | 1018 lines | 864 code | 142 blank | 12 comment | 176 complexity | 6a64a0f9225943060979d8079e76a23e MD5 | raw file
  1. #import "LocalTime.h"
  2. #import <libxml/xmlstring.h>
  3. #if TARGET_OS_IPHONE
  4. #import <CFNetwork/CFNetwork.h>
  5. #endif
  6. @implementation LocalTime_LocalTimeByZipCode
  7. - (id)init
  8. {
  9. if((self = [super init])) {
  10. ZipCode = 0;
  11. }
  12. return self;
  13. }
  14. - (void)dealloc
  15. {
  16. if(ZipCode != nil) [ZipCode release];
  17. [super dealloc];
  18. }
  19. - (NSString *)nsPrefix
  20. {
  21. return @"LocalTime";
  22. }
  23. - (xmlNodePtr)xmlNodeForDoc:(xmlDocPtr)doc elementName:(NSString *)elName elementNSPrefix:(NSString *)elNSPrefix
  24. {
  25. NSString *nodeName = nil;
  26. if(elNSPrefix != nil && [elNSPrefix length] > 0)
  27. {
  28. nodeName = [NSString stringWithFormat:@"%@:%@", elNSPrefix, elName];
  29. }
  30. else
  31. {
  32. nodeName = [NSString stringWithFormat:@"%@:%@", @"LocalTime", elName];
  33. }
  34. xmlNodePtr node = xmlNewDocNode(doc, NULL, [nodeName xmlString], NULL);
  35. [self addAttributesToNode:node];
  36. [self addElementsToNode:node];
  37. return node;
  38. }
  39. - (void)addAttributesToNode:(xmlNodePtr)node
  40. {
  41. }
  42. - (void)addElementsToNode:(xmlNodePtr)node
  43. {
  44. if(self.ZipCode != 0) {
  45. xmlAddChild(node, [self.ZipCode xmlNodeForDoc:node->doc elementName:@"ZipCode" elementNSPrefix:@"LocalTime"]);
  46. }
  47. }
  48. /* elements */
  49. @synthesize ZipCode;
  50. /* attributes */
  51. - (NSDictionary *)attributes
  52. {
  53. NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
  54. return attributes;
  55. }
  56. + (LocalTime_LocalTimeByZipCode *)deserializeNode:(xmlNodePtr)cur
  57. {
  58. LocalTime_LocalTimeByZipCode *newObject = [[LocalTime_LocalTimeByZipCode new] autorelease];
  59. [newObject deserializeAttributesFromNode:cur];
  60. [newObject deserializeElementsFromNode:cur];
  61. return newObject;
  62. }
  63. - (void)deserializeAttributesFromNode:(xmlNodePtr)cur
  64. {
  65. }
  66. - (void)deserializeElementsFromNode:(xmlNodePtr)cur
  67. {
  68. for( cur = cur->children ; cur != NULL ; cur = cur->next ) {
  69. if(cur->type == XML_ELEMENT_NODE) {
  70. xmlChar *elementText = xmlNodeListGetString(cur->doc, cur->children, 1);
  71. NSString *elementString = nil;
  72. if(elementText != NULL) {
  73. elementString = [NSString stringWithCString:(char*)elementText encoding:NSUTF8StringEncoding];
  74. [elementString self]; // avoid compiler warning for unused var
  75. xmlFree(elementText);
  76. }
  77. if(xmlStrEqual(cur->name, (const xmlChar *) "ZipCode")) {
  78. Class elementClass = nil;
  79. xmlChar *instanceType = xmlGetNsProp(cur, (const xmlChar *) "type", (const xmlChar *) "http://www.w3.org/2001/XMLSchema-instance");
  80. if(instanceType == NULL) {
  81. elementClass = [NSString class];
  82. } else {
  83. NSString *elementTypeString = [NSString stringWithCString:(char*)instanceType encoding:NSUTF8StringEncoding];
  84. NSArray *elementTypeArray = [elementTypeString componentsSeparatedByString:@":"];
  85. NSString *elementClassString = nil;
  86. if([elementTypeArray count] > 1) {
  87. NSString *prefix = [elementTypeArray objectAtIndex:0];
  88. NSString *localName = [elementTypeArray objectAtIndex:1];
  89. xmlNsPtr elementNamespace = xmlSearchNs(cur->doc, cur, [prefix xmlString]);
  90. NSString *standardPrefix = [[USGlobals sharedInstance].wsdlStandardNamespaces objectForKey:[NSString stringWithCString:(char*)elementNamespace->href encoding:NSUTF8StringEncoding]];
  91. elementClassString = [NSString stringWithFormat:@"%@_%@", standardPrefix, localName];
  92. } else {
  93. elementClassString = [elementTypeString stringByReplacingOccurrencesOfString:@":" withString:@"_" options:0 range:NSMakeRange(0, [elementTypeString length])];
  94. }
  95. elementClass = NSClassFromString(elementClassString);
  96. xmlFree(instanceType);
  97. }
  98. id newChild = [elementClass deserializeNode:cur];
  99. self.ZipCode = newChild;
  100. }
  101. }
  102. }
  103. }
  104. @end
  105. @implementation LocalTime_LocalTimeByZipCodeResponse
  106. - (id)init
  107. {
  108. if((self = [super init])) {
  109. LocalTimeByZipCodeResult = 0;
  110. }
  111. return self;
  112. }
  113. - (void)dealloc
  114. {
  115. if(LocalTimeByZipCodeResult != nil) [LocalTimeByZipCodeResult release];
  116. [super dealloc];
  117. }
  118. - (NSString *)nsPrefix
  119. {
  120. return @"LocalTime";
  121. }
  122. - (xmlNodePtr)xmlNodeForDoc:(xmlDocPtr)doc elementName:(NSString *)elName elementNSPrefix:(NSString *)elNSPrefix
  123. {
  124. NSString *nodeName = nil;
  125. if(elNSPrefix != nil && [elNSPrefix length] > 0)
  126. {
  127. nodeName = [NSString stringWithFormat:@"%@:%@", elNSPrefix, elName];
  128. }
  129. else
  130. {
  131. nodeName = [NSString stringWithFormat:@"%@:%@", @"LocalTime", elName];
  132. }
  133. xmlNodePtr node = xmlNewDocNode(doc, NULL, [nodeName xmlString], NULL);
  134. [self addAttributesToNode:node];
  135. [self addElementsToNode:node];
  136. return node;
  137. }
  138. - (void)addAttributesToNode:(xmlNodePtr)node
  139. {
  140. }
  141. - (void)addElementsToNode:(xmlNodePtr)node
  142. {
  143. if(self.LocalTimeByZipCodeResult != 0) {
  144. xmlAddChild(node, [self.LocalTimeByZipCodeResult xmlNodeForDoc:node->doc elementName:@"LocalTimeByZipCodeResult" elementNSPrefix:@"LocalTime"]);
  145. }
  146. }
  147. /* elements */
  148. @synthesize LocalTimeByZipCodeResult;
  149. /* attributes */
  150. - (NSDictionary *)attributes
  151. {
  152. NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
  153. return attributes;
  154. }
  155. + (LocalTime_LocalTimeByZipCodeResponse *)deserializeNode:(xmlNodePtr)cur
  156. {
  157. LocalTime_LocalTimeByZipCodeResponse *newObject = [[LocalTime_LocalTimeByZipCodeResponse new] autorelease];
  158. [newObject deserializeAttributesFromNode:cur];
  159. [newObject deserializeElementsFromNode:cur];
  160. return newObject;
  161. }
  162. - (void)deserializeAttributesFromNode:(xmlNodePtr)cur
  163. {
  164. }
  165. - (void)deserializeElementsFromNode:(xmlNodePtr)cur
  166. {
  167. for( cur = cur->children ; cur != NULL ; cur = cur->next ) {
  168. if(cur->type == XML_ELEMENT_NODE) {
  169. xmlChar *elementText = xmlNodeListGetString(cur->doc, cur->children, 1);
  170. NSString *elementString = nil;
  171. if(elementText != NULL) {
  172. elementString = [NSString stringWithCString:(char*)elementText encoding:NSUTF8StringEncoding];
  173. [elementString self]; // avoid compiler warning for unused var
  174. xmlFree(elementText);
  175. }
  176. if(xmlStrEqual(cur->name, (const xmlChar *) "LocalTimeByZipCodeResult")) {
  177. Class elementClass = nil;
  178. xmlChar *instanceType = xmlGetNsProp(cur, (const xmlChar *) "type", (const xmlChar *) "http://www.w3.org/2001/XMLSchema-instance");
  179. if(instanceType == NULL) {
  180. elementClass = [NSString class];
  181. } else {
  182. NSString *elementTypeString = [NSString stringWithCString:(char*)instanceType encoding:NSUTF8StringEncoding];
  183. NSArray *elementTypeArray = [elementTypeString componentsSeparatedByString:@":"];
  184. NSString *elementClassString = nil;
  185. if([elementTypeArray count] > 1) {
  186. NSString *prefix = [elementTypeArray objectAtIndex:0];
  187. NSString *localName = [elementTypeArray objectAtIndex:1];
  188. xmlNsPtr elementNamespace = xmlSearchNs(cur->doc, cur, [prefix xmlString]);
  189. NSString *standardPrefix = [[USGlobals sharedInstance].wsdlStandardNamespaces objectForKey:[NSString stringWithCString:(char*)elementNamespace->href encoding:NSUTF8StringEncoding]];
  190. elementClassString = [NSString stringWithFormat:@"%@_%@", standardPrefix, localName];
  191. } else {
  192. elementClassString = [elementTypeString stringByReplacingOccurrencesOfString:@":" withString:@"_" options:0 range:NSMakeRange(0, [elementTypeString length])];
  193. }
  194. elementClass = NSClassFromString(elementClassString);
  195. xmlFree(instanceType);
  196. }
  197. id newChild = [elementClass deserializeNode:cur];
  198. self.LocalTimeByZipCodeResult = newChild;
  199. }
  200. }
  201. }
  202. }
  203. @end
  204. @implementation LocalTime
  205. + (void)initialize
  206. {
  207. [[USGlobals sharedInstance].wsdlStandardNamespaces setObject:@"xs" forKey:@"http://www.w3.org/2001/XMLSchema"];
  208. [[USGlobals sharedInstance].wsdlStandardNamespaces setObject:@"LocalTime" forKey:@"http://www.ripedev.com/"];
  209. }
  210. + (LocalTimeSoapBinding *)LocalTimeSoapBinding
  211. {
  212. return [[[LocalTimeSoapBinding alloc] initWithAddress:@"http://www.ripedevelopment.com/webservices/LocalTime.asmx"] autorelease];
  213. }
  214. + (LocalTimeSoap12Binding *)LocalTimeSoap12Binding
  215. {
  216. return [[[LocalTimeSoap12Binding alloc] initWithAddress:@"http://www.ripedevelopment.com/webservices/LocalTime.asmx"] autorelease];
  217. }
  218. @end
  219. @implementation LocalTimeSoapBinding
  220. @synthesize address;
  221. @synthesize defaultTimeout;
  222. @synthesize logXMLInOut;
  223. @synthesize cookies;
  224. @synthesize authUsername;
  225. @synthesize authPassword;
  226. - (id)init
  227. {
  228. if((self = [super init])) {
  229. address = nil;
  230. cookies = nil;
  231. defaultTimeout = 10;//seconds
  232. logXMLInOut = NO;
  233. synchronousOperationComplete = NO;
  234. }
  235. return self;
  236. }
  237. - (id)initWithAddress:(NSString *)anAddress
  238. {
  239. if((self = [self init])) {
  240. self.address = [NSURL URLWithString:anAddress];
  241. }
  242. return self;
  243. }
  244. - (void)addCookie:(NSHTTPCookie *)toAdd
  245. {
  246. if(toAdd != nil) {
  247. if(cookies == nil) cookies = [[NSMutableArray alloc] init];
  248. [cookies addObject:toAdd];
  249. }
  250. }
  251. - (LocalTimeSoapBindingResponse *)performSynchronousOperation:(LocalTimeSoapBindingOperation *)operation
  252. {
  253. synchronousOperationComplete = NO;
  254. [operation start];
  255. // Now wait for response
  256. NSRunLoop *theRL = [NSRunLoop currentRunLoop];
  257. while (!synchronousOperationComplete && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
  258. return operation.response;
  259. }
  260. - (void)performAsynchronousOperation:(LocalTimeSoapBindingOperation *)operation
  261. {
  262. [operation start];
  263. }
  264. - (void) operation:(LocalTimeSoapBindingOperation *)operation completedWithResponse:(LocalTimeSoapBindingResponse *)response
  265. {
  266. synchronousOperationComplete = YES;
  267. }
  268. - (LocalTimeSoapBindingResponse *)LocalTimeByZipCodeUsingParameters:(LocalTime_LocalTimeByZipCode *)aParameters
  269. {
  270. return [self performSynchronousOperation:[[(LocalTimeSoapBinding_LocalTimeByZipCode*)[LocalTimeSoapBinding_LocalTimeByZipCode alloc] initWithBinding:self delegate:self
  271. parameters:aParameters
  272. ] autorelease]];
  273. }
  274. - (void)LocalTimeByZipCodeAsyncUsingParameters:(LocalTime_LocalTimeByZipCode *)aParameters delegate:(id<LocalTimeSoapBindingResponseDelegate>)responseDelegate
  275. {
  276. [self performAsynchronousOperation: [[(LocalTimeSoapBinding_LocalTimeByZipCode*)[LocalTimeSoapBinding_LocalTimeByZipCode alloc] initWithBinding:self delegate:responseDelegate
  277. parameters:aParameters
  278. ] autorelease]];
  279. }
  280. - (void)sendHTTPCallUsingBody:(NSString *)outputBody soapAction:(NSString *)soapAction forOperation:(LocalTimeSoapBindingOperation *)operation
  281. {
  282. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.address
  283. cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
  284. timeoutInterval:self.defaultTimeout];
  285. NSData *bodyData = [outputBody dataUsingEncoding:NSUTF8StringEncoding];
  286. if(cookies != nil) {
  287. [request setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:cookies]];
  288. }
  289. [request setValue:@"wsdl2objc" forHTTPHeaderField:@"User-Agent"];
  290. [request setValue:soapAction forHTTPHeaderField:@"SOAPAction"];
  291. [request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  292. [request setValue:[NSString stringWithFormat:@"%u", [bodyData length]] forHTTPHeaderField:@"Content-Length"];
  293. [request setValue:self.address.host forHTTPHeaderField:@"Host"];
  294. [request setHTTPMethod: @"POST"];
  295. // set version 1.1 - how?
  296. [request setHTTPBody: bodyData];
  297. if(self.logXMLInOut) {
  298. NSLog(@"OutputHeaders:\n%@", [request allHTTPHeaderFields]);
  299. NSLog(@"OutputBody:\n%@", outputBody);
  300. }
  301. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:operation];
  302. operation.urlConnection = connection;
  303. [connection release];
  304. }
  305. - (void) dealloc
  306. {
  307. [address release];
  308. [cookies release];
  309. [super dealloc];
  310. }
  311. @end
  312. @implementation LocalTimeSoapBindingOperation
  313. @synthesize binding;
  314. @synthesize response;
  315. @synthesize delegate;
  316. @synthesize responseData;
  317. @synthesize urlConnection;
  318. - (id)initWithBinding:(LocalTimeSoapBinding *)aBinding delegate:(id<LocalTimeSoapBindingResponseDelegate>)aDelegate
  319. {
  320. if ((self = [super init])) {
  321. self.binding = aBinding;
  322. response = nil;
  323. self.delegate = aDelegate;
  324. self.responseData = nil;
  325. self.urlConnection = nil;
  326. }
  327. return self;
  328. }
  329. -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
  330. {
  331. if ([challenge previousFailureCount] == 0) {
  332. NSURLCredential *newCredential;
  333. newCredential=[NSURLCredential credentialWithUser:self.binding.authUsername
  334. password:self.binding.authPassword
  335. persistence:NSURLCredentialPersistenceForSession];
  336. [[challenge sender] useCredential:newCredential
  337. forAuthenticationChallenge:challenge];
  338. } else {
  339. [[challenge sender] cancelAuthenticationChallenge:challenge];
  340. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Authentication Error" forKey:NSLocalizedDescriptionKey];
  341. NSError *authError = [NSError errorWithDomain:@"Connection Authentication" code:0 userInfo:userInfo];
  342. [self connection:connection didFailWithError:authError];
  343. }
  344. }
  345. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)urlResponse
  346. {
  347. NSHTTPURLResponse *httpResponse;
  348. if ([urlResponse isKindOfClass:[NSHTTPURLResponse class]]) {
  349. httpResponse = (NSHTTPURLResponse *) urlResponse;
  350. } else {
  351. httpResponse = nil;
  352. }
  353. if(binding.logXMLInOut) {
  354. NSLog(@"ResponseStatus: %u\n", [httpResponse statusCode]);
  355. NSLog(@"ResponseHeaders:\n%@", [httpResponse allHeaderFields]);
  356. }
  357. NSMutableArray *cookies = [[NSHTTPCookie cookiesWithResponseHeaderFields:[httpResponse allHeaderFields] forURL:binding.address] mutableCopy];
  358. binding.cookies = cookies;
  359. [cookies release];
  360. if ([urlResponse.MIMEType rangeOfString:@"text/xml"].length == 0) {
  361. NSError *error = nil;
  362. [connection cancel];
  363. if ([httpResponse statusCode] >= 400) {
  364. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]] forKey:NSLocalizedDescriptionKey];
  365. error = [NSError errorWithDomain:@"LocalTimeSoapBindingResponseHTTP" code:[httpResponse statusCode] userInfo:userInfo];
  366. } else {
  367. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:
  368. [NSString stringWithFormat: @"Unexpected response MIME type to SOAP call:%@", urlResponse.MIMEType]
  369. forKey:NSLocalizedDescriptionKey];
  370. error = [NSError errorWithDomain:@"LocalTimeSoapBindingResponseHTTP" code:1 userInfo:userInfo];
  371. }
  372. [self connection:connection didFailWithError:error];
  373. }
  374. }
  375. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  376. {
  377. if (responseData == nil) {
  378. responseData = [data mutableCopy];
  379. } else {
  380. [responseData appendData:data];
  381. }
  382. }
  383. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  384. {
  385. if (binding.logXMLInOut) {
  386. NSLog(@"ResponseError:\n%@", error);
  387. }
  388. response.error = error;
  389. [delegate operation:self completedWithResponse:response];
  390. }
  391. - (void)dealloc
  392. {
  393. [binding release];
  394. [response release];
  395. delegate = nil;
  396. [responseData release];
  397. [urlConnection release];
  398. [super dealloc];
  399. }
  400. @end
  401. @implementation LocalTimeSoapBinding_LocalTimeByZipCode
  402. @synthesize parameters;
  403. - (id)initWithBinding:(LocalTimeSoapBinding *)aBinding delegate:(id<LocalTimeSoapBindingResponseDelegate>)responseDelegate
  404. parameters:(LocalTime_LocalTimeByZipCode *)aParameters
  405. {
  406. if((self = [super initWithBinding:aBinding delegate:responseDelegate])) {
  407. self.parameters = aParameters;
  408. }
  409. return self;
  410. }
  411. - (void)dealloc
  412. {
  413. if(parameters != nil) [parameters release];
  414. [super dealloc];
  415. }
  416. - (void)main
  417. {
  418. [response autorelease];
  419. response = [LocalTimeSoapBindingResponse new];
  420. LocalTimeSoapBinding_envelope *envelope = [LocalTimeSoapBinding_envelope sharedInstance];
  421. NSMutableDictionary *headerElements = nil;
  422. headerElements = [NSMutableDictionary dictionary];
  423. NSMutableDictionary *bodyElements = nil;
  424. bodyElements = [NSMutableDictionary dictionary];
  425. if(parameters != nil) [bodyElements setObject:parameters forKey:@"LocalTimeByZipCode"];
  426. NSString *operationXMLString = [envelope serializedFormUsingHeaderElements:headerElements bodyElements:bodyElements];
  427. [binding sendHTTPCallUsingBody:operationXMLString soapAction:@"http://www.ripedev.com/LocalTimeByZipCode" forOperation:self];
  428. }
  429. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  430. {
  431. if (responseData != nil && delegate != nil)
  432. {
  433. xmlDocPtr doc;
  434. xmlNodePtr cur;
  435. if (binding.logXMLInOut) {
  436. NSLog(@"ResponseBody:\n%@", [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease]);
  437. }
  438. doc = xmlParseMemory([responseData bytes], [responseData length]);
  439. if (doc == NULL) {
  440. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Errors while parsing returned XML" forKey:NSLocalizedDescriptionKey];
  441. response.error = [NSError errorWithDomain:@"LocalTimeSoapBindingResponseXML" code:1 userInfo:userInfo];
  442. [delegate operation:self completedWithResponse:response];
  443. } else {
  444. cur = xmlDocGetRootElement(doc);
  445. cur = cur->children;
  446. for( ; cur != NULL ; cur = cur->next) {
  447. if(cur->type == XML_ELEMENT_NODE) {
  448. if(xmlStrEqual(cur->name, (const xmlChar *) "Body")) {
  449. NSMutableArray *responseBodyParts = [NSMutableArray array];
  450. xmlNodePtr bodyNode;
  451. for(bodyNode=cur->children ; bodyNode != NULL ; bodyNode = bodyNode->next) {
  452. if(cur->type == XML_ELEMENT_NODE) {
  453. if(xmlStrEqual(bodyNode->name, (const xmlChar *) "LocalTimeByZipCodeResponse")) {
  454. LocalTime_LocalTimeByZipCodeResponse *bodyObject = [LocalTime_LocalTimeByZipCodeResponse deserializeNode:bodyNode];
  455. //NSAssert1(bodyObject != nil, @"Errors while parsing body %s", bodyNode->name);
  456. if (bodyObject != nil) [responseBodyParts addObject:bodyObject];
  457. }
  458. if (xmlStrEqual(bodyNode->ns->prefix, cur->ns->prefix) &&
  459. xmlStrEqual(bodyNode->name, (const xmlChar *) "Fault")) {
  460. SOAPFault *bodyObject = [SOAPFault deserializeNode:bodyNode];
  461. //NSAssert1(bodyObject != nil, @"Errors while parsing body %s", bodyNode->name);
  462. if (bodyObject != nil) [responseBodyParts addObject:bodyObject];
  463. }
  464. }
  465. }
  466. response.bodyParts = responseBodyParts;
  467. }
  468. }
  469. }
  470. xmlFreeDoc(doc);
  471. }
  472. xmlCleanupParser();
  473. [delegate operation:self completedWithResponse:response];
  474. }
  475. }
  476. @end
  477. static LocalTimeSoapBinding_envelope *LocalTimeSoapBindingSharedEnvelopeInstance = nil;
  478. @implementation LocalTimeSoapBinding_envelope
  479. + (LocalTimeSoapBinding_envelope *)sharedInstance
  480. {
  481. if(LocalTimeSoapBindingSharedEnvelopeInstance == nil) {
  482. LocalTimeSoapBindingSharedEnvelopeInstance = [LocalTimeSoapBinding_envelope new];
  483. }
  484. return LocalTimeSoapBindingSharedEnvelopeInstance;
  485. }
  486. - (NSString *)serializedFormUsingHeaderElements:(NSDictionary *)headerElements bodyElements:(NSDictionary *)bodyElements
  487. {
  488. xmlDocPtr doc;
  489. doc = xmlNewDoc((const xmlChar*)XML_DEFAULT_VERSION);
  490. if (doc == NULL) {
  491. NSLog(@"Error creating the xml document tree");
  492. return @"";
  493. }
  494. xmlNodePtr root = xmlNewDocNode(doc, NULL, (const xmlChar*)"Envelope", NULL);
  495. xmlDocSetRootElement(doc, root);
  496. xmlNsPtr soapEnvelopeNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2003/05/soap-envelope", (const xmlChar*)"soap");
  497. xmlSetNs(root, soapEnvelopeNs);
  498. xmlNsPtr xslNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/1999/XSL/Transform", (const xmlChar*)"xsl");
  499. xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema-instance", (const xmlChar*)"xsi");
  500. xmlNewNsProp(root, xslNs, (const xmlChar*)"version", (const xmlChar*)"1.0");
  501. xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema", (const xmlChar*)"xs");
  502. xmlNewNs(root, (const xmlChar*)"http://www.ripedev.com/", (const xmlChar*)"LocalTime");
  503. if((headerElements != nil) && ([headerElements count] > 0)) {
  504. xmlNodePtr headerNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Header", NULL);
  505. xmlAddChild(root, headerNode);
  506. for(NSString *key in [headerElements allKeys]) {
  507. id header = [headerElements objectForKey:key];
  508. xmlAddChild(headerNode, [header xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);
  509. }
  510. }
  511. if((bodyElements != nil) && ([bodyElements count] > 0)) {
  512. xmlNodePtr bodyNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Body", NULL);
  513. xmlAddChild(root, bodyNode);
  514. for(NSString *key in [bodyElements allKeys]) {
  515. id body = [bodyElements objectForKey:key];
  516. xmlAddChild(bodyNode, [body xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);
  517. }
  518. }
  519. xmlChar *buf;
  520. int size;
  521. xmlDocDumpFormatMemory(doc, &buf, &size, 1);
  522. NSString *serializedForm = [NSString stringWithCString:(const char*)buf encoding:NSUTF8StringEncoding];
  523. xmlFree(buf);
  524. xmlFreeDoc(doc);
  525. return serializedForm;
  526. }
  527. @end
  528. @implementation LocalTimeSoapBindingResponse
  529. @synthesize headers;
  530. @synthesize bodyParts;
  531. @synthesize error;
  532. - (id)init
  533. {
  534. if((self = [super init])) {
  535. headers = nil;
  536. bodyParts = nil;
  537. error = nil;
  538. }
  539. return self;
  540. }
  541. -(void)dealloc {
  542. self.headers = nil;
  543. self.bodyParts = nil;
  544. self.error = nil;
  545. [super dealloc];
  546. }
  547. @end
  548. @implementation LocalTimeSoap12Binding
  549. @synthesize address;
  550. @synthesize defaultTimeout;
  551. @synthesize logXMLInOut;
  552. @synthesize cookies;
  553. @synthesize authUsername;
  554. @synthesize authPassword;
  555. - (id)init
  556. {
  557. if((self = [super init])) {
  558. address = nil;
  559. cookies = nil;
  560. defaultTimeout = 10;//seconds
  561. logXMLInOut = NO;
  562. synchronousOperationComplete = NO;
  563. }
  564. return self;
  565. }
  566. - (id)initWithAddress:(NSString *)anAddress
  567. {
  568. if((self = [self init])) {
  569. self.address = [NSURL URLWithString:anAddress];
  570. }
  571. return self;
  572. }
  573. - (void)addCookie:(NSHTTPCookie *)toAdd
  574. {
  575. if(toAdd != nil) {
  576. if(cookies == nil) cookies = [[NSMutableArray alloc] init];
  577. [cookies addObject:toAdd];
  578. }
  579. }
  580. - (LocalTimeSoap12BindingResponse *)performSynchronousOperation:(LocalTimeSoap12BindingOperation *)operation
  581. {
  582. synchronousOperationComplete = NO;
  583. [operation start];
  584. // Now wait for response
  585. NSRunLoop *theRL = [NSRunLoop currentRunLoop];
  586. while (!synchronousOperationComplete && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
  587. return operation.response;
  588. }
  589. - (void)performAsynchronousOperation:(LocalTimeSoap12BindingOperation *)operation
  590. {
  591. [operation start];
  592. }
  593. - (void) operation:(LocalTimeSoap12BindingOperation *)operation completedWithResponse:(LocalTimeSoap12BindingResponse *)response
  594. {
  595. synchronousOperationComplete = YES;
  596. }
  597. - (LocalTimeSoap12BindingResponse *)LocalTimeByZipCodeUsingParameters:(LocalTime_LocalTimeByZipCode *)aParameters
  598. {
  599. return [self performSynchronousOperation:[[(LocalTimeSoap12Binding_LocalTimeByZipCode*)[LocalTimeSoap12Binding_LocalTimeByZipCode alloc] initWithBinding:self delegate:self
  600. parameters:aParameters
  601. ] autorelease]];
  602. }
  603. - (void)LocalTimeByZipCodeAsyncUsingParameters:(LocalTime_LocalTimeByZipCode *)aParameters delegate:(id<LocalTimeSoap12BindingResponseDelegate>)responseDelegate
  604. {
  605. [self performAsynchronousOperation: [[(LocalTimeSoap12Binding_LocalTimeByZipCode*)[LocalTimeSoap12Binding_LocalTimeByZipCode alloc] initWithBinding:self delegate:responseDelegate
  606. parameters:aParameters
  607. ] autorelease]];
  608. }
  609. - (void)sendHTTPCallUsingBody:(NSString *)outputBody soapAction:(NSString *)soapAction forOperation:(LocalTimeSoap12BindingOperation *)operation
  610. {
  611. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.address
  612. cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
  613. timeoutInterval:self.defaultTimeout];
  614. NSData *bodyData = [outputBody dataUsingEncoding:NSUTF8StringEncoding];
  615. if(cookies != nil) {
  616. [request setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:cookies]];
  617. }
  618. [request setValue:@"wsdl2objc" forHTTPHeaderField:@"User-Agent"];
  619. [request setValue:soapAction forHTTPHeaderField:@"SOAPAction"];
  620. [request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  621. [request setValue:[NSString stringWithFormat:@"%u", [bodyData length]] forHTTPHeaderField:@"Content-Length"];
  622. [request setValue:self.address.host forHTTPHeaderField:@"Host"];
  623. [request setHTTPMethod: @"POST"];
  624. // set version 1.1 - how?
  625. [request setHTTPBody: bodyData];
  626. if(self.logXMLInOut) {
  627. NSLog(@"OutputHeaders:\n%@", [request allHTTPHeaderFields]);
  628. NSLog(@"OutputBody:\n%@", outputBody);
  629. }
  630. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:operation];
  631. operation.urlConnection = connection;
  632. [connection release];
  633. }
  634. - (void) dealloc
  635. {
  636. [address release];
  637. [cookies release];
  638. [super dealloc];
  639. }
  640. @end
  641. @implementation LocalTimeSoap12BindingOperation
  642. @synthesize binding;
  643. @synthesize response;
  644. @synthesize delegate;
  645. @synthesize responseData;
  646. @synthesize urlConnection;
  647. - (id)initWithBinding:(LocalTimeSoap12Binding *)aBinding delegate:(id<LocalTimeSoap12BindingResponseDelegate>)aDelegate
  648. {
  649. if ((self = [super init])) {
  650. self.binding = aBinding;
  651. response = nil;
  652. self.delegate = aDelegate;
  653. self.responseData = nil;
  654. self.urlConnection = nil;
  655. }
  656. return self;
  657. }
  658. -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
  659. {
  660. if ([challenge previousFailureCount] == 0) {
  661. NSURLCredential *newCredential;
  662. newCredential=[NSURLCredential credentialWithUser:self.binding.authUsername
  663. password:self.binding.authPassword
  664. persistence:NSURLCredentialPersistenceForSession];
  665. [[challenge sender] useCredential:newCredential
  666. forAuthenticationChallenge:challenge];
  667. } else {
  668. [[challenge sender] cancelAuthenticationChallenge:challenge];
  669. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Authentication Error" forKey:NSLocalizedDescriptionKey];
  670. NSError *authError = [NSError errorWithDomain:@"Connection Authentication" code:0 userInfo:userInfo];
  671. [self connection:connection didFailWithError:authError];
  672. }
  673. }
  674. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)urlResponse
  675. {
  676. NSHTTPURLResponse *httpResponse;
  677. if ([urlResponse isKindOfClass:[NSHTTPURLResponse class]]) {
  678. httpResponse = (NSHTTPURLResponse *) urlResponse;
  679. } else {
  680. httpResponse = nil;
  681. }
  682. if(binding.logXMLInOut) {
  683. NSLog(@"ResponseStatus: %u\n", [httpResponse statusCode]);
  684. NSLog(@"ResponseHeaders:\n%@", [httpResponse allHeaderFields]);
  685. }
  686. NSMutableArray *cookies = [[NSHTTPCookie cookiesWithResponseHeaderFields:[httpResponse allHeaderFields] forURL:binding.address] mutableCopy];
  687. binding.cookies = cookies;
  688. [cookies release];
  689. if ([urlResponse.MIMEType rangeOfString:@"text/xml"].length == 0) {
  690. NSError *error = nil;
  691. [connection cancel];
  692. if ([httpResponse statusCode] >= 400) {
  693. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]] forKey:NSLocalizedDescriptionKey];
  694. error = [NSError errorWithDomain:@"LocalTimeSoap12BindingResponseHTTP" code:[httpResponse statusCode] userInfo:userInfo];
  695. } else {
  696. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:
  697. [NSString stringWithFormat: @"Unexpected response MIME type to SOAP call:%@", urlResponse.MIMEType]
  698. forKey:NSLocalizedDescriptionKey];
  699. error = [NSError errorWithDomain:@"LocalTimeSoap12BindingResponseHTTP" code:1 userInfo:userInfo];
  700. }
  701. [self connection:connection didFailWithError:error];
  702. }
  703. }
  704. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  705. {
  706. if (responseData == nil) {
  707. responseData = [data mutableCopy];
  708. } else {
  709. [responseData appendData:data];
  710. }
  711. }
  712. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  713. {
  714. if (binding.logXMLInOut) {
  715. NSLog(@"ResponseError:\n%@", error);
  716. }
  717. response.error = error;
  718. [delegate operation:self completedWithResponse:response];
  719. }
  720. - (void)dealloc
  721. {
  722. [binding release];
  723. [response release];
  724. delegate = nil;
  725. [responseData release];
  726. [urlConnection release];
  727. [super dealloc];
  728. }
  729. @end
  730. @implementation LocalTimeSoap12Binding_LocalTimeByZipCode
  731. @synthesize parameters;
  732. - (id)initWithBinding:(LocalTimeSoap12Binding *)aBinding delegate:(id<LocalTimeSoap12BindingResponseDelegate>)responseDelegate
  733. parameters:(LocalTime_LocalTimeByZipCode *)aParameters
  734. {
  735. if((self = [super initWithBinding:aBinding delegate:responseDelegate])) {
  736. self.parameters = aParameters;
  737. }
  738. return self;
  739. }
  740. - (void)dealloc
  741. {
  742. if(parameters != nil) [parameters release];
  743. [super dealloc];
  744. }
  745. - (void)main
  746. {
  747. [response autorelease];
  748. response = [LocalTimeSoap12BindingResponse new];
  749. LocalTimeSoap12Binding_envelope *envelope = [LocalTimeSoap12Binding_envelope sharedInstance];
  750. NSMutableDictionary *headerElements = nil;
  751. headerElements = [NSMutableDictionary dictionary];
  752. NSMutableDictionary *bodyElements = nil;
  753. bodyElements = [NSMutableDictionary dictionary];
  754. if(parameters != nil) [bodyElements setObject:parameters forKey:@"LocalTimeByZipCode"];
  755. NSString *operationXMLString = [envelope serializedFormUsingHeaderElements:headerElements bodyElements:bodyElements];
  756. [binding sendHTTPCallUsingBody:operationXMLString soapAction:@"http://www.ripedev.com/LocalTimeByZipCode" forOperation:self];
  757. }
  758. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  759. {
  760. if (responseData != nil && delegate != nil)
  761. {
  762. xmlDocPtr doc;
  763. xmlNodePtr cur;
  764. if (binding.logXMLInOut) {
  765. NSLog(@"ResponseBody:\n%@", [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease]);
  766. }
  767. doc = xmlParseMemory([responseData bytes], [responseData length]);
  768. if (doc == NULL) {
  769. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Errors while parsing returned XML" forKey:NSLocalizedDescriptionKey];
  770. response.error = [NSError errorWithDomain:@"LocalTimeSoap12BindingResponseXML" code:1 userInfo:userInfo];
  771. [delegate operation:self completedWithResponse:response];
  772. } else {
  773. cur = xmlDocGetRootElement(doc);
  774. cur = cur->children;
  775. for( ; cur != NULL ; cur = cur->next) {
  776. if(cur->type == XML_ELEMENT_NODE) {
  777. if(xmlStrEqual(cur->name, (const xmlChar *) "Body")) {
  778. NSMutableArray *responseBodyParts = [NSMutableArray array];
  779. xmlNodePtr bodyNode;
  780. for(bodyNode=cur->children ; bodyNode != NULL ; bodyNode = bodyNode->next) {
  781. if(cur->type == XML_ELEMENT_NODE) {
  782. if(xmlStrEqual(bodyNode->name, (const xmlChar *) "LocalTimeByZipCodeResponse")) {
  783. LocalTime_LocalTimeByZipCodeResponse *bodyObject = [LocalTime_LocalTimeByZipCodeResponse deserializeNode:bodyNode];
  784. //NSAssert1(bodyObject != nil, @"Errors while parsing body %s", bodyNode->name);
  785. if (bodyObject != nil) [responseBodyParts addObject:bodyObject];
  786. }
  787. if (xmlStrEqual(bodyNode->ns->prefix, cur->ns->prefix) &&
  788. xmlStrEqual(bodyNode->name, (const xmlChar *) "Fault")) {
  789. SOAPFault *bodyObject = [SOAPFault deserializeNode:bodyNode];
  790. //NSAssert1(bodyObject != nil, @"Errors while parsing body %s", bodyNode->name);
  791. if (bodyObject != nil) [responseBodyParts addObject:bodyObject];
  792. }
  793. }
  794. }
  795. response.bodyParts = responseBodyParts;
  796. }
  797. }
  798. }
  799. xmlFreeDoc(doc);
  800. }
  801. xmlCleanupParser();
  802. [delegate operation:self completedWithResponse:response];
  803. }
  804. }
  805. @end
  806. static LocalTimeSoap12Binding_envelope *LocalTimeSoap12BindingSharedEnvelopeInstance = nil;
  807. @implementation LocalTimeSoap12Binding_envelope
  808. + (LocalTimeSoap12Binding_envelope *)sharedInstance
  809. {
  810. if(LocalTimeSoap12BindingSharedEnvelopeInstance == nil) {
  811. LocalTimeSoap12BindingSharedEnvelopeInstance = [LocalTimeSoap12Binding_envelope new];
  812. }
  813. return LocalTimeSoap12BindingSharedEnvelopeInstance;
  814. }
  815. - (NSString *)serializedFormUsingHeaderElements:(NSDictionary *)headerElements bodyElements:(NSDictionary *)bodyElements
  816. {
  817. xmlDocPtr doc;
  818. doc = xmlNewDoc((const xmlChar*)XML_DEFAULT_VERSION);
  819. if (doc == NULL) {
  820. NSLog(@"Error creating the xml document tree");
  821. return @"";
  822. }
  823. xmlNodePtr root = xmlNewDocNode(doc, NULL, (const xmlChar*)"Envelope", NULL);
  824. xmlDocSetRootElement(doc, root);
  825. xmlNsPtr soapEnvelopeNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2003/05/soap-envelope", (const xmlChar*)"soap");
  826. xmlSetNs(root, soapEnvelopeNs);
  827. xmlNsPtr xslNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/1999/XSL/Transform", (const xmlChar*)"xsl");
  828. xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema-instance", (const xmlChar*)"xsi");
  829. xmlNewNsProp(root, xslNs, (const xmlChar*)"version", (const xmlChar*)"1.0");
  830. xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema", (const xmlChar*)"xs");
  831. xmlNewNs(root, (const xmlChar*)"http://www.ripedev.com/", (const xmlChar*)"LocalTime");
  832. if((headerElements != nil) && ([headerElements count] > 0)) {
  833. xmlNodePtr headerNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Header", NULL);
  834. xmlAddChild(root, headerNode);
  835. for(NSString *key in [headerElements allKeys]) {
  836. id header = [headerElements objectForKey:key];
  837. xmlAddChild(headerNode, [header xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);
  838. }
  839. }
  840. if((bodyElements != nil) && ([bodyElements count] > 0)) {
  841. xmlNodePtr bodyNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Body", NULL);
  842. xmlAddChild(root, bodyNode);
  843. for(NSString *key in [bodyElements allKeys]) {
  844. id body = [bodyElements objectForKey:key];
  845. xmlAddChild(bodyNode, [body xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);
  846. }
  847. }
  848. xmlChar *buf;
  849. int size;
  850. xmlDocDumpFormatMemory(doc, &buf, &size, 1);
  851. NSString *serializedForm = [NSString stringWithCString:(const char*)buf encoding:NSUTF8StringEncoding];
  852. xmlFree(buf);
  853. xmlFreeDoc(doc);
  854. return serializedForm;
  855. }
  856. @end
  857. @implementation LocalTimeSoap12BindingResponse
  858. @synthesize headers;
  859. @synthesize bodyParts;
  860. @synthesize error;
  861. - (id)init
  862. {
  863. if((self = [super init])) {
  864. headers = nil;
  865. bodyParts = nil;
  866. error = nil;
  867. }
  868. return self;
  869. }
  870. -(void)dealloc {
  871. self.headers = nil;
  872. self.bodyParts = nil;
  873. self.error = nil;
  874. [super dealloc];
  875. }
  876. @end