Barre Formation IN2P3
École franco-maghrébine 2003 WebServices 13-17 octobre 2003

Travaux pratiques

Précédent

tp2a

Suivant

Enoncé :

Réaliser un WS qui, à partir d'un identifiant de salle (nom de salle, de son numéro et du bâtiment) et d'une date (n° semaine, jour dans la semaine, am/pm) permette de savoir si la salle est libre et, sinon, le cours qui y est organisé.

Prévoir 6 paramètres.

Regarder le WSDL généré.

Tester le WS en écrivant un client du WS.

Aide :

Tester l'utilisation de types primitifs.

On utilise la librairie dom4j et le fichier "planning.xml". il faut donc les installer dans axis.

Copier le fichier "C:\dom4j\dom4j-full.jar" dans "C:\axis\webapps\axis\WEB-INF\lib" et relancer axis.

Copier le fichier planning.xml dans "C:\axis\webapps\axis" afin qu'il soit accessible par l'URL "http://localhost:8080/axis/planning.xml".

Dom4j permet de rechercher directement dans un fichier xml en utilisant une expression XPath (voir la documentation dom4j).

Correction :

Contenu du fichier tp2a/InfoCours.java.

package tp2a;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.*;
import java.util.*;

public class InfoCours {

    Node node;
    String xpathexp;
    XPath xpath;
    
    public String infoCours(int numeroSemaine, String nomSalle, int numeroSalle, String batimentSalle, String
jourSemaine, String ampm) {
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(new java.net.URL("http://localhost:8080/axis/planning.xml"));
            Map uris = new HashMap();
            uris.put( "planning", "http://in2p3.fr/WS/tp1" );
            if ( nomSalle != null) {
                xpathexp = "//planning:semaine[@planning:numerosemaine='"+ (new
Integer(numeroSemaine)).toString() +"' and planning:salle/planning:nom='"+ nomSalle
+"']/planning:jour[@planning:nomjour='"+jourSemaine+"']/planning:"+ampm+"/planning:cours";
                xpath = document.createXPath( xpathexp );
                xpath.setNamespaceURIs( uris );
                node = xpath.selectSingleNode( document );
            } else if (numeroSalle != 0) {
                xpathexp = "//planning:semaine[@planning:numerosemaine='"+ (new
Integer(numeroSemaine)).toString() +"' and planning:salle/planning:numero='"+ (new
Integer(numeroSalle)).toString()
+"']/planning:jour[@planning:nomjour='"+jourSemaine+"']/planning:"+ampm+"/planning:cours";
                xpath = document.createXPath( xpathexp );
                xpath.setNamespaceURIs( uris );
                node = xpath.selectSingleNode( document );
            }
            if (node != null) {
                System.err.println("node.getName() : " + node.getName());
                xpathexp = "planning:intitule/text()";
                xpath = node.createXPath( xpathexp );
                xpath.setNamespaceURIs( uris );
                String intitule = xpath.valueOf( node );
                xpathexp = "planning:responsable/text()";
                xpath = node.createXPath( xpathexp );
                xpath.setNamespaceURIs( uris );
                String responsable = xpath.valueOf( node );
                return "Cours : "+intitule+" donne par : "+ responsable + ".";
            } else {
                return "Pas de cours prevu.";
            }
        } catch (Exception e) {
            return "Error : " + e.toString();
        }
    }
    public static void main(String args[]) {
        tp2a.InfoCours ic = new tp2a.InfoCours();
        System.out.println(ic.infoCours(23, "Bleuet", 0, null, "lundi", "am" ));
    }
}

Contenu du fichier tp2a/deploy.wsdd.

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

 <service name="InfoCours" provider="java:RPC">
  <parameter name="className" value="tp2a.InfoCours"/>
  <parameter name="allowedMethods" value="infoCours"/>
 </service>

</deployment>

Contenu du fichier tp2a/ClientInfoCours.java.

package tp2a;

import org.apache.axis.client.*;

import javax.xml.namespace.QName;

public class ClientInfoCours {
    public static void main(String [] args) {
        try {

            String endpointURL = "http://localhost:8080/axis/services/InfoCours";

            Service  service = new Service();
            Call     call    = (Call) service.createCall();

            call.setTargetEndpointAddress( new java.net.URL(endpointURL) );
            call.setOperationName( new QName("InfoCours","infoCours") );
            call.addParameter("op1",org.apache.axis.encoding.XMLType.XSD_INT,javax.xml.rpc.ParameterMode.IN);
           
call.addParameter("op2",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
            call.addParameter("op3",org.apache.axis.encoding.XMLType.XSD_INT,javax.xml.rpc.ParameterMode.IN);
           
call.addParameter("op4",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
           
call.addParameter("op5",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
           
call.addParameter("op6",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
            call.setReturnType( org.apache.axis.encoding.XMLType.XSD_STRING );
            String ret = (String) call.invoke( new Object[] { new Integer(23), "Bleuet", new Integer(0), null,
"lundi", "am" } );

            System.out.println("Resultat : " + ret);
        } catch (Exception e) {
            System.err.println(e.toString());
            e.printStackTrace();
        }
    }
}

Ouvrir un invite de commandes dans tps.

javac tp2a/*.java
java org.apache.axis.client.AdminClient tp2a\deploy.wsdd

Copier le fichier "tp2a\InfoCours.class" dans "C:\axis\webapps\axis\WEB-INF\classes\tp2a".

java tp2a.ClientInfoCours

École franco-maghrébine 2003 WebServices 13-17 octobre 2003