Slideshare.net (beta)

 

All comments

Add a comment on Slide 1

If you have a SlideShare account, login to comment; else you can comment as a guest


Showing 1-50 of 0 (more)

Rest Overview

From swamy_g, 4 months ago

467 views  |  0 comments  |  0 favorites  |  5 downloads
 

Tags

rest api

 
 

Groups / Events

 

 
Embed
options

More Info

This slideshow is Public
Total Views: 467
on Slideshare: 467
from embeds: 0

Slideshow transcript

Slide 1: REST For Web Services Course, Fall 2005 Henning Niss IT University of Copenhagen Lecture 7 Henning Niss (ITU) REST Lecture 7 1 / 34

Slide 2: Material Main material (curriculum / “pensum”): Fielding & Taylor: Principled Design of the Modern Web Architecture, ACM Transactions on Internet Technology, Vol 2, No. 2, May 2002, pages 115-150. http://www.ics.uci.edu/˜taylor/documents/2002-REST-TOIT.pdf Supplementary material: REST Wiki: http://rest.blueoxen.net/cgi-bin/wiki.pl Paul Prescod: Second Generation Web Services, xml.com, February 2002. http://webservices.xml.com/pub/a/ws/2002/02/06/rest.html Roger L. Costello: Building Web Services the REST Way, July 2002. http://www.xfront.com/REST-Web-Services.html Henning Niss (ITU) REST Lecture 7 2 / 34

Slide 3: REST Overview Overview REST Overview Example: RESTful Web services The REST architectural style Example: REST in the Joke server REST Overview Example: RESTful Web services The REST architectural style Example: REST in the Joke server Henning Niss (ITU) REST Lecture 7 3 / 34

Slide 4: REST Overview Introduction REST is an architectural style. “packaged set of architectural design decisions”, a software architecture abstracts runtime elements of a system. Goals: idealized model of interactions within a Web application, function as guiding principles to avoid flaws, minimize network latency and communication, maximize independence and scalability. Most prominent example: the modern Web. Focus today: REST from a Web services perspective. Henning Niss (ITU) REST Lecture 7 4 / 34

Slide 5: REST Overview REST-based Web services Very high-level description of REST-based Web services: resource-centric XML-over-HTTP services. endpoints are resources; endpoints are accessible via standard HTTP; endpoints are represented in XML. Henning Niss (ITU) REST Lecture 7 5 / 34

Slide 6: Example: RESTful Web services Applying REST — example The successful company Parts Depot, Inc sells many different parts to many customers. They would like well-designed Web services for this. Parts Depot Web services: get list of parts; get detailed part information; submit purchase orders (PO). Example due to R.L. Costello: Building Web Services the REST Way. Henning Niss (ITU) REST Lecture 7 7 / 34

Slide 7: Example: RESTful Web services Getting a part list The list of parts available from Parts Depot is a resource: a resource is identified via a URI; resource representations are available via standard HTTP GET: GET http://www.parts-depot.com/parts HTTP/1.1 representations may be adapted to consumer: <?xml version="1.0"?> <p:Parts xmlns:p="http://www.parts-depot.com" xmlns:xl="http://www.w3.org/1999/xlink"> <Part id="0345" xl:href="http://www.parts-depot.com/parts/0345"/> <Part id="0346" xl:href="http://www.parts-depot.com/parts/0346"/> <Part id="0347" xl:href="http://www.parts-depot.com/parts/0347"/> <Part id="0348" xl:href="http://www.parts-depot.com/parts/0348"/> </p:Parts> a representation typically contains links to other resources. Henning Niss (ITU) REST Lecture 7 8 / 34

Slide 8: Example: RESTful Web services Getting part information Detailed part information is also a resource identified with a URL. getting detailed information: GET http://www.parts-depot.com/parts/0346 HTTP/1.1 representation: <?xml version="1.0"?> <p:Part xmlns:p="http://www.parts-depot.com" xmlns:xl="http://www.w3.org/1999/xlink"> <Part-ID>0346</Part-ID> <Name>Widget-A</Name> <Desc>This part is used within the frap assembly</Desc> <Spec xl:href="http://www.parts-depot.com/parts/0346/spec"/> <UnitCost currency="USD">0.10</UnitCost> </p:Part> how representation is generated is unspecified; logical URI (not necessarily static pages). Henning Niss (ITU) REST Lecture 7 9 / 34

