47
How XQuery and AtomPub Are About To Change Your World

RESTful Services

  • View
    3.195

  • Download
    2

Embed Size (px)

DESCRIPTION

This talk was given at the Balisage XML Markup Conference in August 2008, focusing on the development and deployment of RESTful services.

Citation preview

Page 1: RESTful Services

How XQuery and AtomPub

Are About To Change Your World

Page 2: RESTful Services

Speaker: Kurt CagleOnline Editor, O’Reilly Media, xml.com

[email protected] Services, XML Data and Presentation, Publishing, Vertical Integration

Architect, Metaphorical Web

Page 3: RESTful Services

Overview• The Rise of RESTful Services• Exploring Xquery• The Prescription of XRX• The ROI on ROA

Page 4: RESTful Services

The Web is not built around process.

Rather, the Web should be seen as a database predicated not on the notion of absolute data but instead upon the idea that behind every

address is a multifaceted thing, one that, like the shadows upon Plato’s cave, never reveals itself in toto but instead only give hints about

its true shape to those who are patient.

That is what the RESTFUL web is all about.

Page 5: RESTful Services

Defining ResourceA resource is a thing that:• is unique (i.e., can be identified uniquely),• has at least one representation,• has one or more attributes beyond ID,• has a potential schema, or definition,• can provide context, and• is reachable within the addressable

universe,

Page 6: RESTful Services

Exploring ResourcesResources: Not Resources*:Web SiteResumeAircraftAirframe SpecificationAirline RouteA SongA TransactionAn EmployeeAn Application

Mailing Address$32.76A Gallon of GasolineJune 25, 1963Love

* Note: Even these COULD be resources in the right context, but you have to stretch some to find it.

Page 7: RESTful Services

RepresentationsA representation is a description of a

resource that is used for some computational purpose.

Representations can be thought of as models of the resource in question, and as is the case

with any modeling, a given resource may have any number of distinct representations.

Page 8: RESTful Services

A Presentation of RepresentationsJane Doe is an employee who works at ACME Widget Works as a Senior Design Engineer. He’s been here nce 1995 ....

Text Description

Video

Audio

Rendered Graphics

BitmapImages

<employee> <firstname> Jane </firstname> <lastname> Doe </lastname> <title> Engineer </title>

XML

PrintFormatted

(PDF)

Jane Doe,Engineer(resource

)

Page 9: RESTful Services

Representational State TransferA representation shows a reflection of the

state of a given resource in a given formThis “representational state” collapses to the

resource description itself when the resource is static, but will change if the resource itself changes.

The transfer of this representation state between nodes in the network is the fundamental operation of the web.

Page 10: RESTful Services

REST is ....Representational State TransferResource based, rather than service basedPublishing oriented verbs:

GET things from an addressPUT things back to an addressPOST things to a collection (more on this

soon...)DELETE things at an address

The way that the web itself is built

Page 11: RESTful Services

The Importance of CollectionsCollections are more important to REST than

may be obvious:A collection is a set of related resources.A collection is also a resource – it has an

address, has one or more representations, is discrete and so forth.

Files in a folder may be a collection, but collections are more inclusive, and can include database records, devices and so forth.

Page 12: RESTful Services

Identifying RESTful ServicesUse URIs to differentiate between

representationsExpose only publishing verbsProvide access to binary content, XML

instances and collections.Use query parameters to filter items from a

collection.Representations may be data, presentation,

state or all three.

Page 13: RESTful Services

Sample RESTful Services (Atom)Get a listing of employees as Atom feed:

http://www.myserver.com/atom/feed/employees (GET)Get a single employee’s XML record:

/atom/content/employees?/jane_doe (GET)Add a new employee to a collection

/atom/feed/employees (POST)Get a picture of an employee

/atom/image/employees?/jane_doe (GET)Update an employee’s record:

/atom/feed/employees?/jane_doe (PUT)Delete an employee:

/atom/feed/employees?/jane_doe (DELETE)

Page 14: RESTful Services

REST and Databases In essence, a RESTful approach uses a

