Web Services with SOAP

hungryhorsecabinΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 10 μήνες)

187 εμφανίσεις

Scot White

Farhad Rasapour

CS555


Spring 2012

What is a Web Service?

A web service is any service that:



Is available over the Internet or private
(intranet) networks



Uses an open standard XML
-
based
messaging system



Is not tied to any one operating system
or programming language



Is self
-
describing via a common XML
grammar



Is discoverable via a simple find
mechanism

Benefits of Web Services


Exposes existing functions on to network



Web services can be remotely invoked using HTTP, allowing the
developer to expose the functionality of existing code over the network.



Makes service platform
-

and technology
-
independent:



Web Services allows different applications implemented in different
languages and running on different OS to talk to each other and share
data and services among themselves.




Uses Standardized Protocols:



Web Services use industry standard protocols for the communication,
helping ensure cross
-
platform compatibility.




Low Cost of Communication:



Web Services are implemented using existing low cost internet
protocols such as HTTP or FTP.


Web Service Protocol Layers


Service Transport
-

responsible for transporting
messages between applications


Ex. HTTP, SMTP, FTP


XML Messaging
-

responsible for encoding
messages in a common XML format


Ex. SOAP, XML
-
RPC


Service Description
-

responsible for describing a
public interface for a specific web service


Ex. WSDL


Service Discovery
-

responsible for centralizing
services in a common registry, in which services can
be easily published and found


Ex. UDDI

What is SOAP?


SOAP



S
imple
O
bject
A
ccess
P
rotocol


SOAP is an XML
-
based protocol for exchanging
messages between applications


SOAP is text
-
based, self
-
describing, human
-
readable


SOAP is designed to communicate via Internet



SOAP is platform independent and language
independent



SOAP is extensible



SOAP allows you to get around firewalls



SOAP is a W3C recommendation(2003),
making it vendor
-
neutral

Brief History of SOAP


1997
-

Microsoft,
DevelopMentor

&
Userland

start discussions about XML
-
based distributed
computing, intending to enable applications to
communicate via RPC using standard data
types on top of XML/HTTP. The concept was
named SOAP.




1998
-

Internal politics within Microsoft slow
the project. Some think the company should
forget XML and push for a DCOM solution.
Others like SOAP but think it's too soon to
deploy.
Userland

gets fed up and publishes its
own version as XML
-
RPC.

Brief History of SOAP


1999
-

Microsoft adds XML Schema and
support for namespaces. Interest in SOAP
grows. SOAP 1.0 submitted to IETF as
public draft.




2000
-

W3C starts looking into XML
protocols. SOAP 1.1 submitted as a note
to W3C with IBM as co
-
author.



2003
-

SOAP 1.2 becomes a W3C
recommendation.

SOAP is based on XML


eXtensible

Markup Language
-

a meta
-
language for defining
common formats for storing and exchanging information across
networks



similar to HTML
-

uses markup symbols (tags) to describe the
contents of a file or message



different from HTML
-

markup symbols are unlimited and self
-
defining, don't involve displaying data





<?xml version=”1.0”?>




<
PhoneNumber
>





<
getPhoneNumber
>





<name>John Doe</name>





</
getPhoneNumber
>




</
PhoneNumber
>

XML Namespaces


Used to distinguish common element names with prefixes



URI name identifier guarantees name uniqueness



When combined with XML Schema, provides a method for
validating message structure





<?xml version=”1.0”?>




<
dir:PhoneNumber

xmlns:dir
="http://www.directory.com"

xmlns:xsi
="http://www.w3.org/2001/XMLSchema
-
instance"

xsi:schemaLocation
="http://www.mybooks.com/directory.xsd"
>





<
dir:
getPhoneNumber
>





<
dir:
name
>John Doe</
dir:
name
>





</
dir:
getPhoneNumber
>




</
dir:
PhoneNumber
>

SOAP Message Structure


Envelope:

( Mandatory )

Defines the start and the end of the message.



Header:

( Optional )

Contains any optional attributes of the message
used in processing the message.



Body:

