REST Υπηρεσίες Εργαστήριο Ανάλυσης Συστημάτων και Τεχνολογίας Λογισμικού S 3 Laboratory
Περιεχόμενα 1. Εισαγωγή στις REST Υπηρεσίες 2. Χαρακτηριστικά μοντέλου 3. Πρωτόκολλα JSON WADL 4. REST Υπηρεσίες vs. SOAP Services 5. REST Υπηρεσίες στο Η-Εμπόριο S3 Lab- REST Υπηρεσίες Εισαγωγή 2
Απαρχή του μοντέλου... Ο όρος Representational State Transfer καθιερώθηκε το 2000 από τον Roy Fielding στην διδακτορική του διατριβή Το Representational State Transfer (REST) είναι η αρχιτεκτονική ανάπτυξης κατανεμημένων, διαδικτυακών εφαρμογών που κυριαρχεί στο World Wide Web αναδεικνύει τον τρόπο με τον οποίο η αρχιτεκτονική του Web προέκυψε από τις συνδιαλλαγές των τεσσάρων βασικών συστατικών του, δλδ: origin servers, gateways, proxies and clients Καθορίζει την συμπεριφορά των συμμετεχόντων S3 Lab- REST Υπηρεσίες Εισαγωγή 3
Βασικά στοιχεία Χρήση του URI. Τα σημεία συνδιαλλαγής με RESTful υπηρεσίες προσφέρονται ως πόροι στο διαδίκτυο Οι πόροι αυτοί καθορίζονται από URIs, τα οποία υποστηρίζουν την απαραίτητη μοναδική διευθυνσιοδότηση πόρων και υπηρεσιών Κοινή διεπαφή. Το μοντέλο στηρίζεται στην χρήση τεσσάρων βασικών λειτουργιών του HTTP πρωτοκόλλου PUT, GET, POST, and DELETE S3 Lab- REST Υπηρεσίες Εισαγωγή 4
Βασικά στοιχεία Αυτοπροσδιοριζόμενα μηνύματα. Οι προσφερόμενοι πόροι ανεξαρτητοποιούνται από την αναπαράσταση τους Resources έτσι ώστε το περιεχόμενο τους να μπορεί να προσφερθεί σε διάφορες μορφές (e.g., HTML, XML, plain text). Μετα-πληροφορίες διατίθενται για διάφορους σκοπούς, π.χ. έλεγχος caching, αναζήτηση σφαλμάτων μετάδοσης, διαπραγμάτευση αναπαράστασης, κτλ. Stateful συνδιαλλαγές. Κάθε συνδιαλλαγή με έναν πόρο είναι stateless, i.e., τα αιτήματα selfcontained. Οι Stateful συναλλαγές υποστηρίζονται μέσω ρητής μεταφοράς κατάστασης. Η μεταφορά κατάστασης μπορεί να υποστηριχθεί με διάφορους τρόπους, π.χ., URI αναγραφή, cookies, and κρυφά πεδία. Η πληροφορία κατάστασης μπορεί να μεταφερθεί στις απαντήσεις για να είναι δυνατός ο προσδιορισμός επόμενων αποδεκτών καταστάσεων. S3 Lab- REST Υπηρεσίες Εισαγωγή 5
Περιορισμοί Client Server: Η κοινή διεπαφή επιτρέπει την ανεξαρτησία των πελατών από του εξυπηρετητές. Η ανεξαρτησία αυτή επιτρέπει οι πελάτες να μην απασχολούνται με λειτουργίες όπως η αποθήκευση της πληροφορίας, η οποία αποκρύπτεται από τους εξυπηρετητές, επιτρέποντας την μεταφερσιμότητα του κώδικα των πελατών. Οι εξυπηρετητές δεν απασχολούνται με λειτουργίες σχετικές με την διεπαφή χρηστών η την κατάσταση του πελάτη και κατ αυτό τον τρόπο έχουν απλούστερη υλοποίηση και δυνατότητες για κλιμάκωση Οι πελάτες και οι εξυπηρετητές μπορούν να υλοποιηθούν ανεξάρτητα ο ένας από τον άλλο όσο δεν αλλάζει η μεταξύ τους διεπαφή Stateless: Κάθε αίτημα ενός πελάτη περιέχει όλη την απαραίτητη πληροφορία για την εξυπηρέτηση του και το ιστορικό κατάστασης που διατηρεί Ο εξυπηρετητής μπορεί να είναι stateful; ο περιορισμός αυτός απαιτεί την χρήση URL για την επικοινωνία με τον εξυπηρετητή ως δικτυακό πόρο. Κατ αυτό τον τρόπο είναι πιο εύκολη η διαχείριση των εξυπηρετητών καθώς και η αντιμετώπιση προβλημάτων δικτύου και κλιμάκωσης S3 Lab- REST Υπηρεσίες Εισαγωγή 6
Περιορισμοί συνεχ. Cacheable: Κάθε πληροφορία του World Wide Web μπορεί να αποθηκευτεί προσωρινά. Οι απαντήσεις θα πρέπει να μπορούν να ορίζουν ότι είτε επιτρέπουν να γίνονται cache είτε όχι έτσι ώστε να αποτρέπεται η χρήση μη έγκυρης πληροφορίας Καλά-ορισμένο μερικό ή πλήρες caching μπορεί βελτιώσει σημαντικά την κλιμάκωση και την απόδοση του συστήματος Layered system: Ο πελάτης δεν μπορεί να διακρίνει εάν είναι απευθείας συνδεδεμένος με τον τελικό εξυπηρετητή ή με άλλους ενδιάμεσους Η χρήση ενδιάμεσων servers επιφέρει σημαντική βελτίωση κλιμάκωση και την απόδοση του συστήματος επιτρέποντας load balancing και shared caches S3 Lab- REST Υπηρεσίες Εισαγωγή 7
Περιορισμοί συνεχ. Code on demand: (optional) Οι εξυπηρετητές επιτρέπεται να παροδικά να επεκτείνουν/διαμορφώνουν την λειτουργικότητα του πελάτη μεταφέροντας λογική που μπορεί να εκτελεστεί. Παραδείγματα από κώδικα μπορεί να είναι περιεγραμμένα ώς Java applets ή client-side scripts σε JavaScript Uniform interface: Η κοινή διεπαφή διευκολύνει και αποσυνδέει τα συστατικά της αρχιτεκτονικής του συστήματος, επιτρέποντας την εξέλιξη τους S3 Lab- REST Υπηρεσίες Εισαγωγή 8
RESTful Υπηρεσίες Ιστού A RESTful web service (also called a RESTful web API) is a simple web service implemented using HTTP and the principles of REST It is a collection of resources, with three defined aspects: the base URI for the web service the MIME type of the data supported by the web service. E.g. JSON, XML but can be any other valid MIME type the set of operations supported by the web service using HTTP methods (i.e. POST, GET, PUT or DELETE) S3 Lab- REST Υπηρεσίες Εισαγωγή 9
Υποστηριζόμενες λειτουργίες S3 Lab- REST Υπηρεσίες Εισαγωγή 10
Παραδείγματα Rest Υπηρεσιών A directory with many services is available at: http://www.programmableweb.com/apis/directory Amazon Services: https://developer.amazonservices.com Delicious: http://www.delicious.com/help/api S3 Lab- REST Υπηρεσίες Εισαγωγή 11
Προσεγγίσεις ανάπτυξης Hi-REST advocates using all of the 4 verbs (GET, POST, PUT, DELETE); recommends the use of (so-called) nice URIs suggests the use of Plain Old XML (POX) for formatting the content of messages Lo-REST focuses on the minimum common denominator. Thus, only 2 verbs (GET for idempotent requests, and POST for everything else) are used S3 Lab- REST Υπηρεσίες Εισαγωγή 12
Προσεγγίσεις ανάπτυξης συνεχ. Lo-Rest pros: Firewalls may not always allow HTTP connections that use any other verb POST and GET are the only two verbs that can be used in the method attribute of an XHTML form Lo-Rest cons: Restrictions have led to a series of workarounds, where the real verb is sent using either of a special HTTP header (X-HTTP-Method-Override) or a hidden form field ( method) Workarounds may not be understood by all Web servers, and require additional development and testing effort The message payload format in Lo-REST enforces the use of MIME-Types, but does not restrict the data to be in a particular format S3 Lab- REST Υπηρεσίες Εισαγωγή 13
JSON JavaScript Object Notation
JSON JavaScript Object Notation (JSON) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language [Standard ECMA-262 3rd Edition] The JSON format was originally specified by Douglas Crockford, and is described in IETF RFC 4627 (https://tools.ietf.org/html/rfc4627) JSON's design goals were to be minimal, portable, textual, and a subset of JavaScript. S3 Lab- REST Υπηρεσίες Εισαγωγή 15
JSON συνεχ. It is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages It is built on two structures: A collection of name/value pairs, i.e. this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array in programming languages An ordered list of values, i.e. this is realized as an array, vector, list, or sequence in various programming languages S3 Lab- REST Υπηρεσίες Εισαγωγή 16
Στοιχεία του JSON An object is an unordered set of name/value pairs An array is an ordered collection of values A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. S3 Lab- REST Υπηρεσίες Εισαγωγή 17
Στοιχεία του JSON συνέχ. A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used S3 Lab- REST Υπηρεσίες Εισαγωγή 18
JSON Παράδειγμα {"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }} S3 Lab- REST Υπηρεσίες Εισαγωγή 19
XML Αναπαράσταση <menu id="file" value="file"> <popup> <menuitem value="new" onclick="createnewdoc()" /> <menuitem value="open" onclick="opendoc()" /> <menuitem value="close" onclick="closedoc()" /> </popup> </menu> S3 Lab- REST Υπηρεσίες Εισαγωγή 20
JSON Παράδειγμα { "glossary": { "title": "example glossary", "GlossDiv": {"title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to \create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } } S3 Lab- REST Υπηρεσίες Εισαγωγή 21
XML Αναπαράσταση <!DOCTYPE glossary PUBLIC "-//OASIS//DTD DocBook V3.1//EN"> <glossary><title>example glossary</title> <GlossDiv><title>S</title> <GlossList> <GlossEntry ID="SGML" SortAs="SGML"> <GlossTerm>Standard Generalized Markup Language</GlossTerm> <Acronym>SGML</Acronym> <Abbrev>ISO 8879:1986</Abbrev> <GlossDef> <para>a meta-markup language, used to create markup languages such as DocBook.</para> <GlossSeeAlso OtherTerm="GML"> <GlossSeeAlso OtherTerm="XML"> </GlossDef> <GlossSee OtherTerm="markup"> </GlossEntry> </GlossList> </GlossDiv> </glossary> S3 Lab- REST Υπηρεσίες Εισαγωγή 22
Έλεγχος JSON δεδομένων - JSON Schema JSON Schema is a specification for a JSON-based format for defining the structure of JSON data IETF draft It provides a contract for what JSON data is required for a given application and how it can be modified It is intended to provide validation, documentation, and interaction control of JSON data JSON Schema is based on the concepts from XML Schema, RelaxNG, and Kwalify, but is intended to be JSON-based, so that JSON data in the form of a schema can be used to validate JSON data, the same serialization/deserialization tools can be used for the schema and data, and it can be self descriptive. S3 Lab- REST Υπηρεσίες Εισαγωγή 23
JSON-Schema example S3 Lab- REST Υπηρεσίες Εισαγωγή 24
JSON vs XML Simplicity: JSON is much simpler than XML, it has a much smaller grammar and maps more directly onto the data structures used in modern programming languages Extensibility: it is not a document markup language, so it is not necessary to define new tags or attributes to represent data in it Interoperability: it has the same interoperability potential as XML Openness: JSON is at least as open as XML, because it is not in the center of corporate/political standardization struggles S3 Lab- REST Υπηρεσίες Εισαγωγή 25
Επεκτάσεις του JSON Several extensions to JSON have emerged catering for many additional features. A list of them includes: JSONP: or "JSON with padding" is a complement to the base JSON data format, a usage pattern that allows a page to request and more meaningfully use JSON from a server other than the primary server BSON: is a binary form for representing simple data structures and associative arrays (called objects or documents) GeoJSON: is an open format for encoding a variety of geographic data structures JSON-RPC: is a remote procedure call protocol encoded in JSON SOAPjr: is a protocol specification for exchanging structured information in the implementation of Web services in computer networks. It is a hybrid of SOAP and JSON-RPC S3 Lab- REST Υπηρεσίες Εισαγωγή 26
WADL
Web Application Description Language (WADL) WADL is designed to provide a machine process-able description of HTTPbased Web applications Currently is W3C Member Submission (31 August 2009), by Sun Microsystems In the context of WADL a Web application is defined as a HTTP-based application whose interactions are amenable to machine processing Web applications: Are based on existing Web architecture and infrastructure Are platform and programming language independent Promote re-use of the application beyond the browser Enable composition with other Web or desktop applications Require semantic clarity in content (representations) exchanged during their use S3 Lab- REST Υπηρεσίες Εισαγωγή 28
WADL Στοιχεία Αpplication: element forms the root of a WADL description Doc: may contain text and zero or more child elements that form the body of the documentation Grammars: element acts as a container for definitions of the format of data exchanged during execution of the protocol described by the WADL document Resources element acts as a container for the resources provided by the application. Resource_Type element describes a set of methods that, together, define the behavior of a type of resource. A resource_type may be used to define resource behavior that is expected to be supported by multiple resources. S3 Lab- REST Υπηρεσίες Εισαγωγή 29
WADL Στοιχεία συνεχ. Resource: element describes a set of resources, each identified by a URI that follows a common pattern Param element describes a parameterized component of its parent element. A param element can either be a parameter definition or a reference to a parameter defined elsewhere Method element describes the input to and output from an HTTP protocol method that may be applied to a resource. A method element can either be a method definition or a reference to a method defined elsewhere Representation element describes a representation of a resource's state. A representation element can either be a representation definition or a reference to a representation defined elsewhere S3 Lab- REST Υπηρεσίες Εισαγωγή 30
WADL δομή S3 Lab- REST Υπηρεσίες Εισαγωγή 31
WADL Παράδειγμα 1 <?xml version="1.0"?> 2 <application xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 3 xsi:schemalocation="http://wadl.dev.java.net/2009/02 wadl.xsd" 4 xmlns:tns="urn:yahoo:yn xmlns:xsd="http://www.w3.org/2001/xmlschema" 6 xmlns:yn="urn:yahoo:yn xmlns:ya="urn:yahoo:api xmlns="http://wadl.dev.java.net/2009/02"> 9 <grammars> 10 <include 11 href="newssearchresponse.xsd"/> 12 <include 13 href="error.xsd"/> 14 </grammars> 16 <resources base="http://api.search.yahoo.com/newssearchservice/v1/"> 17 <resource path="newssearch"> 18 <method name="get" id="search"> 19 <request> 20 <param name="appid" type="xsd:string" 21 style="query" required="true"/> 22 <param name="query" type="xsd:string" 23 style="query" required="true"/> 24 <param name="type" style="query" default="all"> 25 <option value="all"/> 26 <option value="any"/> 27 <option value="phrase"/> 28 </param> S3 Lab- REST Υπηρεσίες Εισαγωγή 32
WADL Παράδειγμα συνεχ. 29 <param name="results" style="query" type="xsd:int" default="10"/> 30 <param name="start" style="query" type="xsd:int" default="1"/> 31 <param name="sort" style="query" default="rank"> 32 <option value="rank"/> 33 <option value="date"/> 34 </param> 35 <param name="language" style="query" type="xsd:string"/> 36 </request> 37 <response status="200"> 38 <representation mediatype="application/xml" 39 element="yn:resultset"/> 40 </response> 41 <response status="400"> 42 <representation mediatype="application/xml" 43 element="ya:error"/> 44 </response> 45 </method> 46 </resource> 47 </resources> 49 </application> S3 Lab- REST Υπηρεσίες Εισαγωγή 33
REST vs SOAP Υπηρεσίες Ιστού
Βασικές διαφορές How is HTTP considered: In the context of REST, the Web is seen as the universal medium for publishing globally accessible information In the context of WS Web is seen as the universal transport medium for messages, which are exchanged between Web services endpoints of published applications Dealing with heterogeneity: In the context of REST, the Web is a rather uniform client/server environment, where all components speak the same protocol, i.e. HTTP WS originate from a more complex and heterogeneous domain, the one of enterprise computing Loose Coupling: In the context of REST services exclusively focus on RPC-like, synchronous interactions; in other words, when an HTTP server is down, its clients will be affected as their HTTP requests fail In the context of WS, the underlying message bus makes it possi- ble to achieve such degree of loose coupling as messages can be transferred using persistent, reliable queues S3 Lab- REST Υπηρεσίες Εισαγωγή 35
Εννοιολογικές διαφορές Conceptual differences on interface level: REST constrains the interface of a resource to its generic uniform interface with predefined operations. Thus, apparently no decision has to be made concerning what are the available operations. Designers are advised to concentrate their effort on defining the exposed resources In the context of WS, the porttype of each service must be designed carefully, so that it describes the service functionality in an understandable way Conceptual differences on design process: For REST services one has to: a) identify the related resources, b) design properly the URIs, c) Identify the proper resource interaction semantics i.e. get/post/put/delete, d) define resource relationships and e) define data representation mechanisms For WS one has to: v) specify the XML messages, b) determine the Message Exchange Patterns and c) define the set of actions exposed by the service interface S3 Lab- REST Υπηρεσίες Εισαγωγή 36
Τεχνολογικές διαφορές Transportation protocols: In RESTful Web service, there is no choice but to build services that communicate using HTTP WS are transport independent, which allows SOAP messages to be exchanged using a variety of transport protocols Message Formating: RESTful Web services currently do not use a single format for representing resources WS use standardized message format, i.e. SOAP Service Identification: RESTful Web services leverage the URI standard as the nam- ing mechanism to address resources WS, just recently, started using WS-Addressing that was introduced to represent addressing information through the definition of end-point references Service Description: Web services rely on a standard, machine-process- able, strongly-typed XML interface description language (WSDL) RESTful services have adopted a more human-oriented approach based on informal, textual descriptions, giving developers extensive documentation of the API of the provided service S3 Lab- REST Υπηρεσίες Εισαγωγή 37
Τεχνολογικές διαφορές συνεχ. Quality/Reliability In the context of WS a set of optional specifications covering the QoS properties of messages exchanged is already available No framework compatible to WS is available for REST services Composition In the context of WS languages and tools specifically targeting the composition of WS services are already available, e.g. WS-BPEL The composition of RESTful services is the main focus of so-called Web 2.0 Mashups Discovery Universal Description, Discovery, and Integration (UDDI) registries are available for WS service discovery There is no specific mechanims for RESTful services, apart from the existing Web search engines S3 Lab- REST Υπηρεσίες Εισαγωγή 38
RESTful Υπηρεσίες σε Υπηρεσιοστρεφείς Διαδικασίες
Προβλήματα WS-BPEL: Ισχυρή σύνδεση με την γλώσσα WSDL Υποστήριξη μόνο XML μηνυμάτων RESTful Services Όχι απαραίτητη περιγραφή της υπηρεσίας Γλώσσες περιγραφής περιλαμβάνουν WADL, RSDL Πολλαπλά mime types για τα ανταλλασσόμενα μηνύματα JSON, XML, HTML S3 Lab- REST Υπηρεσίες Εισαγωγή 40
Πιθανοί τρόποι χρήσης Χρήση RESTful υπηρεσιών σε WS-BPEL μόνο κάτω από τις εξής συνθήκες XML encoding για τα ανταλλασσόμενα μηνύματα HTTP Bindings και URL Encodings για την επικοινωνία Virtual WSDL περιγραφές για την υπηρεσία ODE service orchestration engine Με WSDL επεκτάσεις για το binding με RESTful υπηρεσίες S3 Lab- REST Υπηρεσίες Εισαγωγή 41
Υπηρεσίες κ Κατάσταση (State) Stateless Χωρίς Κατάσταση Stateful Με Κατάσταση WS REST P2P GRID OGC S3 Lab- REST Υπηρεσίες Εισαγωγή 42
Μοντέλα Κλήσης Υπηρεσιών Υπηρεσίες Ιστού Αυτόνομες/Ανεξάρτητες κλήσεις Client Provider Rest/OGC Υπηρεσίες Αλυσιδωτές κλήσεις Client Provider Results Page Υπηρεσίες Πλέγματος Κλήσεις με διατήρηση κατάστασης Client + Resource Provider Υπηρεσίες Ομότιμου-προς-Ομότιμο Κλήσεις μόνο εντός του δικτύου Client Node Provider Node Κλήση Απόκριση P2P Network S3 Lab- REST Υπηρεσίες Εισαγωγή 43