Slide 9: Example: RESTful Web services Submitting a purchase order To order a part, the client submits a purchase order. an order submitted using standard HTTP POST; the order is an XML document <?xml version="1.0"?> <p:Order xmlns:p="http://www.parts-depot.com" xmlns:xl="http://www.w3.org/1999/xlink"> <Part xl:href="http://www.parts-depot.com/parts/0346"/> <Quantity>10</Quantity> <Date>2005-10-11</Date> ... </p:Order> an order is shared information between Parts Depot and client; the Web service returns a URI for the submitted order; the client may delete the order using HTTP DELETE. Henning Niss (ITU) REST Lecture 7 10 / 34

Slide 10: Example: RESTful Web services Assessment (example) Salient points of the example: focus on resources (resource-centric vs API-centric); resources are identified using URIs (“name everything”); resources are connected through links (“reveal gradually”); resources may have different representations (HTML, XML, . . . ); based on standard technology (URI, HTTP, XML, . . . ); separation of updatable and immutable resources (POST & GET). Henning Niss (ITU) REST Lecture 7 11 / 34

Slide 11: The REST architectural style The name REST The name “Representational State Transfer” is intended to evoke an image of how a well-designed Web application behaves: a network of Web pages forms a virtual state machine, allowing a user to progress through the application by selecting a link or submitting a short data-entry form, with each action resulting in a transition to the next state of the application by transferring a representation of that state to the user. (Fielding & Taylor, 2002) Henning Niss (ITU) REST Lecture 7 13 / 34

Slide 12: The REST architectural style WWW Requirements The major goal of the Web was to be a shared information space for people and machines to communicate. Low entry-barrier. Extensibility. Distributed hypermedia. Internet scale (scalable & independent). REST is an architectural style for “architecting the Web”: “capture” the success of the Web; guide future extensions. Henning Niss (ITU) REST Lecture 7 14 / 34

Slide 13: The REST architectural style REST Constraints (1) REST consists of a number of architectural constraints: client/server constraint: separation of concerns independent evolution of components; stateless constraint: visibility (single request reveals everything) reliability (easier to recover from failures) scalability (server does not commit resources to each request) degrades network performance (by requiring large requests); caching constraint: improves network efficiency (eliminates interactions) clients have the “right” to reuse cacheable data degrades reliability (due to stale data); Henning Niss (ITU) REST Lecture 7 15 / 34

Slide 14: The REST architectural style REST Constraints (2) uniform interface constrain: general component interface implementations decoupled from interfaces degrades efficiency (to adapt to the uniform interface); layered system constraint: architecture can be composed hierarchically overall system complexity bounded added overhead; (code-on-demand) Henning Niss (ITU) REST Lecture 7 16 / 34

Slide 15: The REST architectural style Architectural elements Three classes of architectural elements: processing elements (components); data elements (resources); connecting elements (connectors). REST is concerned with architecture; for example component implementation left to service providers; protocol syntax left to HTTP and XML. Henning Niss (ITU) REST Lecture 7 17 / 34

Slide 16: The REST architectural style Resources A resource is “anything that can be named”. a document, “today’s weather in Los Angeles”, a collection of other resources. Formally: membership function mapping a time t to a set of values (“representations”). the empty set (resource not yet realized), an XML representation and a HTML representation, a(nother) resource identifier (“redirect”). Resources identified or named by resource identifiers (URIs). The identifier (not the content) captures the “semantics” of the resource. Henning Niss (ITU) REST Lecture 7 18 / 34

Slide 17: The REST architectural style Representations A representation captures the current state of a resource. A representation is computed (“rendered”) when a resource is requested (“late binding”). Where? process data where it is located and send result to client, encapsulate data in an object and send object to client, send data and metadata so client can determine processing. All may be employed by REST. A representation is both data and metadata. representation data typically in XML; metadata typically in HTTP headers. Henning Niss (ITU) REST Lecture 7 19 / 34

Slide 18: The REST architectural style Connectors A connector is an abstract interface for network communication. Key requirement: stateless. each request includes all necessary information to process it, ¯ no resources need be held, ¯ need no knowledge of interaction semantics with other requests, ¯ intermediaries may inspect communication in isolation. Interface: in params: control data, resource id, optional resource repr, out params: control data, optional resource/repr metadata, optional resource repr. Synchronous invocation, but may use streams for input and output. Example connectors: client, server, cache. Henning Niss (ITU) REST Lecture 7 20 / 34

Slide 19: The REST architectural style Processing elements Processing elements (components) “performs” requests and responses. Example processing elements: user agent and origin server. A user agent (uses a client connector) sends requests and receives replies; renders the result; example: Web browser. An origin server (uses a server connector) serves requested resources; resource implementation details not revealed; must be ultimate endpoint for updates. Example: Proxies (acting as both clients and servers). Henning Niss (ITU) REST Lecture 7 21 / 34

