École franco-maghrébine 2003 | WebServices | 13-17 octobre 2003 |
tp2f |
Paramètres en entrée: salle, date, cours
Retour: confirmation de réservation
Il faut prévoir, pour ce WS, plusieurs types d'exceptions pour traiter les différents cas d'erreurs possibles: salle occupée, mauvaise date, mauvaise salle
Réutiliser les classes Cours, Salle et Horaire.
Contenu du fichier tp2f/HoraireFault.java.
// // HoraireFault.java // TPS // // Created by Jean-RenŽ Rouet on Wed Sep 17 2003. // Copyright (c) 2003 __MyCompanyName__. All rights reserved. // package tp2f; import java.rmi.RemoteException; import java.io.Serializable; import java.lang.String; public class HoraireFault extends RemoteException implements Serializable { private String info; public HoraireFault() { } public HoraireFault( String info ) { this.info = info; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } }
Contenu du fichier tp2f/ReservationFault.java.
// // ReservationFault.java // TPS // // Created by Jean-RenŽ Rouet on Wed Sep 17 2003. // Copyright (c) 2003 __MyCompanyName__. All rights reserved. // package tp2f; import java.rmi.RemoteException; public class ReservationFault extends RemoteException implements java.io.Serializable { private java.lang.String info; public ReservationFault() { } public ReservationFault( java.lang.String info) { this.info = info; } public java.lang.String getInfo() { return info; } public void setInfo(java.lang.String info) { this.info = info; } }
Contenu du fichier tp2f/SalleFault.java.
// // SalleFault.java // TPS // // Created by Jean-RenŽ Rouet on Wed Sep 17 2003. // Copyright (c) 2003 __MyCompanyName__. All rights reserved. // package tp2f; import java.rmi.RemoteException; public class SalleFault extends RemoteException implements java.io.Serializable { private java.lang.String info; public SalleFault() { } public SalleFault( java.lang.String info) { this.info = info; } public java.lang.String getInfo() { return info; } public void setInfo(java.lang.String info) { this.info = info; } }
Contenu du fichier tp2f/ReservationWS.java.
// // ReservationWS.java // TPS // // Created by Jean-Rene Rouet on Thu Jul 17 2003. // Copyright (c) 2003 __MyCompanyName__. All rights reserved. // package tp2f; 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.io.*; import org.dom4j.*; import java.util.*; import java.io.*; import javax.naming.*; import tp2f.*; public class ReservationWS { static HashMap mapSalle = new HashMap(); static HashMap mapAmpm = new HashMap(); static HashMap mapJour = new HashMap(); static { Salle salle = new Salle(); salle.setNomSalle("Bleuet"); salle.setNumeroSalle(233); salle.setNomBatiment("R 2"); mapSalle.put(new Integer(salle.getNumeroSalle()), salle); salle = new Salle(); salle.setNomSalle("Marguerite"); salle.setNumeroSalle(224); salle.setNomBatiment("R 2"); mapSalle.put(new Integer(salle.getNumeroSalle()), salle); salle = new Salle(); salle.setNomSalle("Tulipe"); salle.setNumeroSalle(228); salle.setNomBatiment("R 2"); mapSalle.put(new Integer(salle.getNumeroSalle()), salle); mapAmpm.put("am","am"); mapAmpm.put("pm","pm"); mapJour.put("lundi","lundi"); mapJour.put("mardi","mardi"); mapJour.put("mercredi","mercredi"); mapJour.put("jeudi","jeudi"); mapJour.put("vendredi","vendredi"); mapJour.put("samedi","samedi"); } Node node, node2; String xpathexp; XPath xpath; public String reservation(Cours cours, Salle salle, Horaire horaire) throws HoraireFault,SalleFault,ReservationFault,Exception{ Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); String ab = (String) envCtx.lookup("axis.base"); String aw = (String) envCtx.lookup("axis.webapp"); String xmlFile = ab + aw + "/planning.xml"; System.err.println("xmlFile : " + xmlFile); System.err.println("salle.getNumeroSalle() : " + salle.getNumeroSalle()); if (mapSalle.get(new Integer(salle.getNumeroSalle())) == null) throw new SalleFault("Cette salle n'existe pas"); if (horaire.getNumeroSemaine()<0 || horaire.getNumeroSemaine()>52) throw new HoraireFault("Cette semaine n'exite pas"); if (mapAmpm.get(horaire.getAmpm()) == null) throw new HoraireFault("Soit am soit pm."); if (mapJour.get(horaire.getNomJour()) == null) throw new HoraireFault("le jour de la semaine est faux."); SAXReader reader = new SAXReader(); Document document = reader.read(new File(xmlFile)); Map uris = new HashMap(); uris.put( "planning", "http://in2p3.fr/WS/tp1" ); xpathexp = "//planning:semaine[@planning:numerosemaine='" + horaire.getNumeroSemaine() + "' and planning:salle/planning:nom='" + salle.getNomSalle() + "']"; System.err.println(xpathexp); xpath = document.createXPath( xpathexp ); xpath.setNamespaceURIs( uris ); node = xpath.selectSingleNode( document ); if (node == null) { // il faut créer la semaine pour la salle node = document.getRootElement(); System.err.println("node.getName() : " + node.getName()); Element semaineE=((Element)node).addElement( "planning:semaine" , "http://in2p3.fr/WS/tp1").addAttribute( QName.get("planning:numerosemaine", "http://in2p3.fr/WS/tp1"), new Integer(horaire.getNumeroSemaine()).toString() ); Element salleE=semaineE.addElement( "planning:salle" , "http://in2p3.fr/WS/tp1"); salleE.addElement("planning:nom", "http://in2p3.fr/WS/tp1").addText(salle.getNomSalle()); salleE.addElement("planning:numero", "http://in2p3.fr/WS/tp1").addText(new Integer(salle.getNumeroSalle()).toString()); salleE.addElement("planning:batiment", "http://in2p3.fr/WS/tp1").addText(salle.getNomBatiment()); semaineE.addElement( "planning:jour", "http://in2p3.fr/WS/tp1").addAttribute( QName.get("planning:nomjour", "http://in2p3.fr/WS/tp1"),"lundi"); semaineE.addElement( "planning:jour", "http://in2p3.fr/WS/tp1").addAttribute( QName.get("planning:nomjour", "http://in2p3.fr/WS/tp1"),"mardi"); semaineE.addElement( "planning:jour", "http://in2p3.fr/WS/tp1").addAttribute( QName.get("planning:nomjour", "http://in2p3.fr/WS/tp1"),"mercredi"); semaineE.addElement( "planning:jour", "http://in2p3.fr/WS/tp1").addAttribute( QName.get("planning:nomjour", "http://in2p3.fr/WS/tp1"),"jeudi"); semaineE.addElement( "planning:jour", "http://in2p3.fr/WS/tp1").addAttribute( QName.get("planning:nomjour", "http://in2p3.fr/WS/tp1"),"vendredi"); semaineE.addElement( "planning:jour", "http://in2p3.fr/WS/tp1").addAttribute( QName.get("planning:nomjour", "http://in2p3.fr/WS/tp1"),"samedi"); node = semaineE; } System.err.println("node.getName() : " + node.getName()); // l'horaire est-il deja pris xpathexp = "child::planning:jour[@planning:nomjour='"+horaire.getNomJour()+"']/planning:"+horaire.getAmpm()+"/planning:cours"; System.err.println("xpathexp : " + xpathexp); xpath = node.createXPath( xpathexp ); xpath.setNamespaceURIs( uris ); node2 = xpath.selectSingleNode( node ); if (node2 != null) throw new ReservationFault("Creneau horaire deja occupe"); xpathexp = "child::planning:jour[@planning:nomjour='"+horaire.getNomJour()+"']"; System.err.println("xpathexp : " + xpathexp); xpath = node.createXPath( xpathexp ); xpath.setNamespaceURIs( uris ); node2 = xpath.selectSingleNode( node ); Element ampmE=((Element)node2).addElement( "planning:"+horaire.getAmpm() , "http://in2p3.fr/WS/tp1"); Element coursE = ampmE.addElement( "planning:cours" , "http://in2p3.fr/WS/tp1"); coursE.addElement( "planning:intitule" , "http://in2p3.fr/WS/tp1").addText(cours.getIntituleCours()); coursE.addElement( "planning:responsable" , "http://in2p3.fr/WS/tp1").addText(cours.getResponsableCours()); // on crée le cours et on sauvegarde le fichier xml OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter( new FileWriter( xmlFile ), format ); writer.write( document ); writer.flush(); writer.close(); return "Reservation effectuee."; } }
Pour que la classe fonctionne, il faut définir dans Tomcat, des variables d'environnement qui indiquent l'emplacement du fichier "planning.xml".
Contenu du fichier tp2f/deploy.wsdd.
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="ReservationWS" provider="java:RPC"> <namespace>http://ReservationWS.tp1f</namespace> <parameter name="className" value="tp2f.ReservationWS"/> <parameter name="allowedMethods" value="reservation"/> <parameter name="scope" value="Session"/> <operation name="reservation" qname="operNS:reservation" xmlns:operNS="http://ReservationWS.tp1f" returnQName="reservationReturn" returnType="tns:string" xmlns:tns="http://www.w3.org/2001/XMLSchema"> <parameter name="in0" type="tns:Cours" xmlns:tns="http://ReservationWS.tp1f"/> <parameter name="in1" type="tns:Salle" xmlns:tns="http://ReservationWS.tp1f"/> <parameter name="in2" type="tns:Horaire" xmlns:tns="http://ReservationWS.tp1f"/> <fault name="HoraireFault" qname="fns:fault" xmlns:fns="http://localhost:8080/axis/services/ReservationWS" class="tp2f.HoraireFault" type="tns:HoraireFault" xmlns:tns="http://ReservationWS.tp1f"/> <fault name="SalleFault" qname="fns:fault" xmlns:fns="http://localhost:8080/axis/services/ReservationWS" class="tp2f.SalleFault" type="tns:SalleFault" xmlns:tns="http://ReservationWS.tp1f"/> <fault name="ReservationFault" qname="fns:fault" xmlns:fns="http://localhost:8080/axis/services/ReservationWS" class="tp2f.ReservationFault" type="tns:ReservationFault" xmlns:tns="http://ReservationWS.tp1f"/> </operation> <typeMapping xmlns:ns="http://ReservationWS.tp1f" qname="ns:Horaire" type="java:tp2f.Horaire" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> <typeMapping xmlns:ns="http://ReservationWS.tp1f" qname="ns:Salle" type="java:tp2f.Salle" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> <typeMapping xmlns:ns="http://ReservationWS.tp1f" qname="ns:Cours" type="java:tp2f.Cours" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> <typeMapping xmlns:ns="http://ReservationWS.tp1f" qname="ns:HoraireFault" type="java:tp2f.HoraireFault" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> <typeMapping xmlns:ns="http://ReservationWS.tp1f" qname="ns:ReservationFault" type="java:tp2f.ReservationFault" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> <typeMapping xmlns:ns="http://ReservationWS.tp1f" qname="ns:SalleFault" type="java:tp2f.SalleFault" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </service> </deployment>
Contenu du fichier tp2f/ReservationClient.java.
package tp2f; import org.apache.axis.encoding.ser.BeanSerializerFactory; import org.apache.axis.encoding.ser.BeanDeserializerFactory; import org.apache.axis.AxisFault; import org.apache.axis.utils.Options; import org.apache.axis.transport.http.SimpleAxisWorker; import org.apache.axis.description.OperationDesc; import javax.xml.rpc.ServiceFactory; import javax.xml.rpc.Service; import javax.xml.rpc.Call; import javax.xml.rpc.encoding.TypeMappingRegistry; import javax.xml.rpc.encoding.TypeMapping; import javax.xml.namespace.QName; import java.net.URL; import java.util.Map; import java.util.Iterator; import org.apache.axis.client.*; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import tp2f.*; public class ReservationClient { public static void main(String [] args) throws Exception{ try { Salle salle = new Salle(); Horaire horaire = new Horaire(); Cours cours = new Cours(); salle.setNumeroSalle(233); salle.setNomSalle("Bleuet"); salle.setNomBatiment("R 2"); horaire.setAmpm("am"); horaire.setNumeroSemaine(26); horaire.setNomJour("mardi"); cours.setIntituleCours("WebServices"); cours.setResponsableCours("Jean Pars"); String endpointURL = "http://localhost:8080/axis/services/ReservationWS"; String uri = "http://ReservationWS.tp1f"; ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service service = serviceFactory.createService(new QName(uri,"ReservationWS")); TypeMappingRegistry registry = service.getTypeMappingRegistry(); TypeMapping map = registry.getDefaultTypeMapping(); QName qn1 = new QName( uri, "Cours" ); map.register( Cours.class , qn1 , new BeanSerializerFactory(Cours.class, qn1) , new BeanDeserializerFactory(Cours.class, qn1) ); QName qn2 = new QName( uri, "Salle" ); map.register( Salle.class , qn2 , new BeanSerializerFactory(Salle.class, qn2) , new BeanDeserializerFactory(Salle.class, qn2) ); QName qn3 = new QName( uri, "Horaire" ); map.register( Horaire.class , qn3 , new BeanSerializerFactory(Horaire.class, qn3) , new BeanDeserializerFactory(Horaire.class, qn3) ); QName faultQName = new QName(uri, "HoraireFault"); map.register( HoraireFault.class , faultQName , new BeanSerializerFactory(HoraireFault.class, faultQName) , new BeanDeserializerFactory(HoraireFault.class, faultQName) ); QName faultQName2 = new QName(uri, "SalleFault"); map.register( SalleFault.class , faultQName2 , new BeanSerializerFactory(SalleFault.class, faultQName2) , new BeanDeserializerFactory(SalleFault.class, faultQName2) ); QName faultQName3 = new QName(uri, "ReservationFault"); map.register( ReservationFault.class , faultQName3 , new BeanSerializerFactory(ReservationFault.class, faultQName3) , new BeanDeserializerFactory(ReservationFault.class, faultQName3)); Call call = service.createCall(); call.setTargetEndpointAddress( endpointURL ); call.setProperty(Call.SESSION_MAINTAIN_PROPERTY, Boolean.TRUE); call.setProperty(Call.SOAPACTION_USE_PROPERTY, Boolean.TRUE); call.setProperty(Call.SOAPACTION_URI_PROPERTY, uri); call.setOperationName( new QName(uri,"reservation") ); /* call.addParameter( "in0", qn1, ParameterMode.IN ); call.addParameter( "in1", qn2, ParameterMode.IN ); call.addParameter( "in2", qn3, ParameterMode.IN ); ((org.apache.axis.client.Call)call).addFault(new QName(uri,"fault1"), HoraireFault.class, faultQName, false); ((org.apache.axis.client.Call)call).addFault(new QName(uri,"fault2"), SalleFault.class, faultQName2, false); ((org.apache.axis.client.Call)call).addFault(new QName(uri,"fault3"), ReservationFault.class, faultQName3, false); call.setReturnType( org.apache.axis.encoding.XMLType.XSD_STRING );*/ String reservation = (String) call.invoke( new Object[] { cours, salle, horaire } ); System.out.println("Resultat : " + reservation); } catch (HoraireFault hf) { System.err.println("Probleme horaire"); System.err.println(hf.getInfo()); } catch (SalleFault sf) { System.err.println("Probleme salle"); System.err.println(sf.getInfo()); } catch (ReservationFault rf) { System.err.println("Probleme reservaation"); System.err.println(rf.getInfo()); } } }
École franco-maghrébine 2003 | WebServices | 13-17 octobre 2003 |