Last modified by Pascal Robert on 2007/09/03 19:49

From version 3.1
edited by Pascal Robert
on 2007/09/03 19:49
Change comment: There is no comment for this version
To version 5.2
edited by Pascal Robert
on 2007/09/03 19:49
Change comment: Changed document parent to [xwiki:documentation.Home.Development Architecture.Web Services.WebHome].

Summary

Details

Page properties
Parent
... ... @@ -1,0 +1,1 @@
1 +documentation.Home.Development Architecture.Web Services.WebHome
Content
... ... @@ -1,4 +1,4 @@
1 -WebObjects supports Web Services both as a producer and a consumer, and it actually works quite well once you figure out how to get things properly configured. Hopefully this walkthrough can jumpstart that process for you.
1 +WebObjects supports Web Services both as a producer and a consumer, and it actually works quite well once you figure out how to get things properly configured. Hopefully this walkthrough can jumpstart that process for you.
2 2  
3 3  = Setting up a WO Web Services Project =
4 4  
... ... @@ -14,44 +14,38 @@
14 14  11*. saaj.jar
15 15  11*. jaxrpc.jar
16 16  11. Edit the WO Frameworks collection and add the JavaWebServicesSupport framework from the System frameworks
17 -1. Create a class to hold your web service methods. The methods do not need to be static and can both take complex types as parameters and return complex types as return values. For now, just return primitive types and/or String.
17 +1. Create a class to hold your web service methods. The methods do not need to be static and can both take complex types as parameters and return complex types as return values. For now, just return primitive types and/or String.
18 18  1. Edit your Application class and add WOWebServiceRegistrar.registerWebService("PublishedNameOfYourWebService", NameOfTheClassYouJustMade.class, true);
19 19  
20 -That's it. Now when you start your app, you can request [[http://yourserver.com/cgi-bin/WebObjects/YourApp.woa/ws/PublishedNameOfYourWebService?wsdl]] and it will return the autogenerated WSDL document that you can use with any number of web service clients to interact with your server.
20 +That's it. Now when you start your app, you can request [[http:~~/~~/yourserver.com/cgi-bin/WebObjects/YourApp.woa/ws/PublishedNameOfYourWebService?wsdl>>url:http://yourserver.com/cgi-bin/WebObjects/YourApp.woa/ws/PublishedNameOfYourWebService?wsdl||shape="rect"]] and it will return the autogenerated WSDL document that you can use with any number of web service clients to interact with your server.
21 21  
22 22  = Complex Types with WO Web Services =
23 23  
24 -So now the issue of complex types. Returning complex types is fine, but you have to register the serializer and deserializer classes for each complex type you reference. If you do not, the server will attempt to serialize your object using the ArraySerializer (you'll see this exception on the server), and the client will complain about a nonsensical error with SYSTEMID (gotta love terrible error handling). The fix for this is for each of your complex types, call the following method in your Application constructor:
24 +So now the issue of complex types. Returning complex types is fine, but you have to register the serializer and deserializer classes for each complex type you reference. If you do not, the server will attempt to serialize your object using the ArraySerializer (you'll see this exception on the server), and the client will complain about a nonsensical error with SYSTEMID (gotta love terrible error handling!). The fix for this is for each of your complex types, call the following method in your Application constructor:
25 25  
26 26  {{panel}}
27 -
28 -WOWebServiceRegistrar.registerFactoriesForClassWithQName(new BeanSerializerFactory(_class, \_qName), new BeanDeserializerFactory(_class, \_qName), \_class, \_qName);
29 -
27 +WOWebServiceRegistrar.registerFactoriesForClassWithQName(new BeanSerializerFactory(_class, _qName), new BeanDeserializerFactory(_class, _qName), _class, _qName);
30 30  {{/panel}}
31 31  
32 -where class is the Class object that represents your complex type, and qName is the QName (fully qualified name) of the class as it appears in your WSDL document. For instance, if you created a complex return type named Person and it is in the com.yourserver.service package, class would be com.yourserver.service.Person.class and qName would be new QName("http:~/~/service.yourserver.com", "Person"). Notice that the namespace is the inverse of your package name. You will need to call this method for each of the parameters and return types your reference.
30 +where _class is the Class object that represents your complex type, and _qName is the QName (fully qualified name) of the class as it appears in your WSDL document. For instance, if you created a complex return type named Person and it is in the com.yourserver.service package, _class would be com.yourserver.service.Person.class and _qName would be new QName("http:~/~/service.yourserver.com", "Person"). Notice that the namespace is the inverse of your package name. You will need to call this method for each of the parameters and return types your reference.
33 33  
34 -For the record, I have no idea why you have to do this step manually - The WSDL was autogenerated, and thus it KNOWS the classes and their QName WSDL mappings, but I was not able to get things to work properly without this step. If anyone knows why this is, or a way around it, please update this article.
32 +For the record, I have no idea why you have to do this step manually - The WSDL was autogenerated, and thus it KNOWS the classes and their QName WSDL mappings, but I was not able to get things to work properly without this step. If anyone knows why this is, or a way around it, please update this article.
35 35  
36 36  With these registrations, you should now be able to communicate with WO using any standard Web Service client (Axis, .NET, etc).
37 37  
38 38  = Sessions and WO Web Services =
39 39  
40 -You may have noticed in your Web Service methods that you have no WOContext, WORequest, WOSession, and friends passed in. Do not fret. The WebServiceRequestHandler takes care to hook you up in this department using Axis's MessageContext class. You can use the following code to get to your WOSession:
38 +You may have noticed in your Web Service methods that you have no WOContext, WORequest, WOSession, and friends passed in. Do not fret. The WebServiceRequestHandler takes care to hook you up in this department using Axis's MessageContext class. You can use the following code to get to your WOSession:
41 41  
42 42  {{panel}}
43 -
44 44  WOContext context = (WOContext)MessageContext.getCurrentContext().getProperty("com.webobjects.appserver.WOContext");
45 -WOSession session = context.session();
46 -
42 + WOSession session = context.session();
47 47  {{/panel}}
48 48  
49 49  or the shortcut
50 50  
51 51  {{panel}}
52 -
53 53  WOSession session = WOWebServiceUtilities.currentWOContext().session();
54 -
55 55  {{/panel}}
56 56  
57 57  The following additional keys are accessible through the MessageContext:
... ... @@ -58,33 +58,33 @@
58 58  
59 59  * "com.webobjects.appserver.WOContext" = the WOContext for this request
60 60  * "transport.url" = I /believe/ this contains the full request URL up to the query string
61 -* org.apache.axis.transport.http.HTTPConstants.MC//HTTP//SERVLETPATHINFO = contains the request's request handler path
55 +* org.apache.axis.transport.http.HTTPConstants.MC_HTTP_SERVLETPATHINFO = contains the request's request handler path
62 62  * "Authorization" = contains the Authorization header, in the event that you need to process things like Kerberos/SPNEGO, etc.
63 63  * "remoteaddr" = contains the request's remote address
64 64  
65 65  = Consuming with Axis in Java =
66 66  
67 -If you are using Axis to consume a WO Web Service, be advised that there is an outstanding bug (open since circa 2003, no less) that axis by default does not support passing more than one cookie to the server. WO sends both woinst AND wosid, so you lose your session ID from the client on the return trip to the server. This can be fixed by applying the patch from [[http://issues.apache.org/jira/browse/AXIS-1059]] to your client's axis.jar. Axis 1.1 has been archived at Apache, but you can download the source from [[http://archive.apache.org/dist/ws/axis/1_1/]] . The patch does not perfectly apply. There are two rejected hunks, but it should be very obvious how to fix the rejects (the patch has two System.out.printlns that it claims were in the original source that were not). After fixing that, you can setStoreSessionIdInCookies(true) on your server's WOSession and setMaintainSessions(true) on your client's ServiceLocator and you'll be good to go.
61 +If you are using Axis to consume a WO Web Service, be advised that there is an outstanding bug (open since circa 2003, no less) that axis by default does not support passing more than one cookie to the server. WO sends both woinst AND wosid, so you lose your session ID from the client on the return trip to the server. This can be fixed by applying the patch from [[http:~~/~~/issues.apache.org/jira/browse/AXIS-1059>>url:http://issues.apache.org/jira/browse/AXIS-1059||shape="rect"]] to your client's axis.jar. Axis 1.1 has been archived at Apache, but you can download the source from [[http:~~/~~/archive.apache.org/dist/ws/axis/1_1/>>url:http://archive.apache.org/dist/ws/axis/1_1/||shape="rect"]] . The patch does not perfectly apply. There are two rejected hunks, but it should be very obvious how to fix the rejects (the patch has two System.out.printlns that it claims were in the original source that were not). After fixing that, you can setStoreSessionIdInCookies(true) on your server's WOSession and setMaintainSessions(true) on your client's ServiceLocator and you'll be good to go.
68 68  
69 69  This Axis bug appears to be fixed in recent versions of Axis, including version 1.4. Trying to upgrade the version of Axis in your WO Web Services server is not likely to be a happy experience (and likely neither will be upgrading Axis in a Direct To Web Services client - though I haven't tried this). However, it does seem to be possible to use a later version of the Axis jars on the classpath of a WebObjects application that intends to use classes generated by WSDL2Java to connect to a remote Web Services server - assuming that there are no WebObjects classes included in the WSDL. It is important in this case that you use matching version of WSDL2Java.
70 70  
71 71  = Consuming with WebServicesCore.framework =
72 72  
73 -There are several complications when it comes to using WebServicesCore with WebObjects, all of which stem from the WSMakeStubs generated code. Upon using the code generated by WSMakeStubs, you will run into the following issues that need to be fixed in its code:
67 +There are several complications when it comes to using WebServicesCore with WebObjects, all of which stem from the WSMakeStubs generated code. Upon using the code generated by WSMakeStubs, you will run into the following issues that need to be fixed in its code:
74 74  
75 75  = WSMakeStubs =
76 76  
77 -Apple provides a program called WSMakeStubs that is similar to WSDL2Java in Axis, except that it sucks. It will, however, at least give you a starting point for building your web service client code, and with the changes outlined below, you can end up with decent client APIs.
71 +Apple provides a program called WSMakeStubs that is similar to WSDL2Java in Axis, except that it sucks. It will, however, at least give you a starting point for building your web service client code, and with the changes outlined below, you can end up with decent client APIs.
78 78  
79 79  Running WSMakeStubs is very simple:
80 80  
81 -/Developer/Tools/WSMakeStubs x ObjC name NameOfServiceClass url [[http://yourserver.com/cgi-bin/WebObjects/YourWOA.woa/ws/YourService?wsdl]]
75 +/Developer/Tools/WSMakeStubs -x ObjC -name NameOfServiceClass -url [[http:~~/~~/yourserver.com/cgi-bin/WebObjects/YourWOA.woa/ws/YourService?wsdl>>url:http://yourserver.com/cgi-bin/WebObjects/YourWOA.woa/ws/YourService?wsdl||shape="rect"]]
82 82  
83 -This will produce Objective-C code that you can use to call your web service. As opposed to Axis, WSMakeStubs produces stateless code for your service (i.e. no session tracking or cookie support - only static methods for each method of your web service). All of the methods appear at the end of NameOfServiceClass.m that you will need to call. WSMakeStubs also produces WSGeneratedObj.m, which contains the lower level web service core calls.
77 +This will produce Objective-C code that you can use to call your web service. As opposed to Axis, WSMakeStubs produces stateless code for your service (i.e. no session tracking or cookie support - only static methods for each method of your web service). All of the methods appear at the end of NameOfServiceClass.m that you will need to call. WSMakeStubs also produces WSGeneratedObj.m, which contains the lower level web service core calls.
84 84  
85 85  = Service Methods Without Return Values =
86 86  
87 -Another bug in WSMakeStubs is related to methods that don't have return values. For void methods, the methods are never actually CALLED by WSMakeStubs. If you look at the code for the returnValue method, you will see that it never calls [[WO:super getResultDictionary]]. The problem with this is that [[WO:super getResultDictionary]] is the code that actually executes the web service method. Simply change the definition for your void method to be:
81 +Another bug in WSMakeStubs is related to methods that don't have return values. For void methods, the methods are never actually CALLED by WSMakeStubs. If you look at the code for the returnValue method, you will see that it never calls [[doc:WO.super getResultDictionary]]. The problem with this is that [[doc:WO.super getResultDictionary]] is the code that actually executes the web service method. Simply change the definition for your void method to be:
88 88  
89 89  {{code}}
90 90  
... ... @@ -99,7 +99,7 @@
99 99  
100 100  = Bugs and Changes to WSGeneratedObj =
101 101  
102 -WSGeneratedObj is MOSTLY bug free. However, there there are a couple changes required to fix a memory leak it generates (from cocoadev.com):
96 +WSGeneratedObj is MOSTLY bug free. However, there there are a couple changes required to fix a memory leak it generates (from cocoadev.com):
103 103  
104 104  At the end of getResultDictionary, add:
105 105  
... ... @@ -126,7 +126,7 @@
126 126  
127 127  {{/code}}
128 128  
129 -Another change I like to make in the generated is to remove the hard-coded service URLs and pass them in from the code that calls the service (much like Axis does). This should be a fairly straightforward change, but I wanted to make a note about doing it. It will be fairly common that you want to talk to a development server and a production server using the same code, and as a result, you will want that variable to be parameterized.
123 +Another change I like to make in the generated is to remove the hard-coded service URLs and pass them in from the code that calls the service (much like Axis does). This should be a fairly straightforward change, but I wanted to make a note about doing it. It will be fairly common that you want to talk to a development server and a production server using the same code, and as a result, you will want that variable to be parameterized.
130 130  
131 131  = Passing a Complex Type to WO =
132 132  
... ... @@ -141,13 +141,13 @@
141 141  
142 142  {{/code}}
143 143  
144 -Where kWSRecordNamespaceURI's value is the XML namespace of the type of the complex object you are passing, and kWSRecordType's value is the name of the type. On the WO side, the namespace will be the reverse of the type's class name, and the record type will be the name of the class. For instance, in the example above, the actual class on the server was named com.mdimension.mdtask.extranet.WSCompany .
138 +Where kWSRecordNamespaceURI's value is the XML namespace of the type of the complex object you are passing, and kWSRecordType's value is the name of the type. On the WO side, the namespace will be the reverse of the type's class name, and the record type will be the name of the class. For instance, in the example above, the actual class on the server was named com.mdimension.mdtask.extranet.WSCompany .
145 145  
146 -The rest of the dictionary contains attribute=>value mappings. For instance, WSCompany in the example above has a "name" attribute, so the dictionary would also contains a "name" key that maps to the corresponding value.
140 +The rest of the dictionary contains attribute=>value mappings. For instance, WSCompany in the example above has a "name" attribute, so the dictionary would also contains a "name" key that maps to the corresponding value.
147 147  
148 -When sending NSDictionary instances from Cocoa, the WO will fire the WOGlobalIDDeserializer and it will not properly parse the nsdictionary or nsarray, it appears that there is no default deserializer on the WO side for those classes.
142 +When sending NSDictionary instances from Cocoa, the WO will fire the WOGlobalIDDeserializer and it will not properly parse the nsdictionary or nsarray, it appears that there is no default deserializer on the WO side for those classes.
149 149  
150 -One solution is to add
144 +One solution is to add
151 151  
152 152  {{code}}
153 153  
... ... @@ -166,11 +166,11 @@
166 166  {{/code}}
167 167  
168 168  on the cocoa side, than call it when compiling the arguments for the WSMethodInvocationRef
169 -Than on the WO side use NSPropertyListSerialization.propertyListFromString(xmlPlist) to recreate the object.
163 + Than on the WO side use NSPropertyListSerialization.propertyListFromString(xmlPlist) to recreate the object.
170 170  
171 171  = Return Values from WO =
172 172  
173 -One of the other problems WSMakeStubs has is that it doesn't produce a valid identifier for retrieving a WO web service return value. In the generated code, you will see something like
167 +One of the other problems WSMakeStubs has is that it doesn't produce a valid identifier for retrieving a WO web service return value. In the generated code, you will see something like
174 174  
175 175  {{code}}
176 176  
... ... @@ -181,7 +181,7 @@
181 181  
182 182  {{/code}}
183 183  
184 -however, the actual return value name requires its namspace to be included. The fixed version of the routine looks like:
178 +however, the actual return value name requires its namspace to be included. The fixed version of the routine looks like:
185 185  
186 186  {{code}}
187 187  
... ... @@ -191,14 +191,13 @@
191 191  
192 192  {{/code}}
193 193  
194 -Notice the key starts with "ns1:". This value should match the value that appears in your WSDL.
188 +Notice the key starts with "ns1:". This value should match the value that appears in your WSDL.
195 195  
196 196  = Example Type Wrappers =
197 197  
198 -Here's an example type wrapper I use based on the WSCompany example above. In the static methods that WSMakeStubs creates that wrap my web service methods, I simply initWithDictionary this type with the result dictionary from the web service and return an instance of WSCompany rather than the dictionary. When I send one of these objects back, I simply send [[WO:wsCompany dictionary]] in the wrapper method.
192 +Here's an example type wrapper I use based on the WSCompany example above. In the static methods that WSMakeStubs creates that wrap my web service methods, I simply initWithDictionary this type with the result dictionary from the web service and return an instance of WSCompany rather than the dictionary. When I send one of these objects back, I simply send [[doc:WO.wsCompany dictionary]] in the wrapper method.
199 199  
200 200  {{code}}
201 -
202 202   @interface WSCompany : NSObject {
203 203   NSMutableDictionary *myDictionary;
204 204   }
... ... @@ -208,9 +208,8 @@
208 208   -(NSString *)name;
209 209   -(NSString *)companyID;
210 210   @end
204 +{{/code}}
211 211  
212 -{{/code}}
213 -
214 214  {{code}}
215 215  
216 216   @implementation WSCompany
... ... @@ -245,7 +245,7 @@
245 245  
246 246  = Fault Handling =
247 247  
248 -WSMakeStubs doesn't handle the fault properly but it's in the dictionary. In resultForInvocation: I added a few lines to check for and return the fault
240 +WSMakeStubs doesn't handle the fault properly but it's in the dictionary. In +resultForInvocation: I added a few lines to check for and return the fault
249 249  
250 250  {{code}}
251 251  
... ... @@ -287,7 +287,6 @@
287 287  Here are the new methods to add to WSGeneratedObject.m:
288 288  
289 289  {{code}}
290 -
291 291   -- (id) initWithWebServicesURLString:(NSString*)urlString
292 292   {
293 293   if (self = [super init]) {
... ... @@ -319,9 +319,8 @@
319 319   fCookies = [[NSHTTPCookie requestHeaderFieldsWithCookies: cookies] retain];
320 320   WSMethodInvocationSetProperty([self getRef], kWSHTTPExtraHeaders, fCookies);
321 321   }
313 +{{/code}}
322 322  
323 -{{/code}}
324 -
325 325  {{code}}
326 326  
327 327   - (int)timeoutValue { return fTimeout; }
... ... @@ -334,7 +334,7 @@
334 334  
335 335  {{/code}}
336 336  
337 -You will need to modify dealloc to release fCookies and fURLString. Below is my modified version getCreateInvocationRef. It is modified to get the URL using the new accessor methods above, to get the method name from the class name (which makes a lot more sense than hard-coding it to the class name in every subclass), and to set the timeout. After that is a generic resultValues method so that your generated subclasses can have their resultValues and getCreateInvocationRef methods removed~-~--the only methods they require are for setting parameters. There is also a commented out line that you can uncomment to have debug information included in the results dictionary. This is very helpful when trying to debug the transfer of complex objects.
327 +You will need to modify -dealloc to release fCookies and fURLString. Below is my modified version getCreateInvocationRef. It is modified to get the URL using the new accessor methods above, to get the method name from the class name (which makes a lot more sense than hard-coding it to the class name in every subclass), and to set the timeout. After that is a generic resultValues method so that your generated subclasses can have their -resultValues and -getCreateInvocationRef methods removed~-~--the only methods they require are for setting parameters. There is also a commented out line that you can uncomment to have debug information included in the results dictionary. This is very helpful when trying to debug the transfer of complex objects.
338 338  
339 339  {{code}}
340 340