Академический Документы
Профессиональный Документы
Культура Документы
Web services,
JAX-RPC,
WSDL,
XML Schema,
and SOAP.
1
Web service
A Web service is a software system
designed to support interoperable
machine-to-machine interaction over a
network.
It has an interface described in a
machine-processable format (specifically
WSDL).
Other systems interact with the Web
service in a manner prescribed by its
description using SOAP messages.
2
Definition
A web service is a collection of open protocols and
standards used for exchanging data between
applications or systems.
Ex : ATM machine
3
Web service..
A web service is a piece of business
logic, located somewhere on the Internet,
that is accessible through standard-based
Internet protocols such as HTTP or
SMTP.
Web Services allows to expose the
functionality of existing code over the
network. Once it is exposed on the
network, other application can use the
functionality of program
4
Benefits or characteristics of Web
Services:
Exposing the function on to network:
A Web service is a unit of managed code that can be remotely invoked
using HTTP, that is, it can be activated using HTTP requests. So,
Web Services allows you to expose the functionality of your existing
code over the network. Once it is exposed on the network, other
application can use the functionality of your program.
Connecting Different Applications:
Web Services allows different applications to talk to each other and
share data and services among themselves. Other applications can
also use the services of the web services. For example VB or .NET
application can talk to java web services and vice versa. So, Web
services is used to make the application platform and technology
independent.
Standardized Protocol:
Web Services uses standardized industry standard protocol for the
communication. All the four layers (Service Transport, XML
Messaging, Service Description and Service Discovery layers) uses
the well defined protocol in the Web Services protocol stack. This
standardization of protocol stack gives the business many advantages
like wide range of choices, reduction in the cost due to competition
and increase in the quality.
5
Benefits or characteristics of Web
Services:
Web Services Sharing:
These days due to complexness of the business, organizations
are using different technologies like EAI, EDI, B2B, Portals etc.
for distributing computing. Web Services supports all these
technologies, thus helping the business to use existing
investments in other technologies.
Web Services are Self Describing:
Web Services are self describing applications, which reduces
the software development time. This helps the other business
partners to quickly develop application and start doing
business. This helps business to save time and money by
cutting development time.
Automatic Discovery:
Web Services automatic discovery mechanism helps the
business to easy find the Service Providers. This also helps
your customer to find your services easily. With the help of Web
Services your business can also increase revenue by exposing
their own Web Services available to others.
6
Components of Web Services?
7
Web Services
Web Services = WSDL + SOAP + UDDI
What is SOAP?
SOAP is an XML-based protocol to let
applications exchange information over HTTP.
SOAP is a protocol for accessing a Web Service.
SOAP stands for Simple Object Access Protocol
SOAP is a communication protocol
SOAP is a format for sending messages
SOAP is designed to communicate via Internet
SOAP is platform independent
SOAP is language independent
SOAP is based on XML
8
Web Services..
What is WSDL?
WSDL is an XML-based language for
locating and describing Web services.
WSDL stands for Web Services Definition
Language
WSDL is based on XML
WSDL is used to describe Web services
WSDL is used to locate Web services
9
Web Services..
What is UDDI?
UDDI is a directory service where companies
can register and search for Web services.
UDDI stands for Universal Description, Discovery
and Integration
UDDI is a directory for storing information about web
services
UDDI is a directory of web service interfaces
described by WSDL
UDDI communicates via SOAP
UDDI is built into the Microsoft .NET platform
10
Web Services Architecture
11
Web Service Roles
There are three major roles within the web
service architecture:
Service provider: This is the provider of the web
service. The service provider implements the service
and makes it available on the Internet.
14
XML Messaging Using SOAP
15
NDFD/java interface
package NDFD;
public interface NdfdXMLPorttype extends Remote{
Public String NDFDgen(BigDecimal
latitude,BigDecimal longitude)throws RemoteException;
}
16
Accessing Java web service
// create an object representing the web service
NdfdXMLPorttype ndfdws=new
NdfdXML_Impl().getNdfdXMLPort()
SOAP Envelope
SOAP Head
SOAP Body
18
HTTP request embedded SOAPdocument
POST/forcast/XML/SOAP_server/NdfdXMLPorttype http/1.1
Content type:txt/html:charset=utf-8
</SOAP-ENV:Body>
</SOAP-ENV:Envelope> 19
HTTP response embedded SOAPdocument
http/1.1 200 ok server:apache/2.0.46 (Red hat)
22
9.2 writing a Java web service
Currency conversion service
To perform conversion between different
currencies.
The web service will return the equivalent
amount of other two currencies
The web service provide 3 operations
Fromdollers,fromeuros,fromyens
23
Writing server software
To built the web service using 2 tools
Wscompile and wsdeploy
The webservice consists of an interface and a
class to implements this interface.
After writing this code,we run wscompile to
convert java files into WSDL document.
Then run wsdeploy to package webservice.
Working directories
currencyConverter
WEB-INF
Src
Mycurcon
classes
24
The starting point of web service is writing
java interface which provide the
operations. This is known as service end
point interface.
Basic rules for service end point interface
is
The interface must extend the
java.rmi.Remote interface
Every method in the interface must throws
RemoteExeption
The interface must not contain any public
static final declarations
25
Class exchangevalues
The class exchangevalues representing the
return value from operations of the currency
converter.
Package mycurCon
Public class exchangevalues{
public double dollars;
Public double euros;
Public double yen;
}
26
Service end point interface
Package mycurCon
Public interface curcon extends java.rmi.Remote{
public exchangevalues fromdollars(double dollars)
throws RemoteException
public exchangevalues fromEuros(double euros)
throws RemoteException;
public exchangevalues fromyen(double yen) throws
RemoteException
}
27
implementation
Public exchangevalues fromdollars(double
dollars) throws RemoteException
{
exchangevalues Ev=new exchangevalues();
Ev.dollars=dollars;
Ev.euros=dollars*dollars2euro;
Ev.yen=dollars * dollars2yen;
return Ev;
}
28
Packaging server software
Java d classes src/mycurCon/*.java
29
config.xml
The config.xml file consists of a series of XML
elements. The Domain element is the top-level element,
and all elements in the Domain are children of the
Domain element.
The Domain element includes child elements, such as
</ configuration>
The purpose of the config.xml file is to provide configuration data to each of the
blocks that require configuration data.
31
Another XML configuration file jaxrpc-ri.xml
<?xml version='1.0' encoding='UTF-8'?>
<webservices
xmlns=http://java.sun.com/xml/ns/jax-rpc/ri/dd
version=1.0
targetnamespaceBase=http://temp-uri-org/wsdl
typenamespaceBase= http://emp-uri-org/types
urlpatternBase=/converter >
<endpoint
name=currencyconverter
displayName=currency Converter
description=converts between dollars,euros and yen
interface name =mycurCon.curcon
Model=WEB-INF/model.xml.gz
Implementation=mycurCon.curconimpl />
<endpointMapping
endpointName=currencyconverter
urlpattern=/currency />
</webservices>
<web-app
xmlns=http://java.sun.com/xml/ns/j2ee
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemalocation=http://java.sun.com/xml/ns/j2ee/web-app-
2_4.xsd
version=2.4 >
<displayName> Historic currency converter</displayName>
<description>
----
</description>
33
wsdeploy
Jar cf converter-temp.war WEB-INF
34
9.3 writing a java web service client
XML document for wscompile
</ configuration>
Package mycurconclient
Public interface historiccurrencyconverter extends rpc.Service
{
public mycurconlient.curcon.getcurconport();
}
36
Package mycurConclient
Public interface curcon extends java.rmi.Remote{
Package mycurCon
Public class exchangevalues{
protected double dollars;
protected double euros;
protected double yen;
Public double getdollars() {
return dollars; }
ev.curcon.fromeuros(value); }
else if(currency.equals(yen)) {
ev.curcon.fromeyen(value);
else {
38
ev.curcon.fromedollars(value); } return ev; }
Wsdl-1st set
<?xml version='1.0' encoding='UTF-8'>
<definitions
name="historiccurrencyconverter "
targetnamespace=http://tempuri-org/wsdl
xmlns:tns="http://tempuri-org/wsdl
xmlns:ns2=" http://tempuri-org/wsdl/types "
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" >
39
Wsdl- 2 nd set
<types>
<xsd:schema>
targetnamespace=http://tempuri-org/types
xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/
xmlns="http://www.w3.org/2001/XMLSchema"
<xsd:import namespace="http:// historiccurrencyconverter/"
schemaLocation="http://localhost:8080/ currencyconverterWebService/
currencyconverterWebServiceService?xsd=1" />
<complextype name =exchangevalue>
<sequence>
<element name=dollars type=double />
<element name=euros type=double />
<element name=yen type=double />
</sequence>
</complextype>
</xsd:schema>
</types>
Types element define the data type that can be used as a input parameter or
return value of wsdl document
40
Wsdl- 3 rd set
<message name=" curcon_fromdollars ">
<part name="double1 " type="xsd:double " />
</message>
42
Wsdl-5 th set
<binding name="curconBinding" type="tns:curcon">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name=" fromdollars ">
<soap:operation soapAction="" />
<input>
<soap:body
Encodingstyle= http://schemas.xmlsoap.org/soap/encoding
use="encoded" Namespace= http://tempuri-org/wsdl />
</input>
<output>
<soap:body
Encodingstyle= http://schemas.xmlsoap.org/soap/encoding
use="encoded" Namespace= http://tempuri-org/wsdl />
</output>
</operation>
<operation name=" fromeuros ">
<soap:operation soapAction="" />
<input>
<soap:body
Encodingstyle= http://schemas.xmlsoap.org/soap/encoding
use="encoded" Namespace= http://tempuri-org/wsdl />
</input>
<output>
<soap:body
Encodingstyle= http://schemas.xmlsoap.org/soap/encoding
use="encoded" Namespace= http://tempuri-org/wsdl />
</output> 43
</operation>
</binding>
Final set
<service name=" historiccurrencyconverter ">
<port name="curconPort" binding="tns:curconBinding">
<soap:address location="http://localhost:8080/
currencyconverterWebService / currencyconverterWebService
Service" />
</port>
</service>
</definitions>
44
XML Schemas
Schemas is a general term--DTDs are a form of XML
schemas
According to the dictionary, a schema is a
structured framework or plan
When we say XML Schemas, we usually mean the
W3C XML Schema Language
This is also known as XML Schema Definition
language, or XSD
DTDs, XML Schemas, and RELAX NG are all XML schema
languages
45
Why XML Schemas?
DTDs provide a very weak specification language
cant put any restrictions on text content
very little control over mixed content (text plus elements)
little control over ordering of elements
DTDs are written in a strange (non-XML) format
need separate parsers for DTDs and XML
The XML Schema Definition language solves these problems
XSD gives much more control over structure and content
XSD is written in XML
46
Referring to a schema
To refer to a DTD in an XML document, the reference
goes before the root element:
<?xml version="1.0"?>
<!DOCTYPE rootElement SYSTEM "url">
<rootElement> ... </rootElement>
To refer to an XML Schema in an XML document, the
reference goes in the root element:
<?xml version="1.0"?>
<rootElement
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
(The XML Schema Instance reference is required)
xsi:noNamespaceSchemaLocation="url.xsd">
(This is where your XML Schema definition can be
found)
... 47
</rootElement>
The XSD document
Since the XSD is written in XML
Except for the additions to the root element of
XML data document,
The file extension is .xsd
The root element is <schema>
The XSD starts like this:
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.rg/2001/XMLSchema">
48
<schema>
The <schema> element may have
attributes:
xmlns:xs="http://www.w3.org/2001/XMLS
chema"
This is necessary to specify where all our XSD
tags are defined
elementFormDefault="qualified"
This means that all XML elements must be
qualified
49
Simple and complex elements
A simple element is one that contains text and
nothing else
A simple element cannot have attributes
A simple element cannot contain other elements
A simple element cannot be empty
However, the text can be of many different types,
and may have various restrictions applied to it
If an element isnt simple, its complex
A complex element may have attributes
A complex element may be empty, or it may
contain text, other elements, or both text and other
elements
50
User defined simple type
A simple element is defined as
<xs:element name="name" type="type" />
where:
name is the name of the element
the most common values for type are
xs:boolean xs:integer
xs:date xs:string
xs:decimal xs:time
Other attributes a simple element may have:
default="default value" if no other value is specified
fixed="value" no other value may be
specified
51
Defining an attribute
Attributes themselves are always declared as simple types
An attribute is defined as
<xs:attribute name="name" type="type" />
where:
name and type are the same as for xs:element
Other attributes a simple element may have:
default="default value" if no other value is specified
fixed="value" no other value may be specified
use="optional" the attribute is not required
(default)
use="required" the attribute must be present
52
Restrictions, or facets
The general form for putting a restriction on a text value is:
<xs:element name="name"> (or xs:attribute)
<xs:restriction base="type">
... the restrictions ...
</xs:restriction>
</xs:element>
For example:
<xs:element name=priorityType">
<xs:restriction base="xs:integer">
<xs:minInclusive value=10">
<xs:maxInclusive value="140">
</xs:restriction>
</xs:element>
53
Restrictions on numbers
minInclusive -- number must be the given value
minExclusive -- number must be > the given value
maxInclusive -- number must be the given value
maxExclusive -- number must be < the given value
totalDigits -- number must have exactly value digits
fractionDigits -- number must have no more than
value digits after the decimal point
54
Restrictions on strings
length -- the string must contain exactly value characters
minLength -- the string must contain at least value characters
maxLength -- the string must contain no more than value characters
pattern -- the value is a regular expression that the string must match
whiteSpace -- not really a restriction--tells what to do with whitespace
value="preserve" Keep all whitespace
value="replace" Change all whitespace characters to spaces
value="collapse" Remove leading and trailing whitespace, and replace
all sequences of whitespace with a single space
55
Enumeration
An enumeration restricts the value to be one of a
fixed set of values
Example:
<xs:element name="season">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Spring"/>
<xs:enumeration value="Summer"/>
<xs:enumeration value="Autumn"/>
<xs:enumeration value="Fall"/>
<xs:enumeration value="Winter"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
56
User defined Complex Type
A complex element is defined as
<xs:element name="name">
<xs:complexType>
... information about the complex type...
</xs:complexType>
</xs:element>
Example:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:sequence> says that elements must occur in this order
Remember that attributes are always simple types
57
Global and local definitions
Elements declared at the top level of a <schema> are available
for use throughout the schema
Elements declared within a xs:complexType are local to that
type
Thus, in
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
the elements firstName and lastName are only locally declared
The order of declarations at the top level of a <schema> do not
specify the order in the XML data document 58
Declaration and use
So far weve been talking about how to declare types,
not how to use them
To use a type we have declared, use it as the value
of type="..."
Examples:
<xs:element name="student" type="person"/>
<xs:element name="professor" type="person"/>
Scope is important: you cannot use a type if is
local to some other type
59
xs:sequence
Weve already seen an example of a complex type
whose elements must occur in a specific order:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName"
type="xs:string" />
<xs:element name="lastName"
type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
60
xs:all
xs:all allows elements to appear in any order
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:element>
Despite the name, the members of an xs:all
group can occur once or not at all
You can use minOccurs="n" and maxOccurs="n" to
specify how many times an element may
occur (default value is 1)
In this context, n may only be 0 or 1 61
Referencing
Once you have defined an element or attribute (with
name="..."), you can refer to it with ref="..."
Example:
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstName"
type="xs:string" />
<xs:element name="lastName"
type="xs:string" />
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="student" ref="person">
Or just: <xs:element ref="person">
62
SOAP
SOAP is a simple, lightweight XML
protocol for exchanging structured and
typed information on the Web
63
SOAP Contains Four Parts:
An extensible envelope expressing (mandatory)
what features and services are represented in a message;
who should deal with them,
whether they are optional or mandatory.
A set of encoding rules for data (optional)
Exchange instances of application-defined data types and
directed graphs
Uniform model for serializing abstract data models that can not
directly be expressed in XML schema
A Convention for representation RPC (optional)
How to make calls and responses
A protocol binding to HTTP and HTTP-EF (optional)
64
Packaging Soap
HTTP Post
SOAP Envelope
SOAP Head
SOAP Body
65
SOAP Example in HTTP
POST /Accounts/Henrik HTTP/1.1 SOAP-HTTP Binding
Host: www.webservicebank.com HTTP Request
Content-Length: nnnn
Content-Type: text/xml; charset="utf-8"
SOAP Body
SOAPAction: "Some-URI" SOAP Header
SOAP Envelope
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"
SOAP:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP:Header>
<t:Transaction xmlns:t="some-URI" SOAP:mustUnderstand="1">
5
</t:Transaction>
</SOAP:Header>
<SOAP:Body>
<m:Deposit xmlns:m="Some-URI">
<m:amount>200</m:amount>
</m:Deposit>
</SOAP:Body>
66
</SOAP:Envelope>
Rpc representation
Style attribute of soap:binding element in wsdl
expected to communicate with clients in an rpc
style.
Soap:body indicated using an encoding
specified by soap1.1 to represent the RPC
The soap specified rpc calls is a modeled as a
form of data structure known as struct.(instance
of class)
From an xml point of view , the type of data
structure defined by a complex data type is a
struct.
67
Soap document sent by client to currency converter
<env:body>
<ns0:fromdollars>
<double1 xsi:type=xsd:double>1.0</double1>
</ns0:fromdollars>
</env:body>
</env:Envelope>
68
Soap document sent by web service to client
<?xml version='1.0' encoding='UTF-8'>
<env:Envelope
xmlns:env=http://schemas.xmlsoap.org/soap/envelope
xmlns:xsd=http://www.w3.org/2001/XMLSchema
xmlns:xsi=http://www.w3.org/2001/XMLSchema/instance
xmlns:enc=http://schemas.xmlsoap.org/soap/encoding
xmlns:ns0=http://tempuri-org/wsdl
xmlns:ns1=" http://tempuri-org/wsdl/types "
xmlns:encdingstyle=http://schemas.xmlsoap.org/soap/encoding />
<env:body>
<ans1:fromdollarsResponse
xmlns:ans1=http://tempuri.org/wsdl>
<result href=#id1/>
</ans1:fromdollarsResponse>
69
Soap encoding of structure data
Soap defines a simple but effective mechanism for
representing struct data, whether it be a struct
representing an RPC call or response or parameter or
return value.
<env:body>
<ans1:fromdollarsResponse
xmlns:ans1=http://tempuri.org/wsdl>
<result href=#id1/>
</ans1:fromdollarsResponse>
70
Soap encoding of array
<import
Namespace=http://schemas.xmlsoap.org/soap/en
coding//>
<complextype name =arrayofdouble>
<complexcontent>
<restriction base=soap11-enc:Array>
<attribute ref= soap11-enc:Arraytype
wsdl:arrayType=double[] />
<element name=yen type=double />
</ restriction >
</complexcontent>
</complextype>
71
Soap response document
<env:body>
<ans1:fromdollarsResponse
xmlns:ans1=http://tempuri.org/wsdl>
<result href=#id1/>
</ans1:fromdollarsResponse>
<ns0:exarrayofsouble id=#id1
xsi:type=enc:array
enc:arrayType=xsd:double[3]>