Wiki source code of WebServices
Show last authors
1 | = Introduction = |
2 | |
3 | The REQUEA Platform is a service oriented platform. Every entity definition can be mapped as a WebService using the SOAP protocol. |
4 | |
5 | Entity Properties become attributes in the Document schema definition. |
6 | Operations become SOAP operations. |
7 | |
8 | = Calling REQUEA as a Web Service = |
9 | |
10 | The SOAP Servlet generates automatically WSDL and accept SOAP request over HTTP(s) based on the Entity definition. |
11 | |
12 | The syntax for the WebService servlet is the following: |
13 | |
14 | http://[server]/dysoweb/ws/wsdl.ws?doc=[entity name] |
15 | |
16 | So for example: |
17 | |
18 | http://localhost:8080/dysoweb/ws/wsdl.ws?doc=custTest |
19 | |
20 | |
21 | == Using Visual Studio 2010 == |
22 | |
23 | You can map automatically the REQUEA Web service as a service reference in Visual Studio: |
24 | |
25 | [[image:img45.png]] |
26 | |
27 | |
28 | [[image:img46.png||alt="Entering the Web service WSDL URL"]] |
29 | |
30 | |
31 | [[image:img47.png||alt="Discovering Service Operations"]] |
32 | |
33 | [[image:img48.png]] |
34 | |
35 | |
36 | = Calling a Web Service in REQUEA = |
37 | |
38 | Invoking a Web Service from REQUEA requires that you adapt the semantics of the Web Service and wrap it as a JavaScript Plugin service. |
39 | |
40 | The wrapping code should be written in Java. |
41 | |
42 | There are (at least) two ways to wrap the code and invoke the Web Service: |
43 | |
44 | * using Apache Axis |
45 | * using SaaJ and XML |
46 | |
47 | == Using Apache Axis == |
48 | |
49 | Using Apache Axis you can generate Java wrappers around the Web Service and invoke those wrapper from the Java code. |
50 | |
51 | In this example, we will use the ViaMichelin SOAP API. |
52 | |
53 | 1 - Generate the stubs |
54 | |
55 | This is done with the Axis tool WSDL2Java: |
56 | |
57 | {{code language="java"}} |
58 | java org.apache.axis.wsdl.WSDL2Java http://www.viamichelin.com/ws/services/Geocoding?wsdl |
59 | {{/code}} |
60 | |
61 | |
62 | This will generate some java classes, that you should include in your plugin project: |
63 | |
64 | com\viamichelin\ws\geo\GeoCoordinates.java |
65 | com\viamichelin\ws\localization\Address.java |
66 | com\viamichelin\ws\localization\FindLocation.java |
67 | com\viamichelin\ws\localization\FindLocations.java |
68 | com\viamichelin\ws\localization\GeocodingRequest.java |
69 | com\viamichelin\ws\localization\InputAddress.java |
70 | com\viamichelin\ws\localization\Location.java |
71 | com\viamichelin\ws\localization\Poi.java |
72 | com\viamichelin\ws\localization\PoiDatasheet.java |
73 | com\viamichelin\ws\localization\PoiDescriptionList.java |
74 | com\viamichelin\ws\localization\PoiId.java |
75 | com\viamichelin\ws\localization\PoiMetaNumList.java |
76 | com\viamichelin\ws\localization\PoiMetaStringList.java |
77 | com\viamichelin\ws\localization\service\Geocoding.java |
78 | com\viamichelin\ws\localization\service\GeocodingService.java |
79 | com\viamichelin\ws\localization\service\GeocodingServiceLocator.java |
80 | com\viamichelin\ws\localization\service\GeocodingSoapBindingStub.java |
81 | com\viamichelin\ws\object\Integer.java |
82 | |
83 | === Calling the Web Service === |
84 | |
85 | The call is done within the plugin script in a jsFunction_execute for example: |
86 | |
87 | {{code language="java"}} |
88 | |
89 | |
90 | InputAddress[] addressList = new InputAddress[1]; |
91 | addressList[0] = inputAddress; |
92 | request.setAddressesList(addressList); |
93 | |
94 | // call the web service |
95 | GeocodingServiceLocator locator = new GeocodingServiceLocator(); |
96 | Geocoding service = locator.getGeocoding(); |
97 | |
98 | String login = registry.getParam("com.requea.viamichelin.login"); |
99 | String passwd = registry.getParam("com.requea.viamichelin.password"); |
100 | if(login == null || passwd == null) { |
101 | throw new RegistryException("Viamichelin service not properly configured. Missing login or password"); |
102 | } |
103 | String url = registry.getParam("com.requea.viamichelin.url"); |
104 | if(url != null) { |
105 | locator.setGeocodingEndpointAddress(url); |
106 | } |
107 | |
108 | try { |
109 | FoundLocationList result = service.getLocationsList(request, login+"|"+passwd)[0]; |
110 | if(result.getFoundLocations().length > 0) { |
111 | FoundLocation loc = result.getFoundLocations()[0]; |
112 | return new GeocodedLocation(loc.getLocationDesc()); |
113 | } else { |
114 | // nothing found |
115 | return null; |
116 | } |
117 | } catch(Exception e) { |
118 | throw new EndUserException(e); |
119 | } |
120 | |
121 | {{/code}} |
122 | |
123 | |
124 | === Setting up classpath and dependencies === |
125 | |
126 | in your pom.xml, you need to add the axis dependencies for inclusion in your bundle: |
127 | |
128 | {{code language="xml"}} |
129 | <dependency> |
130 | <groupId>axis</groupId> |
131 | <artifactId>axis</artifactId> |
132 | <version>1.4</version> |
133 | <scope>provided</scope> |
134 | </dependency> |
135 | {{/code}} |
136 | |
137 | and for the bundle class path: |
138 | |
139 | {{code language="xml"}} |
140 | <Bundle-ClassPath> |
141 | ., |
142 | lib/axis-1.4.jar, |
143 | lib/axis-jaxrpc-1.4.jar, |
144 | lib/axis-saaj-1.4.jar, |
145 | lib/axis-wsdl4j-1.5.1.jar, |
146 | lib/commons-discovery-0.2.jar |
147 | </Bundle-ClassPath> |
148 | {{/code}} |
149 | |
150 | [[Download the complete pom.xml>>attach:pom.xml||title="Download the pom.xml"]] |
151 | |
152 | == Using SAAJ == |
153 | |
154 | Using SAAJ, you can invoke the Web service without having to generate stubs. |
155 | |
156 | It is a lower level protocol, allowing for a finer control of the WebService invokation, at the expense of the simplicity. |
157 | |
158 | Here is a snipet of code that does a SAAJ invokation: |
159 | |
160 | {{code language="java"}} |
161 | Thread th = Thread.currentThread(); |
162 | ClassLoader cl = th.getContextClassLoader(); |
163 | // build a GetList request |
164 | try { |
165 | th.setContextClassLoader(this.getClass().getClassLoader()); |
166 | String userName = registry.getParam("username"); |
167 | String password = registry.getParam("password"); |
168 | String strURL = registry.getParam("endpoint"); |
169 | |
170 | SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance(); |
171 | SOAPConnection connection = factory.createConnection(); |
172 | URL endpoint = new URL(strURL); |
173 | |
174 | // this web services requires the SOAPAction. |
175 | MessageFactory mf = MessageFactory.newInstance(); |
176 | SOAPMessage message = mf.createMessage(); |
177 | MimeHeaders hd = message.getMimeHeaders(); |
178 | hd.addHeader("SOAPAction", "urn:WS/"+action); |
179 | |
180 | SOAPHeader header = message.getSOAPHeader(); |
181 | // adds auth info in the header |
182 | Element elAuth = header.getOwnerDocument().createElementNS( |
183 | "urn:WS", "AuthenticationInfo"); |
184 | elAuth.setAttribute("xlmns", "urn:WS"); |
185 | XMLUtils.addElement(elAuth, "userName", userName); |
186 | XMLUtils.addElement(elAuth, "password", password); |
187 | header.appendChild(elAuth); |
188 | |
189 | // include the request in the body |
190 | SOAPBody body = message.getSOAPBody(); |
191 | body.appendChild(body.getOwnerDocument().adoptNode( |
192 | elRequest.cloneNode(true))); |
193 | |
194 | SOAPMessage response = connection.call(message, endpoint); |
195 | // fault? |
196 | Element elBody = response.getSOAPBody(); |
197 | Element elFault = XMLUtils.getChild(elBody, "Fault"); |
198 | if(elFault != null) { |
199 | log.severe(XMLUtils.getChildText(elFault, "faultstring")); |
200 | throw new EndUserException(XMLUtils.getChildText(elFault, "faultstring")); |
201 | |
202 | } |
203 | |
204 | Element elResp = XMLUtils.getChild(elBody, "urn:WS", respName); |
205 | // close the connection |
206 | connection.close(); |
207 | |
208 | return elResp; |
209 | |
210 | } catch(Exception e) { |
211 | throw new RegistryException(e); |
212 | |
213 | } finally { |
214 | th.setContextClassLoader(cl); |
215 | } |
216 | {{/code}} |
217 | |
218 | |
219 | Required dependencies in your project: |
220 | |
221 | {{code language="xml"}} |
222 | <dependency> |
223 | <groupId>javax.xml</groupId> |
224 | <artifactId>saaj-api</artifactId> |
225 | <version>1.3</version> |
226 | </dependency> |
227 | <dependency> |
228 | <groupId>com.sun.xml.messaging.saaj</groupId> |
229 | <artifactId>saaj-impl</artifactId> |
230 | <version>1.3</version> |
231 | </dependency> |
232 | {{/code}} |
233 | |
234 | and the plugin section in the pom.xml |
235 | |
236 | {{code language="xml"}} |
237 | <plugin> |
238 | <groupId>com.requea.dysoapp</groupId> |
239 | <artifactId>maven-dysoapp-bundle</artifactId> |
240 | <extensions>true</extensions> |
241 | <configuration> |
242 | <instructions> |
243 | <Bundle-Name>${pom.name}</Bundle-Name> |
244 | <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName> |
245 | <Export-Package /> |
246 | <Import-Package> |
247 | org.apache.commons.logging, |
248 | org.osgi.framework, |
249 | com.requea.app, |
250 | com.requea.util, |
251 | com.requea.util.xml, |
252 | org.w3c.dom, |
253 | javax.net.*, |
254 | javax.crypto.*, |
255 | javax.xml.*, |
256 | javax.activation, |
257 | org.mozilla.javascript;version=1.7.2, |
258 | javax.imageio.*, |
259 | org.xml.*, |
260 | !org.jvnet.*, |
261 | !com.sun.* |
262 | </Import-Package> |
263 | <Bundle-Activator>com.requea.myplugin.Activator</Bundle-Activator> |
264 | <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency> |
265 | </instructions> |
266 | </configuration> |
267 | </plugin> |
268 | {{/code}} |