Final Rest
Final Rest
Final Rest
REST is a design pattern. It is a certain approach for creating Web Services. To understand the REST design pattern, let's look at an example
client
Frequent Flyer Members client Regular Members
Approach 1 Disadvantages
There is currently no industry accepted practice (rules) for expressing priorities, so rules would need to be made. The clients must learn the rule, and the Web service application must be written to understand the rule.
This approach is based upon the incorrect assumption that a URL is "expensive" .
Axiom 0: all resources on the Web must be uniquely identified with a URI.
URL1
resource1
URL2
resource2
URL3
resource3
http://www.kings-air/reservations/frequent-flyer
Approach 2 Advantages
The different URLs are discoverable by search engines and UDDI registries. It's easy to understand what each service does simply by examining the URL, i.e., it exploits the Principle of Least Surprise. There is no need to introduce rules. Priorities are elevated to the level of a URL. "What you see is what you get." It's easy to implement high priority - simply assign a fast machine at the premier member URL. There is no bottleneck. There is no central point of failure.
Premier Members
Reservation Web
Determine Priority
Service
client
Regular Members
http://www.kings-air/reservations/frequent-flyer
client
REST Fundamentals
The data that a Web service returns should link to other data. Thus, design your data as a network of information. Create a resource for every service. Identify each resource using a URL
Resource
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 in a new state. When the client selects a hyperlink in Boeing747.html, it accesses another resource. The new representation places the client application into yet another state. Thus, the client application transfers state with each resource representation.
Learn by Example
The REST design pattern is best explained with an example. Example :A company deploying three Web services using the REST design pattern.
URL 1
HTTP response
Parts List
Web Server
Part Data
HTTP POST
PO (HTML/XML)
URL 3
PO HTTP response
URL to submitted PO
Note that the parts list has links to get detailed info about each part. This is a key feature of the REST design pattern. The client transfers from one state to the next by examining and choosing from among the alternative URLs in the response document.
Again observe how this data is linked to still more data - the specification for this part may be found by traversing the hyperlink. Each response document allows the client to drill down to get more detailed information.
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.
HTTP POST
URL 1 getPartsList()
HTTP Response
URL 1
HTTP Response
Web Server
HTTP POST
SOAP Server
getPart(id)
PO (XML doc)
HTTP POST
URL 1 submit(PO)
HTTP Response
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.
HTTP POST
getPartsList()
HTTP POST
URL 1
SOAP Server
getPart(id)
submit(PO)
HTTP POST
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
</soap:Body> </soap:Envelope>
Then the client will HTTP POST this document to the SOAP server at: http://www.parts-depot.com/soap/servlet/messagerouter The SOAP server takes a quick peek into this document to determine what procedure to invoke.
Note the absence of links. Why is this? A URL that points to a SOAP service is meaningless since the URL to a SOAP service is just to the SOAP server. Thus, the URL would need to be supplemented with some indication of which method to invoke at that URL. [Note: of course this response could contain a URL to a REST-ful service.]
Again, the client will HTTP POST this document to the SOAP server at: http://www.parts-depot.com/soap/servlet/messagerouter Note 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.
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.
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. 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.
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 i.e.company policy prohibits accessing Resource 1 using the company's lines
http://www.somewhere.org/Resource1
Proxy Server
Web Server
Resource 2
Method
Proxy Server I cant determine if the message is allowed since I dont know what Resource it is targeting [that information is hidden in the Envelope]
Web Server
SOAP Server
Resource 2
Resource 3
Does this mean that the client is trying to access Resource 1? The proxy server must understand the semantics of every SOAP application!
http://www.somewhere.org/Resource1
Cache Server
Web Server
Resource 1
The cache shortens the distance that the client must go to get the data, thus speeding up the request.
Desired resource
Forward request
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!
Cache Server
Method = POST
"I dont know what is the target resource. Furthermore, I don't even know if the resource is being requested. So, I must forward the request. No caching"