Slide 20: The REST architectural style Architectural views Views describe how the architectural elements works together. A process view shows how data flows through the system. A connector view shows how components communicate. A data view shows how the state of the system evolves. Henning Niss (ITU) REST Lecture 7 22 / 34

Slide 21: The REST architectural style Example view (process view) Henning Niss (ITU) REST Lecture 7 23 / 34

Slide 22: Example: REST in the Joke server Web of Jokes Infrastructure for a world-wide network of servers and clients for authoring, publishing, and reading jokes. Henning Niss (ITU) REST Lecture 7 25 / 34

Slide 23: Example: REST in the Joke server Operations Joke server operations: list (joke categories); retrieve(category) (jokes of a specified category); submit() (joke in body of POST request). Henning Niss (ITU) REST Lecture 7 26 / 34

Slide 24: Example: REST in the Joke server Joke server (List) public class List extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { try { resp.setContentType("text/xml"); Document doc = new Jokes(getServletContext()).getJokes(); String xslt = getServletContext().getInitParameter("CategoriesXSLT"); XSLTransformer t = new XSLTransformer(xslt); new XMLOutputter().output(t.transform(doc), resp.getWriter()); } catch (Exception e) { resp.sendError(500, "Internal error"); } } } Henning Niss (ITU) REST Lecture 7 27 / 34

Slide 25: Example: REST in the Joke server Joke server (List stylesheet) <xsl:stylesheet version="1.0" xmlns="http://www.brics.dk/ixwt/categories" xmlns:jml="http://www.brics.dk/ixwt/jokes" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="jml:collection"> <categories> <xsl:apply-templates select= "jml:joke[not(@category=following::jml:joke/@category)]"/> </categories> </xsl:template> <xsl:template match="jml:joke"> <category> <xsl:value-of select="@category"/> </category> </xsl:template> </xsl:stylesheet> Henning Niss (ITU) REST Lecture 7 28 / 34

Slide 26: Example: REST in the Joke server Joke server (Retrieve) public class Retrieve extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { try { resp.setContentType("text/xml"); Document doc = new Jokes(getServletContext()).getJokes(); Transformer t = TransformerFactory.newInstance() .newTransformer(new StreamSource(new File( getServletContext().getInitParameter("RetrieveXSLT")))); t.setParameter("category", req.getParameter("category")); t.transform(new JDOMSource(doc), new JDOMResult()); new XMLOutputter().output(out.getDocument(), resp.getWriter()); } catch (Exception e) { resp.sendError(500, "Internal error"); } } } Henning Niss (ITU) REST Lecture 7 29 / 34

Slide 27: Example: REST in the Joke server Joke server (Submit) public class Submit extends HttpServlet { static Namespace jml = Namespace.getNamespace("http://www.brics.dk/ixwt/jokes"); public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { PrintWriter out = resp.getWriter(); SAXBuilder b = new SAXBuilder(); // set schema validation properties ... try { Document doc = b.build(req.getInputStream()); if (!doc.getRootElement().getNamespaceURI().equals(jml)) resp.sendError(400, "Wrong namespace of root element!"); else new Jokes(getServletContext()).addJokes(doc); } catch (Exception e) { resp.sendError(500, "Internal error"); } } } Henning Niss (ITU) REST Lecture 7 30 / 34

Slide 28: Example: REST in the Joke server Jokes database (I) public class Jokes { static Namespace jml = Namespace.getNamespace("http://www.brics.dk/ixwt/jokes"); ServletContext context; String jokeFile; Document jokes; public Jokes(ServletContext ctx) throws JDOMException, IOException { context = ctx; jokes = (Document)ctx.getAttribute("jokes"); jokeFile = ctx.getInitParameter("JokeFile"); if (jokes==null) { try { jokes = new SAXBuilder().build(new File(jokeFile)); } catch (Exception e) { jokes = new Document(new Element("collection", jml)); } ctx.setAttribute("jokes", jokes); } } Henning Niss (ITU) REST Lecture 7 31 / 34

Slide 29: Example: REST in the Joke server Jokes database (II) public Document getJokes() { return jokes; } public void addJokes(Document m) throws JDOMException, IOException { synchronized(context) { jokes.getRootElement() .addContent(m.getRootElement().removeContent()); new XMLOutputter() .output(jokes, new FileOutputStream(new File(jokeFile))); } } } Henning Niss (ITU) REST Lecture 7 32 / 34

Slide 30: Example: REST in the Joke server Summary resource-centric; resources are named (identified); representations rather than resources; based on standardized protocol (HTTP); focus on architecture rather than implementation detail. Henning Niss (ITU) REST Lecture 7 33 / 34