Presentation is loading. Please wait.

Presentation is loading. Please wait.

Services Web et SOAP  Coulouris, Dollimore and Kindberg, Distributed Systems: Concepts and Design, Edition 4, © Addison-Wesley 2005 zTutoriel Sun sur.

Similar presentations


Presentation on theme: "Services Web et SOAP  Coulouris, Dollimore and Kindberg, Distributed Systems: Concepts and Design, Edition 4, © Addison-Wesley 2005 zTutoriel Sun sur."— Presentation transcript:

1 Services Web et SOAP  Coulouris, Dollimore and Kindberg, Distributed Systems: Concepts and Design, Edition 4, © Addison-Wesley 2005 zTutoriel Sun sur SAAJ

2 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 SOAP zInitialement Simple Object Access Protocol, et dernièrement Service Oriented Architecture Protocol zProtocole de communication yCommunication entre applications yFormat de transmission des messages yCommunication via Internet zIndépendant des plate-formes zIndépendant du langage zXML zSimple et extensible zTraverse les pare-feux zUn standard W3C

3 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Figure 19.3 SOAP message in an envelope envelope header body header element body element header element body element

4 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Figure 19.4 Example of a simple request without headers m:exchange env:envelope xmlns:env =namespace URI for SOAP envelopes m:arg1 env:body xmlns:m = namespace URI of the service description Hello m:arg2 World In this figure and the next, each XML element is represented by a shaded box with its name in italic followed by any attributes and its content

5 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Figure 19.5 Example of a reply corresponding to the request in Figure 19.4 env:envelope xmlns:env = namespace URI for SOAP envelope m:res1 env:body xmlns:m = namespace URI for the service description m:res2 World m:exchangeResponse Hello

6 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Figure 19.6 Use of HTTP POST Request in SOAP client-server communication endpoint address action POST /examples/stringer Host: www.cdk4.net Content-Type: application/soap+xml Action: http://www.cdk4.net/examples/stringer#exchange Soap message HTTP header

7 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Règles syntaxiques zEn XML zDoit utiliser y SOAP Envelope namespace y SOAP Encoding namespace znamespace par défaut pour l’enveloppe SOAP : yhttp://www.w3.org/2001/12/soap-envelopehttp://www.w3.org/2001/12/soap-envelope znamespace par défaut pour encodage et les types de données SOAP : yhttp://www.w3.org/2001/12/soap-encodinghttp://www.w3.org/2001/12/soap-encoding zUn message SOAP ne doit pas contenir de yréférence à une DTD yInstructions de traitement XML

8 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Squelette d’un message SOAP

9 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 SAAJ zSOAP with Attachments API for Java (SAAJ) yManière standard de transmettre des documents XML sur Internet en Java yhttp://java.sun.com/j2ee/1.4/docs/api/index.htmlhttp://java.sun.com/j2ee/1.4/docs/api/index.html

10 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Messages zInterfaces y Node extends org.w3c.dom.Node y SOAPEnvelope y SOAPElement extends Node, org.w3c.dom.Element zClasses y SOAPMessage y SOAPPart implements org.w3c.dom.Document xUn document DOM zMany SAAJ API interfaces extend DOM interfaces

11 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Messages avec et sans pièces jointes

12 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Invocation de la méthode getLastTradePrice sur un service Web zREQUETE SUNW zREPONSE

13 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Construction d’une requête SOAP import javax.xml.soap.*; import java.util.*; import java.net.URL; public class SoapRequest { public static void main(String[] args) { try { SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); SOAPFactory soapFactory = SOAPFactory.newInstance(); MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); SOAPHeader header = message.getSOAPHeader(); SOAPBody body = message.getSOAPBody(); header.detachNode();

14 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Construction d’une requête SOAP Name bodyName = soapFactory.createName("GetLastTradePrice", "m", "http://wombats.ztrade.com"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); Name name = soapFactory.createName("symbol"); SOAPElement symbol = bodyElement.addChildElement(name); symbol.addTextNode("SUNW"); URL endpoint = new URL("http://wombat.ztrade.com/quotes"); SOAPMessage response = connection.call(message, endpoint); connection.close(); SOAPBody soapBody = response.getSOAPBody(); Iterator iterator = soapBody.getChildElements(bodyName); bodyElement = (SOAPBodyElement) iterator.next(); String lastPrice = bodyElement.getValue(); System.out.print("The last price for SUNW is "); System.out.println(lastPrice); } catch (Exception ex) { ex.printStackTrace(); }

