10. Web Services
Web services are software programs that use XML to exchange information with other software via common internet protocols. They enable the programmable (not just interactive) Web.
· The most universal communication vehicle is still: TEXT
Ø More exactly: (semi-structured structured) text: XML
Basic goals
· Expose services to other processes
Ø Interconnection over internet or intranet
Ø Distribution and integration of application logic
Ø Component-like, reusable black boxes
Ø Loosely coupled, fine grained services
· Supported by many platforms; Java, the .NET Framework ...
Ø JAXP, ASP.NET Web Services
· Based on open standards
Ø HTTP, XML, and SOAP
· Overcome of some shortages of available solutions
Ø CORBA or DCOM scale wrong, because of tight coupling
Ø DCOM is limited to Windows
Ø Component based architecture required
§ CORBA is “remoting”, the component model is hardly implemented yet
§ EJB is restricted to Java
Ø ASPs provide typically large, coarse grained interfaces
Basic Technology
· WSDL (Web Services Description Language) – WHAT
· UDDI (Universal Description, Discovery and Integration) –WHERE
· SOAP (Simple Object Access Protocol) – HOW
Ø Soap messages are texts – in XML format
Ø Interoperability relies on XML – that’s all
Web Services Infrastructure
An Example
· ZipCode Resolver
Ø Accepts a (maybe partial) address as input and returns the postal code
Ø Methods
§ string FullZipCode(string accessCode, string address, string city, string state)
Given a valid accesscode, street address, city, and state, returns the ZIP code in NNNNN-NNNN format. If an error occurs, returns 00000-0000 instead. Use accessCode of '0' or '9999' for testing.
§ string ShortZipCode(string accessCode, string address, string city, string state)
Given a valid street address, city, and state, returns the ZIP code in NNNNN format. If an error occurs, returns 00000 instead.
§ . . .
EraServer.NET ZipCodeResolver Web Service Test Page
Testing .NET SOAP Proxy
.NET SOAP Return Value:
1 MICROSOFT WAY, REDMOND, WA, 98052-6399, 98052
Address......
City......
State......
The Web Services Technology Stack
· On the bottom level: well-known Internet and Web protocols
Ø TCP/IP and HTTP
· Core XML level
Ø XSL, DTD; XML Schema
· Horizontal XML Vocabularies
Ø Functionality across industry boundaries
Ø Developer and industry consortia create consensus and standards
Ø E.g. ebXML for e-business
· Web Services Technologies build on top of the core and horizontal (if available) vocabularies
· Vertical languages are XML-based processes for single application domains
· E.g. RosettaNet defines business processes for IT, electronics and semiconductor industries
Vertical Language / Vertical Language / Vertical Language / Vertical LanguageWeb Services Technologies: SOAP, WSDL, UDDI
Horizontal XML Vocabularies: ebXML …
Core XML Processing: XML, XML Schema, XSLT
Web Framework: Internet Protocols; HTTP, TCP/IP
10.1. XML (Extensible Markup Language)
· Language specification for describing data (meta language)
Ø Syntax rules
Ø Syntax & Grammar for creating Document Type Definitions
· Widely used and open standard
Ø Defined by the World Wide Web Consortium (W3C)
· Designed for describing and interchanging data
Ø Data is logically structured
Ø Human readable, writeable and understandable text file!
Ø Easy to Parse; Easy to Read; and Easy to Write!
· Metadata
Ø Data that describes data; data with semantics
· Looks like HTML…but it isn’t!
Ø Uses tags to delimit data and create structure
Ø HTML is designed
§ for presentation
§ with a fixed set of tags
Ø XML does not specify how to display the data
§ The tags can be used for any purpose, e.g. search
Ø XML has few predefined tags, it is extensible
Ø XML is case sensitive (HTML is not)
Components of an XML Document
· XML Processing Instruction
Ø Declares the document as an XML document
<?xml version = “1.0” encoding = “UTF-8”
standalone=”yes” ?>
Ø Version information
§ Currently always 1.0
Ø Encoding type
§ UTF-8, UTF-16, ISO-10646-UCS-2, etc
Ø Standalone declaration
§ Indicates if there are external file references
Ø Namespace declaration(s)
Ø Processing Instructions (for applications), etc
· Document Type Declaration (DTD) or XML Schema
Ø Internal DTD
<!DOCTYPE CustomerOrder [
<!-- internal DTD goes here! -->
]>
Ø External DTD reference
<!DOCTYPE CustomerOrder
SYSTEM "http://www.myco.com/CustOrder.dtd">
· Document Instance
Ø This is the XML document instance; the “XML-ized” data
Validation of an XML document
· Syntax check, or checking for being “well-formed”
Ø Check against basic XML syntax rules
· Validation
·
·
Ø Check against Document Type Declaration or
against XML Schema
Example customer order
<?xml version = “1.0” encoding = “UTF-8” ?>
<! DOCTYPE CustomerOrder
SYSTEM “http://www.myco.com/dtd/order.dtd” >
<CustomerOrder>
<Customer>
<Person>
<FName> Olaf </FName>
<LName> Smith </LName>
</Person>
<Address AddrType = “shipping”>
91 Park So, New York, NY 10018 </Address>
<Address AddrType = “billing”>
Hauptstrasse 55, D-81671 Munich </Address>
</Customer>
<Orders>
<OrderNo> 10 </OrderNo>
<ProductNo> 100 </ProductNo>
<ProductNo> 200 </ProductNo>
</Orders>
<!-- More <Customer>s ... -->
</CustomerOrder>
· Document Root Element
Ø Required if a document type declaration exists
Ø Must have the same name as the declaration
· Elements
Ø Can contain other elements and have attributes assigned to
Ø May or may not have a value
· Attributes
Ø Properties that are assigned to elements
Ø Provide additional element information
DTD: Defines document syntax and semantics
<?xml version = “1.0” encoding = “UTF-8” ?>
<!DOCTYPE CustomerOrder [
<!ELEMENT CustomerOrder (Customer, Orders*) >
<!ELEMENT Customer (Person, Address+) >
<!ELEMENT Person (FName, LName) >
<!ELEMENT FName (#PCDATA) >
<!ELEMENT LName (#PCDATA) >
<!ELEMENT Address (#PCDATA) >
<!ATTLIST Address
AddrType ( billing | shipping | home ) “shipping” >
<!ELEMENT Orders (OrderNo, ProductNo+) >
<!ELEMENT OrderNo (#PCDATA) >
<!ELEMENT ProductNo (#PCDATA) > ]>
XML parsing
· The parser checks the validity of the document with the help of the DTD or XML Schema document
· It usually builds up a DOM (Document Object Model) tree in main memory
Ø Elements and values are the nodes of the tree
Ø Attributes are values assigned to the nodes
· The application can use the procedures of an XML parser to access individual elements, values and attributes
· The parser may also generate call-backs on tags (SAX)
XML Schema
· Similar goal as a DTD, both are XML document definition languages
· XML Schema is written in XML
· Unlike DTD’s, XML Schema are Extensible – like XML!
· More verbose than DTD’s but easier to read & write
· Defining datatypes
Ø The simple or primitive datatypes
§ Based on (or derived) from the Schema datatypes
Ø Complex types
· Facets
Ø Constraining values, e.g.
<simpleType name = “FirstName”>
<restriction base = “string”>
<minLength value = “0” />
<maxLength value = “255” />
</restriction>
</ simpleType >
· Declaring data types
· Two kinds of datatypes: Built-in and User-defined
· Built-in
Ø Primitive Datatypes
§ string, double, recurringDuration, etc.
Ø Derived Datatypes
§ CDATA, integer, date, byte, etc
§ Derived from the primitive types
§ Example:
· integer is derived from double, time is derived from recurringDuration, CDATA is derived from string
Ø User-defined
§ Derived from built-in or other user-defined datatypes
Examples
<simpleType name = “FirstName” type = “string”/>
<complexType name= “Customer”>
<sequence>
<element name= “Person” type=“Name” />
<element name= “Address” type=“Address” /> </sequence>
</complexType>
<complexType name=“Address”>
<sequence>
<element name=“Street” type=“string” />
<element name=“City” type=“string” />
<element name=“State” type=“State_Region” />
<element name=“PostalCode” type=“string” />
<element name=“Country” type=“string” />
</sequence>
<!-- AddrType attribute not shown -->
</complexType
XSLT: Stylesheets & Transformations
· Offers the ability to transform one XML document into another
Ø It is typically used to control the presentation of an XML document, i.e. to generate an HTML document
Ø Can be used for any kind of transformation
· XSLT is an XML programming language
Ø Can have rules, evaluate conditions, etc
Using XSLT to create a formatted table
10 / 200
20 / 501
An example XSLT definition to create a formatted table
<?xml version='1.0'?>
<xsl:stylesheet
xmlns:xsl=
"http://www.w3.org/TR/2000/CR-xsl-20001121/">
<xsl:template match="/"> <!—Match the entire documet -->
<HTML>
<BODY>
<TABLE border = “3”>
<xsl:for-each select=“Customer/Orders/OrderNo">
<xsl:for-each select=“Customer/Orders/ProductNo">
<TR>
<TD> <xsl:value-of select=“OrderNo"/</TD
<TD> <xsl:value-of select=“ProductNo"/</TD
</TR>
</xsl:for-each>
<TR</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
<xsl:stylesheet>
10.2. SOAP (Simple Object Access Protocol)
"Lightweight mechanism for exchanging structured and typed information between peers using XML."
· Simple Object Access Protocol 1.1 (1.2 is coming)
Ø Object: any active entity that can accept and respond to messages – NOT in the usual oo sense
· Designed both for messaging and RPC
· Specifies three parts:
Ø SOAP envelope for message framework
§ “Envelops” XML “letters” (e.g. BizTalk standard)
Ø SOAP encoding for standard, interoperable marshaling
Ø SOAP RPC as remote procedure call convention on the top of HTTP
§ HTTP is a connectionless request/response mechanism
§ GET is the access protocol for resources
§ POST is the application protocol for interaction
§ Calls are directed to a URI
§ Headers specify options
§ Response codes indicate success/failure/status
· A simplified example
HTTP/1.1 POST /soap/mycomponent <!--RPC on HTTP -->
Content-Type: text/xml <!-- POST protocol+text/xml -->
SOAPAction: MyComponentMethod
<SOAP:Envelope>
<SOAP:Body>
<m:MyMethodArgumentsRoot>
<MyArgument>1</MyArgument>
</m:MyMethodArgumentsRoot>
</SOAP:Body>
</SOAP:Envelope>
SOAP as RPC Protocol
· Marshals procedure calls via XML
· Transport can be HTTP, SMTP, etc.
Ø Any text based protocol can transport SOAP data
Ø “Anyone” can implement it easily
Ø Designed for existing Internet infrastructure
· No expensive middleware required
· Default wire-protocol for .NET Remoting
Ø The .NET framework, JAXP etc. provide built-in support
SOAP as Messaging Protocol
· Descriptive container for any XML data exchange
· Transport can be SMTP, X.400, Queues etc.
· May rely on a message provider: in this case asynchronous
· Mail-like: Similar to Exchange Forms idea
· Basis for BizTalk Framework
· Interoperable and platform agnostic
The SOAP Protocol Layers
· SOAP Envelope
Ø Uniform container for XML messages
Ø Defined by XML Schema
§ http://schemas.xmlsoap.org/soap/envelope/
§ this name space must be referenced to
Ø SOAP:Header for handling instructions
Ø SOAP:Body for XML Payload
Ø Specifies order, cardinality for structural elements
Ø Defines SOAP:Fault for communication errors
HTTP/1.1 POST /soap/myservice
Content-Type: text/xml
SOAPAction: MyInterface#MyComponentMethod
<SOAP:Envelope>
<SOAP:Header> <!-- Header is optional -->
<m:myHeader xmlns:m=“myURI“
SOAP:mustUnderstand=“0“> HeaderInfo
</m:myHeader> <!-- Server may not understand it -->
</SOAP:Header>
<SOAP:Body>
<m:MyMethodArgumentsRoot xmlns:m=“myURI“ >
<MyArgument>1</MyArgument>
</m:MyMethodArgumentsRoot>
</SOAP:Body>
</SOAP:Envelope>
· SOAP Encoding
Ø Uniform type and encoding system for data in SOAP Envelopes
Ø Encoding is defined by XML schema
§ http://schemas.xmlsoap.org/soap/encoding/
Ø Usual options of format / encoding type
§ document / literal (usually default)
§ rpc / encoded
Ø Defines rich type system:
§ Simple Types, Enums
§ Compound Values, Structures and References
§ Arrays (incl. Sparse Arrays, Partial Arrays)
· SOAP RPC
Ø Interface/Service identified by URI
Ø Method invocation is modeled as structure
Ø Parameter order for [in/out] in response must be the same as in request.
Ø Behavior on error (return SOAP:Fault)
· Example for calling a Web Service from Java
import java.io.*; import java.net.*;
public class JavaClient { // Calls GetTime of TimeService
public static void main(String [] args) {
try {
URL url = new URL("http://localhost/time/TimeService.asmx");
// Build up HTTP connection to service
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true); conn.setDoInput(true);
// Create SOAP request to call GetTime
conn.setRequestProperty("Content-type", "text/xml;charset=utf-8");
conn.setRequestProperty("SOAPAction", "http://tempuri.org/GetTime");
String msg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap:Envelope" +
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
"<soap:body>\n" +
" <GetTime xmlns=\"http://tempuri.org/\" /> \n" +
"</soap:body>\n" +
"</soap:Envelope";
byte [] bytes = msg.getBytes();
conn.setRequestProperty("Content-length", String.valueOf(bytes.length));
OutputStream out = conn.getOutputStream();
out.write(bytes); // Send SOAP request
out.close();
BufferedReader in =
new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println("SOAP-Response:");
String inputLine = in.readLine(); // Take SOAP response
while (inputLine != null) {
System.out.println(inputLine);
inputLine = in.readLine();
} // while inputLine != null
in.close();
} catch (Exception e) {
System.out.println("error: " + e.getMessage());
} // try-catch
} // main
} // JavaClient
After execution we get the following result:
SOAP-Response:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap = “http://schemas.xmlsoap.org/soap/envelope/”
xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”
xmlns:xsd = “http://www.w3.org/2001/XMLSchema”>
<soap:Body>
<GetTimeResponse>
<GetTimeResult> 10:13:32 </GetTimeResult>
</GetTimeResponse>
</soap:Body>
</soap:Envelope>
10.3. WSDL
(Web Service Description Language)
· Is a kind of "TypeLib for SOAP"
· Usually automatically created
Ø Nevertheless, sometimes must be read by people
· Uniform representation of services
Ø Transport Protocol neutral
Ø Access Protocol neutral (not only SOAP)
· Describes:
Ø Schema for Data Types
Ø Call Signatures (Message)
Ø Interfaces (Port Types)
Ø Endpoint Mappings (Bindings)
Ø Endpoints (Services)
· Uses XML grammar, defining:
Ø Services and ports that communicate via messages
Ø Binding
§ Specify a protocol or a data format for a message or a port
§ Extensions for SOAP 1.1, HTTP GET/POST, and MIME
· Public description of a Web Service and its content
Ø WSDL contract
· Example
<definitions name=“serviceName“>
<import namespace=“http://namespacePath“
location=“http://path/fileName.wsdl“>
<types>
<xsd:schema>
</types>
<portType name=“serviceNamePortType“>
<operation name=“opName“>
<input message=“msgNameInput“ />
<output message=“msgNameOutput“ />
</operation>
</portType>
<binding name=“serviceNameSoapBinding“>
<soap:operation soapAction=“http://...“ />
</binding>
<service name=“serviceName“>
<port name=“serviceNamePort“ binding=“bindingName“>
<soap:address location="http://..." />
</port>
</service>
</definitions>
10.4. Web Service Discovery
DISCO
· Dynamic Discovery of Services by Microsoft
· Is a kind of “bookmark”
Ø Contains URIs to WSDL documents
· Easy discovery model for HTTP
1. Call endpoint with GET
2. Check whether this is a DISCO file or a redirect
3. DISCO file contains link to WSDL
4. If redirect goto 1
UDDI
(Universal Description and Discovery Interface)
· A search engine for WebServices
· WebService-Programmable "Yellow Pages"
· Advertise Sites and Services
· May point to DISCO resources
2
Distributed Systems, 10. Web Services László Böszörményi and Harald Kosch