similar metaphor to the CRUD (Create, Read, Update, Delete) methods that a database uses, in the form (Post, Get, Put, Delete).

Because of this, one way of summarizing RESTful services is that it is a way to treat the Web as a database.

Page 15: RESTful Services

If the web is becoming a database, then XQuery will very likely end up becoming its global query language.

Developed as part of the W3C core services, XQuery uses XML as an abstraction layer for working with data repositories, though neither the source nor the output of such a query needs in fact to be XML itself.

As such, XQuery would be the next stage of data abstraction.

Page 16: RESTful Services

Data Abstraction 1990 In the late 1980s and early 1990s, the

database market coalesced around a new standard called the Structured Query Language, better known as SQL.SQL used set theory and tables with relational keys in order to construct complex queries. SQL focused on querying and data system updates, rather than serialization.

Page 17: RESTful Services

Data Abstraction 2000By 2000, most databases used SQL,

but SQL didn’t handle three areas well:Creating hierarchical output, typical of

XML,Handling extensions in a consistent

fashion,Working with multiple data repositories

Similarly, XPath was used for XML queries, but wasn’t even remotely supported in traditional databases.

Page 18: RESTful Services

Data Abstraction 2010XQuery will serve to abstract the

underlying data repository, regardless of whether that data is a file, in a relational database, in a news feed or in an XML database.

XQuery may augment or even replace traditional server languages such as PHP, Ruby, JSP or ASP.NET.

Page 19: RESTful Services

Components of XQuery 1.0The core language is intended as a shell

around XPath 2.0, and as such, should be seen as a language for SET manipulation.

The language supports the XPath 2.0 functions and operators, as well as custom defined functions

XQuery also provides a modularization component for extending XQuery with user functions.

XQuery is schema aware.

Page 20: RESTful Services

XQuery’s FLOWRFLOWR provides analogs to SQL query

notation:

SQL XQuerySELECT a FROM b For $a in $bSET a = b Let $a := $bORDER BY Order byWHERE Where{result of set} Return

Page 21: RESTful Services

<employee id="be129"> <firstname>Jane</firstname> <lastname>Doe</lastname> <title>Engineer</title> <division>Materials</division> <building>327</building> <room>19</room> <supervisor>be131</supervisor> </employee> <employee id="be130"> <firstname>William</firstname> <lastname>Defoe</lastname> <title>Accountant</title> <division>AcctsPayable</division> ...

<building>326</building> <room>14a</room> </employee> <employee id="be131"> <firstname>Jack</firstname> <lastname>Dee</lastname> <title>Manager</title> <division>Materials</division> <building>327</building> <room>21</room> </employee> <!-- more employees -->

Page 22: RESTful Services

let $employees := collection("/db/assets/employees")let $mat_employees := for $employee in $employees where $employee/division = "Materials" order by $employee/lastname ascending return $employeereturn <html> <head> <title>Materials Employees</title> <style type= "text/css“>@import url("style.css");</style> </head> <body> <h1>Materials Employees</h1> <table border="1"> <tr> <th>Name</th> <th>Title</th> <th>Location</th> </tr> {for $emp in $mat_employees return <tr class="{

if (index-of($mat_employees,$emp) mod 2 = 0) then 'even' else 'odd'}">

<td>{concat($emp/firstname," ",$emp/lastname)}</td> <td>{string($emp/title)}</td> <td>Building {string($emp/building)}, Room {string($emp/room)}</td> </tr> } </table> </body></html>

Page 23: RESTful Services
Page 24: RESTful Services

XQuery ModulizationXQuery Modules are “next generation”

stored proceduresModules make libraries possibleModules use namespaces to differentiate

functionsModules can be defined within XQuery, or

can be defined by host environmentModules permit OOP-like programmingMost XQuery engines use modules to

differentiate offerings

Page 25: RESTful Services

