/WebServiceLocalTimeSoap/LocalTime/LocalTime.m
Objective C | 1018 lines | 864 code | 142 blank | 12 comment | 176 complexity | 6a64a0f9225943060979d8079e76a23e MD5 | raw file
- #import "LocalTime.h"
- #import <libxml/xmlstring.h>
- #if TARGET_OS_IPHONE
- #import <CFNetwork/CFNetwork.h>
- #endif
- @implementation LocalTime_LocalTimeByZipCode
- - (id)init
- {
- if((self = [super init])) {
- ZipCode = 0;
- }
-
- return self;
- }
- - (void)dealloc
- {
- if(ZipCode != nil) [ZipCode release];
-
- [super dealloc];
- }
- - (NSString *)nsPrefix
- {
- return @"LocalTime";
- }
- - (xmlNodePtr)xmlNodeForDoc:(xmlDocPtr)doc elementName:(NSString *)elName elementNSPrefix:(NSString *)elNSPrefix
- {
- NSString *nodeName = nil;
- if(elNSPrefix != nil && [elNSPrefix length] > 0)
- {
- nodeName = [NSString stringWithFormat:@"%@:%@", elNSPrefix, elName];
- }
- else
- {
- nodeName = [NSString stringWithFormat:@"%@:%@", @"LocalTime", elName];
- }
-
- xmlNodePtr node = xmlNewDocNode(doc, NULL, [nodeName xmlString], NULL);
-
-
- [self addAttributesToNode:node];
-
- [self addElementsToNode:node];
-
- return node;
- }
- - (void)addAttributesToNode:(xmlNodePtr)node
- {
-
- }
- - (void)addElementsToNode:(xmlNodePtr)node
- {
-
- if(self.ZipCode != 0) {
- xmlAddChild(node, [self.ZipCode xmlNodeForDoc:node->doc elementName:@"ZipCode" elementNSPrefix:@"LocalTime"]);
- }
- }
- /* elements */
- @synthesize ZipCode;
- /* attributes */
- - (NSDictionary *)attributes
- {
- NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
-
- return attributes;
- }
- + (LocalTime_LocalTimeByZipCode *)deserializeNode:(xmlNodePtr)cur
- {
- LocalTime_LocalTimeByZipCode *newObject = [[LocalTime_LocalTimeByZipCode new] autorelease];
-
- [newObject deserializeAttributesFromNode:cur];
- [newObject deserializeElementsFromNode:cur];
-
- return newObject;
- }
- - (void)deserializeAttributesFromNode:(xmlNodePtr)cur
- {
- }
- - (void)deserializeElementsFromNode:(xmlNodePtr)cur
- {
-
-
- for( cur = cur->children ; cur != NULL ; cur = cur->next ) {
- if(cur->type == XML_ELEMENT_NODE) {
- xmlChar *elementText = xmlNodeListGetString(cur->doc, cur->children, 1);
- NSString *elementString = nil;
-
- if(elementText != NULL) {
- elementString = [NSString stringWithCString:(char*)elementText encoding:NSUTF8StringEncoding];
- [elementString self]; // avoid compiler warning for unused var
- xmlFree(elementText);
- }
- if(xmlStrEqual(cur->name, (const xmlChar *) "ZipCode")) {
-
- Class elementClass = nil;
- xmlChar *instanceType = xmlGetNsProp(cur, (const xmlChar *) "type", (const xmlChar *) "http://www.w3.org/2001/XMLSchema-instance");
- if(instanceType == NULL) {
- elementClass = [NSString class];
- } else {
- NSString *elementTypeString = [NSString stringWithCString:(char*)instanceType encoding:NSUTF8StringEncoding];
-
- NSArray *elementTypeArray = [elementTypeString componentsSeparatedByString:@":"];
-
- NSString *elementClassString = nil;
- if([elementTypeArray count] > 1) {
- NSString *prefix = [elementTypeArray objectAtIndex:0];
- NSString *localName = [elementTypeArray objectAtIndex:1];
-
- xmlNsPtr elementNamespace = xmlSearchNs(cur->doc, cur, [prefix xmlString]);
-
- NSString *standardPrefix = [[USGlobals sharedInstance].wsdlStandardNamespaces objectForKey:[NSString stringWithCString:(char*)elementNamespace->href encoding:NSUTF8StringEncoding]];
-
- elementClassString = [NSString stringWithFormat:@"%@_%@", standardPrefix, localName];
- } else {
- elementClassString = [elementTypeString stringByReplacingOccurrencesOfString:@":" withString:@"_" options:0 range:NSMakeRange(0, [elementTypeString length])];
- }
-
- elementClass = NSClassFromString(elementClassString);
- xmlFree(instanceType);
- }
-
- id newChild = [elementClass deserializeNode:cur];
-
- self.ZipCode = newChild;
- }
- }
- }
- }
- @end
- @implementation LocalTime_LocalTimeByZipCodeResponse
- - (id)init
- {
- if((self = [super init])) {
- LocalTimeByZipCodeResult = 0;
- }
-
- return self;
- }
- - (void)dealloc
- {
- if(LocalTimeByZipCodeResult != nil) [LocalTimeByZipCodeResult release];
-
- [super dealloc];
- }
- - (NSString *)nsPrefix
- {
- return @"LocalTime";
- }
- - (xmlNodePtr)xmlNodeForDoc:(xmlDocPtr)doc elementName:(NSString *)elName elementNSPrefix:(NSString *)elNSPrefix
- {
- NSString *nodeName = nil;
- if(elNSPrefix != nil && [elNSPrefix length] > 0)
- {
- nodeName = [NSString stringWithFormat:@"%@:%@", elNSPrefix, elName];
- }
- else
- {
- nodeName = [NSString stringWithFormat:@"%@:%@", @"LocalTime", elName];
- }
-
- xmlNodePtr node = xmlNewDocNode(doc, NULL, [nodeName xmlString], NULL);
-
-
- [self addAttributesToNode:node];
-
- [self addElementsToNode:node];
-
- return node;
- }
- - (void)addAttributesToNode:(xmlNodePtr)node
- {
-
- }
- - (void)addElementsToNode:(xmlNodePtr)node
- {
-
- if(self.LocalTimeByZipCodeResult != 0) {
- xmlAddChild(node, [self.LocalTimeByZipCodeResult xmlNodeForDoc:node->doc elementName:@"LocalTimeByZipCodeResult" elementNSPrefix:@"LocalTime"]);
- }
- }
- /* elements */
- @synthesize LocalTimeByZipCodeResult;
- /* attributes */
- - (NSDictionary *)attributes
- {
- NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
-
- return attributes;
- }
- + (LocalTime_LocalTimeByZipCodeResponse *)deserializeNode:(xmlNodePtr)cur
- {
- LocalTime_LocalTimeByZipCodeResponse *newObject = [[LocalTime_LocalTimeByZipCodeResponse new] autorelease];
-
- [newObject deserializeAttributesFromNode:cur];
- [newObject deserializeElementsFromNode:cur];
-
- return newObject;
- }
- - (void)deserializeAttributesFromNode:(xmlNodePtr)cur
- {
- }
- - (void)deserializeElementsFromNode:(xmlNodePtr)cur
- {
-
-
- for( cur = cur->children ; cur != NULL ; cur = cur->next ) {
- if(cur->type == XML_ELEMENT_NODE) {
- xmlChar *elementText = xmlNodeListGetString(cur->doc, cur->children, 1);
- NSString *elementString = nil;
-
- if(elementText != NULL) {
- elementString = [NSString stringWithCString:(char*)elementText encoding:NSUTF8StringEncoding];
- [elementString self]; // avoid compiler warning for unused var
- xmlFree(elementText);
- }
- if(xmlStrEqual(cur->name, (const xmlChar *) "LocalTimeByZipCodeResult")) {
-
- Class elementClass = nil;
- xmlChar *instanceType = xmlGetNsProp(cur, (const xmlChar *) "type", (const xmlChar *) "http://www.w3.org/2001/XMLSchema-instance");
- if(instanceType == NULL) {
- elementClass = [NSString class];
- } else {
- NSString *elementTypeString = [NSString stringWithCString:(char*)instanceType encoding:NSUTF8StringEncoding];
-
- NSArray *elementTypeArray = [elementTypeString componentsSeparatedByString:@":"];
-
- NSString *elementClassString = nil;
- if([elementTypeArray count] > 1) {
- NSString *prefix = [elementTypeArray objectAtIndex:0];
- NSString *localName = [elementTypeArray objectAtIndex:1];
-
- xmlNsPtr elementNamespace = xmlSearchNs(cur->doc, cur, [prefix xmlString]);
-
- NSString *standardPrefix = [[USGlobals sharedInstance].wsdlStandardNamespaces objectForKey:[NSString stringWithCString:(char*)elementNamespace->href encoding:NSUTF8StringEncoding]];
-
- elementClassString = [NSString stringWithFormat:@"%@_%@", standardPrefix, localName];
- } else {
- elementClassString = [elementTypeString stringByReplacingOccurrencesOfString:@":" withString:@"_" options:0 range:NSMakeRange(0, [elementTypeString length])];
- }
-
- elementClass = NSClassFromString(elementClassString);
- xmlFree(instanceType);
- }
-
- id newChild = [elementClass deserializeNode:cur];
-
- self.LocalTimeByZipCodeResult = newChild;
- }
- }
- }
- }
- @end
- @implementation LocalTime
- + (void)initialize
- {
- [[USGlobals sharedInstance].wsdlStandardNamespaces setObject:@"xs" forKey:@"http://www.w3.org/2001/XMLSchema"];
- [[USGlobals sharedInstance].wsdlStandardNamespaces setObject:@"LocalTime" forKey:@"http://www.ripedev.com/"];
- }
- + (LocalTimeSoapBinding *)LocalTimeSoapBinding
- {
- return [[[LocalTimeSoapBinding alloc] initWithAddress:@"http://www.ripedevelopment.com/webservices/LocalTime.asmx"] autorelease];
- }
- + (LocalTimeSoap12Binding *)LocalTimeSoap12Binding
- {
- return [[[LocalTimeSoap12Binding alloc] initWithAddress:@"http://www.ripedevelopment.com/webservices/LocalTime.asmx"] autorelease];
- }
- @end
- @implementation LocalTimeSoapBinding
- @synthesize address;
- @synthesize defaultTimeout;
- @synthesize logXMLInOut;
- @synthesize cookies;
- @synthesize authUsername;
- @synthesize authPassword;
- - (id)init
- {
- if((self = [super init])) {
- address = nil;
- cookies = nil;
- defaultTimeout = 10;//seconds
- logXMLInOut = NO;
- synchronousOperationComplete = NO;
- }
-
- return self;
- }
- - (id)initWithAddress:(NSString *)anAddress
- {
- if((self = [self init])) {
- self.address = [NSURL URLWithString:anAddress];
- }
-
- return self;
- }
- - (void)addCookie:(NSHTTPCookie *)toAdd
- {
- if(toAdd != nil) {
- if(cookies == nil) cookies = [[NSMutableArray alloc] init];
- [cookies addObject:toAdd];
- }
- }
- - (LocalTimeSoapBindingResponse *)performSynchronousOperation:(LocalTimeSoapBindingOperation *)operation
- {
- synchronousOperationComplete = NO;
- [operation start];
-
- // Now wait for response
- NSRunLoop *theRL = [NSRunLoop currentRunLoop];
-
- while (!synchronousOperationComplete && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
- return operation.response;
- }
- - (void)performAsynchronousOperation:(LocalTimeSoapBindingOperation *)operation
- {
- [operation start];
- }
- - (void) operation:(LocalTimeSoapBindingOperation *)operation completedWithResponse:(LocalTimeSoapBindingResponse *)response
- {
- synchronousOperationComplete = YES;
- }
- - (LocalTimeSoapBindingResponse *)LocalTimeByZipCodeUsingParameters:(LocalTime_LocalTimeByZipCode *)aParameters
- {
- return [self performSynchronousOperation:[[(LocalTimeSoapBinding_LocalTimeByZipCode*)[LocalTimeSoapBinding_LocalTimeByZipCode alloc] initWithBinding:self delegate:self
- parameters:aParameters
- ] autorelease]];
- }
- - (void)LocalTimeByZipCodeAsyncUsingParameters:(LocalTime_LocalTimeByZipCode *)aParameters delegate:(id<LocalTimeSoapBindingResponseDelegate>)responseDelegate
- {
- [self performAsynchronousOperation: [[(LocalTimeSoapBinding_LocalTimeByZipCode*)[LocalTimeSoapBinding_LocalTimeByZipCode alloc] initWithBinding:self delegate:responseDelegate
- parameters:aParameters
- ] autorelease]];
- }
- - (void)sendHTTPCallUsingBody:(NSString *)outputBody soapAction:(NSString *)soapAction forOperation:(LocalTimeSoapBindingOperation *)operation
- {
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.address
- cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
- timeoutInterval:self.defaultTimeout];
- NSData *bodyData = [outputBody dataUsingEncoding:NSUTF8StringEncoding];
-
- if(cookies != nil) {
- [request setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:cookies]];
- }
- [request setValue:@"wsdl2objc" forHTTPHeaderField:@"User-Agent"];
- [request setValue:soapAction forHTTPHeaderField:@"SOAPAction"];
- [request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
- [request setValue:[NSString stringWithFormat:@"%u", [bodyData length]] forHTTPHeaderField:@"Content-Length"];
- [request setValue:self.address.host forHTTPHeaderField:@"Host"];
- [request setHTTPMethod: @"POST"];
- // set version 1.1 - how?
- [request setHTTPBody: bodyData];
-
- if(self.logXMLInOut) {
- NSLog(@"OutputHeaders:\n%@", [request allHTTPHeaderFields]);
- NSLog(@"OutputBody:\n%@", outputBody);
- }
-
- NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:operation];
-
- operation.urlConnection = connection;
- [connection release];
- }
- - (void) dealloc
- {
- [address release];
- [cookies release];
- [super dealloc];
- }
- @end
- @implementation LocalTimeSoapBindingOperation
- @synthesize binding;
- @synthesize response;
- @synthesize delegate;
- @synthesize responseData;
- @synthesize urlConnection;
- - (id)initWithBinding:(LocalTimeSoapBinding *)aBinding delegate:(id<LocalTimeSoapBindingResponseDelegate>)aDelegate
- {
- if ((self = [super init])) {
- self.binding = aBinding;
- response = nil;
- self.delegate = aDelegate;
- self.responseData = nil;
- self.urlConnection = nil;
- }
-
- return self;
- }
- -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
- {
- if ([challenge previousFailureCount] == 0) {
- NSURLCredential *newCredential;
- newCredential=[NSURLCredential credentialWithUser:self.binding.authUsername
- password:self.binding.authPassword
- persistence:NSURLCredentialPersistenceForSession];
- [[challenge sender] useCredential:newCredential
- forAuthenticationChallenge:challenge];
- } else {
- [[challenge sender] cancelAuthenticationChallenge:challenge];
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Authentication Error" forKey:NSLocalizedDescriptionKey];
- NSError *authError = [NSError errorWithDomain:@"Connection Authentication" code:0 userInfo:userInfo];
- [self connection:connection didFailWithError:authError];
- }
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)urlResponse
- {
- NSHTTPURLResponse *httpResponse;
- if ([urlResponse isKindOfClass:[NSHTTPURLResponse class]]) {
- httpResponse = (NSHTTPURLResponse *) urlResponse;
- } else {
- httpResponse = nil;
- }
-
- if(binding.logXMLInOut) {
- NSLog(@"ResponseStatus: %u\n", [httpResponse statusCode]);
- NSLog(@"ResponseHeaders:\n%@", [httpResponse allHeaderFields]);
- }
-
- NSMutableArray *cookies = [[NSHTTPCookie cookiesWithResponseHeaderFields:[httpResponse allHeaderFields] forURL:binding.address] mutableCopy];
-
- binding.cookies = cookies;
- [cookies release];
- if ([urlResponse.MIMEType rangeOfString:@"text/xml"].length == 0) {
- NSError *error = nil;
- [connection cancel];
- if ([httpResponse statusCode] >= 400) {
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]] forKey:NSLocalizedDescriptionKey];
-
- error = [NSError errorWithDomain:@"LocalTimeSoapBindingResponseHTTP" code:[httpResponse statusCode] userInfo:userInfo];
- } else {
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:
- [NSString stringWithFormat: @"Unexpected response MIME type to SOAP call:%@", urlResponse.MIMEType]
- forKey:NSLocalizedDescriptionKey];
- error = [NSError errorWithDomain:@"LocalTimeSoapBindingResponseHTTP" code:1 userInfo:userInfo];
- }
-
- [self connection:connection didFailWithError:error];
- }
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- if (responseData == nil) {
- responseData = [data mutableCopy];
- } else {
- [responseData appendData:data];
- }
- }
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- {
- if (binding.logXMLInOut) {
- NSLog(@"ResponseError:\n%@", error);
- }
- response.error = error;
- [delegate operation:self completedWithResponse:response];
- }
- - (void)dealloc
- {
- [binding release];
- [response release];
- delegate = nil;
- [responseData release];
- [urlConnection release];
-
- [super dealloc];
- }
- @end
- @implementation LocalTimeSoapBinding_LocalTimeByZipCode
- @synthesize parameters;
- - (id)initWithBinding:(LocalTimeSoapBinding *)aBinding delegate:(id<LocalTimeSoapBindingResponseDelegate>)responseDelegate
- parameters:(LocalTime_LocalTimeByZipCode *)aParameters
- {
- if((self = [super initWithBinding:aBinding delegate:responseDelegate])) {
- self.parameters = aParameters;
- }
-
- return self;
- }
- - (void)dealloc
- {
- if(parameters != nil) [parameters release];
-
- [super dealloc];
- }
- - (void)main
- {
- [response autorelease];
- response = [LocalTimeSoapBindingResponse new];
-
- LocalTimeSoapBinding_envelope *envelope = [LocalTimeSoapBinding_envelope sharedInstance];
-
- NSMutableDictionary *headerElements = nil;
- headerElements = [NSMutableDictionary dictionary];
-
- NSMutableDictionary *bodyElements = nil;
- bodyElements = [NSMutableDictionary dictionary];
- if(parameters != nil) [bodyElements setObject:parameters forKey:@"LocalTimeByZipCode"];
-
- NSString *operationXMLString = [envelope serializedFormUsingHeaderElements:headerElements bodyElements:bodyElements];
-
- [binding sendHTTPCallUsingBody:operationXMLString soapAction:@"http://www.ripedev.com/LocalTimeByZipCode" forOperation:self];
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- if (responseData != nil && delegate != nil)
- {
- xmlDocPtr doc;
- xmlNodePtr cur;
-
- if (binding.logXMLInOut) {
- NSLog(@"ResponseBody:\n%@", [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease]);
- }
-
- doc = xmlParseMemory([responseData bytes], [responseData length]);
-
- if (doc == NULL) {
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Errors while parsing returned XML" forKey:NSLocalizedDescriptionKey];
-
- response.error = [NSError errorWithDomain:@"LocalTimeSoapBindingResponseXML" code:1 userInfo:userInfo];
- [delegate operation:self completedWithResponse:response];
- } else {
- cur = xmlDocGetRootElement(doc);
- cur = cur->children;
-
- for( ; cur != NULL ; cur = cur->next) {
- if(cur->type == XML_ELEMENT_NODE) {
-
- if(xmlStrEqual(cur->name, (const xmlChar *) "Body")) {
- NSMutableArray *responseBodyParts = [NSMutableArray array];
-
- xmlNodePtr bodyNode;
- for(bodyNode=cur->children ; bodyNode != NULL ; bodyNode = bodyNode->next) {
- if(cur->type == XML_ELEMENT_NODE) {
- if(xmlStrEqual(bodyNode->name, (const xmlChar *) "LocalTimeByZipCodeResponse")) {
- LocalTime_LocalTimeByZipCodeResponse *bodyObject = [LocalTime_LocalTimeByZipCodeResponse deserializeNode:bodyNode];
- //NSAssert1(bodyObject != nil, @"Errors while parsing body %s", bodyNode->name);
- if (bodyObject != nil) [responseBodyParts addObject:bodyObject];
- }
- if (xmlStrEqual(bodyNode->ns->prefix, cur->ns->prefix) &&
- xmlStrEqual(bodyNode->name, (const xmlChar *) "Fault")) {
- SOAPFault *bodyObject = [SOAPFault deserializeNode:bodyNode];
- //NSAssert1(bodyObject != nil, @"Errors while parsing body %s", bodyNode->name);
- if (bodyObject != nil) [responseBodyParts addObject:bodyObject];
- }
- }
- }
-
- response.bodyParts = responseBodyParts;
- }
- }
- }
-
- xmlFreeDoc(doc);
- }
-
- xmlCleanupParser();
- [delegate operation:self completedWithResponse:response];
- }
- }
- @end
- static LocalTimeSoapBinding_envelope *LocalTimeSoapBindingSharedEnvelopeInstance = nil;
- @implementation LocalTimeSoapBinding_envelope
- + (LocalTimeSoapBinding_envelope *)sharedInstance
- {
- if(LocalTimeSoapBindingSharedEnvelopeInstance == nil) {
- LocalTimeSoapBindingSharedEnvelopeInstance = [LocalTimeSoapBinding_envelope new];
- }
-
- return LocalTimeSoapBindingSharedEnvelopeInstance;
- }
- - (NSString *)serializedFormUsingHeaderElements:(NSDictionary *)headerElements bodyElements:(NSDictionary *)bodyElements
- {
- xmlDocPtr doc;
-
- doc = xmlNewDoc((const xmlChar*)XML_DEFAULT_VERSION);
- if (doc == NULL) {
- NSLog(@"Error creating the xml document tree");
- return @"";
- }
-
- xmlNodePtr root = xmlNewDocNode(doc, NULL, (const xmlChar*)"Envelope", NULL);
- xmlDocSetRootElement(doc, root);
-
- xmlNsPtr soapEnvelopeNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2003/05/soap-envelope", (const xmlChar*)"soap");
- xmlSetNs(root, soapEnvelopeNs);
-
- xmlNsPtr xslNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/1999/XSL/Transform", (const xmlChar*)"xsl");
- xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema-instance", (const xmlChar*)"xsi");
-
- xmlNewNsProp(root, xslNs, (const xmlChar*)"version", (const xmlChar*)"1.0");
-
- xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema", (const xmlChar*)"xs");
- xmlNewNs(root, (const xmlChar*)"http://www.ripedev.com/", (const xmlChar*)"LocalTime");
-
- if((headerElements != nil) && ([headerElements count] > 0)) {
- xmlNodePtr headerNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Header", NULL);
- xmlAddChild(root, headerNode);
-
- for(NSString *key in [headerElements allKeys]) {
- id header = [headerElements objectForKey:key];
- xmlAddChild(headerNode, [header xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);
- }
- }
-
- if((bodyElements != nil) && ([bodyElements count] > 0)) {
- xmlNodePtr bodyNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Body", NULL);
- xmlAddChild(root, bodyNode);
-
- for(NSString *key in [bodyElements allKeys]) {
- id body = [bodyElements objectForKey:key];
- xmlAddChild(bodyNode, [body xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);
- }
- }
-
- xmlChar *buf;
- int size;
- xmlDocDumpFormatMemory(doc, &buf, &size, 1);
-
- NSString *serializedForm = [NSString stringWithCString:(const char*)buf encoding:NSUTF8StringEncoding];
- xmlFree(buf);
-
- xmlFreeDoc(doc);
- return serializedForm;
- }
- @end
- @implementation LocalTimeSoapBindingResponse
- @synthesize headers;
- @synthesize bodyParts;
- @synthesize error;
- - (id)init
- {
- if((self = [super init])) {
- headers = nil;
- bodyParts = nil;
- error = nil;
- }
-
- return self;
- }
- -(void)dealloc {
- self.headers = nil;
- self.bodyParts = nil;
- self.error = nil;
- [super dealloc];
- }
- @end
- @implementation LocalTimeSoap12Binding
- @synthesize address;
- @synthesize defaultTimeout;
- @synthesize logXMLInOut;
- @synthesize cookies;
- @synthesize authUsername;
- @synthesize authPassword;
- - (id)init
- {
- if((self = [super init])) {
- address = nil;
- cookies = nil;
- defaultTimeout = 10;//seconds
- logXMLInOut = NO;
- synchronousOperationComplete = NO;
- }
-
- return self;
- }
- - (id)initWithAddress:(NSString *)anAddress
- {
- if((self = [self init])) {
- self.address = [NSURL URLWithString:anAddress];
- }
-
- return self;
- }
- - (void)addCookie:(NSHTTPCookie *)toAdd
- {
- if(toAdd != nil) {
- if(cookies == nil) cookies = [[NSMutableArray alloc] init];
- [cookies addObject:toAdd];
- }
- }
- - (LocalTimeSoap12BindingResponse *)performSynchronousOperation:(LocalTimeSoap12BindingOperation *)operation
- {
- synchronousOperationComplete = NO;
- [operation start];
-
- // Now wait for response
- NSRunLoop *theRL = [NSRunLoop currentRunLoop];
-
- while (!synchronousOperationComplete && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
- return operation.response;
- }
- - (void)performAsynchronousOperation:(LocalTimeSoap12BindingOperation *)operation
- {
- [operation start];
- }
- - (void) operation:(LocalTimeSoap12BindingOperation *)operation completedWithResponse:(LocalTimeSoap12BindingResponse *)response
- {
- synchronousOperationComplete = YES;
- }
- - (LocalTimeSoap12BindingResponse *)LocalTimeByZipCodeUsingParameters:(LocalTime_LocalTimeByZipCode *)aParameters
- {
- return [self performSynchronousOperation:[[(LocalTimeSoap12Binding_LocalTimeByZipCode*)[LocalTimeSoap12Binding_LocalTimeByZipCode alloc] initWithBinding:self delegate:self
- parameters:aParameters
- ] autorelease]];
- }
- - (void)LocalTimeByZipCodeAsyncUsingParameters:(LocalTime_LocalTimeByZipCode *)aParameters delegate:(id<LocalTimeSoap12BindingResponseDelegate>)responseDelegate
- {
- [self performAsynchronousOperation: [[(LocalTimeSoap12Binding_LocalTimeByZipCode*)[LocalTimeSoap12Binding_LocalTimeByZipCode alloc] initWithBinding:self delegate:responseDelegate
- parameters:aParameters
- ] autorelease]];
- }
- - (void)sendHTTPCallUsingBody:(NSString *)outputBody soapAction:(NSString *)soapAction forOperation:(LocalTimeSoap12BindingOperation *)operation
- {
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.address
- cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
- timeoutInterval:self.defaultTimeout];
- NSData *bodyData = [outputBody dataUsingEncoding:NSUTF8StringEncoding];
-
- if(cookies != nil) {
- [request setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:cookies]];
- }
- [request setValue:@"wsdl2objc" forHTTPHeaderField:@"User-Agent"];
- [request setValue:soapAction forHTTPHeaderField:@"SOAPAction"];
- [request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
- [request setValue:[NSString stringWithFormat:@"%u", [bodyData length]] forHTTPHeaderField:@"Content-Length"];
- [request setValue:self.address.host forHTTPHeaderField:@"Host"];
- [request setHTTPMethod: @"POST"];
- // set version 1.1 - how?
- [request setHTTPBody: bodyData];
-
- if(self.logXMLInOut) {
- NSLog(@"OutputHeaders:\n%@", [request allHTTPHeaderFields]);
- NSLog(@"OutputBody:\n%@", outputBody);
- }
-
- NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:operation];
-
- operation.urlConnection = connection;
- [connection release];
- }
- - (void) dealloc
- {
- [address release];
- [cookies release];
- [super dealloc];
- }
- @end
- @implementation LocalTimeSoap12BindingOperation
- @synthesize binding;
- @synthesize response;
- @synthesize delegate;
- @synthesize responseData;
- @synthesize urlConnection;
- - (id)initWithBinding:(LocalTimeSoap12Binding *)aBinding delegate:(id<LocalTimeSoap12BindingResponseDelegate>)aDelegate
- {
- if ((self = [super init])) {
- self.binding = aBinding;
- response = nil;
- self.delegate = aDelegate;
- self.responseData = nil;
- self.urlConnection = nil;
- }
-
- return self;
- }
- -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
- {
- if ([challenge previousFailureCount] == 0) {
- NSURLCredential *newCredential;
- newCredential=[NSURLCredential credentialWithUser:self.binding.authUsername
- password:self.binding.authPassword
- persistence:NSURLCredentialPersistenceForSession];
- [[challenge sender] useCredential:newCredential
- forAuthenticationChallenge:challenge];
- } else {
- [[challenge sender] cancelAuthenticationChallenge:challenge];
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Authentication Error" forKey:NSLocalizedDescriptionKey];
- NSError *authError = [NSError errorWithDomain:@"Connection Authentication" code:0 userInfo:userInfo];
- [self connection:connection didFailWithError:authError];
- }
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)urlResponse
- {
- NSHTTPURLResponse *httpResponse;
- if ([urlResponse isKindOfClass:[NSHTTPURLResponse class]]) {
- httpResponse = (NSHTTPURLResponse *) urlResponse;
- } else {
- httpResponse = nil;
- }
-
- if(binding.logXMLInOut) {
- NSLog(@"ResponseStatus: %u\n", [httpResponse statusCode]);
- NSLog(@"ResponseHeaders:\n%@", [httpResponse allHeaderFields]);
- }
-
- NSMutableArray *cookies = [[NSHTTPCookie cookiesWithResponseHeaderFields:[httpResponse allHeaderFields] forURL:binding.address] mutableCopy];
-
- binding.cookies = cookies;
- [cookies release];
- if ([urlResponse.MIMEType rangeOfString:@"text/xml"].length == 0) {
- NSError *error = nil;
- [connection cancel];
- if ([httpResponse statusCode] >= 400) {
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]] forKey:NSLocalizedDescriptionKey];
-
- error = [NSError errorWithDomain:@"LocalTimeSoap12BindingResponseHTTP" code:[httpResponse statusCode] userInfo:userInfo];
- } else {
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:
- [NSString stringWithFormat: @"Unexpected response MIME type to SOAP call:%@", urlResponse.MIMEType]
- forKey:NSLocalizedDescriptionKey];
- error = [NSError errorWithDomain:@"LocalTimeSoap12BindingResponseHTTP" code:1 userInfo:userInfo];
- }
-
- [self connection:connection didFailWithError:error];
- }
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- if (responseData == nil) {
- responseData = [data mutableCopy];
- } else {
- [responseData appendData:data];
- }
- }
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- {
- if (binding.logXMLInOut) {
- NSLog(@"ResponseError:\n%@", error);
- }
- response.error = error;
- [delegate operation:self completedWithResponse:response];
- }
- - (void)dealloc
- {
- [binding release];
- [response release];
- delegate = nil;
- [responseData release];
- [urlConnection release];
-
- [super dealloc];
- }
- @end
- @implementation LocalTimeSoap12Binding_LocalTimeByZipCode
- @synthesize parameters;
- - (id)initWithBinding:(LocalTimeSoap12Binding *)aBinding delegate:(id<LocalTimeSoap12BindingResponseDelegate>)responseDelegate
- parameters:(LocalTime_LocalTimeByZipCode *)aParameters
- {
- if((self = [super initWithBinding:aBinding delegate:responseDelegate])) {
- self.parameters = aParameters;
- }
-
- return self;
- }
- - (void)dealloc
- {
- if(parameters != nil) [parameters release];
-
- [super dealloc];
- }
- - (void)main
- {
- [response autorelease];
- response = [LocalTimeSoap12BindingResponse new];
-
- LocalTimeSoap12Binding_envelope *envelope = [LocalTimeSoap12Binding_envelope sharedInstance];
-
- NSMutableDictionary *headerElements = nil;
- headerElements = [NSMutableDictionary dictionary];
-
- NSMutableDictionary *bodyElements = nil;
- bodyElements = [NSMutableDictionary dictionary];
- if(parameters != nil) [bodyElements setObject:parameters forKey:@"LocalTimeByZipCode"];
-
- NSString *operationXMLString = [envelope serializedFormUsingHeaderElements:headerElements bodyElements:bodyElements];
-
- [binding sendHTTPCallUsingBody:operationXMLString soapAction:@"http://www.ripedev.com/LocalTimeByZipCode" forOperation:self];
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- if (responseData != nil && delegate != nil)
- {
- xmlDocPtr doc;
- xmlNodePtr cur;
-
- if (binding.logXMLInOut) {
- NSLog(@"ResponseBody:\n%@", [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease]);
- }
-
- doc = xmlParseMemory([responseData bytes], [responseData length]);
-
- if (doc == NULL) {
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Errors while parsing returned XML" forKey:NSLocalizedDescriptionKey];
-
- response.error = [NSError errorWithDomain:@"LocalTimeSoap12BindingResponseXML" code:1 userInfo:userInfo];
- [delegate operation:self completedWithResponse:response];
- } else {
- cur = xmlDocGetRootElement(doc);
- cur = cur->children;
-
- for( ; cur != NULL ; cur = cur->next) {
- if(cur->type == XML_ELEMENT_NODE) {
-
- if(xmlStrEqual(cur->name, (const xmlChar *) "Body")) {
- NSMutableArray *responseBodyParts = [NSMutableArray array];
-
- xmlNodePtr bodyNode;
- for(bodyNode=cur->children ; bodyNode != NULL ; bodyNode = bodyNode->next) {
- if(cur->type == XML_ELEMENT_NODE) {
- if(xmlStrEqual(bodyNode->name, (const xmlChar *) "LocalTimeByZipCodeResponse")) {
- LocalTime_LocalTimeByZipCodeResponse *bodyObject = [LocalTime_LocalTimeByZipCodeResponse deserializeNode:bodyNode];
- //NSAssert1(bodyObject != nil, @"Errors while parsing body %s", bodyNode->name);
- if (bodyObject != nil) [responseBodyParts addObject:bodyObject];
- }
- if (xmlStrEqual(bodyNode->ns->prefix, cur->ns->prefix) &&
- xmlStrEqual(bodyNode->name, (const xmlChar *) "Fault")) {
- SOAPFault *bodyObject = [SOAPFault deserializeNode:bodyNode];
- //NSAssert1(bodyObject != nil, @"Errors while parsing body %s", bodyNode->name);
- if (bodyObject != nil) [responseBodyParts addObject:bodyObject];
- }
- }
- }
-
- response.bodyParts = responseBodyParts;
- }
- }
- }
-
- xmlFreeDoc(doc);
- }
-
- xmlCleanupParser();
- [delegate operation:self completedWithResponse:response];
- }
- }
- @end
- static LocalTimeSoap12Binding_envelope *LocalTimeSoap12BindingSharedEnvelopeInstance = nil;
- @implementation LocalTimeSoap12Binding_envelope
- + (LocalTimeSoap12Binding_envelope *)sharedInstance
- {
- if(LocalTimeSoap12BindingSharedEnvelopeInstance == nil) {
- LocalTimeSoap12BindingSharedEnvelopeInstance = [LocalTimeSoap12Binding_envelope new];
- }
-
- return LocalTimeSoap12BindingSharedEnvelopeInstance;
- }
- - (NSString *)serializedFormUsingHeaderElements:(NSDictionary *)headerElements bodyElements:(NSDictionary *)bodyElements
- {
- xmlDocPtr doc;
-
- doc = xmlNewDoc((const xmlChar*)XML_DEFAULT_VERSION);
- if (doc == NULL) {
- NSLog(@"Error creating the xml document tree");
- return @"";
- }
-
- xmlNodePtr root = xmlNewDocNode(doc, NULL, (const xmlChar*)"Envelope", NULL);
- xmlDocSetRootElement(doc, root);
-
- xmlNsPtr soapEnvelopeNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2003/05/soap-envelope", (const xmlChar*)"soap");
- xmlSetNs(root, soapEnvelopeNs);
-
- xmlNsPtr xslNs = xmlNewNs(root, (const xmlChar*)"http://www.w3.org/1999/XSL/Transform", (const xmlChar*)"xsl");
- xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema-instance", (const xmlChar*)"xsi");
-
- xmlNewNsProp(root, xslNs, (const xmlChar*)"version", (const xmlChar*)"1.0");
-
- xmlNewNs(root, (const xmlChar*)"http://www.w3.org/2001/XMLSchema", (const xmlChar*)"xs");
- xmlNewNs(root, (const xmlChar*)"http://www.ripedev.com/", (const xmlChar*)"LocalTime");
-
- if((headerElements != nil) && ([headerElements count] > 0)) {
- xmlNodePtr headerNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Header", NULL);
- xmlAddChild(root, headerNode);
-
- for(NSString *key in [headerElements allKeys]) {
- id header = [headerElements objectForKey:key];
- xmlAddChild(headerNode, [header xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);
- }
- }
-
- if((bodyElements != nil) && ([bodyElements count] > 0)) {
- xmlNodePtr bodyNode = xmlNewDocNode(doc, soapEnvelopeNs, (const xmlChar*)"Body", NULL);
- xmlAddChild(root, bodyNode);
-
- for(NSString *key in [bodyElements allKeys]) {
- id body = [bodyElements objectForKey:key];
- xmlAddChild(bodyNode, [body xmlNodeForDoc:doc elementName:key elementNSPrefix:nil]);
- }
- }
-
- xmlChar *buf;
- int size;
- xmlDocDumpFormatMemory(doc, &buf, &size, 1);
-
- NSString *serializedForm = [NSString stringWithCString:(const char*)buf encoding:NSUTF8StringEncoding];
- xmlFree(buf);
-
- xmlFreeDoc(doc);
- return serializedForm;
- }
- @end
- @implementation LocalTimeSoap12BindingResponse
- @synthesize headers;
- @synthesize bodyParts;
- @synthesize error;
- - (id)init
- {
- if((self = [super init])) {
- headers = nil;
- bodyParts = nil;
- error = nil;
- }
-
- return self;
- }
- -(void)dealloc {
- self.headers = nil;
- self.bodyParts = nil;
- self.error = nil;
- [super dealloc];
- }
- @end