Document 7453424

Download Report

Transcript Document 7453424

SOAP : Simple Object Access Protocol
Dr. Yuhong Yan
NRC-IIT-Fredericton
Internet logic
1
Service Oriented Architecture (SOA)/Web
Service triangle
UDDI
WSDL
SOAP
From “Web Services Architecture W3C Working Draft”
http://www.w3.org/TR/2002/WD-ws-arch-20021114/
2
Web Service Stack
Process
BPEL4WS, WSCI, WS-CDL
Discovery
Description
XML messaging
Transport
UDDI
WSDL
XML-RPC, SOAP, XML
HTTP, SMTP, FTP, BEEP
3
SOAP (Simple Object Access Protocol)
SOAP is a lightweight protocol for exchange of
information in a decentralized, distributed
environment. 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, and a convention for representing remote
procedure calls and responses.
4
Look into a SOAP message
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/
" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:getQuote
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/e
ncoding/" xmlns:ns1="urn:xmltoday-delayed-quotes">
<symbol xsi:type="xsd:string">XXX</symbol>
</ns1:getQuote>
</soapenv:Body>
</soapenv:Envelope>
5
Response SOAP message
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:getQuoteResponse
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns1="urn:xmltoday-delayed-quotes">
<ns1:getQuoteReturn href="#id0"/>
</ns1:getQuoteResponse>
<multiRef id="id0" soapenc:root="0"
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xsi:type="xsd:float"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">55.25</mul
tiRef>
</soapenv:Body>
</soapenv:Envelope>
6
Inside SOAP
SOAP message
Envelope (required)
Header (optional)
Body (required)
Fault (optional)
P53, figure 3-2
7
Why SOAP
• Inter-application communication between
systems written in arbitrary languages, across
the Internet.
• An XML-based protocol for exchanging
messages over Internet transport protocols, like
HTTP, SMTP, FTP, etc.
• SOAP is platform-independent.
8
Apache SOAP architecture
4
Service:
HelloService.java
sayHello(“Yuhong”, “Yan”)
SOAP client:
HelloClient.java
3
“Yuhong Yan, Welcome to SOAP…”
AXIS SOAP engine
rpcrouter servlet
1
SOAP request:
2
Jakarta Tomcat
Service name: urn:HelloWorld
Method name: sayHello
server
Parameter: firstName=“Yuhong”
Http POST
lastName=“Yan”
5
SOAP response:
Return value: “Yuhong Yan,
welcome to SOAP”
Http GET
9
p69,. Fig 4-3
Anatomy of HelloWorld
•
•
•
•
Server side code
Client side code
SOAP request
SOAP response
10
HelloWorldService.java
package samples.HelloWorld;
public class HelloWorldService
{
public String sayHello(String firstName, String lastName) throws Exception
{
String aString = firstName + “ " + lastName + ", welcome to SOAP Web Service
World!";
return aString;
}
public String addString(String symbol, String dataType) throws Exception
{
String aString = symbol + dataType;
return aString;
}
}
11
TestClient
public class TestClient
{
Parse the arg[ ] into options
public static void main(String args[])
(user,url,etc) and non-options
{
args.
try
{
Options opts = new Options( args );
args = opts.getRemainingArgs();
get non-options args.
12
TestClient.java (2)
Call invokes SOAP web services
The start
point
of access SOAP web services
Default
URL
"http://localhost:8080/axis/servlet/AxisServlet
Or You can use the SOAP endpoint as in WSDL
"http://localhost:8080/axis/services/urn:HelloWorld"
new
Service();
Service service =
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new
java.net.URL(opts.getURL())
); Service Method
Service Name
if( args[0].equals("1") )
call.setOperationName( new
QName("urn:HelloWorld", "sayHello") );
else
call.setOperationName( new
QName("urn:HelloWorld", "addString") );
13
Add the parameters for the
TestClient.java
remote method
Parameter name. Arbitrary. Appear
XML
data type for the para
in request SOAP
message
IN/OUT/INOUT
call.addParameter( “p1", XMLType.XSD_STRING,
ParameterMode.IN );
call.addParameter( “p2",Define
XMLType.XSD_STRING,
the return value
ParameterMode.IN );
Security options
call.setReturnType( XMLType.XSD_STRING
);
call.setUsername( opts.getUser() );
call.setPassword( opts.getPassword() );
14
Request SOAP message
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
Remote method name
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Name of the web service
Parameter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
name.
<soapenv:Body>
Parameter type. need to
<ns1:sayHellomatch WSDL. Value of the parameter
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encod
ing/" xmlns:ns1="urn:HelloWorld">
<p1 xsi:type="xsd:string">Yuhong</p1>
<p2 xsi:type="xsd:string">Yan</p2>
</ns1:sayHello>
</soapenv:Body>
</soapenv:Envelope>
15
TestClient.java
Invoke the web service
Return value
Passing parameters
String res = (String) call.invoke( new Object[] { args[1],
args[2] } );
System.out.println( "Return is: " + res );
}
catch( Exception e )
{
e.printStackTrace();
}
}
}//end of class
16
Response SOAP message
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:sayHelloResponse
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encod
ing/" xmlns:ns1="urn:HelloWorld">
<ns1:sayHelloReturn xsi:type="soapenc:string"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Yu
hong,Yan, Welcome to SOAP Web Service
World!</ns1:sayHelloReturn>
</ns1:sayHelloResponse>
</soapenv:Body>
</soapenv:Envelope>
17
Inside SOAP
SOAP message
Envelope (required)
Header (optional)
Body (required)
Fault (optional)
P53, figure 3-2
18
Envelope
•
•
•
•
The root element of SOAP message
Uses XML namespaces to differentiate versions
Two versions 1.1, 1.2
No third string
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
19
Header (optional)
• For authentication, transaction management, and
payment authorization
• Two defined attributes
– Actor attribute: the chained node
– MustUnderstand attribute: force the recipient
to process the element, if not understandable,
return a fault
20
Header (optional) (2)
<SOAP-ENV:Header>
<ns1:PaymentAccount xmlns:ns1=“urn:ecerami”
SOAP-ENV:mustUnderstand=“true”>
orsenigo473
</ns1:PaymentAccount>
</SOAP-ENV:Header>
P54. the soapheader
21
Body
• Where the transferred data is
• Data encoding via XML Schemas
<soapenv:Body>
<ns1:sayHello
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns1="urn:HelloWorld">
<p1 xsi:type="xsd:string">Yuhong</p1>
<p2 xsi:type="xsd:string">Yan</p2>
</ns1:sayHello>
</soapenv:Body>
22
Fault (optional)
• faultCode
– SOAP-ENV:VersionMismatch
– SOAP-ENV:MustUnderstand
– SOAP-ENV:Client (non existing methods)
– SOAP-ENV:Server (not able to access DB)
• faultString
• faultActor
• detail
23
Fault (optional)-2
<?xml version=‘1.0’ encoding=‘UTF-8’?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV=“http://schemas.xmlsoap.org/soap/envelope/”
xmlns:xsi=“http://www.w3.org/1999/XMLSchema-instance”
xmlns:xsd=“http://www.s3.org/1999/XMLSchema”>
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode xsi:type=“xsd:string”>SOAP-ENV:Client</faultcode>
<faultstring xsi:type=“xsd:string”>
Failed to locate method (ValidateCreditCard) in class
(examplesCreditCard) at /usr/local/ActivePerl-5.6/lib/
site_perl/5.6.0/SOAP/Lite.pm line 1555.
</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
24
P55. xml part (for faults)
SOAP message inside HTTP message
• SOAP is inside a HTTP message
• How client-server talks in HTTP
– The client identifies the server via a URI
– connects to it using the underlying TCP/IP
network
– issues a HTTP request message (POST)
– receives a HTTP response message (GET)
25
SOAP request message is within HTTP POST
Here is the HTTP header you see from the log
POST /axis/services/urn:HelloWorld HTTP/1.0
Content-Type: text/xml; charset=utf-8
Accept: application/soap+xml, application/dime,
multipart/related, text/*
User-Agent: Axis/1.2alpha
Host: localhost:8080
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: ""
Content-Length: 474
Authorization: Basic dXNlcjE6cGFzczE=
26
SOAP request message is within HTTP POST
Here is the HTTP body – That is the SOAP message
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:sayHello
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encod
ing/" xmlns:ns1="urn:HelloWorld">
<p1 xsi:type="xsd:string">Yuhong</p1>
<p2 xsi:type="xsd:string">Yan</p2>
</ns1:sayHello>
</soapenv:Body>
</soapenv:Envelope>
27
SOAP response message is within HTTP GET
This should be in HTTP header
GET /axis/services/urn:HelloWorld HTTP/1.0
Content-Type: text/xml; charset=utf-8
Accept: application/soap+xml, application/dime,
multipart/related, text/*
User-Agent: Axis/1.2alpha
Host: localhost:8080
……
28
SOAP response message is within HTTP GET
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:sayHelloResponse
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encod
ing/" xmlns:ns1="urn:HelloWorld">
<ns1:sayHelloReturn xsi:type="soapenc:string"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Yu
hong,Yan, Welcome to SOAP Web Service
World!</ns1:sayHelloReturn>
</ns1:sayHelloResponse>
</soapenv:Body>
</soapenv:Envelope>
29
Axis Client
Client
XML
Schema
follows
Java Objects
and data types
XML
data types
SOAP
Server
30
Axis client
• Map java data types to XSD data type
• Serialize java objects into XML
• Send SOAP request to server (not necessarily
Axis server)
• Interpret SOAP response
31
XML <-> Java Data Mapping in Axis
• Primitives
• Beans
• User Defined Types (classes)
• Code Demo
References:
AXIS User Guide
AXIS sample code
32
Primitives: the mapping table
xsd:base64Binary
xsd:boolean
xsd:byte
xsd:dateTime
xsd:decimal
xsd:double
xsd:float
xsd:hexBinary
xsd:int
xsd:integer
xsd:long
xsd:QName
xsd:short
xsd:string
byte[]
boolean
byte
java.util.Calendar
java.math.BigDecimal
double
float
byte[]
int
java.math.BigInteger
long
javax.xml.namespace.QName
short
java.lang.String
33