Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 42

Web Services

M. L. Liu

05/05/23 Distributed Computing, M. L. Liu 1


Sources of Information
Programming Web Services with SOAP, by Snell
et al, O’Reilly
SOAP Tutorial,
http://www.w3schools.com/soap/default.asp
SoapRPC.com: Tutorials,
(http://www.soaprpc.com/tutorials/) A Busy
Developer’s Guide To Soap1.1
DaveNet : XML-RPC for Newbies,
http://davenet.userland.com/1998/07/14/xmlRpcF
orNewbies
SoapRPC.com: Other resources,
http://www.soaprpc.com/resources/
05/05/23 Distributed Computing, M. L. Liu 2
Web Services
Network services provided over HTTP –
“wired services”
It is promoted as a new way to build
network applications from distributed
components that are language- and
platform-independent
The technologies are still evolving
We are more interested in the concept and
principles, but we will look into one API
(Apache SOAP).
05/05/23 Distributed Computing, M. L. Liu 3
Web Services

we b s e rv ice

Th e we b
clie n t
(H TTP- ba s e d
we b s e rv ice
n e two rk )

we b s e rv ice

05/05/23 Distributed Computing, M. L. Liu 4


Web Service Software Components
A web service is a message-based network
service.
A server which provides a web service must
be capable of “sending and receiving
messages using some combination of
standard Internet protocols”

a pplica tio n s e rv ice s e rv ice s e rv ice


lo g ic pro x y lis te n e r re qu e s t

05/05/23 Distributed Computing, M. L. Liu 5


Just-in-time integration
Network services can be integrated
dynamically, on an as-needed basis.
SunMicro’s jini is a framework that
supports the idea.
Network services are registered with a
service registry; a service consumer/client
looks up the registry to fulfill its needs.
The binding of a client to the service can
occur at runtime.
05/05/23 Distributed Computing, M. L. Liu 6
Web service protocol stack
a pplica t io n a pplica tio n

s e rv ice dis co v e ry s e rv ice dis co v e ry

s e rv ice de s criptio n s e rv ice de s criptio n

m e s s a g in g m e s s a g in g

tra n s po rt tra n s po rt

n e two rk n e two rk

05/05/23 Distributed Computing, M. L. Liu 7


Web service protocols
a pplica tio n

s e rv ice dis co v e ry UD D I ( Un iv e rs a l D e s criptio n , D is co v e ry ,


a n d I n te g ra tio n )

W S D L ( W e b S e rv ice D e s cript io n L a n g u a g e )
s e rv ice de s cript io n

m e s s a g in g X M L , S O A P ( S im ple O bje ct A cce s s Pro t o co l)

TC P, H TTP, S M TP, J a bbe r


tra n s po rt

IP
n e two rk

05/05/23 Distributed Computing, M. L. Liu 8


SOAP
SOAP is a protocol which applies XML for
message exchange in support of remote
method calls over the Internet.
Compared to remote method invocation or
CORBA-based facilities:
– SOAP is web-based or “wired” and hence is not
subject to firewall restrictions
– Language-independent
– Can provide just-in-time service integration

05/05/23 Distributed Computing, M. L. Liu 9


Remote Procedure Call using HTTP

m e th o d n a m e ,
p a r a m e te r l i s t we b
s e rvi ce we b
obje ct s e rv e r r e t u r n va l u e
clie n t

H TTP re qu e s t
H TTP re s po n s e

05/05/23 Distributed Computing, M. L. Liu 10


SOAP Messages
S O A P e n v e lo pe

S O A P h e a de r

o p tion al
h e a de r block

h e a de r block

S O A P bo dy

req u ired
m e s s ag e bo dy

05/05/23 Distributed Computing, M. L. Liu 11


An XML-encoded SOAP RPC

< s o a p:En v e lo pe x m ln s :s o a p= 'h ttp://www.w3 .o rg /2 0 0 1 /1 0 /s o a p-e n v e lo pe '>


< s o a p:H e a de r>

< -- H e a de rs g o h e re -->

< /s o a p:H e a de r>


< s o a p:B o dy >

< -- R e qu e s t g o e s h e re -->

< /s o a p:B o dy >


< /s o a p:En v e lo pe >

S o u rce : h ttp:// www.x m l.co m /

05/05/23 Distributed Computing, M. L. Liu 12


Example of a SOAP message
< s o a p:En ve lo pe x m ln s :s o a p= 'h ttp://www.w3 .o rg /2 0 0 1 /1 0 /s o a p-e n v e lo pe '>
< s o a p:H e a de r>
< h :L o g x m ln s :h = 'h ttp://e x a m ple .o rg /cv s /lo g g in g '>
< tra ce > 4 < /tra ce >
< /h :L o g >
< h : PS e rv e r x m ln s :h = 'h ttp://e x a m ple .o rg /cv s /ps e rv e r'
s o a p: m u s tUn de rs ta n d= 'tru e ' >
< u s e rn a m e > a n o n cv s @ e x a m ple .o rg < /u s e rn a m e >
< pa s s wo rd> a n o n cv s < /pa s s wo rd>
< /h : PS e rv e r>
< /s o a p:H e a de r>
< s o a p:B o dy >
< m :C h e ck o u t x m ln s :m = 'h ttp://e x a m ple .o rg /cv s '>
< s o u rce > /x m l/s o a p/we bs e rv ice s /cv s < /s o u rce >
< re v is io n > 1 .1 < /re v is io n >
< de s tin a tio n > /e tc/u s r/m a rtin g /s o u rce /x m l< /de s tin a tio n >
< /m :C h e ck ou t>
< /s o a p:B o dy >
< /s o a p:En v e lo pe >
s o u rce : h ttp://www.x m l.co m

05/05/23 Distributed Computing, M. L. Liu 13


A SOAP Message that contains a remote
procedure call
source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">

<SOAP-ENV:Body>
       <m:getStateName xmlns:m="http://www.soapware.org/">
         <statenum xsi:type="xsd:int">41</statenum>
         </m:getStateName>
 </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

05/05/23 Distributed Computing, M. L. Liu 14


An Example SOAP Request
source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1

< ?xm l ve rs ion = " 1.0" ?>


< S O A P-ENV :En ve lope S O A P-ENV :e n codin gS tyle =" h ttp://sch e m as.xm lsoap.org /s oap/e n codin g/"
xm ln s :S O A P-ENC = " h ttp://sch e m as.xm lsoap.org/soap/e n codin g/"
xm ln s :S O A P-ENV = " h ttp://sch e m as.xm lsoap.org/soap/e n ve lope /"
xm ln s :xsd= " h ttp://www.w3.org/1999/X MLS ch e m a"
n am e of se rve r
proce du re xm ln s :xsi= " h ttp://www.w3.org/1999/X MLS ch e m a-in stan ce " >
n am e < S O A P-ENV :Body>
< m :ge tS tate Nam e xm ln s :m = " h ttp://www.soapware .org /" >
< state n u m xsi:type = " xsd:in t" >41</state n u m >
< /m :ge tS tate Nam e >
< /S O A P-ENV :B ody>
< /S O A P-ENV :En ve lope >

param e te r of type in t an d valu e 41

05/05/23 Distributed Computing, M. L. Liu 15


Response example 
source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1
<?xm l ve rsion="1.0"?>
<SO AP-ENV:Enve lope SO A P-ENV :
encodingStyle ="http://sche m as.xm lsoap.org/soap/e ncoding/"
xm lns:SO A P-ENC ="http://sche m as.xm lsoap.org/soap/e ncoding/"
xm lns:SO A P-ENV ="http://sche m as.xm lsoap.org/soap/e nvelope /"
proce dure nam e xm lns:xsd="http://www.w3.org/1999/X MLSchem a"
nam e of serve r
xm lns:xsi="http://www.w3.org/1999/X MLSche m a-instance">
<SO A P-ENV :Body>
<m :ge tState Nam eR e sponse xm lns:m ="http://www.soapware .org/">
<Re sult xsi:type ="xsd:string">South Dak ota</R e sult>
</m :ge tState Nam e R e sponse >
</SO A P-ENV :Body>
</SO AP-ENV :Enve lope >

returned value

05/05/23 Distributed Computing, M. L. Liu 16


Example of a SOAP message for
an error RPC response
source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1
< ? x m l v e rs io n = " 1 .0 " ? >
< S O A P-ENV :En v e lo pe S O A P- ENV :
e n co din g S ty le = " h t tp://s ch e m a s .x m ls o a p.o rg /s o a p/e n co din g /"
x m ln s :S O A P- ENV = " h t tp://s ch e m a s .x m ls o a p.o rg /s o a p/e n v e lo pe /"
x m ln s :x s d= " h tt p://www.w3 .o rg /1 9 9 9 /X M L S ch e m a "
x m ln s :x s i= " h t tp://www.w3 .o rg /1 9 9 9 /X M L S ch e m a -in s ta n ce " >
< S O A P- ENV :B o dy >
< S O A P- ENV :Fa u lt>
< fa u ltco de > S O A P- ENV :C lie n t < /fa u ltco de >
< fa u lts trin g >
C a n 't ca ll g e tS ta t e Na m e be ca u s e th e re a re
to o m a n y pa ra m e te rs .
< /fa u lts trin g >
< /S O A P- ENV :Fa u lt>
< /S O A P- ENV : B o dy >
< /S O A P- ENV :En v e lo pe >

05/05/23 Distributed Computing, M. L. Liu 17


HTTP and SOAP RPC Request
source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1

A SOAP message can be used to transport a


SOAP remote procedure request/response, as
follows:
POST /examples HTTP/1.1
User-Agent: Radio UserLand/7.0 (WinNT)
Host: localhost:81
Content-Type: text/xml; charset=utf-8
Content-length: 474
SOAPAction: "/examples"
<blank line>
<text for SOAP message>

05/05/23 Distributed Computing, M. L. Liu 18


An HTTP request that carries a SOAP RPC request
source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1
POST /examples HTTP/1.1
User-Agent: Radio UserLand/7.0 (WinNT)
Host: localhost:81
Content-Type: text/xml; charset=utf-8
Content-length: 474
SOAPAction: "/examples"

<?xml version="1.0"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/" xmlns:
SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:
SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
<SOAP-ENV:Body>
       <m:getStateName xmlns:m="http://www.soapware.org/">
         <statenum xsi:type="xsd:int">41</statenum>
         </m:getStateName>
 </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

05/05/23 Distributed Computing, M. L. Liu 19


An HTTP request that carries a SOAP RPC response
source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1

HTTP/1.1 200 OK
Connection: close
Content-Length: 499
Content-Type: text/xml; charset=utf-8
Date: Wed, 28 Mar 2001 05:05:04 GMT
Server: UserLand Frontier/7.0-WinNT

<?xml version="1.0"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
   <SOAP-ENV:Body>
      <m:getStateNameResponse xmlns:m="http://www.soapware.org/">
         <Result xsi:type="xsd:string">South Dakota</Result>
         </m:getStateNameResponse>
      </SOAP-ENV:Body>
   </SOAP-ENV:Envelope>

05/05/23 Distributed Computing, M. L. Liu 20


Error Example
source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1
HTTP/1.1 500 Server Error
Connection: close
Content-Length: 511
Content-Type: text/xml; charset=utf-8
Date: Wed, 28 Mar 2001 05:06:32 GMT
Server: UserLand Frontier/7.0-WinNT

<?xml version="1.0"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Client</faultcode>
         <faultstring>Can't call getStateName because there are too many
parameters.</faultstring>
         </SOAP-ENV:Fault>
      </SOAP-ENV:Body>
   </SOAP-ENV:Envelope>

05/05/23 Distributed Computing, M. L. Liu 21


SOAP Packages
source: http://www.soapuser.com

Apache SOAP for Java


Apache Axis for Java
Idoox WASP for C++
Microsoft SOAP Toolkit (part of the .net
framework)
SOAP::Lite for Perl

05/05/23 Distributed Computing, M. L. Liu 22


Apache SOAP
Allows clients and services to be written in
Java
Part of the Apache-XML project (
http://xml.apache.org/)
SOAP package downloadable:
http://xml.apache.org/dist/soap/
Installation instruction:

http://www.xmethods.com/gettingstarted/ap
ache.html
05/05/23 Distributed Computing, M. L. Liu 23
Apache SOAP installation
C:

s o ap % T O M C AT _ HO M E%

w eb ap p s
s o ap -2 _ 2

lib
s o ap s o ap .w ar

x er c es ,jar m ail.jar s o ap .jar ac tiv atio n .jar


W EB-I N F

c las s es

o n jav a

C alc S erv ic e.jav a


C alc S erv ic e.c las s
D ep lo y m en tD es c rip to r .x m l

05/05/23 Distributed Computing, M. L. Liu 24


Classpath setting
set CLASSPATH=
C:\soap\soap-2_2\lib\xerces.jar;
C:\jdk1.3\bin;
C:\jdk1.3\lib\tools.jar;
C:\soap\soap-2_2\lib\mail.jar;
C:\soap\soap-2_2\lib\soap.jar;
C:\soap\soap-2_2\lib\activation.jar;
C:\tomcat\lib\servlet.jar;
.;
05/05/23 Distributed Computing, M. L. Liu 25
Writing a Client Application using Apache SOAP
source: http://www.xmethods.com/gettingstarted/apache.html

Classpath
Your CLASSPATH environment variable should have both
the "soap.jar" and "xerces.jar" JAR files included.
Importing packages
For basic SOAP method invocation, you should import the
following at minimum:
// Required due to use of URL class , required by Call class
import java.net.*;

// Required due to use of Vector class


import java.util.*;

// Apache SOAP classes used by client


import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;

05/05/23 Distributed Computing, M. L. Liu 26


Ready-made SOAP Services
A number of SOAP ready-made services are
available at http://www.xmethods.com/
XMethods Service Weather Temperature
ID8
Service Owner:xmethods.net
Contact Email:support@xmethods.net
Service Description:Current temperature in a
given U.S. zipcode region.
SOAP Implementation:Apache SOAP

05/05/23 Distributed Computing, M. L. Liu 27


Sample SOAP service

Found at http://www.xmethods.com

W eather - T emperature
Analyze W S D L | View RP C P rofile | http://w w w .xm ethods.net/sd/2001/T em peratureS ervic e.w sdl
X M ethods ID 8
S ervic e O w ner: xm ethods.net
Contac t Em ail: support@ xm ethods.net
S ervic e Hom e P age:
D esc ription: Current tem perature in a given U.S . zipc ode region.
S O AP Im plem entation: Apac he S O AP

05/05/23 Distributed Computing, M. L. Liu 28


RPC Profile for Service

R PC Pro file fo r S e rv ice " W e a th e r - Te m pe ra tu re "

M e th o d Na m e g e tTe m p
En dpo in t UR L h ttp://s e rv ice s .x m e th o ds .n e t :8 0 /s o a p/s e rv le t /rpcro u te r
S O A PA ctio n
M e th o d Na m e s pa ce UR I u rn :x m e th ods -Te m pe ra tu re
I n pu t Pa ra m e te rs zip c o d e s t rin g

O u tpu t Pa ra m e t e rs r etu rn flo a t

See sample: TempClient.java


05/05/23 Distributed Computing, M. L. Liu 29
Client program samples
See samples in client folder:
– TempClient.java
– StockQuoteClient.java
– CurrencyClient.java

05/05/23 Distributed Computing, M. L. Liu 30


SOAP data types
SOAP Data Types,
http://www.sdc.iup.edu/outreach/spring200
2/webservices/datatypes.html
Uses XML Schema data types
Primitive Types
string, boolean, decimal, float, double,
duration, dateTime, time, date,
gYearMonth, gYear, gMonthDay, gDay,
gMonth, hexBinary, base64Binary, anyURI,
QName, NOTATION
05/05/23 Distributed Computing, M. L. Liu 31
SOAP data types
Derived Types
Simple types (derived from a single primitive
type)
* integer is derived from decimal
* int (-2147483648 <= int <= 2147483647) is
derived from long which is derived from integer
* 5-digit zip code can be derived from int
* may use regular expressions to specify derived
types, such as ([A-Z]){2,3}-\d{5}

05/05/23 Distributed Computing, M. L. Liu 32


Complex Type
Complex types (struct or array)
Struct example
<instructor>
<firstname xsi:type="xsd:string">Ed</firstname>
<lastname xsi:type="xsd:string">Donley</lastname>
</instructor>
Array example
<mathcourses xsi:type=
"SOAP-ENC:Array" SOAP ENC:arrayType="se:string[3]">
<se:string>10452C</se:string>
<se:string>10454C</se:string>
<se:string>11123T</se:string>
</mathcourses>
05/05/23 Distributed Computing, M. L. Liu 33
Creating Web Services (server-side
SOAP)
O'Reilly Network: Using SOAP with Tomcat [Feb
. 27, 2002]

http://www.onjava.com/pub/a/onjava/2002/02/27/t
omcat.htm
Apache SOAP allows you to create and deploy a
SOAP web service.
You must install some .jar files on your system
and set the CLASSPATH to them:
Algorithm:
1. Write a class for providing the service.
2. Create a deployment descriptor in XML.
3. Deploy the service with the service manager.
05/05/23 Distributed Computing, M. L. Liu 34
The Apache SOAP service
manager
The service manager is itself implemented as a SOAP
service.
To see what services are deployed on your system:
java org.apache.soap.server.ServiceManagerClient
http://localhost:8080/soap/servlet/rpcrouter list

To deploy a service:
java org.apache.soap.server.ServiceManagerClient
http://localhost:8080/soap/servlet/rpcrouter deploy
foo.xml

05/05/23 Distributed Computing, M. L. Liu 35


Creating a SOAP Service
O'Reilly Network: Using SOAP with Tomcat [Feb. 27, 2002]
http://www.onjava.com/pub/a/onjava/2002/02/27/tomcat.html
A SOAP service can be just about any Java
class that exposes public methods for
invocation. The class does not need to know
anything about SOAP, or even that it is
being executed as a SOAP service.
The method parameters of a SOAP service
must be serializable. The available types
that can be used as SOAP service
parameters are shown in (Listing 2) (shown
on next slide)
05/05/23 Distributed Computing, M. L. Liu 36
SOAP Service Parameter Types
All Java primitive types and their corresponding
wrapper classes
Java arrays
java.lang.String
java.util.Date
java.util.GregorianCalendar
java.util.Vector
java.util.Hashtable
java.util.Map

05/05/23 Distributed Computing, M. L. Liu 37


SOAP Service Parameter Types
java.math.BigDecimal
javax.mail.internet.MimeBodyPart
java.io.InputStream
javax.activation.DataSource
javax.activation.DataHandler
org.apache.soap.util.xml.QName
org.apache.soap.rpc.Parameter
java.lang.Object (must be a JavaBean)

05/05/23 Distributed Computing, M. L. Liu 38


Sample SOAP Service Implementations
See sample in SOAP folder:
TempService: A temperature service
Exchange: currency exchange service and
client

05/05/23 Distributed Computing, M. L. Liu 39


Sample SOAP Service Class
// A sample SOAP service class
// source: http://www.onjava.com/pub/a/onjava/2002/02/27/tomcat.html

package onjava;
public class CalcService {
public int add(int p1, int p2) {

return p1 + p2;
}

public int subtract(int p1, int p2) {

return p1 - p2;
}
}

05/05/23 Distributed Computing, M. L. Liu 40


Deployment Descriptor
<isd:service
xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="urn:onjavaserver">
<isd:provider type="java"
scope="Application"
methods="add subtract">
<isd:java class="onjava.CalcService"/>
</isd:provider>

<isd:faultListener>org.apache.soap.server.DOMFaultList
ener</isd:faultListener>
</isd:service>
05/05/23 Distributed Computing, M. L. Liu 41
Summary
SOAP is a protocol that makes use of HTTP
requests and responses to effect remote method
calls to web services.
A SOAP method call is encoded in XML and is
embedded in an HTTP request
The return value of a method call is likewise
embedded and encoded in an HTTP response
A number of SOAP APIs are available for
programming web services and client method
calls. The Apache API was introduced.

05/05/23 Distributed Computing, M. L. Liu 42

You might also like