SOAP

fishnibblersspongySoftware and s/w Development

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

65 views

1

SOAP : Simple Object Access Protocol

Dr. Yuhong Yan

NRC
-
IIT
-
Fredericton

Internet logic

2

Service Oriented Architecture (SOA)/Web
Service triangle

From “Web Services Architecture W3C Working Draft”

http://www.w3.org/TR/2002/WD
-
ws
-
arch
-
20021114/

WSDL

UDDI

SOAP

3

Web Service Stack


Discovery UDDI

Transport HTTP, SMTP, FTP, BEEP

Description WSDL

XML messaging XML
-
RPC, SOAP, XML


Process BPEL4WS, WSCI, WS
-
CDL


4

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.

5

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
>

6

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
>


7

Inside SOAP

P53, figure 3
-
2

Envelope (required)

SOAP message

Header (optional)

Body (required)

Fault (optional)

8

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.


9

Apache SOAP architecture

p69,. Fig 4
-
3

Service:

HelloService.java

AXIS SOAP engine

rpcrouter servlet

SOAP client:
HelloClient.java

Jakarta Tomcat
server

SOAP request:

Service name: urn:HelloWorld

Method name: sayHello

Parameter: firstName=“Yuhong”


lastName=“Yan”

SOAP response:

Return value: “Yuhong Yan,

welcome to SOAP”

“Yuhong Yan, Welcome to SOAP…”

sayHello(“Yuhong”, “Yan”)

1

2

3

4

5

Http POST

Http GET

10

Anatomy of HelloWorld


Server side code


Client side code


SOAP request


SOAP response

11

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;


}

}

12

TestClient

public class TestClient

{


public static void main(String args[])


{


try


{


Options opts = new Options( args );


args = opts.getRemainingArgs();


Parse the arg[ ] into options
(user,url,etc) and non
-
options
args.

get non
-
options args.

13

TestClient.java (2)

Service service = new Service();

Call call = (Call) service.createCall();

call.setTargetEndpointAddress( new
java.net.URL(opts.getURL()) );

if( args[0].equals("1") )


call.setOperationName( new
QName("urn:HelloWorld", "sayHello") );

else


call.setOperationName( new
QName("urn:HelloWorld", "addString") );


The start point of access SOAP web services

Call invokes 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"


Service Name

Service Method

14

TestClient.java

call.addParameter( “p1", XMLType.XSD_STRING,
ParameterMode.IN );


call.addParameter( “p2", XMLType.XSD_STRING,
ParameterMode.IN );


call.setReturnType( XMLType.XSD_STRING );


call.setUsername( opts.getUser() );

call.setPassword( opts.getPassword() );


Add the parameters for the
remote method

Parameter name. Arbitrary. Appear
in request SOAP message

XML data type for the para

IN/OUT/INOUT

Security options

Define the return value

15

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


Remote method name

Name of the web service

Parameter name.

Parameter type. need to
match WSDL.

Value of the parameter

16

TestClient.java

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

Return value

Invoke the web service

Passing parameters

17

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
>


18

Inside SOAP

P53, figure 3
-
2

Envelope (required)

SOAP message

Header (optional)

Body (required)

Fault (optional)

19

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

20

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


21

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

22

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
>

23

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


24

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>

P55. xml part (for faults)

25

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)

26

SOAP request message is within HTTP POST

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=

Here is the HTTP header you see from the log

27

SOAP request message is within HTTP POST

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

Here is the HTTP body


That is the SOAP message

28

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

……

29

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
>



30

Axis Client

XML

Schema

XML

data types

SOAP

Server

Java Objects

and data types

Client

follows

31

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


32

XML <
-
> Java Data Mapping in Axis



Primitives


Beans


User Defined Types (classes)


Code Demo


References:

AXIS User Guide

AXIS sample code

33

Primitives: the mapping table

xsd:base64Binary



byte[]

xsd:boolean




boolean

xsd:byte




byte

xsd:dateTime




java.util.Calendar

xsd:decimal




java.math.BigDecimal

xsd:double




double

xsd:float




float

xsd:hexBinary




byte[]

xsd:int





int

xsd:integer




java.math.BigInteger

xsd:long




long

xsd:QName



javax.xml.namespace.QName

xsd:short




short

xsd:string




java.lang.String