copyright (c) [2002]. roger l. costello. all rights reserved. 1 rest (representational state...

61
(c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Upload: melissa-brewer

Post on 26-Mar-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1

REST (Representational State Transfer)

Roger L. Costello

XML Technologies Course

Page 2: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 2

Acknowledgements

• I would like to thank Paul Prescod for his very helpful comments on improving this tutorial, as well as his outstanding articles on REST.

• Also, thanks to the following people for their comments and suggestions:– Sam Ruby

– Mark Baker

– Robert McKinnon

– Mike Dierken

– Amy Kazura

– Kit Lueder

– Mark Nottingham

Page 3: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 3

What is REST?

• REST is a term coined by Roy Fielding in his Ph.D dissertation [1] to describe an architecture style of networked systems.

[1] http://www.ebuilt.com/fielding/pubs/dissertation/top.htm

Page 4: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 4

Why is it called "Representation State Transfer"?

ResourceClienthttp://www.boeing.com/aircraft/747

Boeing747.html

The Client references a Web resource using a URL. A representation of the resource is returned (in this case as an HTML document).The representation (e.g., Boeing747.html) places the client application in a state. The result of the client traversing a hyperlink in Boeing747.htmlis another resource is accessed. The new representation places the clientapplication into yet another state. Thus, the client application changes (transfers) state with each resource representation --> Representation State Transfer!

Fuel requirementsMaintenance schedule...

Page 5: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 5

Representation State Transfer

"Representation State Transfer is intended to evoke an image of how a well-designedWeb application behaves: a network of web pages (a virtual state-machine), wherethe user progresses through an application by selecting links (state transitions), resultingin the next page (representing the next state of the application) being transferred to theuser and rendered for their use."

- Roy Fielding

Page 6: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 6

Motivation for REST

"The motivation for developing REST was to create an architectural model forhow the Web should work, such that it could serve as the guiding frameworkfor the Web protocol standards.

REST has been applied to describe the desired Web architecture, help identify existing problems, compare alternative solutions, and ensure that protocol extensions would not violate the core constraints that make the Web successful."

- Roy Fielding

Page 7: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 7

REST - not a Standard

• REST is not a standard. You will not see the W3 putting out a REST specification. You will not see IBM or Microsoft or Sun selling a REST developer's toolkit. Why? Because REST is just an architectural style. You can't bottle up that style. You can only understand it, and design your Web services in that style.

• While REST is not a standard, it does prescribe the use of standards:

– HTTP– URL– XML/HTML/GIF/JPEG/etc (Resource Representations)– text/xml, text/html, image/gif, image/jpeg, etc (Resource Types, MIME Types)

Page 8: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 8

REST and the Web

• The Web is an example of a REST system!

• All of those Web services that you have been using all these many years - book ordering services, search services, online dictionary services, etc - are REST-based Web services.

• Alas, you have been using REST, building REST services and you didn't even know it.

Page 9: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 9

Learn by Example

• This architectural style is best explained with an example.

• I will present an example of a company deploying 3 Web services using the REST architectural style, then show how they would be deployed using SOAP.

• After presenting the example I will contrast the REST and SOAP.

Page 10: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 10

Parts Depot Web Services

• Parts Depot, Inc has deployed some web services to enable its customers to:– get a list of parts– get detailed information about a particular part– submit a Purchase Order (PO)

Page 11: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 11

The REST way of Implementing the Web Services

Response(HTML/XML doc)

Web

Ser

ver

HTTP GET request URL 1

HTTP response

Response(HTML/XML doc)

HTTP GET request URL 2

HTTP response

HTTP POST URL 3

HTTP responseURL to submitted PO

PO(HTML/XML)

PartsList

Part

PO

Page 12: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 12

The REST way of Implementing the Web Service

• Service: Get a list of parts– The web service makes available a URL to a parts list

resource. Example, a client would use this URL to get the parts list:

• http://www.parts-depot.com/parts• Note that how the web service generates the parts list is completely

transparent to the client. This is loose coupling.

– The web service may wish to allow the client to specify whether he/she wants the parts list as an HTML document, or as an XML document. This is how to specify that an XML document is desired:

• http://www.parts-depot.com/parts?flavor=xml

Page 13: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 13

Data Returned - Parts List<?xml version="1.0"?><p:Parts xmlns:p="http://www.parts-depot.com" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.parts-depot.com http://www.parts-depot.com/parts.xsd"> <Part id="00345" xlink:href="http://www.parts-depot.com/parts/00345"/> <Part id="00346" xlink:href="http://www.parts-depot.com/parts/00346"/> <Part id="00347" xlink:href="http://www.parts-depot.com/parts/00347"/> <Part id="00348" xlink:href="http://www.parts-depot.com/parts/00348"/></p:Parts>

Note that the parts list has links to get detailed info about each part. This is a key feature of REST. The client transfers from one state to the next by examining and choosing from among the alternative URLs in the response document.

Page 14: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 14

The REST way of Implementing the Web Service

• Service: Get detailed information about a particular part– The web service makes available a URL to each part

resource. Example, here's how a client requests a specific part:

• http://www.parts-depot.com/parts/00345?flavor=xml

Page 15: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 15

Data Returned - Part

<?xml version="1.0"?><p:Part xmlns:p="http://www.parts-depot.com" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.parts-depot.com http://www.parts-depot.com/part.xsd"> <Part-ID>00345</Part-ID> <Name>Widget-A</Name> <Description>This part is used within the frap assembly</Description> <Specification xlink:href="http://www.parts-depot.com/parts/00345/specification"/> <UnitCost currency="USD">0.10</UnitCost> <Quantity>10</Quantity></p:Part>

Again observe how this data is linked to still more data - the specificationfor this part may be found by traversing the hyperlink. Each response document allows the client to drill down to get more detailed information.

Page 16: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 16

Questions and AnswersWhat if Parts Depot has a million parts, will there be a million static pages? For example: http://www.parts-depot/parts/000000 http://www.parts-depot/parts/000001 ... http://www.parts-depot/parts/999999

We need to distinguish between a logical and a physical URL. The above URLs are logical. They express what resource is desired. They do not identify a physical object. The advantage of using logical URLs is that changes to the underlying implementation of the resource will be transparent to clients (that's loose coupling!).

Quite likely Parts Depot will store all parts data in a database. Code at the Parts Depot web site will receive each logical URL request, parse it to determine which part is being requested, query the database,and generate the part response document which is returned to the client.

Contrast the above logical URLs with these physical URLs: http://www.parts-depot/parts/000000.html http://www.parts-depot/parts/000001.html ... http://www.parts-depot/parts/999999.htmlThese URLs are clearly pointing to physical (HTML) pages. If there are a million parts it will not be very attractive to have a million static pages. Furthermore, changes to how these parts data is represented will result in impact all clients that were using the old representation. So, no, there will not be a million static pages.

Q:

A:

Page 17: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 17

Questions and AnswersWhat if I have a complex query? For example: "show me all parts whose unit cost is under $0.50 and for which the quantityis less than 10". How would you do that with a simple URL?

For complex queries Parts Depot will provide a service to allow a client to retrieve a form that would then be filled in by the client. When the client hits "Go" the form would gather up the clients responses and generate a URL based upon the responses. Thus,oftentimes the client doesn't generate the URL (think about usingamazon - you start by entering the URL to amazon; from then onyou simply fill in forms and the URLs are automatically createdfor you).

Q:

A:

Page 18: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 18

Questions and Answers

How are REST services described?

In brief, REST services may be described using WSDL and/orWRDL (Web Resource Description Language). In the future Iwill describe more details of how this is accomplished.

Q:

A:

Page 19: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 19

The REST way of Implementing the Web Service

• Service: Submit a Purchase Order (PO)– The web service makes available a URL to submit a PO. The

client creates a PO instance document which conforms to the PO schema that Parts Depot has designed (and publicized in a WSDL document). The client submits PO.xml as the payload of an HTTP POST.

W

eb S

erve

r

PO.xsd

PO.xml

HTTP POST

conforms to.

Page 20: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 20

Submit PO Service (cont.)

• The PO service responds to the HTTP POST with a URL to the submitted PO. Thus, the client can retrieve the PO any time thereafter.

– The PO has become a piece of information which is shared between the client and the server. The shared information (PO) is given an address (URL) by the server and is exposed as a Web service.

W

eb S

erve

r

PO.xml

HTTP ResponseURL

Page 21: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 21

Characteristics of a REST-based Network

• Client-Server: a pull-based interaction style: consuming components pull representations.

• Stateless: each request from client to server must contain all the information necessary to understand the request, and cannot take advantage of any stored context on the server.

• Cache: to improve network efficiency responses must be capable of being labeled as cacheable or non-cacheable.

• Uniform interface: all resources are accessed with a generic interface (e.g., HTTP GET, POST, PUT, DELETE).

• Named resources - the system is comprised of resources which are named using a URI.

• Interconnected resource representations - the representations of the resources are interconnected using URLs, thereby enabling a client to progress from one state to another.

Page 22: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 22

SOAP Version

• We have taken a look at how Parts Depot may implement its services in a RESTful manner.

• Now let's look at how the services may be implemented using SOAP.

Page 23: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 23

Implementing the Web Services using SOAP

Request(XML doc)

Response(XML doc)

Web

Ser

ver

SOAP envelope

HTTP POSTURL 1

HTTP Response

getPartsList()

Request(XML doc)

Response(XML doc)

HTTP POSTURL 1

HTTP ResponsegetPart(id)SOAP Server

Note the use of the same URL (URL 1) for all transactions. The SOAP Server parses the SOAP message to determine which method to invoke. All SOAP messages are sent using an HTTP POST.

PO(XML doc)

HTTP POSTURL 1

submit(PO)Response

(XML doc)HTTP Response

Page 24: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 24

Note about the SOAP URI

URL 1SOAPServer

HTTP POST

HTTP POST

HTTP POST

It is not a SOAP requirement all messages be funneled to the same URL:

However, it is common among SOAP vendors to follow this practice. For example, here is the URL for all requests when using Apache SOAP:

[host]/soap/servlet/messagerouter

getPartsList()

getPart(id)

submit(PO)

Page 25: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 25

Implementing the Web Service using SOAP

• Service: Get a list of parts– The client creates a SOAP document that specifies the

procedure desired.<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>

<p:getPartsList xmlns:p="http://www.parts-depot.com"/>

</soap:Body></soap:Envelope>

Then the client will HTTP POST this document to the SOAP server at: http://www.parts-depot.com/soap/servlet/messagerouterThe SOAP server takes a quick peek into this document to determine what procedure to invoke.

Page 26: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 26

Data Returned - Parts List<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>

<p:getPartsListResponse xmlns:p="http://www.parts-depot.com"> <Parts> <Part-ID>00345<Part-ID> <Part-ID>00346<Part-ID> <Part-ID>00347<Part-ID> <Part-ID>00348<Part-ID> </Parts> <p:getPartsListResponse>

</soap:Body></soap:Envelope>

Note the absence of links. Why is this? A URL that points to a SOAPservice is meaningless since the URL to a SOAP service is just to theSOAP server. Thus, the URL would need to be supplemented withsome indication of which method to invoke at that URL.[Note: of course this response could contain a URL to a REST-ful service.]

Page 27: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 27

Implementing the Web Service using SOAP

• Service: Get detailed information about a particular part– The client creates a SOAP document that specifies the procedure

desired, along with the part-id parameter.

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>

<p:getPart xmlns:p="http://www.parts-depot.com"> <part-id>00345</part-id> </p:getPart>

</soap:Body></soap:Envelope>

Again, the client will HTTP POST this document to the SOAP server at: http://www.parts-depot.com/soap/servlet/messagerouterNote that this is the same URL as was used when requesting the parts list.The SOAP server peeks into this document to determine what procedure to invoke.

Page 28: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 28

Data Returned - Part<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>

<p:getPartResponse xmlns:p="http://www.parts-depot.com"> <Part-ID>00345</Part-ID> <Name>Widget-A</Name> <Description>This part is used within the frap assembly</Description> <UnitCost currency="USD">0.10</UnitCost> <Quantity>10</Quantity> </p:getPartResponse>

</soap:Body></soap:Envelope>

Again, notice the absence of links. Thus, there is nothing in the response to enable a client to "go to the next level of detail". The information about how to go to the next level of detail must be found out-of-band.

Page 29: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 29

Implementing the Web Service using SOAP

• Service: Submit a Purchase Order (PO)– The client creates a SOAP document that contains a PO instance

document (which conforms to the PO schema that Parts Depot has created)

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>

<p:PurchaseOrder xmlns:p="http://www.parts-depot.com"> ... </p:PurchaseOrder>

</soap:Body></soap:Envelope>

Once again, the client will HTTP POST this document to the SOAP server at: http://www.parts-depot.com/soap/servlet/messagerouterNote that this is the same URL as was used with the other two services.The SOAP server peeks into this document to determine what procedure to invoke.

Page 30: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 30

Data Returned - PO Acknowledgment

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>

<p:PO-SubmittalResponse xmlns:p="http://www.parts-depot.com"> <PO-ID>x-010123fjdsk390f</PO-ID> </p:PO-SubmittalResponse>

</soap:Body></soap:Envelope>

Again, notice the absence of links.

Page 31: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 31

Contrasting REST and SOAP

• On the next slides I will contrast REST and SOAP in terms of:– Proxy Servers (Web intermediaries)– Transitioning state in a client application– Caching (i.e., performance)– Web evolution (semantic Web)– Generic interface (versus custom interface)– Interoperability– Processing the payload

Page 32: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 32

Letter Analogy

• My company has a receiving warehouse. All letters and packages first go there, and from there they are distributed.

• A SOAP Server is analogous to the receiving warehouse - the SOAP Server receives all incoming SOAP messages and then distributes each message to the appropriate application for processing.

• However, there is one big difference:– No one in the receiving warehouse is allowed to look inside any letter or package. All

decisions about what to do with letters/packages must be made purely by looking at the addressing on the outside. Any attempt to look inside of letters/packages is a violation of Federal Law (U.S.).

– A SOAP Server, on the other hand, is able to "peek inside" the SOAP envelope. In fact, it must do so because the actual target resource is not specified on the outside, but rather, is hidden within the envelope.

• With REST all decisions are made based upon the URL and HTTP method.• Thus, REST and SOAP have a fundamental difference in this regard. We will see in

the next slides that this causes SOAP to clash with the Web.

Page 33: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 33

Proxy Servers

• Consider this scenario:– A company has deployed 3 resources - Resource 1,

Resource 2, and Resource 3– A client wishes to access Resource 1 (get a representation

of Resource 1)– All client requests go through a proxy server– The proxy server enforces the policy that access to

Resource 2 and Resource 3 is allowed. However, Resource 1 is off limits (for example, suppose that Resource 1 is Hotmail, and the client's company policy prohibits accessing Hotmail using the company's lines).

Page 34: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 34

WebServer

Resource 1

Resource 2

Resource 3

ProxyServer

http://www.somewhere.org/Resource1

“You’re not allowedto access Resource 1”

REST and Proxy Servers

Page 35: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 35

REST and Proxy Servers (cont.)

• The URL identifies the resource that is desired (e.g., Resource 1)

• The HTTP method identifies the desired operation (e.g, HTTP GET)

• A proxy server can decide, based upon the identified resource, and the HTTP method whether or not to allow the operation.

Desired resource

Method

Accept/reject

Page 36: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 36

WebServer

Resource 1

Resource 2

Resource 3

ProxyServer

http://www.somewhere.org/soap-server

SOAPServer

“I can’t determineif the message isallowed since I don’tknow what Resource it is targeting [that information is hiddenin the Envelope]”

SOAP and Proxy Servers

Page 37: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 37

SOAP and Proxy Servers (cont.)• The URL is not to the target resource, but rather to a SOAP

server. This makes it harder, and less likely, for a proxy server to determine which resource is actually being targeted.

• The proxy server would need to look inside the SOAP message to determine which resource is being requested.– Further, the proxy server would need to understand the semantics of

the message:

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getResource xmlns="…"> <id>R1</id> </getResource> </soap:Body></soap:Envelope>

Does this mean that the clientis trying to access Resource 1?The proxy server must understandthe semantics of every SOAPapplication!

Page 38: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 38

SOAP and Proxy Servers (cont.)

ProxyServer

SOAPmessage

"What resource is this SOAP messagetrying to access? What is it trying todo with the resource?"

There are 2 ways to implement the proxy server:1. Program the proxy server to understand the semantics of each SOAP application that a client will access. This approach is not scalable - for each new SOAP application the proxy server will need to be updated.2. If all SOAP messages are written in RDF/DAML then it may be possible for the proxy server to dynamically discover the resource/method being requested.

Page 39: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 39

Web Intermediaries

• A proxy server is one example of a Web intermediary. Other examples are gateways, caches, etc. A typical Web request may be routed through multiple intermediaries.

• A Web intermediary will have a much greater chance of making reasonable decisions when a client's request shows, in the clear, the targeted resource, and the method requested is understood.

• As we have seen with SOAP both the targeted resource, as well as the requested method is nested within the SOAP envelope, thus making it much more difficult for intermediaries to do their job.

Page 40: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 40

State Transitions

• Let us consider a client application as a state machine - each resource representation that it receives causes it to transition to the next state.

S0 S1Receive Resource 1Representation

S2Receive Resource 2Representation

...

State Transition Diagram for a Client Application

Page 41: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 41

State Transitions in a REST-based Network

S0 S1

Receive Resource 1Representation

S2a

S2b

S2c

2a2b2c

Receive Resource 2aRepresentation

3a3b

S3a

S3b

Receive Resource 3bRepresentation

4a4b4c

Each resource representation containshyperlinks. Following a hyperlink takesthe client to the next state. Thus, withinthe resource representations themselves are pointers to the next states.

Page 42: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 42

State Transitions in a REST-based Network

Recall the Parts Depot example. The parts list resource returns this representation:

<?xml version="1.0"?><p:Parts xmlns:p="http://www.parts-depot.com" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.parts-depot.com http://www.parts-depot.com/parts.xsd"> <Part id="00345" xlink:href="http://www.parts-depot.com/parts/00345"/> <Part id="00346" xlink:href="http://www.parts-depot.com/parts/00346"/> <Part id="00347" xlink:href="http://www.parts-depot.com/parts/00347"/> <Part id="00348" xlink:href="http://www.parts-depot.com/parts/00348"/></p:Parts>

This document contains hyperlinks to resources that provide detailedinformation about each part. When a client application follows one of thehyperlinks it receives a representation of the resource identified by the hyperlink.This transfers the client application to the next state.

Page 43: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 43

State Transitions in a REST-based Network

Question: If I am sitting at a browser then I can understand how the decision ismade on which hyperlink to select (my brain decides). But if this isall being done programmatically, without human intervention, then howwill the decision be made on which hyperlink to select?

Answer: The XML hyperlinking technology is XLink. With this technology in addition to providing a URL to the target resource, you can also providedata about the resource you are linking to (using xlink:role). If theclient application is able to understand the semantics of xlink:role then itwill be able to make a decision on which resource to choose next.This is ultra cool - the application is dynamically making decisions about what resources to access (it is becoming a self-propelled automata).If the client application is not able to evaluate the xlink:role attributethen the decision will need to be made out-of-band (that is, the decisionmust have been made when the application was written).

Page 44: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 44

State Transitions in a SOAP-based Network

S0 S1

Receive SOAPfrom Application 1

S2a

S2b

S2c

Receive SOAPfrom Application 2a

S3a

S3b

Receive SOAPfrom Application 3b

In a pure SOAP system each SOAP messagewill be just data, no hyperlinks. Consequently,the decision on which resource to access nextmust be made out-of-band.

Page 45: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 45

State Transitions in a SOAP-based Network

Recall the Parts Depot example. The parts list resource returns this SOAP document:

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>

<p:getPartsListResponse xmlns:p="http://www.parts-depot.com"> <Parts> <Part-ID>00345<Part-ID> <Part-ID>00346<Part-ID> <Part-ID>00347<Part-ID> <Part-ID>00348<Part-ID> </Parts> <p:getPartsListResponse>

</soap:Body></soap:Envelope>

The SOAP document contains no hyperlinks. In the Web world this document is an island, cut off from the rest of the Web. Informationabout "what to do next" must be obtained elsewhere, i.e., out-of-band.

Page 46: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 46

Why don't SOAP Documents Contain Hyperlinks?

• In a pure SOAP system, that is where all accesses are to SOAP-based Web services, then each SOAP document will be an island.

• The reason for this is simple: with SOAP a URL is meaningless by itself. The URL just points to a SOAP server. To be useful the URL must be accompanied by the SOAP message.

• Note: the SOAP Working Group is currently working on a way to do SOAP HTTP GET.

Page 47: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 47

Caching (i.e., performance)

• In a network-based application it is ofterntimes the communications which are the bottleneck.

Page 48: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 48

REST and Caching

WebServer

Resource 1CacheServer

http://www.somewhere.org/Resource1

“I have a copy in mycache. Here it is.”

2a2b2c The cache shortens the distance that

the client must go to get the data, thusspeeding up the request.

Page 49: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 49

REST and Caching (cont.)

• The results of a resource request contains an indication in the HTTP header of whether these results are cacheable (and when the data will become stale).

• If the HTTP header says that it is cacheable, then cache servers can make a local copy.

• Later, if a client requests the same resource then the client can return a cached copy.

CacheServer

Desired resource

Method = GET

Forward request

Return cached copy

Page 50: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 50

SOAP and Caching• When you submit a SOAP message it is always with an HTTP POST,

even though the intent of the message may be to "get" data.– So a cache server would not know from the HTTP method whether the

client is doing a request.

• A SOAP URI is always to the SOAP server, not to the actual target

– Consequently, a cache server would not know from the URI what resource is being requested.

• Thus, with a SOAP message the cache server cannot determine (1) if data is being requested, nor (2) what resource is being requested.– Conclusion: No caching possible with SOAP!

CacheServer

URI to SOAP Server

Method = POST

"I don’t know what is the targetresource. Furthermore, I don'teven know if the resource isbeing requested. So, I mustforward the request. No caching"

Page 51: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 51

Evolving the Web (Semantic Web)

• The vision of Tim Berners-Lee is to turn the Web into a semantic Web[1].

• One of the key components of his vision is that every resource on the Web have its own URI.– Axiom 0: Universality 1

• Any resource anywhere can be given a URI

– Axiom 0a: Universality 2• Any resource of significance should be given a URI.

• The REST style is consistent with this vision - every resource has a logical URI.

• SOAP URI's are funneled through a single URI to the SOAP server.

This is not consistent with the semantic Web vision.

[1] http://www.w3.org/DesignIssues/Axioms.html

Page 52: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 52

Generic Interface

• A key feature of REST (and the Web) is that every resource have a generic interface. Namely, access to every resource is accomplished using HTTP GET, POST, PUT, and DELETE.

• We have seen how the combination of a URI and a generic method set {URI, method} enables Web components to perform useful work:– Proxy Server(URI, method) -> accept/reject

– cache(URI, method) -> forward request/return cached representation

Page 53: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 53

Generic Interface (cont.)

• With SOAP there is no defined set of methods. Each SOAP application is free to define its own set of methods.– Consequently, tools must be customized on a

per-application basis. This is not scalable. In the Web, where independent evolution and scalability are of supreme importance, this is not a very attractive idea.

Page 54: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 54

Interoperability

• The key to interoperability is standardization. The reason why independent resources on the Web are able to interoperate today is because the Web has standardized on:– Addressing and naming resources -> URI– Generic resource interface -> HTTP GET, POST, PUT,

DELETE– Resource representations -> HTML, XML, GIF, JPEG, etc– Media types -> MIME types (text/html, text/plain, etc)

• These are the standards that REST advocates.

Page 55: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 55

Interoperability (cont.)

• SOAP depends much more on customization:– Addressing and naming resources -> each SOAP

message provides its own unique method of naming a resource

– Resource interface -> each SOAP application defines its own interface

Page 56: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 56

Processing the Request/Response Payload

• With both REST and SOAP you need prior agreement on the semantics of the data.

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>

<p:getPartsListResponse xmlns:p="http://www.parts-depot.com"> <Parts> <Part-ID>00345<Part-ID> <Part-ID>00346<Part-ID> <Part-ID>00347<Part-ID> <Part-ID>00348<Part-ID> </Parts> <p:getPartsListResponse>

</soap:Body></soap:Envelope>

<?xml version="1.0"?><p:Parts xmlns:p="http://www.parts-depot.com" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.parts-depot.com http://www.parts-depot.com/parts.xsd"> <Part id="00345" xlink:href="http://www.parts-depot.com/parts/00345"/> <Part id="00346" xlink:href="http://www.parts-depot.com/parts/00346"/> <Part id="00347" xlink:href="http://www.parts-depot.com/parts/00347"/> <Part id="00348" xlink:href="http://www.parts-depot.com/parts/00348"/></p:Parts>

With both REST and SOAP client applicationswill need to understand these responses.

Page 57: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 57

Processing the Request/Response Payload

• Note: if the payload is in the format of RDF or DAML then the client application may be able to dynamically learn the meaning of the response data.

• As we saw earlier, with REST there are links to the next state built within each response. We hinted earlier at how a client application may be able to reason dynamically about which link to traverse. Thus, dynamic learning of response data combined with dynamic reasoning of link traversals would yield a self-reasoning automata. This is the next step of the Web!

Page 58: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 58

Recommendation• In the context of the Web, the REST approach is the preferred and most cost-effective

architectural style for implementing services due to its scalability, its ability to allow dynamic connectivity without prior planning, its ability to enable independent evolution of clients and services, and its built-in support for caching and security.

• There are several major problems with using SOAP in the Web environment:– The resource being targeted is not known simply from the URL. It is hidden within the SOAP message.

– The method being invoked is not known from the HTTP method. It is also hidden within the SOAP message.

– The set of methods is completely arbitrary. Every SOAP application is free to define its own set of methods.

• As we have discussed these problems create an impedance mismatch with today's Web - SOAP messages cannot be utilized by proxy servers, cache servers, etc. The lack of URLs within SOAP messages is very foreign to the Web, and isolates itself from the Web. Finally, the evolution of the Web is where every resource is identified with a URI. SOAPs clumping of resources behind a single URI is contrary to the Web vision.

• So what is the role of SOAP? SOAP is best utilized in closed systems (systems where all participants are known beforehand). In a closed system each participant can be customized to understand the APIs of the other participants and can be optimized for maximum efficiency.

Page 59: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 59

REST Best Practices1. Provide a URI for each resource that you want (or will want) exposed. This is consistent with Tim Berners-Lee's axioms for the Web,as well as the W3 TAG recommendations.

2. Prefer URIs that are logical over URIs that are physical. For example:

Prefer: http://www.boeing.com/airplanes/747 Over: http://www.boeing.com/airplanes/747.html

Logical URIs allow the resource implementation to change without impacting client applications.

3. As a corollary to (2) use nouns in the logical URI, not verbs. Resources are "things" not "actions".

4. Make all HTTP GETs side-effect free. Doing so makes the request "safe".

5. Use links in your responses to requests! Doing so connects your response with other data. It enables client applications to be"self-propelled". That is, the response itself contains info about "what's the next step to take". Contrast this to responses that do notcontain links. Thus, the decision of "what's the next step to take" must be made out-of-band.

6. Minimize the use of query strings. For example:

Prefer: http://www.parts-depot.com/parts/00345 Over: http://www.parts-depot.com/parts?part-id=00345

Rationale: the relationship between 'parts' and '00345' is clear, and you can instantiate subresources of '00345' easily; this is not possible if that information is tucked away in a query string.

Page 60: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 60

REST Best Practices (cont.)7. Use the slash "/" in a URI to represent a parent-child, whole-part relationship.

8. Use a "gradual unfolding methodology" for exposing data to clients. That is, a resource representation should provide links to obtain more details.

9. Always implement a service using HTTP GET when the purpose of the service is to allow a client to retrieve a resource representation, i.e., don't use HTTP POST.

Page 61: Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 1 REST (Representational State Transfer) Roger L. Costello XML Technologies Course

Copyright (c) [2002]. Roger L. Costello. All Rights Reserved. 61

References

• Paul Prescod has written several excellent articles on REST:– Second Generation Web Services

• http://www.xml.com/pub/a/2002/02/06/rest.html

– REST and the Real World• http://www.xml.com/pub/a/2002/02/20/rest.html

– SOAP, REST and Interoperability• http://www.prescod.net/rest/standardization.html

– Evaluating XML for Protocol Control Data• http://www.prescod.net/xml/envelopes/