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

Travaux pratiques

Précédent

tp2d

Suivant

Enoncé :

Réaliser un WS qui, étant donnés une salle et un numéro de semaine, retourne un document XML correspondant à la description de l'occupation de la salle (document XML réalisé plus haut).

Aide :

Réutiliser les classes déjà écrites.

Correction :

Contenu du fichier tp2d/InfoCours3.java.

package tp2d;
import org.apache.axis.message.SOAPBodyElement;
import org.apache.axis.utils.Options;
import org.apache.axis.utils.XMLUtils;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

import tp2d.*;
 public class InfoCours3 {

    Node node;
    String xpathexp;
    XPath xpath;

    public  org.w3c.dom.Element[] infoCours(org.w3c.dom.Element[] elems) {

        String nomSalle = XMLUtils.getChildCharacterData(elems[0]);
        String numeroSemaine = XMLUtils.getChildCharacterData(elems[1]);;

        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" );
            xpathexp = "//planning:semaine[@planning:numerosemaine='"+ numeroSemaine +"' and
planning:salle/planning:nom='"+ nomSalle +"']";
                        //System.err.println(xpathexp);
                xpath = document.createXPath( xpathexp );
                xpath.setNamespaceURIs( uris );
                node = xpath.selectSingleNode( document );
                if (node != null) {
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    javax.xml.parsers.DocumentBuilder  builder = factory.newDocumentBuilder() ;
                    InputStream in = (InputStream) new ByteArrayInputStream(node.asXML().getBytes());
                    org.w3c.dom.Document documentRes = builder.parse(in);
                    return new org.w3c.dom.Element[]{documentRes.getDocumentElement()};
                } else {
                    return new org.w3c.dom.Element[]{XMLUtils.StringToElement("urn:foo","e1","pas de noeud")};
                }
        } catch (Exception e) {
            return new org.w3c.dom.Element[]{XMLUtils.StringToElement("urn:foo","e2",e.toString())};
        }
    }
}

Contenu du fichier tp2d/deploy.wsdd.

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <service name="InfoCours3" style="java:RPC">
        <parameter name="className" value="tp2d.InfoCours3"/>
        <parameter name="allowedMethods" value="infoCours"/>
    </service>
</deployment>

Contenu du fichier tp2d/ClientInfoCours3.java.

package tp2d;

import org.apache.axis.message.SOAPBodyElement;
import org.apache.axis.utils.Options;
import org.apache.axis.utils.XMLUtils;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.net.URL;
import java.util.Vector;

import org.apache.axis.client.*;

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import tp2d.*;

public class ClientInfoCours3 {
    public static void main(String [] args) {
        try {
            SOAPBodyElement[] input = new SOAPBodyElement[2];

            input[0] = new SOAPBodyElement(XMLUtils.StringToElement("urn:foo", 
                                                                    "nomsalle", "Marguerite"));
            input[1] = new SOAPBodyElement(XMLUtils.StringToElement("urn:foo", 
                                                                    "numerosemaine", "23"));

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

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

            call.setTargetEndpointAddress( new java.net.URL(endpointURL) );
            call.setOperationName( new QName("InfoCours3","infoCours") );

            Vector          elems = (Vector) call.invoke( input );
            SOAPBodyElement elem  = null ;
            Element         e     = null ;

            elem = (SOAPBodyElement) elems.get(0);
            e    = elem.getAsDOM();

            String str = "Res elem[0]=" + XMLUtils.ElementToString(e);
            
            System.out.println (str);
            
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

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