1 / 91

XML, SOAP and WSDL with SOAP::Lite

XML, SOAP and WSDL with SOAP::Lite. Stefano Occhetti CERN - IT/ADC/CA. March 4, 2003. 1 XML. What is XML. XML. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

tuyet
Download Presentation

XML, SOAP and WSDL with SOAP::Lite

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML, SOAP and WSDL with SOAP::Lite Stefano Occhetti CERN - IT/ADC/CA March 4, 2003

  2. 1XML What is XML

  3. XML <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>HTML</title> </head> <body> <p>At first sight, XML looks like HTML.</p> </body> </html> • At first sight, XML looks like HTML…

  4. XML • …this is because HTML is defined as a subset of SGML. XML is actually more similar to SGML than to HTML, because HTML is only one specific subset of SGML used to describe web pages. • XML was created to simplify SGML, and the W3C has now decided to redefine HTML 4.0 as an XML application, thereby creating XHTML 1.0

  5. XML • XML is a clearly defined way to structure, describe, and interchange data, where data really means every conceivable kind of data.

  6. XML – elements <family> <name>Smith</name> <members>4</members> </family> The < and > angle brackets are used to distinguish between the so-called “markup” and the “actual data” of the document

  7. XML – elements <family> <name>Smith</name> <members>4</members> </family> Markups and actual data define an element, like <name>Smith</name>

  8. XML – elements <family> <name>Smith</name> <members>4</members> </family> Elements can be nested, like <name> and <members> inside <family>

  9. XML – elements Unlike HTML, XML does not enforce a predefined set of element names (such as “body”, “h1”, and “p”): other can be made up to suit the particular needs of data. <family> <name>Smith</name> <members>4</members> </family>

  10. XML – elements Some elements can contain additional information within the start-tag: attributes. <TelephoneNumber prefix="+41">   22 76-74739 </TelephoneNumber>

  11. XML – namespace Nested elements can clash. <widget type="gadget"> <head size="medium"/> <big> <subwidget ref="gizmo"/> </big> <info> <head> <title>Description of gadget</title> </head> <body> <h1>Gadget</h1> A gadget contains a big gizmo </body> </info> </widget> • In this example, a widget language uses XHTML as sub-language • Which language do <big> and <head> belong to?

  12. XML – namespace Solution: qualify names using namespaces. <widget xmlns="http://www.widget.org" xmlns:xhtml="http://www.w3.org/TR/xhtml1" type="gadget"> <head size="medium"/> <big> <subwidget ref="gizmo"/> </big> <info> <xhtml:head> <xhtml:title>Description of gadget</xhtml:title> </xhtml:head> <xhtml:body> <xhtml:h1>Gadget</xhtml:h1> A gadget contains a big gizmo </xhtml:body> </info> </widget>

  13. XML – schema • XML schema enable defining types. • All data types are either primitive or derived. • A primitive data type cannot be expressed in terms of any other data type. • All primitive data types are atomic. • It is possible to derive data types either by extension or by restriction.

  14. XML – schema • Example of type definition: <xs:simpleType name="CERN_Internal_Phone_Number"> <xs:restriction base="xs:string"> <xs:pattern value="\d{2}-\d{5}"/> </xs:restriction> </xs:simpleType>

  15. 2SOAP What is SOAP

  16. SOAP • Which is the correct encoding? <phoneNumber> +41 (22) 76-74739 </phoneNumber> <phoneNumber> <Prefix>41</Prefix> <areaCode>22</areaCode> <exchange>76</exchange> <number>74739</number> </phoneNumber> • Whatever the application is expecting! <phone prefix="+41" area="22"> <exchange>76</exchange> <number>74739</number> </phone>

  17. SOAP • Simply saying that server and client are using XML to exchange information is not enough. We need to define: • the types of information we are exchanging; • how that information is to be expressed as XML; • how to actually go about sending that information. <phoneNumber> +41 (22) 76-74739 </phoneNumber> <phoneNumber> <Prefix>41</Prefix> <areaCode>22</areaCode> <exchange>76</exchange> <number>74739</number> </phoneNumber> <phone prefix="+41" area="22"> <exchange>76</exchange> <number>74739</number> </phone>

  18. SOAP • SOAP is a lightweight protocol for information exchange in decentralized, distributed environments. • It is an XML-based protocol that consists of three parts: • an envelope that defines a framework for describing what is in a message and how to process it; • a set of encoding rules for expressing instances of application-defined data types; • a convention for representing remote procedure calls and responses.

  19. SOAP message SOAP message • A request from a client, or a response from a server is officially referred to as a “SOAP message”. • Every SOAP message has: • a mandatory Envelope element, • an optional Header element, • a mandatory Body element. Envelope (required) Header (optional) Body (required) Fault (optional)

  20. SOAP message – Envelope SOAP message • Every SOAP message has a root Envelope element. • SOAP uses XML namespaces to differentiate versions. The version must be referenced within the Envelope element. Envelope (required) Header (optional) Body (required) Fault (optional)

  21. SOAP message – Header SOAP message • The optional Header element offers a flexible framework for specifying additional application-level requirements. • For example, the Header element can be used to specify a digital signature for password-protected services. • Many current SOAP services do not utilize the Header element Envelope (required) Header (optional) Body (required) Fault (optional)

  22. SOAP message – Body SOAP message • The Body element is mandatory for all SOAP messages. • Typical uses of the Body element include RPC requests and responses. Envelope (required) Header (optional) Body (required) Fault (optional)

  23. SOAP message – Fault SOAP message • In the event of an error, the Body element will include a Fault element. • The Fault subelements include the • faultCode, • faultString, • faultActor, • detail. Envelope (required) Header (optional) Body (required) Fault (optional)

  24. SOAP encoding • SOAP includes a built-in set of rules for encoding data types. • SOAP data types are divided into two broad categories: • scalar types, containing exactly one value, • compound types, contain multiple values. Compound types are further subdivided into • Arrays, containing multiple values, specified by an ordinal position. • Structs, also containing multiple values, specified by an accessor name.

  25. SOAP encoding • The SOAP specification adopts the XML Schema conventions. • The encoding style for a particular set of XML elements is defined through the use of the encodingStyle attribute. • For example, the value for SOAP 1.1 encoding is: http://schemas.xmlsoap.org/soap/encoding/

  26. SOAP for RPCs • At the moment, RPC is the most common application of SOAP.

  27. SOAP for RPCs - request • The rules for packaging an RPC request in a SOAP envelope are simple: • The method call is represented as a single structure with each in or in-out parameter modeled as a field in that structure. • The names and physical order of the parameters must correspond to the names and physical order of the parameters in the method being invoked. String division( String name, String FamilyName ); result = division("John", "Smith") <SOAP:Envelope xmlns:SOAP="..."> <SOAP:Body> <division SOAP:encodingStyle="…"> <name xsi:type="string"> John </name> <FamilyName xsi:type="string"> Smith </FamilyName> </division> </SOAP:Body> </SOAP:Envelope>

  28. SOAP for RPCs - response • The structure of the response is modeled as a single structure with a field for each in-out or out parameter in the method signature. • The name of the message response structure (divisionResponse) element is not important, but the convention is to name it after the method, with “Response” appended. • The name of the return element is arbitrary: the first field in the message response structure is assumed to be the return value. "IT - Information Technologies Division" <SOAP:Envelope xmlns:SOAP="..."> <SOAP:Body> <divisionResponse SOAP:encodingStyle="..."> <return xsi:type="xsd:string"> IT – Information Technologies Division </return> </divisionResponse> </SOAP:Body> </SOAP:Envelope>

  29. 3WSDL What is WSDL

  30. WSDL • As the number of communication formats and protocols used on the Internet continues to increase, finding a standard way of describing how two machines should communicate with one another has become increasingly important. • WSDL describes what a service does, how to invoke its operations, and where to find it. • Everything defined within a WSDL file is abstract: it’s just the definition of parameters and constraints for how communication should occur at runtime.

  31. WSDL • The web service implementation has to adhere to the guidelines defined in the WSDL. • WSDL also provides the ability to define a binding that attaches an abstract set of message definitions to a concrete protocol or data format. • WSDL is a specification defining how to describe web services in a common XML grammar. • Using WSDL, a client can locate a web service and invoke any of its publicly available functions. With WSDL-aware tools this process can also be automated.

  32. WSDL anatomy <definitions> root of WSDL element • The specification itself is divided into: • definitions, • types, • message, • portType, • binding, • service. • In addition to the six major elements, the WSDL specification also defines two utility elements: • Documentation, • import. <types> what data type will be trasmitted <message> what message will be trasmitted <portType> what operations (functions, methods) will be supported <binding> how the messages will be transmitted on the wire <service> where the service is located

  33. WSDL anatomy:definitions <definitions> root of WSDL element • The “definitions” element must be the root element of all WSDL documents. • It: • defines the name of the web service, • declares multiple namespaces used throughout the remainder of the document, • contains all the other service elements. <types> what data type will be trasmitted <message> what message will be trasmitted <portType> what operations (functions, methods) will be supported <binding> how the messages will be transmitted on the wire <service> where the service is located

  34. WSDL anatomy:types <definitions> root of WSDL element • The “types” element describes all the data types used between the client and server. • WSDL is not tied exclusively to a specific typing system, but it uses the XML Schema specification as its default choice. • If the service uses only XML Schema built-in simple types, such as strings and integers, the “types” element is not required <types> what data type will be trasmitted <message> what message will be trasmitted <portType> what operations (functions, methods) will be supported <binding> how the messages will be transmitted on the wire <service> where the service is located

  35. WSDL anatomy:message <definitions> root of WSDL element • The “message” element describes a one-way message, whether it is a single message request or a single message response. • It defines the name of the message and contains zero or more message part elements, which can refer to message parameters or message return values. <types> what data type will be trasmitted <message> what message will be trasmitted <portType> what operations (functions, methods) will be supported <binding> how the messages will be transmitted on the wire <service> where the service is located

  36. WSDL anatomy:portType <definitions> root of WSDL element • The “portType” element combines multiple message elements to form a complete oneway or round-trip operation. • For example, a portType can combine one request and one response message into a single request/response operation, most commonly used in SOAP services. • Note that a “portType” can (and frequently does) define multiple operations. <types> what data type will be trasmitted <message> what message will be trasmitted <portType> what operations (functions, methods) will be supported <binding> how the messages will be transmitted on the wire <service> where the service is located

  37. WSDL anatomy:binding <definitions> root of WSDL element • The “binding” element describes the concrete specifics of how the service will be implemented on the wire. • WSDL includes built-in extensions for defining SOAP services. • SOAP-specific information therefore goes here. <types> what data type will be trasmitted <message> what message will be trasmitted <portType> what operations (functions, methods) will be supported <binding> how the messages will be transmitted on the wire <service> where the service is located

  38. WSDL anatomy:service <definitions> root of WSDL element • The “service” element defines the address for invoking the specified service. • Most commonly, this includes a URL for invoking the SOAP service. <types> what data type will be trasmitted <message> what message will be trasmitted <portType> what operations (functions, methods) will be supported <binding> how the messages will be transmitted on the wire <service> where the service is located

  39. WSDL anatomy:documentation and import • The “documentation” element is used to provide human-readable documentation and can be included inside any other WSDL element. • The “import” element is used to import other WSDL documents or XML Schemas. • This enables more modular WSDL documents. For example, two WSDL documents can import the same basic elements and yet include their own service elements to make the same service available at two physical addresses.

  40. WSDL anatomy - example definitions <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.MyEchoService.com/echo.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyEchoService.com/echo.wsdl" name="EchoService"> <message name="EchoRequest"> <part name="sentence" type="xsd:string"/> </message> <message name="EchoResponse"> <part name="echoed_sentence" type="xsd:string"/> </message> <portType name="Echo_PortType"> <operation name="Echo"> <input message="tns:EchoRequest"/> <output message="tns:EchoResponse"/> </operation> </portType> <binding name="Echo_Binding" type="tns:Echo_PortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="Echo"> <soap:operation soapAction="JustEcho"/> <input> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </input> <output> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </output> </operation> </binding> <service name="Echo_Service"> <documentation>WSDL File for EchoService</documentation> <port name="Echo_Port" binding="tns:Echo_Binding"> <soap:address location="http://localhost:8080/echo"/> </port> </service> </definitions>

  41. WSDL anatomy - example <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.MyEchoService.com/echo.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyEchoService.com/echo.wsdl" name="EchoService"> <message name="EchoRequest"> <part name="sentence" type="xsd:string"/> </message> <message name="EchoResponse"> <part name="echoed_sentence" type="xsd:string"/> </message> <portType name="Echo_PortType"> <operation name="Echo"> <input message="tns:EchoRequest"/> <output message="tns:EchoResponse"/> </operation> </portType> <binding name="Echo_Binding" type="tns:Echo_PortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="Echo"> <soap:operation soapAction="JustEcho"/> <input> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </input> <output> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </output> </operation> </binding> <service name="Echo_Service"> <documentation>WSDL File for EchoService</documentation> <port name="Echo_Port" binding="tns:Echo_Binding"> <soap:address location="http://localhost:8080/echo"/> </port> </service> </definitions> message

  42. WSDL anatomy - example portType <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.MyEchoService.com/echo.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyEchoService.com/echo.wsdl" name="EchoService"> <message name="EchoRequest"> <part name="sentence" type="xsd:string"/> </message> <message name="EchoResponse"> <part name="echoed_sentence" type="xsd:string"/> </message> <portType name="Echo_PortType"> <operation name="Echo"> <input message="tns:EchoRequest"/> <output message="tns:EchoResponse"/> </operation> </portType> <binding name="Echo_Binding" type="tns:Echo_PortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="Echo"> <soap:operation soapAction="JustEcho"/> <input> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </input> <output> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </output> </operation> </binding> <service name="Echo_Service"> <documentation>WSDL File for EchoService</documentation> <port name="Echo_Port" binding="tns:Echo_Binding"> <soap:address location="http://localhost:8080/echo"/> </port> </service> </definitions>

  43. WSDL anatomy - example binding <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.MyEchoService.com/echo.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyEchoService.com/echo.wsdl" name="EchoService"> <message name="EchoRequest"> <part name="sentence" type="xsd:string"/> </message> <message name="EchoResponse"> <part name="echoed_sentence" type="xsd:string"/> </message> <portType name="Echo_PortType"> <operation name="Echo"> <input message="tns:EchoRequest"/> <output message="tns:EchoResponse"/> </operation> </portType> <binding name="Echo_Binding" type="tns:Echo_PortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="Echo"> <soap:operation soapAction="JustEcho"/> <input> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </input> <output> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </output> </operation> </binding> <service name="Echo_Service"> <documentation>WSDL File for EchoService</documentation> <port name="Echo_Port" binding="tns:Echo_Binding"> <soap:address location="http://localhost:8080/echo"/> </port> </service> </definitions>

  44. WSDL anatomy - example service <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.MyEchoService.com/echo.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyEchoService.com/echo.wsdl" name="EchoService"> <message name="EchoRequest"> <part name="sentence" type="xsd:string"/> </message> <message name="EchoResponse"> <part name="echoed_sentence" type="xsd:string"/> </message> <portType name="Echo_PortType"> <operation name="Echo"> <input message="tns:EchoRequest"/> <output message="tns:EchoResponse"/> </operation> </portType> <binding name="Echo_Binding" type="tns:Echo_PortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="Echo"> <soap:operation soapAction="JustEcho"/> <input> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </input> <output> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </output> </operation> </binding> <service name="Echo_Service"> <documentation>WSDL File for EchoService</documentation> <port name="Echo_Port" binding="tns:Echo_Binding"> <soap:address location="http://localhost:8080/echo"/> </port> </service> </definitions>

  45. WSDL anatomy - example service <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.MyEchoService.com/echo.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyEchoService.com/echo.wsdl" name="EchoService"> <message name="EchoRequest"> <part name="sentence" type="xsd:string"/> </message> <message name="EchoResponse"> <part name="echoed_sentence" type="xsd:string"/> </message> <portType name="Echo_PortType"> <operation name="Echo"> <input message="tns:EchoRequest"/> <output message="tns:EchoResponse"/> </operation> </portType> <binding name="Echo_Binding" type="tns:Echo_PortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="Echo"> <soap:operation soapAction="JustEcho"/> <input> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </input> <output> <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:echoservice"/> </output> </operation> </binding> <service name="Echo_Service"> <documentation>WSDL File for EchoService</documentation> <port name="Echo_Port" binding="tns:Echo_Binding"> <soap:address location="http://localhost:8080/echo"/> </port> </service> </definitions> documentation

  46. 4Using SOAP::Lite How to implement a simple SOAP client/server application using Perl

  47. Using SOAP::Lite • In this example we show how to use SOAP::Lite for Perl to create a simple client/server application able to exchange complex data structure using SOAP. Note: • For space reason, the following lines have been omitted from the sample code: use strict; use warnings; use diagnostics; • perl is always executed with the -w option.

  48. Using SOAP::Lite • The service we are going to implement receives as parameters: • An array of strings, • An array of dates. • It then returns: • The address of a family, • An array of data representing the information of each component.

  49. Note the use of square brackets “[” and “]” to instantiate the parameters as references to arrays Using SOAP::Lite • Client: The client instantiate the parameters $par0 and $par1, then contacts the server and invokes the remote method “search”. #!perl use Service; use family; my $par0= [ "Tom", "Luis", ]; my $par1= [ "1955-04-26T13:31:23Z", "1956-04-26T13:31:23Z", "1957-04-26T13:31:23Z", ]; print "Contacting server...\n"; my $res= SOAP::Lite -> uri('myURI') -> proxy("http://127.0.0.1:8082"); $res=$res -> search( $par0, $par1 ); my $result= $res -> result;

  50. Using SOAP::Lite • Server: The server dispatches the call to the “Service” object #!perl use Service; use SOAP::Lite; use SOAP::Transport::HTTP; my $server = SOAP::Transport::HTTP::Daemon -> new( LocalAddr => 'localhost', LocalPort => 8082, Reuse => 1) ->dispatch_with({'myURI' => 'Service' }); print "Contact to SOAP server at ", $server->url, "\n"; $server ->handle or die("Couldn't establish listening socket for SOAP server");

More Related