(: Module emp.xq :)module namespace emp = "http://www.myco.com/xmlns/emp";declare function emp:division-employees($employees as element()*,$div-name as xs:string){ for $employee in $employees where $employee/division = $div-name order by $employee/lastname ascending return $employee }; declare function emp:employee-table($employees as element()*,$div-name as xs:string){ <h:html xmlns:h="http://www.w3.org/1999/xhtml"> <h:head> <h:title>{$div-name} Employees</h:title> <h:style type="text/css"><![CDATA[... Style inf .... ]]></h:style> </h:head> <h:body> <h:h1>{$div-name} Employees</h:h1> <h:table> <h:tr> <h:th>Employee Name</h:th> <h:th>Employee Title</h:th> <h:th>Location</h:th> </h:tr> {for $emp in $employees return … as previous … } </h:table> </h:body> </h:html> };

Page 26: RESTful Services

(: Module emp.xq :)import module namespace emp="http://www.myco.com/xmlns/emp" at "emp.xq";

let $employees := collection(“/db/assets/employees“)let $division := "Materials"let $div_employees := emp:division-employees($employees,$division)let $output := emp:employee-table($div_employees,$division)return $output

Page 27: RESTful Services

Extension Modules for eXistRequestResponseSessionUtilxmldbTextNgramValidationTransformSystem

CompressionDateHTTPImageMailMathSchedulerSpatialSQLXML DifferencingXSL-FO

Page 28: RESTful Services

XQuery Server LanguageMany XQuery implementations support using

the language as a servlet, with considerable benefits:Servlet content – request parameters, uploaded

files, server and client information – is available within queries

No need for plumbing – you’re in the XML context

Extension modules make it possible to access specialized server-specific functions (such as geospatial routines)

SQL extensions make using SQL in XQuery feasible

XQuery can consolidate multiple data streams

Page 29: RESTful Services

If you can abstract your data on the back end, shift towards a RESTful representation of that data through the server, then provide a compelling mechanism for displaying and editing that data, you will collapse most contemporary multi-tiered application architectures.

That is the promise of XRX.

The Prescription of XRXThe Prescription of XRX

Page 30: RESTful Services

XQuery and AtomPubAtomPub – using Atom syndication and

RESTful interfaces as a way of publishing documents (of any type) – can be implemented easily in XQuery.

URLS (and HTTP methods) can be associated to specific XQuery scripts.

Collections can be generated as Atom feeds, complete with categorization and link information.

Atom entries can be used to publish or retrieve data content and to provide publishing info (author, pubdate, etc.) so content doesn’t have to.

Page 31: RESTful Services

XQuery and AtomPub (II)On the other hand, different Atom services

can filter or transform content in different ways, say for output as HTML, as JSON, or as “raw” XML.

AtomPub provides a paging interface for supporting retrieving blocks of content at a time

Finally, and perhaps most importantly, queries can be stored (perhaps as Atom entries themselves) and invoked to retrieve parametric (searchable) content.

Page 32: RESTful Services

Typical AtomPub Pipeline

Source Feed

Sort Filter

Query Filter

Paging Filter

Transform Filter Output

URL Parameters

Page 33: RESTful Services

XRXXRX – Xquery/REST/XFormsXForms, can be used to create either a read-

only or read-write visual interface for the modification of XML.

REST via AtomPub can be used consequently to retrieve XML content that’s been published, or to post or put XML content to the server as appropriate.

XQuery handles integration & abstraction with data stores.

XQuery/REST(AtomPub)/XForms should be seen as a major emerging design pattern for the web.

Page 34: RESTful Services

XRX and AtomPub ProjectsXRX - http://code.google.com/p/xrx/X2O – http://code.google.com/p/x2o/ (coming