( Mandatory )

Contains the XML data comprising the message
being sent.



Fault:

( Optional )

Provides information about errors that occurred while
processing the message


SOAP Message Structure

<?xml version="1.0"?>

<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.w3.org/1999/XMLSchema”

SOAP
-
ENV:encodingStyle
=”http://schemas.xmlsoap.org/soap/encoding/”>


<SOAP
-
ENV:Header
>


</SOAP
-
ENV:Header
>


<SOAP
-
ENV:Body
>


<
ns:getPhoneNumber

xmlns:ns
=”
PhoneNumber
”>


<name
xsi:type
=”
xsd:string
”>John Doe</name>


</
ns:getPhoneNumber
>


</SOAP
-
ENV:Body
>

</SOAP
-
ENV:Envelope
>

SOAP Header


Provides support for new features and special message
processing


Optional part of SOAP messages.


First immediate child element of the SOAP envelope.


SOAP Header element can have following two attributes



Actor attribute (Role attribute in SOAP 1.2):

Enroute

to their destination, SOAP messages may receive
additional processing at intermediate nodes along the way. By
setting the Actor attribute, the client can specify the recipient of
the SOAP header.



MustUnderstand

attribute

Indicates whether a Header element is optional or mandatory. If
set to true, the recipient must understand and process the
Header attribute according to its defined semantics, or return a
fault.


SOAP Body


Mandatory element containing the
application
-
defined XML data being
exchanged in the SOAP message


Child element of Envelope, follows
Header

SOAP Fault


When an error occurs during message
processing, the recipient returns a SOAP
message containing a SOAP fault element,
which contains:


faultcode
: Predefined

text code indicating the
type of error


faultstring
: Text message explaining the error


faultactor
: text string indicating who caused the
fault


detail: element used to carry application
-
specific
error messages

SOAP Fault

<SOAP
-
ENV:Envelope

xmlns:SOAP
-
ENV=”http://schemas.xmlsoap.org/soap/envelope/”>


<SOAP
-
ENV:Body
>


<SOAP
-
ENV:Fault
>


<
faultcode
>SOAP
-
ENV:Server
</
faultcode
>


<
faultstring
>Unable to process message</
faultstring
>


<detail>


<
dtl:faultDetail

xmlns:dtl
=”Some
-
URI”>


<message>Namespace mismatch</message>


<
errorcode
>47</
errorcode
>


</
dtl:faultDetail
>


</detail>


</SOAP
-
ENV:Fault
>


</SOAP
-
ENV:Body
>

</SOAP
-
ENV:Envelope
>


SOAP Encoding


SOAP supports all scalar data types defined in XML Schema (
int
, float,
boolean
, string, etc.)


SOAP also supports serialization of more complex objects (
structs
,
arrays) with
encodingStyle

attribute:



<SOAP
-
ENV:Body
>


<ns1:getPriceListResponse
xmlns:ns1="
urn:examples:pricelistservice
" SOAP
-
ENV:encodingStyle
="http://www.w3.org/2001/12/soap
-
encoding">


<return xmlns:ns2="http://www.w3.org/2001/09/soap
-
encoding"
xsi:type
="ns2:Array" ns2:arrayType="
xsd:double
[2]">


<item
xsi:type
="
xsd:double
">54.99</item>


<item
xsi:type
="
xsd:double
">19.99</item>


</return>


</ns1:getPriceListResponse>


</SOAP
-
ENV:Body
>

SOAP RPC


SOAP includes a namespace for defining RPC
interaction:



<SOAP
-
ENV:Body
>


<
myNS:getPhoneNumber
Response



xmlns:myNS
=“www.directory.com"


xmlns:rpc
=“http://www.w3.org/2003/05/soap
-
rpc“>


<
rpc:result
>ret</
rpc:result
>


<ret
xsi:type
="
xsd:string
">426
-
3821</ret>


</
myNS:getPhoneNumber
Response

>


</SOAP
-
ENV:Body
>

SOAP/HTTP


SOAP can be transported via HTTP, SMTP,
FTP, etc.


