Professional Documents
Culture Documents
1. outils
2. programmation dun client : (a) principe de JAX-RPC (b) exemples (trois types de clients) (c) types Java et types SOAP (d) traduction dun service complet (e) gestion des erreurs 3. programmation dun service (avec Axis) : (a) dploiement instantan (b) dploiement adapt (c) EJB
Outils utiliss
API Java : JAX-RPC (XML RPC), http://java.sun.com/xml/jaxrpc/, sera intgre J2EE 1.4 implmentations : le Java Web Services Developer Pack 1.1 http://java.sun.com/webservices/ AXIS http://ws.apache.org/axis/ implmentation open source du groupe apache descendant de Apache SOAP 2.x et de IBM SOAP4J Dans ce cours : AXIS !
Rle de JAX-RPC
Rendre les RPC bass sur XML presque aussi simples que RMI : un service web devient une interface hritant de Remote une opration devient une mthode une erreur SOAP (Fault) devient une exception hritant de RemoteException JAX-RPC dnit un ensemble de traduction : faire le lien entre les types Java et les types des schmas du W3 reprsenter informatiquement les concepts de WSDL (service, port, etc.) etc.
Exemple de base
Service web minimaliste : Echo proposant une opration echo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Echo.wsdl <?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://localhost:8080/axis/services/Echo" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/axis/services/Echo" xmlns:intf="http://localhost:8080/axis/services/Echo" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:message name="echoResponse"> <wsdl:part name="echoReturn" type="xsd:string"/> </wsdl:message> <wsdl:message name="echoRequest"> <wsdl:part name="in0" type="xsd:string"/> </wsdl:message> <wsdl:portType name="Echo"> <wsdl:operation name="echo" parameterOrder="in0"> <wsdl:input message="impl:echoRequest" name="echoRequest"/> <wsdl:output message="impl:echoResponse" name="echoResponse"/> </wsdl:operation> </wsdl:portType>
Interfaces
Echo.java package localhost.axis.services.Echo; public interface Echo extends java.rmi.Remote { public java.lang.String echo(java.lang.String in0) throws java.rmi.RemoteException; } EchoService.java package localhost.axis.services.Echo; public interface EchoService extends javax.xml.rpc.Service { public java.lang.String getEchoAddress(); public localhost.axis.services.Echo.Echo getEcho() throws javax.xml.rpc.ServiceException; public localhost.axis.services.Echo.Echo getEcho(java.net.URL portAddress) throws javax.xml.rpc.ServiceException; }
1 2 3 4 5 6 7 8 9 10 11
Le client
1 2 3 4 5 6 7 8 Client.java import localhost.axis.services.Echo.*; public class Client { public static void main(String[] args) throws Exception { EchoService service=new EchoServiceLocator(); Echo port=service.getEcho(); System.out.println(port.echo(args[0])); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Message envoy
echoRequest POST /axis/services/Echo HTTP/1.0 Content-Type: text/xml; charset=utf-8 Accept: application/soap+xml, application/dime, multipart/related, text/* User-Agent: Axis/1.1RC2 Host: 127.0.0.1 Cache-Control: no-cache Pragma: no-cache SOAPAction: "" Content-Length: 455 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:echo soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://localhost:8080/axis/services/Echo"> <in0 xsi:type="xsd:string">Essai</in0> </ns1:echo> </soapenv:Body> </soapenv:Envelope>
Services Web SOAP en Java p.10/71
Ce client est (en partie) spcique AXIS (ligne 4) la norme impose dengendrer les interfaces EchoService et Echo, mais pas la classe EchoServiceLocator solution diffrente dans limplmentation de Sun (le nom de la classe nest pas le mme) dans un contexte J2EE, on obtient une implmentation de EchoService grce lannuaire J2EE
Message reu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 echoAnswer HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Date: Thu, 03 Apr 2003 12:01:01 GMT Server: Apache Coyote/1.0 Connection: close <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:echoResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://localhost:8080/axis/services/Echo"> <echoReturn xsi:type="xsd:string">Essai</echoReturn> </ns1:echoResponse> </soapenv:Body> </soapenv:Envelope>
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import import import import import ClientDynamique.java javax.xml.rpc.Call; javax.xml.rpc.Service; javax.xml.rpc.ServiceFactory; javax.xml.rpc.ParameterMode; javax.xml.namespace.QName;
Solution intermdiaire
Mcanisme de proxy dynamique, bas sur le systme de dynamic proxy introduit par le jdk 1.3 : un dynamic proxy est une classe qui implmente une ou plusieurs interfaces spcies au moment de lexcution elle implmente linterface InvocationHandler (package java.lang.reflect) :
1 2 3 4 5 public interface InvocationHandler { public Object invoke(Object proxy, Method method, Object[] args); }
public class ClientDynamique { public static void main(String [] args) throws Exception { String ns="http://localhost:8080/axis/services/Echo"; String endpoint="http://localhost:8080/axis/services/Echo"; QName serviceName=new QName(ns,"EchoService"); QName portName=new QName(ns,"Echo"); QName operationName=new QName(ns,"echo"); ServiceFactory factory=ServiceFactory.newInstance(); Service service=factory.createService(serviceName); Call call=service.createCall(portName,operationName); call.setTargetEndpointAddress(endpoint); QName XSDString=new QName("http://www.w3.org/2001/XMLSchema","string"); call.addParameter("in0",XSDString,ParameterMode.IN); call.setReturnType(XSDString); String ret = (String) call.invoke( new String[] { "Essai" } ); System.out.println(ret); } }
un appel une mthode dune interface implmente par un dynamic proxy est transform en un appel la mthode invoke JAX-RPC propose dengendrer dynamiquement un stub partir du WSDL
Services Web SOAP en Java p.14/71
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public class ClientDynamiqueProxy { public static void main(String [] args) throws Exception { String ns="http://localhost:8080/axis/services/Echo"; String wsdl="http://localhost:8080/axis/services/Echo?wsdl"; URL wsdlURL=new URL(wsdl); QName serviceName=new QName(ns,"EchoService"); QName portName=new QName(ns,"Echo"); ServiceFactory factory=ServiceFactory.newInstance(); Service service=factory.createService(wsdlURL,serviceName); Echo port=(Echo)service.getPort(portName,Echo.class); System.out.println(port.echo("Essai")); } }
28 29 30 31 32
le reste de la classe est trs complexe il est spcique Axis le programmeur na pas besoin de le connatre Message SOAP engendr (partie) :
1 2 3 4 5 6 7 8 Exemple <multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Person" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:PhoneBookWS"> <first xsi:type="xsd:string">John</first> <last xsi:type="xsd:string">Doe</last> </multiRef>
Test.java import PhoneBookWS.Person; import localhost.axis.services.PhoneBookWS.*; public class Test { public static void main(String[] args) throws Exception { PhoneBookService service=new PhoneBookServiceLocator(); PhoneBook port=service.getPhoneBookWS(); Person p=new Person(); p.setFirst("John"); p.setLast("Doe"); int[] result=port.getPhoneNumber(p); for(int i=0;i<result.length;i++) { System.out.print(result[i]+" "); } System.out.println(); } }
Requte
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Requ^te e POST /axis/services/PhoneBookWS HTTP/1.0 Content-Type: text/xml; charset=utf-8 Accept: application/soap+xml, application/dime, multipart/related, text/* User-Agent: Axis/1.1RC2 Host: localhost Cache-Control: no-cache Pragma: no-cache SOAPAction: "" Content-Length: 776 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:getPhoneNumber soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://localhost:8080/axis/services/PhoneBookWS"> <in0 href="#id0"/> </ns1:getPhoneNumber> <multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Person" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:PhoneBookWS"> <first xsi:type="xsd:string">John</first> <last xsi:type="xsd:string">Doe</last> </multiRef> </soapenv:Body> </soapenv:Envelope>
Services Web SOAP en Java p.21/71
Rponse
Rponse e
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Date: Wed, 30 Apr 2003 08:13:59 GMT Server: Apache Coyote/1.0 Connection: close <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:getPhoneNumberResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://localhost:8080/axis/services/PhoneBookWS"> <getPhoneNumberReturn xsi:type="soapenc:Array" soapenc:arrayType="xsd:int[3]" xmlns:ns2="http://www.w3.org/2002/12/soap-encoding" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <item>1</item> <item>2</item> <item>3</item> </getPhoneNumberReturn> </ns1:getPhoneNumberResponse> </soapenv:Body> </soapenv:Envelope>
Services Web SOAP en Java p.22/71
Exemple
un service de calculs proposant le port Calc type de port Arrays opration meanVar : prend comme paramtre in un tableau de double prend comme paramtres inout deux doubles, la moyenne et la variance du tableau paramtre en Java, il ny a pas vraiment de diffrences en out et inout
Exemple : WSDL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Calc.wsdl <wsdl:types> <schema targetNamespace="http://localhost:8080/axis/services/Calc" xmlns="http://www.w3.org/2001/XMLSchema"> <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/> <complexType name="ArrayOf_xsd_double"> <complexContent> <restriction base="soapenc:Array"> <attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:double[]"/> </restriction> </complexContent> </complexType> </schema> </wsdl:types> <wsdl:message name="meanVarRequest"> <wsdl:part name="x" type="impl:ArrayOf_xsd_double"/> <wsdl:part name="mean" type="xsd:double"/> <wsdl:part name="var" type="xsd:double"/> </wsdl:message> <!-- ... --> <wsdl:message name="meanVarResponse"> <wsdl:part name="mean" type="xsd:double"/> <wsdl:part name="var" type="xsd:double"/> </wsdl:message> <wsdl:portType name="Arrays"> <!-- ... --> <wsdl:operation name="meanVar" parameterOrder="x mean var"> <wsdl:input message="impl:meanVarRequest" name="meanVarRequest"/> <wsdl:output message="impl:meanVarResponse" name="meanVarResponse"/> </wsdl:operation> </wsdl:portType>
Requte
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Requ^te e POST /axis/services/Calc HTTP/1.0 Content-Type: text/xml; charset=utf-8 Accept: application/soap+xml, application/dime, multipart/related, text/* User-Agent: Axis/1.1RC2 Host: localhost Cache-Control: no-cache Pragma: no-cache SOAPAction: "" Content-Length: 696 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:meanVar soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://localhost:8080/axis/services/Calc"> <x xsi:type="soapenc:Array" soapenc:arrayType="xsd:double[3]" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <item>1.5</item> <item>2.5</item> <item>2.0</item> </x> <mean xsi:type="xsd:double">0.0</mean> <var xsi:type="xsd:double">0.0</var> </ns1:meanVar> </soapenv:Body> </soapenv:Envelope>
Services Web SOAP en Java p.27/71
Requte
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Rponse e HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Date: Thu, 01 May 2003 10:05:14 GMT Server: Apache Coyote/1.0 Connection: close <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:meanVarResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://localhost:8080/axis/services/Calc"> <mean xsi:type="xsd:double">2.0</mean> <var xsi:type="xsd:double">0.25</var> </ns1:meanVarResponse> </soapenv:Body> </soapenv:Envelope>
une classe : de nom name ServiceLocator spcique Axis implmentation de linterface name Service
Services Web SOAP en Java p.32/71
Requte
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Requ^te e POST /axis/services/Calc HTTP/1.0 Content-Type: text/xml; charset=utf-8 Accept: application/soap+xml, application/dime, multipart/related, text/* User-Agent: Axis/1.1RC2 Host: localhost Cache-Control: no-cache Pragma: no-cache SOAPAction: "" Content-Length: 789 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:sum soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://localhost:8080/axis/services/Calc"> <in0 xsi:type="soapenc:Array" soapenc:arrayType="xsd:double[3]" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <item>1.5</item><item>2.5</item><item>3.0</item> </in0> <in1 xsi:type="soapenc:Array" soapenc:arrayType="xsd:double[2]" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"> <item>1.0</item><item>-2.0</item> </in1> </ns1:sum></soapenv:Body> </soapenv:Envelope>
Services Web SOAP en Java p.34/71
Rponse (Fault)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Requ^te e HTTP/1.1 500 Erreur Interne de Servlet Content-Type: text/xml; charset=utf-8 Date: Thu, 01 May 2003 06:37:42 GMT Server: Apache Coyote/1.0 Connection: close <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <soapenv:Fault> <faultcode>soapenv:Server.userException</faultcode> <faultstring>java.lang.ArrayIndexOutOfBoundsException: 2</faultstring> <detail/> </soapenv:Fault> </soapenv:Body> </soapenv:Envelope>
Un exemple complet
Service : requtes DNS implment en .NET modle document (pas RPC) propos par www.esynaps.com WSDL assez complexe : 264 lignes binding POST et GET
Code engendr
Le service :
1 2 3 4 5 6 7 8 9
package org.tempuri; public interface YourHost extends javax.xml.rpc.Service { public java.lang.String getYourHostSoapAddress(); public org.tempuri.YourHostSoap getYourHostSoap() throws javax.xml.rpc.ServiceException; public org.tempuri.YourHostSoap getYourHostSoap(java.net.URL portAddress) throws javax.xml.rpc.ServiceException; } YourHost.java
Le port :
YourHostSoap.java
1 2 3 4 5 6 7 8 9 10
package org.tempuri; public interface YourHostSoap extends java.rmi.Remote { public org.tempuri.HostInfo whoAmI() throws java.rmi.RemoteException; public org.tempuri.HostInfo getHostInfoByIP(java.lang.String IPAddress) throws java.rmi.RemoteException; public org.tempuri.HostInfo getHostInfoByName(java.lang.String name) throws java.rmi.RemoteException; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
package org.tempuri; public class HostInfo implements java.io.Serializable { private java.lang.String name; private org.tempuri.ArrayOfString IPList; private org.tempuri.ArrayOfString1 aliases; private java.lang.String error; public HostInfo() { } public java.lang.String getName() { return name; } public void setName(java.lang.String name) { this.name = name; } public org.tempuri.ArrayOfString getIPList() { return IPList; } public void setIPList(org.tempuri.ArrayOfString IPList) { this.IPList = IPList; } public org.tempuri.ArrayOfString1 getAliases() { return aliases; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package org.tempuri; public class ArrayOfString implements java.io.Serializable { private java.lang.String[] IPAddress; public ArrayOfString() { } public java.lang.String[] getIPAddress() { return IPAddress; } public void setIPAddress(java.lang.String[] IPAddress) { this.IPAddress = IPAddress; } public java.lang.String getIPAddress(int i) { return IPAddress[i]; } public void setIPAddress(int i, java.lang.String value) { this.IPAddress[i] = value; }
Client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Client.java import org.tempuri.*; public class Client { public static void print(String[] str) { if(str != null) { for(int i=0;i<str.length;i++) { System.out.println(\t+str[i]); } } else { System.out.println("nant"); e } } public static void main(String[] args) throws Exception { YourHost service=new YourHostLocator(); YourHostSoap port=service.getYourHostSoap(); HostInfo info=port.getHostInfoByName(args[0]); System.out.println("Nom : "+info.getName()); ArrayOfString ips=info.getIPList(); System.out.println("Adresse(s) IP :"); print(ips.getIPAddress()); ArrayOfString1 aliases=info.getAliases(); System.out.println("Alias :"); print(aliases.getAlias()); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package org.tempuri; public class ArrayOfString1 implements java.io.Serializable { private java.lang.String[] alias; public ArrayOfString1() { } public java.lang.String[] getAlias() { return alias; } public void setAlias(java.lang.String[] alias) { this.alias = alias; } public java.lang.String getAlias(int i) { return alias[i]; } public void setAlias(int i, java.lang.String value) { this.alias[i] = value; }
Excution
1 2 3 4 5 6 7 8 9 10 11 12
Requte
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Requ^te e POST /webservices/YourHostInfo.asmx HTTP/1.0 Content-Type: text/xml; charset=utf-8 Accept: application/soap+xml, application/dime, multipart/related, text/* User-Agent: Axis/1.1RC2 Host: www.esynaps.com Cache-Control: no-cache Pragma: no-cache SOAPAction: "http://tempuri.org/GetHostInfoByName" Content-Length: 374 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <GetHostInfoByName xmlns="http://tempuri.org/"> <Name>www.microsoft.com</Name> </GetHostInfoByName> </soapenv:Body> </soapenv:Envelope>
java Client www.microsoft.com Nom : www.microsoft.akadns.net Adresse(s) IP : 207.46.249.27 207.46.134.155 207.46.249.190 207.46.134.190 207.46.134.222 207.46.249.222 Alias : www.microsoft.com
Rponse
Rponse e
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
HTTP/1.1 200 OK Server: Microsoft-IIS/5.0 Date: Wed, 30 Apr 2003 14:20:06 GMT Cache-Control: private, max-age=0 Content-Type: text/xml; charset=utf-8 Content-Length: 706 <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <GetHostInfoByNameResponse xmlns="http://tempuri.org/"> <GetHostInfoByNameResult> <Name>www.microsoft.akadns.net</Name> <IPList> <IPAddress>207.46.249.27</IPAddress> <IPAddress>207.46.134.155</IPAddress> <IPAddress>207.46.249.190</IPAddress> <IPAddress>207.46.134.190</IPAddress> <IPAddress>207.46.134.222</IPAddress> <IPAddress>207.46.249.222</IPAddress> </IPList> <Aliases> <Alias>www.microsoft.com</Alias> </Aliases> </GetHostInfoByNameResult> </GetHostInfoByNameResponse> </soap:Body> </soap:Envelope>
Solution volue
Avenir pour limplmentation en Java : exposer un Entreprise Java Bean en tant que web service limitations : Stateless Session Bean prvu dans J2EE 1.4 (qui nexiste pas encore ofciellement !) version basique des services web (problme dintgration avec les mcanismes de scurit, les transactions, etc.) avantages : on rcupre toute la puissance des EJB possible avec Axis ds maintenant
1 2 3 4 5 6 7 8 9
si on modie le chier .jws, le web service est mis jour automatiquement (recompilation et production du .wsdl)
Services Web SOAP en Java p.47/71 Services Web SOAP en Java p.48/71
Exemple de client
1 2 3 4 5 6 7 8 9 10 11 12 Client import localhost.axis.Calendrier_jws.*; import java.util.Date; import java.util.Calendar; import java.text.DateFormat; public class Client { public static void main(String[] args) throws Exception { CalendrierService service=new CalendrierServiceLocator(); Calendrier port=service.getCalendrier(); Calendar now=port.getDate(); System.out.println(DateFormat.getDateInstance().format(now.getTime())); } }
Cration
Avec lInstant Deployment, lobjet est recr chaque invocation : exemple :
Compteur.java 1 2 3 4 5 6 7 8 9 public class Compteur { private int compte; public Compteur() { compte = 0; } public int next() { return compte++; } }
nouvel objet chaque appel : le compteur est toujours nul ! problmes : pas de persistance simple gaspillage de ressources (reconnexion une BD par exemple)
Services Web SOAP en Java p.53/71
deploy.wsdd <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="Compteur" provider="java:RPC"> <parameter name="className" value="Compteur"/> <parameter name="allowedMethods" value="*"/> </service> </deployment>
Java beans
On reprend lexemple de lannuaire : type Person :
Person.java
gestion du cycle de vie : sous-lment de service <parameter name="scope" value="value"/> lattribut value peut tre : request : un objet par requte (par dfaut) application : un seul objet par serveur session : un objet par session (extension Axis)
Services Web SOAP en Java p.55/71
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package org.apiacoa.phonebook; public class Person { private String first; private String last; public void setFirst(String f) { first=f; } public String getFirst() { return first; } public void setLast(String f) { last=f; } public String getLast() { return last; } }
service :
1 2 3 4 5 6
package org.apiacoa.phonebook; public class PhoneBook { public int[] getPhoneNumber(Person p) { return new int[] {1,2,3}; } }
PhoneBook.java
Dploiement
1 2 3 4 5 6 7 8 9 10
deploy.wsdd <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="PhoneBookWS" provider="java:RPC"> <parameter name="className" value="org.apiacoa.phonebook.PhoneBook"/> <parameter name="allowedMethods" value="*"/> <beanMapping qname="myNS:Person" xmlns:myNS="http://localhost:8080/axis/services/PhoneBookWS" languageSpecificType="java:org.apiacoa.phonebook.Person"/> </service> </deployment>
Types engendrs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 PhoneBookWS.wsdl <wsdl:types> <schema targetNamespace="http://localhost:8080/axis/services/PhoneBookWS" xmlns="http://www.w3.org/2001/XMLSchema"> <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/> <complexType name="Person"> <sequence> <element name="first" nillable="true" type="xsd:string"/> <element name="last" nillable="true" type="xsd:string"/> </sequence> </complexType> <complexType name="ArrayOf_xsd_int"> <complexContent> <restriction base="soapenc:Array"> <attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:int[]"/> </restriction> </complexContent> </complexType> </schema> </wsdl:types>
chier WSDD :
beanMapping : qname : nom du type pour WSDL languageSpecificType : objet correspondant (attention au prxe java) mapping standard : une struct SOAP un champ par proprit du Java Bean
WSDL
engendr automatiquement au dploiement peut tre engendr avant : java org.apache.axis.wsdl.Java2WSDL ... permet de prciser certains lments : traduction package vers namespace emplacement du WS encoding (literal ou encoded) style (document ou RPC) etc. emplacement prcis par llment wsdlFile (ls de service)
Fonctions avances
mapping XML spcique : permet de transformer toute classe Java en XML et vice-versa demande la programmation des traductions ( !) Handlers : sintercale entre le serveur et le client peut modier les messages SOAP au vol applications : cryptographie, signature, compression, logging, etc. transport par messages ( la place de HTTP) bas sur JMS signature sessions (HTTP et header SOAP) attachments
XDoclet
Engendre le reste du code :
Echo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* * Generated file - Do not edit! */ package org.apiacoa.echo; import import import import import java.lang.*; javax.ejb.CreateException; javax.ejb.EJBException; javax.ejb.SessionBean; javax.ejb.SessionContext;
/** * Remote interface for Echo. * @xdoclet-generated at 6 mai 2003 14:27:42 */ public interface Echo extends javax.ejb.EJBObject { public java.lang.String echo( java.lang.String s ) throws java.rmi.RemoteException; }
XDoclet (2)
EchoHome.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* * Generated file - Do not edit! */ package org.apiacoa.echo; import import import import import java.lang.*; javax.ejb.CreateException; javax.ejb.EJBException; javax.ejb.SessionBean; javax.ejb.SessionContext;
/** * Home interface for Echo. Lookup using {1} * @xdoclet-generated at 6 mai 2003 12:21:40 */ public interface EchoHome extends javax.ejb.EJBHome { public static final String COMP_NAME="java:comp/env/ejb/Echo"; public static final String JNDI_NAME="ejb/org/apiacoa/echo/Echo"; public org.apiacoa.echo.Echo create() throws javax.ejb.CreateException, java.rmi.RemoteException; }
attention, il ne faut pas mettre * dans allowedMethods. on dploie (au sens web service) avec la commande :
java org.apache.axis.client.AdminClient -lhttp://localhost:8080/jboss-net/services/Administration deploy.wsdd
WSDL engendr
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 EchoService.wsdl <?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://localhost:8080/jboss-net/services/EchoService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/jboss-net/services/EchoService" xmlns:intf="http://localhost:8080/jboss-net/services/EchoService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:types/> <wsdl:message name="echoRequest"> <wsdl:part name="in0" type="xsd:string"/> </wsdl:message> <wsdl:message name="echoResponse"> <wsdl:part name="echoReturn" type="xsd:string"/> </wsdl:message> <wsdl:portType name="Echo"> <wsdl:operation name="echo" parameterOrder="in0"> <wsdl:input message="impl:echoRequest" name="echoRequest"/> <wsdl:output message="impl:echoResponse" name="echoResponse"/> </wsdl:operation> </wsdl:portType>
Services Web SOAP en Java p.67/71
Clients
1 2 3 4 5 6 7 8
Web services import localhost.jboss_net.services.EchoService.*; public class Client { public static void main(String[] args) throws Exception { EchoService service=new EchoServiceLocator(); Echo port=service.getEchoService(); System.out.println(port.echo(args[0])); } } EJB
Conseils pratiques
pas de solution portable pour la gestion de session : utiliser des stateless session beans grer la main la session, par exemple : une cl permanente par lutilisateur une cl temporaire cre par un mcanisme de login partir dune cl permanente transmettre linformation de session chaque appel par encore de solution portable pour la scurit : utiliser HTTPS ( ?) on peut envisager des wrappers web services construits la main (solution intrimaire)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package test; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import org.apiacoa.echo.*; public class Client { public static void main(String[] args) throws Exception { InitialContext context = new InitialContext(); EchoHome home = (EchoHome) PortableRemoteObject.narrow(context.lookup("ejb/org/apiacoa/echo/Echo"), EchoHome.class); Echo service = home.create(); System.out.println(service.echo(args[0])); } }