Web services

wispxylopolistInternet and Web Development

Aug 7, 2012 (5 years and 5 months ago)

457 views

A. Haeberlen (based on slides by Zachary G. Ives)

CIS 455/555: Internet and Web Systems

1

University of Pennsylvania

Web services


March 14, 2012

A. Haeberlen (based on slides by Zachary G. Ives)

Administrativa


HW2 MS2 is due on March 21st


Strongly recommended: Try to finish by Sunday



No class

on March 21st


Andreas in New York for ATC PC meeting


Why not use this slot for your first team project meeting?


2

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

What is a web service?


Intuition: An application that is accessible to
other applications over the web


Examples: Google Search, Google Maps API, Facebook
Graph API, eBay APIs, Amazon Web Services, ...


3

University of Pennsylvania

Alice

Alice

Bob

Bob

Charlie

Map service

(used by Alice)

Web page combines
data from different
sources ('mashup')

A. Haeberlen (based on slides by Zachary G. Ives)

A more detailed definition


Key elements:


Machine
-
to
-
machine interaction


Interoperable (with other applications and services)


Machine
-
processable format


Key technologies:


SOAP (and also REST, both of which we've already seen)


WSDL (Web Services Description language; XML
-
based)


4

University of Pennsylvania

"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, typically conveyed using HTTP with
an XML serialization in conjunction with other Web
-
related standards."

http://www.w3.org/TR/ws
-
arch/

A. Haeberlen (based on slides by Zachary G. Ives)

Plan for today


Remote Procedure Calls


Abstraction


Mechanism


Stub
-
code generation


Web services


REST


SOAP


WSDL


5

University of Pennsylvania

NEXT

A. Haeberlen (based on slides by Zachary G. Ives)

Motivation for RPCs


Coding your own messaging is hard


Example: Look up a name on our directory server


Assemble the message at the sender, parse at the receiver...


Need to think carefully about message flow etc.



Can we hide this in the programming
language or middleware?


Similar strategy works great for many other hard or
cumbersome tasks, e.g., memory management


Wouldn't it be nice if we could simply call a function
lookup(name) in the client code, and it executes remotely on
the name server?


That is the abstraction provided by
Remote Procedure Calls

6

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

The intuition behind RPCs

7

University of Pennsylvania

void foo()

{


int x, y;


...


x = bar(45, &y, false);


...

}

void bar(int a, int *b, bool c)

{


...


if (!c)


*b = a + 17;


...

}

Stack

45

&y

false

retaddr

y

Machine A

Machine B

"Message"

A. Haeberlen (based on slides by Zachary G. Ives)

8

Remote Procedure Calls


Remote procedure calls have been around forever


Goes back to at least 1976; Birrell/Nelson paper is from 1984


Implementation examples: COM+, CORBA, DCE, Java RMI, ...



An RPC API defines a format for:


Initiating a call on a given server, generally in a reliable way


At
-
most
-
once, at
-
least
-
once, exactly
-
once semantics


Sending parameters (
marshalling
) to the server


Receiving a return value
-

may require marshalling as well


Different
language bindings
may exist


Java client can call C++ server, Fortran client can call Pascal server, …



RPC calls typically are
synchronous


Caller blocks until response is received from callee


Exception: One
-
way RPCs

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

9

RPC visualized

working

function

executing

server waits for

next request

client
blocked

(waiting for response)

RPC

Server

RPC

Client

University of Pennsylvania

client

continues

server is busy

A. Haeberlen (based on slides by Zachary G. Ives)

10

How RPC generally works


You write an application with a series of functions


Some of these functions will be distributed remotely


You call a
stub
-
code generator
, which produces


A
client stub
, which emulates each function
F
:


Marshals all the parameters and produces a request message


Opens a connection to the server and sends the request


Receives response, unmarshals+returns
F
’s return values, status


A
server stub
, which emulates the caller on the server side:


Receives a request for
F

with parameters


Unmarshals the parameters, invokes
F


Takes
F
’s return status (e.g., protection fault), return value,
marshals it, produces a response, and sends it back to the client


Waits for the next request (or returns to the
server loop
)

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

11

Passing value parameters


Steps involved in doing remote computation through RPC

2
-
8

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

12

RPC components


Generally, you need to write:


Your function, in a compatible language


An
interface definition
, analogous to a C header file, so other
people can program for
F

without having its source


Includes annotations for marshalling, e.g., [in] and [out]


Special
interface definition languages (IDLs)

exist for this


Stub
-
code generator takes the interface
definition and generate the appropriate stubs


(In the case of Java, RMIC knows enough about Java to run
directly on the source file)


The server stubs will generally run in some
type of daemon process on the server


Each function will need a globally unique name or GUID

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

An example

13

University of Pennsylvania

module StockObjects

{


struct Quote {


string symbol;


long at_time;


double price;


long volume;


};



exception Unknown{};



interface Stock {


// Returns the current stock quote.


Quote get_quote() raises(Unknown);



// Sets the current stock quote.


void set_quote(in Quote stock_quote);



// Provides the stock description, e.g. company name.


readonly attribute string description;


};



interface StockFactory {


Stock create_stock(in string symbol, in string description );


};

};

http://java.sun.com/developer/onlineTraining/corba/corba.html

A. Haeberlen (based on slides by Zachary G. Ives)

14

Remote
-
object references


R1 essentially represented as (server,object)


Result: Can pass references as parameters to RPCs

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

15

What are the hard problems with RPC?


Resolving different data formats between
languages (e.g., Java vs. Fortran arrays)


Reliability, security


Finding remote procedures in the first place


Extensibility/maintainability


(Some of these might look familiar from when
we talked about data exchange!)

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

Plan for today


Remote Procedure Calls


Abstraction


Mechanism


Stub
-
code generation


Web services


REST


SOAP


WSDL


16

University of Pennsylvania

NEXT

A. Haeberlen (based on slides by Zachary G. Ives)

17

Web services


Goal: Provide an infrastructure for connecting
components, building applications in a way similar to
hyperlinks between data


Concept of a
mashup


It’s another distributed computing platform for

the Web


Goal: Internet
-
scale, language
-
independent, upwards
-
compatible where possible


This one is based on many familiar concepts


Standard protocols: HTTP


Standard marshalling formats: XML
-
based, XML Schemas


All new data formats are XML
-
based

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

18

The “Standard” for Web Services

Three parts:

1.
“Wire” / messaging protocols


Data encodings, RPC calls or document passing, etc.


We will discuss: SOAP and REST

2.
Describing what goes on the wire


Schemas for the data


We have already discussed: XML Schema

3.
“Service discovery”


Means of finding web services


UDDI

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

REST and SOAP


Example: Access AWS from your program


Via the REST or SOAP protocols


Example: Launch an EC2 instance, store a value in S3, ...



Simple Object Access protocol (
SOAP
)


Not as simple as the name suggests


XML
-
based, extensible, general, standardized, but also
somewhat heavyweight and verbose



Representational State Transfer (
REST
)


Much simpler to develop than SOAP


Web
-
specific; lack of standards



19

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

Representational State Transfer (REST)


One example of a messaging protocol


Not really a standard


a style of development


Data is represented in XML, e.g., with a schema


Function call interface uses URIs


Server is to be stateless


And the HTTP request type specifies the operation


e.g., GET
http://my.com/rest/service1


e.g., POST
http://my.com/rest/service1

{body} adds the body to the
service



20

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

Example: REST

21

University of Pennsylvania

https://sdb.amazonaws.com/?Action=PutAttributes

&DomainName=MyDomain

&ItemName=Item123

&Attribute.1.Name=Color&Attribute.1.Value=Blue

&Attribute.2.Name=Size&Attribute.2.Value=Med

&Attribute.3.Name=Price&Attribute.3.Value=0014.99

&AWSAccessKeyId=<
valid_access_key>

&Version=2009
-
04
-
15

&Signature=[valid signature]

&SignatureVersion=2

&SignatureMethod=HmacSHA256

&Timestamp=2010
-
01
-
25T15%3A01%3A28
-
07%3A00

<PutAttributesResponse>

<ResponseMetadata>

<StatusCode>Success</StatusCode>

<RequestId>f6820318
-
9658
-
4a9d
-
89f8
-
b067c90904fc</RequestId>

<BoxUsage>0.0000219907</BoxUsage>

</ResponseMetadata>

</PutAttributesResponse>


Sample request

Sample response

Source: http://awsdocs.s3.amazonaws.com/SDB/latest/sdb
-
dg.pdf

Invoked

method

Parameters

Credentials

Response

elements

A. Haeberlen (based on slides by Zachary G. Ives)

The 'protocol stacks' of web services

Enhanced + expanded from a figure from IBM’s “Web Services Insider”,

http://www.ibm.com/developerworks/webservices/library/ws
-
ref2/index.html

Other extensions

SOAP Attachments

WS
-
Security

WS
-
AtomicTransaction,

WS
-
Coordination

SOAP,
XML
-
RPC

XML

XML Schema

Service Description

(WSDL)

Service Capabilities

(WS
-
Capability)

Message

Sequencing

Orchestration

(WS
-
BPEL)

Inspection

Directory

(UDDI)

Wire Format Stack

Discovery Stack

Description Stack

WS
-
Addressing

High
-
level

state transition +
messaging

diagrams

between modules

22

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

23

Simple Object Access Protocol (SOAP)


Another example of a messaging protocol


XML
-
based format for passing parameters


Has a SOAP header and body inside an
envelope


Has a defined HTTP
binding

(POST with content
-
type of
application/soap+xml)


A companion SOAP Attachments encapsulates other
(MIME) data


The header defines information about processing:
encoding, signatures, etc.


It’s extensible, and there’s a special attribute called
mustUnderstand

that is attached to elements that
must

be
supported by the callee


The body defines the actual application
-
defined data

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

24

Making a SOAP Call


To execute a call to service PlaceOrder:


POST /PlaceOrder HTTP/1.1

Host: my.server.com

Content
-
Type: application/soap+xml; charset=“utf
-
8”

Content
-
Length:
nnn


<SOAP
-
ENV:Envelope>




</SOAP
-
ENV:Envelope>

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

25

SOAP Return Values


If successful, the SOAP response will generally be
another SOAP message with the return data values,
much like the request


If failure, the contents of the SOAP envelop will
generally be a Fault message, along the lines of:


<SOAP
-
ENV:Body>


<SOAP
-
ENV:Fault xmlns=“mynamespace”>



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



<faultstring>Could not parse message</faultstring>



University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

Example: SOAP envelope

26

University of Pennsylvania

<?xml version='1.0' encoding='UTF
-
8'?>

<SOAP
-
ENV:Envelope

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

xmlns:SOAP
-
ENC='http://schemas.xmlsoap.org/soap/encoding/'

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

xmlns:xsd='http://www.w3.org/2001/XMLSchema'>

<SOAP
-
ENV:Body>

<PutAttributesRequest xmlns='http://sdb.amazonaws.com/doc/

2009
-
04
-
15'>

<Attribute><Name>a1</Name><Value>2</Value></Attribute>

<Attribute><Name>a2</Name><Value>4</Value></Attribute>

<DomainName>domain1</DomainName>

<ItemName>eID001</ItemName>

<Version>2009
-
04
-
15</Version>

</PutAttributesRequest>

</SOAP
-
ENV:Body>

</SOAP
-
ENV:Envelope>

<?xml version="1.0"?>

<SOAP
-
ENV:Envelope xmlns:SOAP
-
ENV="http://schemas.xmlsoap.org/soap/envelope/">

<SOAP
-
ENV:Body>

<PutAttributesResponse>

<ResponseMetadata>

<RequestId>4c68e051
-
fe45
-
43b2
-
992a
-
a24017ffe7ab</RequestId>

<BoxUsage>0.0000219907</BoxUsage>

</ResponseMetadata>

</PutAttributesResponse>

</SOAP
-
ENV:Body>

</SOAP
-
ENV:Envelope>

Sample request

Sample response

Source: http://awsdocs.s3.amazonaws.com/SDB/latest/sdb
-
dg.pdf

A. Haeberlen (based on slides by Zachary G. Ives)

Plan for today


Remote Procedure Calls


Abstraction


Mechanism


Stub
-
code generation


Web services


REST


SOAP


WSDL


27

University of Pennsylvania

NEXT

A. Haeberlen (based on slides by Zachary G. Ives)

28

How do we declare functions?


Remember interface definition languages?


CORBA IDL, DCE IDL, ...


In Java, the interface uses the same language as the Java
code



Web Services Description Language (WSDL)
is the interface definition language for

web services


Defines notions of protocol bindings, ports, and services


Generally describes data types using XML Schema



University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

A WSDL Service

Service

Port

Port

Port

PortType

Operation

Operation

PortType

Operation

Operation

PortType

Operation

Operation

Binding

Binding

Binding

29

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

30

Web service terminology


Service:

The entire Web Service


Port:

Maps a set of port types to a transport
binding (a protocol, frequently SOAP, COM,
CORBA, …)


Port Type:

Abstract grouping of operations,
i.e. a class


Operation:

The type of operation


request/response, one
-
way


Input message and output message; maybe also fault
message


Types:
The XML Schema type definitions

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

WSDL example

31

University of Pennsylvania

<definitions>


<types>



definition of types......

</types>


<message>



definition of a message..

</message>


<portType>



definition of a port.....

</portType>


<binding>



definition of a binding..

</binding>


</definitions>

<message name="getTermRequest">



<part name="term" type="xs:string"/>

</message>


<message name="getTermResponse">



<part name="value" type="xs:string"/>

</message>

<portType name="glossaryTerms">



<operation name="getTerm">



<input message="getTermRequest"/>



<output message="getTermResponse"/>



</operation>

</portType>

Uses

XML

Schema

<binding type="glossaryTerms" name="b1">



<soap:binding style="document" transport=


"http://schemas.xmlsoap.org/soap/http" />



<operation>



<soap:operation


soapAction="http://example.com/getTerm"/>



<input><soap:body use="literal"/></input>



<output><soap:body use="literal"/></output>



</operation>

</binding>

http://www.w3schools.com/wsdl/

A. Haeberlen (based on slides by Zachary G. Ives)

32

JAX
-
RPC: Java and web services


Java API for XML
-
based RPCs


To write JAX
-
RPC web service “endpoint”,

you need two parts:


An endpoint interface


this is basically like the IDL statement


An implementation class


your actual code


http://www.ibm.com/developerworks/xml/library/x
-
tipjaxrpc/

University of Pennsylvania

public interface BookQuote extends java.rmi.Remote {


public float getBookPrice(String isbn)


throws java.rmi.RemoteException;

}


public class BookQuote_Impl_1 implements BookQuote {


public float getBookPrice(String isbn) {


return 3.22;


}

}

A. Haeberlen (based on slides by Zachary G. Ives)

Different options for calling


The conventional approach is to generate a
stub, as in the RPC model described earlier


WSDL
-
to
-
Java code generator produces, among other
things, a Service Endpoint Interface (SEI)
-

the 'client stub'



You can also
dynamically

generate the call to
the remote interface, e.g., by looking up an
interesting function to call


The “DII” (Dynamic Instance Invocation) method allows you
to assemble the SOAP call on your own


Create a Call object, configure it with parameters, return
type, binding information, endpoint, ..., then invoke the call

33

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

Creating a Java web service


A compiler called
wscompile

is used to
generate your WSDL file and stubs


You need to start with a configuration file that says something
about the service you’re building and the interfaces that you’re
converting into Web Services

<?xml version="1.0" encoding="UTF
-
8"?>

<configuration xmlns="http://java.sun.com/xml/ns/jax
-
rpc/ri/config">


<service name="StockQuote"


targetNamespace="http://example.com/stockquote.wsdl"


typeNamespace="http://example.com/stockquote/types"


packageName="edu.upenn.cis555">


<interface name="edu.upenn.cis555.StockQuoteProvider"


servantName="edu.upenn.cis555.StockQuoteServiceImpl"/>


</service>

</configuration>

34

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

35

Starting a WAR


The Web Service version of a Java JAR file is a Web
Archive, WAR


There’s a tool called
wsdeploy

that generates
deployable WAR files from 'raw' WAR files


Input WAR contains endpoint interface, implementation class, web.xml and
a descriptor called jaxrpc
-
ri.xml


wsdeploy edits web.xml & calls wscompile to generate WSDL + classes


Generally this will automatically be called from a build tool such as Ant


Finally, you may need to add the WAR file to the
appropriate location in Apache Tomcat (or
WebSphere, etc.) and enable it



For a detailed example, see


http://java.sun.com/developer/technicalArticles/WebServices/WSPack2/jaxrpc.html

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

Plan for today


Remote Procedure Calls


Abstraction


Mechanism


Stub
-
code generation


Web services


REST


SOAP


WSDL


UDDI


36

University of Pennsylvania

NEXT

A. Haeberlen (based on slides by Zachary G. Ives)

37

Finding a web service


UDDI: Universal Description, Discovery, and
Integration registry


Think of it as DNS for web services


Was a replicated database, hosted by IBM, HP, SAP, MS


UDDI took SOAP requests to add and query
web service interface data


Failed to be widely adopted


Microsoft removed UDDI capability from Server 2008


University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

38

What was in UDDI


White pages:


Information about business names, contact info, Web site
name, etc.


Yellow pages:


Types of businesses, locations, products


Includes predefined taxonomies for location, industry, etc.


Green pages


what we probably care the most about:


How to interact with business services; business process
definitions; etc


Pointer to WSDL file(s)


Unique ID for each service

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

39

Data types in UDDI


businessEntity
: top
-
level structure describing
info about the business


businessService
: name and description of a
service


bindingTemplate
: how to access the service


tModel

(t = type/technical): unique identifier
for each service
-
template specification


publisherAssertion
: describes relationship
between
businessEntities

(e.g., department,
division)

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

40

Relationships between UDDI Structures

publisherAssertion

businessEntity

businessService

bindingTemplate

tModel

n

2

1

n

1

n

m

n

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

41

Example UDDI businessEntity

<businessEntity businessKey=“0123…” xmlns=“urn:uddi
-
org:api_v2”>

<discoveryURLs>

<discoveryURL useType=“businessEntity”>

http://uddi.ibm.com/registery/uddiget?businessKey=0123
...

</discoveryURL>

<name>My Books</name>

<description>Technical Book Wholesaler</description>



<businessServices>



</businessServices>

<identifierBag>

<!


keyedReferences to tModels


㰯楤敮<楦楥牂r朾

<categoryBag> … </categoryBag>

</businessEntity>

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

42

UDDI in perspective


Original idea was that it would just
organize itself in a way that people could
find anything they wanted



UDDI developed into a very simple catalog
of services, which can be queried with
standard APIs


It’s not clear that it really does what people really want:
they want to find services “like Y” or “that do Z”

University of Pennsylvania

A. Haeberlen (based on slides by Zachary G. Ives)

43

The problem


There’s no universal, unambiguous way of describing
“what I mean”


Relational database idea of “normalization” doesn’t convert
concepts into some normal form


it just helps us cluster our
concepts in meaningful ways


“Knowledge representation” tries to encode definitions

clearly


but even then, much is up to interpretation



The best we can do: describe
how things relate


pollo

=
chicken

=
poulet
=



=


= jī =


र्गी

= murg


Note that this
mapping

may be imprecise or situation
-
specific!


Calling someone a chicken, vs. a chicken that’s a bird



This brings us to XQuery...



... whose main role is to relate XML

University of Pennsylvania