EraServer.NET ZipCodeResolver Web Service Test Page

stizzahaddockSoftware and s/w Development

Dec 14, 2013 (3 years and 8 months ago)

67 views

Distributed Systems, 10. Web Services


László Böszörményi

and Harald Kosch

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



M
ore 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 grai
ned 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

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


2

Basic Technology





WSDL (Web Services Descripti
on 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
























1.

Client queries registry to
locate service


2.

Registry refers to WSDL
document


3.

Client accesses WSDL


4.

WSDL provides data for
interaction w
ith WS


5.

Clients sends request to
WS via SOAP


6.

Web Service replies via
SOAP

Client


UDDI
Registry

WSDL
Document

Web
Service

1

2

3

4

5

6

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


3

Web Services Infra
structure











Web

Service








Web

Service

Client



Discovery


Request .v
sdisco

Return disco (XML)




Description


Request WSDL

Return WSDL (XML)



Protocol


Request .asmx

Return response (XML)

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


4

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 addres
s, 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, 980
52



Address......

City.........

State........




Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


5

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) vocabu
laries




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
Language

Web Se
rvices Technologies: SOAP, WSDL, UDDI

Horizontal XML Vocabularies: ebXML …

Core XML Processing: XML, XML Schema, XSLT

Web Framework: Internet Protocols; HTTP, TCP/IP


Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


6

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)


Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


7

Components of an XML Document




XML Processing Ins
truction



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 exte
rnal 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 Cus
tomerOrder

SYSTEM "http://www.myco.com/CustOrder.dtd">



Document Instance



This is
the

XML document instance; the “XML
-
ized” data


Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


8

Validation of an XML document




Syntax check, or checking for being “well
-
formed”



Check against basic XML syntax rules



Valid
ation



Check against Document Type Declaration or

against XML Schema




<!
--

XML Data
--
>

<a>


<b> Some </b>


<c> 100 </c>


<c> 101 </c>

</a>


<!
--

XML Data
--
>

<a>



<b> Some </b>


<b> Thing </b>

</a>


DTD


<!ELEMENT a (b+, c?) >

<!ELEMENT b (#PCDATA) >

<!ELEMENT c (#PCDATA) >

Not Valid!

Valid

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


9

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 declarati
on



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

Root Element

Element

Value

Attribute

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


10

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) >

<!EL
EMENT Address (#PCDATA) >

<!ATTLIST Address


AddrType ( billing | shipping | home ) “shipping” >

<!ELEMENT Orders (OrderNo, ProductNo+) >

<!ELEMENT OrderNo (#PCDATA) >

<!ELEMENT ProductNo (#PCDATA) > ]>

Custom
er

Person

Orders

CustomerOrder

FName

LName

Addres
s

Addres
s

Addres
s

Orders

Orders

OrderN
o

Product
No

Product
No

Product
No

OrderN
o

Product
No

Product
No

0 or more order

Regular expr.

1 or more addr.

Parsed char.
data = text

Default attribute

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


11

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 procedure
s of an XML parser to
access individual elements, values and attributes



The parser may also generate call
-
backs on tags (SAX)








Browser or

Application

XML
Parser

XML

Schema

Or

DTD

XML

Source

Document

Validated

XML

Document

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


12

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 n
ame = “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, re
curringDuration, 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 o
ther user
-
defined datatypes


Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


13

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=“C
ountry”

type=“string” />


</sequence>


<!
--

AddrType attribute not shown
--
>

</complexType

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


14

XSLT: Stylesheets & Transformations




Offers the ability to transform one XML document into
another



It is typically used to control the presentation of an XML
doc
ument, 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





XSLT

Processor

XSLT

Style
Sheet

Source

Schema

Target

Schema

XML

Source

Document

XML

Target

Document

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


15

Using XSLT to create a formatted table




10


100


10


200


20


501

<Orders >


<OrderNo> 10 </OrderNo>


<ProductNo> 100 </ProductNo>


<ProductNo> 200 <
/ProductNo>

</Orders >

<Orders >


<OrderNo> 20 </OrderNo>


<ProductNo> 501 </ProductNo>

</Orders >

<HTML>


<BODY>


<TABLE border = “3”>


<TR>


<TD> 10 </TD>


<TD> 100 </TD>


</TR>



<TR>


<TD> 10 </TD>


<TD> 200 </TD>


</TR>


<TR>


<TD> 20 </TD>


<TD> 501 </TD>


</TR>


</TABLE>


</BODY>

</HTML>

XSLT Processor

XSLT

Stylesheet

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


16

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>


<T
ABLE 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>


Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


17

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 framew
ork



“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 re
sources



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>

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


18

SOAP as RPC Protocol




Marshals procedure cal
ls 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 Exchan
ge Forms idea




Basis for BizTalk Framework




Interoperable and platform agnostic

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


19

The SOAP Protocol Layers



















XML Framework

SOAP Encoding

SOAP RPC

SOAP Messaging

Authenti
-

cation

Encryption

BTF2.0

Attachments

XML (Documents and Entities)

XML + Namespaces (Elements and Attributes)

XML Informat
ion Set (Structures as DOM tree)

XML Schema (Classes)

SOAP

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


20



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: MyInter
face#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>



Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


21




SOAP Encoding




Uniform type and encoding system for data in SOAP
Envelopes



Encoding is defined by XML sch
ema



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


Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


22


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 se
rvice




HttpURLConnection conn = (HttpURLConnection)url.openConnection();




conn.setRequestMethod("POST");




conn.setDoOutput(true); conn.setDoInput(true);




// Create SOAP request to call GetTime




conn.setRequestProperty("Content
-
type", "tex
t/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.getMessag
e());



} // try
-
catch


} // main

} // JavaClient

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


23

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>


Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


24

10.3. WSDL

(Web Service Description Language)




Is a ki
nd 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


Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


25



Example


<definitions name=“serviceName“>



<import namespace=“http://namespacePath“


location=“http://path/fileName.wsdl“>



<types>


<xsd:schema>


</ty
pes>



<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>

Distributed Systems, 10. Web Services


László Böszörm
ényi

and Harald Kosch


26

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 De
scription and Discovery Interface)




A search engine for WebServices




WebService
-
Programmable "Yellow Pages"




Advertise Sites and Services




May point to DISCO resources