Un service SOAP-RPC déployé sur TOMCAT
1. Récupérez les packages et lancez tomcat
Dans /opt, décompresser :
demo/ jakarta-tomcat-3.2.4/ xerces-1_2_3/ jaf-1.0.2/ javamail-1.2/ soap-2_2/Préparez les variables :
JAVA_HOME=......./j2sdk1.4.0 TOMCAT_HOME=/opt/jakarta-tomcat-3.2.4 PATH=$JAVA_HOME:$PATH export JAVA_HOME PATH TOMCAT_HOMEEssayez Tomcat :
cd $TOMCAT_HOME/bin startup.shessayer http://localhost:8080 dans un Navigateur
shutdown.shConfigurez Tomcat pour Soap :
en changeant l'ordre du CLASSPATH dans tomcat.sh : d'abord xerces.java
il suffit de mettre le old Classpath avant l'ajout et non le contraire !Ajoutez les packages nécessaires :
SOAP_HOME=/opt/soap-2_2 ; export SOAP_HOME
CLASSPATH=/opt/xerces-1_2_3/xerces.jar:/opt/javamail-1.2/mail.jar
CLASSPATH=$CLASSPATH:/opt/jaf-1.0.2/activation.jar
CLASSPATH=$CLASSPATH:$SOAP_HOME/lib/soap.jar:$SOAP_HOME
export CLASSPATHet celui de notre demo :
CLASSPATH=$CLASSPATH:/opt/demo export CLASSPATHdémarrez tomcat
2. l'application sur le serveur et le client
mettez dans demo et compilez :
// le service RPC simplissime public class Calcul { public double getDouble(double valeur) { return (2 * valeur); } } // le Client import java.net.*; import java.util.*; import org.apache.soap.*; // Body, Envelope, Fault, Header import org.apache.soap.rpc.*; // Call, Parameter, Response public class ClientCalcul { public static void main( String[] arg ) throws Exception { if (arg.length != 1) { System.out.println("erreur : usage java ClientCalcul valeur"); System.exit(1); } URL url = new URL("http://localhost:8080/soap/servlet/rpcrouter"); String urn = "urn:calcul"; // nom du service Call call = new Call(); // prepare un appel Soap call.setTargetObjectURI( urn ); call.setMethodName( "getDouble" ); // méthode invoquée call.setEncodingStyleURI( Constants.NS_URI_SOAP_ENC ); Vector params = new Vector(); params.addElement( new Parameter( "valeur", Double.class, new Double(arg[0]), null)); call.setParams( params ); // les paramètres de la méthode try { System.out.println("invoke service\n" + " URL= " + url + "\n URN =" + urn ); Response response = call.invoke( url, "" ); // lancer l'appel if( !response.generatedFault() ) { Parameter result = response.getReturnValue(); // le résultat System.out.println( "Result= " + result.getValue() ); } else { Fault f = response.getFault(); // an error occurred System.err.println( "Fault= " + f.getFaultCode() + ", " + f.getFaultString() ); } } catch( SOAPException e ) { System.err.println( "SOAPException= " + e.getFaultCode() + ", " + e.getMessage() ); } } }3. Déployez l'application sur le serveur
mettez dans demo le fichier DeploymentDescriptor.xml :
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment" id="urn:calcul"> <isd:provider type="java" scope="Application" methods="getDouble"> <isd:java class="Calcul" static="false"/> </isd:provider> <isd:faultListener>org.apache.soap.server.DOMFaultListener</isd:faultListener> </isd:service>déployez sur le serveur :
cd /opt/demo java org.apache.soap.server.ServiceManagerClient \ http://localhost:8080/soap/servlet/rpcrouter \ deploy DeploymentDescriptor.xmlvisualisez http://localhost:8080/soap dans un Navigateur
puis clicquer sur admin
Clicquez Deploy et renseignez :
urn:calcul dans ID
Calcul dans Provider Class
getDouble dans Methods
puis validez et regardez List :
Clicquez sur le service pour voir les détails :4. Exécutez le Client
rien de plus simple, dans demo :
$ java ClientCalcul 4.5 invoke service URL= http://localhost:8080/soap/servlet/rpcrouter URN =urn:calcul Result= 9.0Regardons avec ethereal le flot échangé :
Lancez le client puis stoppez:
Clicquer avec le bouton droit le début de la requete HTTP POST SOAP
et sélectionnez follow TCP Stream pour obtenir :POST /soap/servlet/rpcrouter HTTP/1.0 Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: 438 SOAPAction: "" <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <ns1:getDouble xmlns:ns1="urn:calcul" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <valeur xsi:type="xsd:double">4.5</valeur> </ns1:getDouble> </SOAP-ENV:Body> </SOAP-ENV:Envelope> HTTP/1.0 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: 455 Set-Cookie2: JSESSIONID=xgsc55u4n1;Version=1;Discard;Path="/soap" Set-Cookie: JSESSIONID=xgsc55u4n1;Path=/soap Date: Tue, 03 Dec 2002 13:03:10 GMT Servlet-Engine: Tomcat Web Server/3.2.4 (JSP 1.1; Servlet 2.2; Java 1.4.0; Linux 2.4.2-2 i386; java.vendor=Sun Microsystems Inc.) <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <ns1:getDoubleResponse xmlns:ns1="urn:calcul" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <return xsi:type="xsd:double">9.0</return> </ns1:getDoubleResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>5. Exercice
Ecrire le client pour faire une requete à l'URL http://services.xmethods.net:80/soap/servlet/rpcrouter avec l'enveloppe ci-dessous et en utilisant les "commodités" des packages soap 2.2.
<?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <tns:getTemp xmlns:tns="urn:xmethods-Temperature" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <zipcode>55406</zipcode> </tns:getTemp> </SOAP-ENV:Body> </SOAP-ENV:Envelope>