HTTP remains the most popular SOAP
transport protocol


HTTP SOAP Message

POST /
PhoneNumEntry

HTTP/1.1

Host: www.directory.com

Content
-
Type: application/soap;
charset
="utf
-
8“

Content
-
Length:
nnnn


<?xml version="1.0"?>

<SOAP
-
ENV:Envelope


xmlns:SOAP
-
ENV=”http://www.w3.org/2003/05/soap
-
envelope/”

xmlns:xsi
=”http://www.w3.org/1999/XMLSchema
-
instance”

xmlns:xsd
=”http://www.w3.org/1999/XMLSchema”

SOAP
-
ENV:encodingStyle
=”http://www.w3.org/2001/12/soap
-
encoding”>


<SOAP
-
ENV:Header
>


</SOAP
-
ENV:Header
>


<SOAP
-
ENV:Body
>


<
ns:getPhoneNumber

xmlns:ns
=”
PhoneNumber
”>


<name
xsi:type
=”
xsd:string
”>John Doe</name>


</
ns:getPhoneNumber
>


</SOAP
-
ENV:Body
>

</SOAP
-
ENV:Envelope
>

What is WSDL?


WSDL (pronounced “wiz
-
dill”)


Web
Service Description Language


XML
-
based format for describing Web
Services


describes which operations Web
Services can execute and the format of
the messages Web Services can send
and receive

WSDL Structure

WSDL files contain the following
elements:


definitions



root element


types



XML schema defining
data types used


message



describes format of
messages exchanged by a service


portType



describes operations
provided by a service, in terms of
sequence of messages exchanged


binding



associates a
portType

with the desired protocol (HTTP
GET, SOAP, etc)


port



associates a binding with a
network address


service



collection of ports
providing the web service

What is UDDI?


Universal Description, Discovery and Integration


Specification for a distributed registry of Web
services


Open industry initiative enabling businesses to
discover each other and define how they interact
over the Internet


UDDI has two parts:


A registry of all web service metadata including a pointer
to the WSDL description for each service


A set of WSDL port type definitions for manipulating and
searching that registry



UDDI Data Model


businessEntity



represents
a web service provider,
including contact information,
industry categories, business
identifiers, and a list of
services provided


businessService



identifies
a web service and how to
bind to it


bindingTemplate



represents an implementation
of the web service


tModel



contains reference
to a WSDL file describing this
implementation

SOAP Web Service Example

SOAP Example w/Java

1) Install


Java 1.1 or higher


Apache Jakarta Tomcat 3.2.1 Web server
and servlet engine


Apache Xerxes XML Parser 1.2.3


JavaMail (mail.jar)


Apache SOAP 2.2

SOAP Example w/Java

2) Configure Tomcat to detect SOAP:



Edit
\
jakarta
-
tomcat
-
3.2
\
conf
\
server.xml,
adding the lines:



<Context path=”/soap”
docBase
=”C:/soap
-
2_0/webapps/soap”
reloadable=”true”>


</Context>



3) Launch Tomcat


4) Browse
http://localhost/soap

to launch
Apache SOAP


Java SOAP Server App

public class
PhoneNumber
{


private
HashMap

PhoneBook
;

….

public String
getPhoneNumber

(String name) {


String num = “999
-
9999”;


if (
PhoneBook.containsKey
( name )


num =
PhoneBook.get
( name );


return num;

….

}


Deploying the Service

Java SOAP Client

import
org.apache.axis.AxisEngine
;

import
org.apache.axis.client.Call
;

import
org.apache.axis.soap.SOAPConstants
;



public class
PhoneNumber
{


/** Service URL */

static String
url

= "http://www.directory.com:8080/axis/ PhoneNumber.jws";

….

public static String
getPhoneNumber

(String name) throws Exception {


Call
call

= new Call(
url
);


call.setSOAPVersion
(SOAPConstants.SOAP12_CONSTANTS);


Object[]
params

= new Object[] { name };


String result = (String)
call.invoke
("", "
getPhoneNumber

",
params

);


return result;

}

….



Questions?