soon)Atom Server (http://atomserver.org)Apache AtomPub Mod (

http://code.google.com/p/mod-atom/)

Page 35: RESTful Services

XForms EnginesOrbeon – Server Based + AJAX, supports

Xforms 1.1 + extensionsChiba – Server Based + AJAX, supports

Xforms 1.0Firefox Xforms Addon – Client based,

Supports Xforms 1.0 + some Xforms 1.1, betaPicoforms – Mobile phone and IE clientXport Forms Player – IE based ActiveX

(recently open sourced)Ubiquity – OSS, AJAX (YUI,

Dojo,Scriptaculous) – IBM + WebBackPlane (Mark Birbeck) – Xforms 1.1 (Alpha)

Page 36: RESTful Services

REST (or Resource) Oriented Architectures represent a significant shift in the web services model, though one that is not necessarily adversarial to the more familiar SOA model (indeed, it complements the model in places where SOA is weakest).

This suggests that the return on investment on an ROA application should be relatively high, especially as the back-end Xquery engine market matures.

Page 37: RESTful Services

REST vs. SOAREST SOAResources (noun)

orientedHTTP TransportUses XML or JSON

protocols with XML-centric tools

Publish/Syndicate Pattern

Discovery via Services document (or WADL)

Works best with W3C Stack

Best for Computer/Human Interaction

Services (verb) orientedHTTP, SOAP, SMTPMarshalls between XML

instances and binary objects

Remote Procedure CallDiscovery via WSDL and

UDDIWorks best with WS-*

StackBest for

Computer/Computer Interaction

Page 38: RESTful Services

ROA As SOA ComplementRESTful services work far better for the

creation, manipulation, and workflow management of resources than SOA – the human side of web services.

ROA/syndication works well with SOA queues, such that processing of “data-documents” can be handled asynchronously to the publish model

ROA focuses on publishing, which makes it considerably less appropriate for transactional processing (an SOA strength).

Page 39: RESTful Services

Upcoming Xquery StandardsXQuery Update Facility (XUF)

Provides syntax to alter “inline” XML contentProvides a standard way to save content to a

data storeExpected to be fully standardized in 2008

XQuery Scripting LanguageAdds additional keyword primitives and

operators to utilize XQuery as a stand-alone scripting language

Includes while conditional loops, variables by references and similar constructs

Expected to be fully standardized in 2009

Page 40: RESTful Services

ConclusionWhile not yet mature, RESTful Web Services

look to be a solid growth area for some time, especially within the next four years.

ROA simplifies the development and deployment of user interfaces, makes data (and data editing) available from a wide variety of devices and applications, and is better suited for interacting with data on the web.

The core pieces of ROA strategies are at or near a workable maturity.

ROA works well with data as a document strategies.

Page 41: RESTful Services
Page 42: RESTful Services

The market for XQuery Engines and XML Databases has exploded in the last couple of years, but there are a few that are becoming dominant in the marketplace

Page 43: RESTful Services

eXist XML Database• Open Source Software, Java Based• Best tool for experimenting with XQuery and

XML databases• Most recent version supports XQuery

1.0,XUF and AtomPub, along with WebDAV, REST, XML-RPC and SOAP

• Extensive extension library• Supports SQL layer and web servlets• Provides XQJ Interfaces• Support for XML Indexing

Page 44: RESTful Services

MarkLogic XML Server• Proprietary Software, Written in C++• The Cadillac of XML Databases (both in

quality and price), multi-terabyte scaling• Most recent version supports XQuery 1.0,

XUF, and AtomPub• Lacks native XSLT support• Extensive extension module library• Support for XML Indexing• Supports extensive conversion capabilities

with MS formats and PDF, pipelines, web servlets, triggers, extensive text search capabilities

Page 45: RESTful Services

IBM DB2 9 pureXML Database• Proprietary Software, C++ Based• Combines XQuery and SQL Support in same

interfaces.• Most recent version supports XQuery 1.0 and

XUF, AtomPub support soon• Supports SQL layer but not web servlets• Support for XML Indexing• Provides XQJ Interfaces• Good for working with IBM DB2 systems and

fair at working with other relational db systems.

Page 46: RESTful Services

DataDirect XQuery• Proprietary Software, Java Based• DD is not a database per se, but rather a

gateway for performing XQuery manipulations upon other data stores/targets.

• Less optimized for any given data-type, DD’s strength is in working with multiple data repositories.

• Support for XQuery + SQL, RESTful interfaces, SOAP, XML-RPC

• Good solution if integration rather than performance is key.

Page 47: RESTful Services