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

Travaux pratiques

Précédent

tp2b

Suivant

Enoncé :

Refaire l'exercice précédent en utilisant cette fois un regroupement des paramètres et des résultats dans de nouveaux types :

type salle: nom, n°, bâtiment

type date: n° semaine, jour, am/pm

type cours: intitulé, responsable

Le WS ne comporte maintenant plus que deux paramètres.

Regarder le WSDL généré.

Tester le WS en écrivant un client du WS.

Aide :

Il faut définir de nouvelles classes pour les types personnalisés.

Le fichier WSDD est à modifier en conséquence.

Correction :

Contenu du fichier tp2b/Horaire.java.

package tp2b;
public class Horaire {

    private String nomJour;
    private int numeroSemaine;
    private String ampm;

    public String getNomJour() {
        return this.nomJour;
    }
    public void setNomJour(String nomJour) {
        this.nomJour = nomJour;
    }

    public int getNumeroSemaine() {
        return this.numeroSemaine;
    }
    public void setNumeroSemaine(int numeroSemaine) {
        this.numeroSemaine = numeroSemaine;
    }

    public String getAmpm() {
        return this.ampm;
    }
    public void setAmpm(String ampm) {
        this.ampm = ampm;
    }
}

Contenu du fichier tp2b/Cours.java.

package tp2b;

public class Cours {

    private String intituleCours;
    private String responsableCours;

    public String getIntituleCours() {
        return this.intituleCours;
    }
    public void setIntituleCours(String intituleCours) {
        this.intituleCours = intituleCours;
    }

    public String getResponsableCours() {
        return this.responsableCours;
    }
    public void setResponsableCours(String responsableCours) {
        this.responsableCours = responsableCours;
    }
}

Contenu du fichier tp2b/Salle.java.

package tp2b;

public class Salle {
    
    private String nomSalle;
    private int numeroSalle;
    private String nomBatiment;

    public String getNomSalle() {
        return this.nomSalle;
    }
    public void setNomSalle(String nomSalle) {
        this.nomSalle = nomSalle;
    }
    
    public int getNumeroSalle() {
        return this.numeroSalle;
    }
    public void setNumeroSalle(int numeroSalle) {
        this.numeroSalle = numeroSalle;
    }
    
    public String getNomBatiment() {
        return this.nomBatiment;
    }
    public void setNomBatiment(String nomBatiment) {
        this.nomBatiment = nomBatiment;
    }
    
}

Contenu du fichier tp2b/InfoCours2.java.

package tp2b;

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

public class InfoCours2 {

    Node node;
    String xpathexp;
    XPath xpath;

    public Cours infoCours(Salle salle, Horaire horaire) throws Exception {
        int numeroSemaine;
        String nomJour;
        String ampm;

        String nomSalle;
        int numeroSalle;
        String nomBatiment;

        numeroSemaine = horaire.getNumeroSemaine();
        nomJour = horaire.getNomJour();
        ampm = horaire.getAmpm();

        nomSalle = salle.getNomSalle();
        numeroSalle = salle.getNumeroSalle();
        nomBatiment = salle.getNomBatiment();

        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='"+nomJour+"']/planning:"+ampm+"/planning:cours";
                            //System.err.println(xpathexp);
                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='"+nomJour+"']/planning:"+ampm+"/planning:cours";
                            //System.err.println(xpathexp);
                xpath = document.createXPath( xpathexp );
                xpath.setNamespaceURIs( uris );
                node = xpath.selectSingleNode( document );
            } else if (nomBatiment != null) {
                return null;
            }
            if (node != null) {
                System.err.println("node.getName() : " + node.getName());
                xpathexp = "planning:intitule/text()";
                //System.err.println(xpathexp);
                xpath = node.createXPath( xpathexp );
                xpath.setNamespaceURIs( uris );
                String intitule = xpath.valueOf( node );
                xpathexp = "planning:responsable/text()";
                //System.err.println(xpathexp);
                xpath = node.createXPath( xpathexp );
                xpath.setNamespaceURIs( uris );
                String responsable = xpath.valueOf( node );
                Cours cours = new Cours();
                cours.setIntituleCours(intitule);
                cours.setResponsableCours(responsable);
                return cours;
            } else {
                return null;
            }
        } catch (Exception e) {
            throw e;
        }
    }
}

Contenu du fichier tp2b/deploy.wsdd.

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <service name="InfoCours2" provider="java:RPC">
        <parameter name="className" value="tp2b.InfoCours2"/>
        <parameter name="allowedMethods" value="infoCours"/>
        <beanMapping qname="myNS:Cours" xmlns:myNS="urn:InfoCours2" languageSpecificType="java:tp2b.Cours"/>
        <beanMapping qname="myNS:Salle" xmlns:myNS="urn:InfoCours2" languageSpecificType="java:tp2b.Salle"/>
        <beanMapping qname="myNS:Horaire" xmlns:myNS="urn:InfoCours2"
languageSpecificType="java:tp2b.Horaire"/>
    </service>
</deployment>

Contenu du fichier tp2b/ClientInfoCours2.java.

package tp2b;

import org.apache.axis.client.*;

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import tp2b.*;

public class ClientInfoCours2 {
    public static void main(String [] args) {
        try {
            Salle salle = new Salle();
            Horaire horaire = new Horaire();
            
            salle.setNumeroSalle(0);
            salle.setNomSalle("Bleuet");
            salle.setNomBatiment(null);
            horaire.setAmpm("am");
            horaire.setNumeroSemaine(23);
            horaire.setNomJour("lundi");
            
            String endpointURL = "http://localhost:8080/axis/services/InfoCours2";

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

            QName    qn1      = new QName( "urn:InfoCours2", "Cours" );
            call.registerTypeMapping(Cours.class, qn1,
                                     new org.apache.axis.encoding.ser.BeanSerializerFactory(Cours.class, qn1),
       
                                     new org.apache.axis.encoding.ser.BeanDeserializerFactory(Cours.class,
qn1));
            
            QName    qn2      = new QName( "urn:InfoCours2", "Salle" );
            call.registerTypeMapping(Salle.class, qn2,
                                     new org.apache.axis.encoding.ser.BeanSerializerFactory(Salle.class, qn2),
       
                                     new org.apache.axis.encoding.ser.BeanDeserializerFactory(Salle.class,
qn2));        

            QName    qn3      = new QName( "urn:InfoCours2", "Horaire" );
            call.registerTypeMapping(Horaire.class, qn3,
                                     new org.apache.axis.encoding.ser.BeanSerializerFactory(Horaire.class,
qn3),        
                                     new org.apache.axis.encoding.ser.BeanDeserializerFactory(Horaire.class,
qn3));        
            
            call.setTargetEndpointAddress( new java.net.URL(endpointURL) );
            call.setOperationName( new QName("InfoCours","infoCours") );
            
            Cours cours = (Cours) call.invoke( new Object[] { salle, horaire } );

            System.out.println("Resultat : " + cours.getIntituleCours() + " donne par " +
cours.getResponsableCours());
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

Ouvrir un invite de commandes dans tps.

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

Copier les fichiers "tp2b\InfoCours2.class","tp2b\Salle.class","tp2b\Horaire.class","tp2b\Cours.class" dans "C:\axis\webapps\axis\WEB-INF\classes\tp2b".

java tp2b.ClientInfoCours2
École franco-maghrébine 2003 WebServices 13-17 octobre 2003