15 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Connexions zTous les messages SOAP sont transmis et reçus à l’aide d’une connexion. yMessages de type request-response. yméthode call, bloquantes en attente d’une réponse SOAPConnectionFactory soapConnectionFactory; SOAPConnection connection; URL endpoint; SOAPMessage response; soapConnectionFactory = SOAPConnectionFactory.newInstance(); connection = soapConnectionFactory.createConnection();...// create a request message and give it content endpoint = new URL("http://wombat.ztrade.com/quotes"); response = connection.call(message, endpoint);

16 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Création d’une connexion import javax.xml.soap.*; import java.util.*; import java.net.URL; public class SoapRequest { public static void main(String[] args) { SOAPConnectionFactory soapConnectionFactory; SOAPConnection connection; SOAPFactory soapFactory; try { soapConnectionFactory = SOAPConnectionFactory.newInstance(); connection = soapConnectionFactory.createConnection(); soapFactory = SOAPFactory.newInstance();

17 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Création d’un message MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); // header : données XML qui affecte la manière dont le contenu // spécifique à une application sera traité par le fournisseur du // message, par exemple : sémantique d’une transaction, information SOAPHeader header = message.getSOAPHeader(); SOAPBody body = message.getSOAPBody(); // l’en-tête n’est pas nécessaire header.detachNode();

18 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Création d’un message zL’instance de SOAPFactory permet de créer les différents objets qui seront dans l’arborescence XML SOAP zcreateName(String localName, String prefix, String uri)createNameString Name bodyName = soapFactory.createName("GetLastTradePrice", "m", "http://wombats.ztrade.com"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); Name name = soapFactory.createName("symbol"); SOAPElement symbol = bodyElement.addChildElement(name); symbol.addTextNode("SUNW");

19 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Le message SOAP résultant SUNW

20 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Envoi du message URL endpoint ; SOAPMessage response; endpoint = new URL("http://wombat.ztrade.com/quotes"); response = connection.call(message, endpoint); connection.close();

21 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Analyse de la réponse SOAPBody soapBody = response.getSOAPBody(); Iterator iterator = soapBody.getChildElements(bodyName); bodyElement = (SOAPBodyElement) iterator.next(); String lastPrice = bodyElement.getValue(); System.out.print("The last price for SUNW is "); System.out.println(lastPrice);

22 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Création d’une pièce jointe de type texte AttachmentPart attachment = message.createAttachmentPart(); String stringContent = "Update address for Sunny Skies " + "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439"; attachment.setContent(stringContent, "text/plain"); attachment.setContentId("update_address"); message.addAttachmentPart(attachment);

23 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Création d’une pièce jointe de type image URL url = new URL("http://greatproducts.com/gizmos/img.jpg"); DataHandler dataHandler = new DataHandler(url); AttachmentPart attachment = message.createAttachmentPart(dataHandler); attachment.setContentId("attached_image"); message.addAttachmentPart(attachment); zLe DataHandler va s’occuper de déterminer le type MIME zsetContentID : déterminer le nom qui sera utilisé pour identifier la pièce jointe

24 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Accéder aux pièces jointes java.util.Iterator iterator = message.getAttachments(); while (iterator.hasNext()) { AttachmentPart attachment = (AttachmentPart)iterator.next(); String id = attachment.getContentId(); String type = attachment.getContentType(); System.out.print("Attachment " + id + " has content type " + type); if (type == "text/plain") { Object content = attachment.getContent(); System.out.println("Attachment " + "contains:\n" + content); }

25 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Attributs de l’en-tête zDétermine comment le destinataire doit traiter le message zFaçon d’étendre un message en donnant de l’information sur des éléments comme authentification, gestion des transactions, paiement etc. zAttributs de l’en-tête (SOAP 1.1) yactor ymustUnderstand

26 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Attribut actor zIndique le destinataire de l’élément header zPar défaut, le destinataire ultime zUn acteur est une application qui peut yRecevoir des messages SOAP yLes acheminer à un autre acteur zExemple : traitement d’un bon de commande yRoute: order desk, shipping desk, confirmation desk, billing department. yChaque application prend les actions nécessaires, retire les objets pertinents pour son travail, achemine le message à l’acteur suivant

27 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Chaîne d’acteurs SOAPHeader header = message.getSOAPHeader(); SOAPFactory soapFactory = SOAPFactory.newInstance(); String nameSpace = "ns"; String nameSpaceURI = "http://gizmos.com/NSURI"; Name order = soapFactory.createName("orderDesk", nameSpace, nameSpaceURI); SOAPHeaderElement orderHeader = header.addHeaderElement(order); orderHeader.setActor("http://gizmos.com/orders"); Name shipping = soapFactory.createName("shippingDesk",nameSpace, nameSpaceURI); SOAPHeaderElement shippingHeader = header.addHeaderElement(shipping); shippingHeader.setActor("http://gizmos.com/shipping"); Name confirmation = soapFactory.createName("confirmationDesk",nameSpace, nameSpaceURI); SOAPHeaderElement confirmationHeader = header.addHeaderElement(confirmation); confirmationHeader.setActor( "http://gizmos.com/confirmations"); Name billing = soapFactory.createName("billingDesk", nameSpace, nameSpaceURI); SOAPHeaderElement billingHeader = header.addHeaderElement(billing); billingHeader.setActor("http://gizmos.com/billing");

28 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Attribut mustUnderstand zIndique si oui ou non le destinataire doit savoir traiter les en-têtes SOAPHeader header = message.getSOAPHeader(); Name name = soapFactory.createName("Transaction", "t","http://gizmos.com/orders"); SOAPHeaderElement transaction = header.addHeaderElement(name); transaction.setMustUnderstand(true); transaction.addTextNode("5"); zFichier résultant 5

29 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Fautes zSi un message n’a pas réussi zUn seul élément « faute » par message SOAP zDoit être dans le corps (body) du message SOAP.

30 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 SOAPFault fault = body.addFault(); Name faultName = soapFactory.createName("Client", "", SOAPConstants.URI_NS_SOAP_ENVELOPE); fault.setFaultCode(faultName); fault.setFaultString("Message does not have necessary info"); Detail detail = fault.addDetail(); Name entryName = soapFactory.createName("order", "PO", "http://gizmos.com/orders/"); DetailEntry entry = detail.addDetailEntry(entryName); entry.addTextNode("Quantity element does not have a value"); Name entryName2 = soapFactory.createName("confirmation", "PO", "http://gizmos.com/confirm"); DetailEntry entry2 = detail.addDetailEntry(entryName2); entry2.addTextNode("Incomplete address: no zip code"); zFichier résultant SOAP-ENV:Client Message does not have necessary info http://gizmos.com/order Quantity element does not have a value Incomplete address: no zip code

31 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Récupérer la faute SOAPBody body = newMessage.getSOAPBody(); if ( body.hasFault() ) { SOAPFault newFault = body.getFault(); Name code = newFault.getFaultCodeAsName(); String string = newFault.getFaultString(); String actor = newFault.getFaultActor(); System.out.println("SOAP fault contains: "); System.out.println(" Fault code = " + code.getQualifiedName()); System.out.println(" Fault string = " + string); if ( actor != null ) { System.out.println(" Fault actor = " + actor); } Detail newDetail = newFault.getDetail(); if (newDetail != null) { Iterator entries = newDetail.getDetailEntries(); while ( entries.hasNext() ) { DetailEntry newEntry = (DetailEntry)entries.next(); String value = newEntry.getValue(); System.out.println(" Detail entry = " + value); } zOutput SOAP fault contains: Fault code = SOAP-ENV:Client Local name = Client Namespace prefix = SOAP-ENV, bound to http://schemas.xmlsoap.org/soap/envelope/ Fault string = Message does not have necessary info Fault actor = http://gizmos.com/orderhttp://gizmos.com/order Detail entry = Quantity element does not have a value Detail entry = Incomplete address: no zip code

32 Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Addison-Wesley Publishers 2005 Références zJ2EE 1.5 tutorial yChapter 19 ySOAP with Attachments API for Java xhttp://java.sun.com/javaee/5/docs/tutorial/doc/bnbhf.html


Download ppt "Services Web et SOAP  Coulouris, Dollimore and Kindberg, Distributed Systems: Concepts and Design, Edition 4, © Addison-Wesley 2005 zTutoriel Sun sur."

Similar presentations


Ads by Google