Slide 1

Slide 1 text

Web Service Design with AtomPub Ben Ramsey ■ Code Works

Slide 2

Slide 2 text

Atom & AtomPub?

Slide 3

Slide 3 text

What’s the difference?

Slide 4

Slide 4 text

Atom: RFC 4287

Slide 5

Slide 5 text

Atom is an XML language.

Slide 6

Slide 6 text

AtomPub: RFC 5023

Slide 7

Slide 7 text

AtomPub is a publishing protocol.

Slide 8

Slide 8 text

What about RSS?

Slide 9

Slide 9 text

Content types ■ RSS allows only for plain text and escaped HTML content types ■ Atom provides for plain text, escaped HTML, XHTML, XML, and Base64- encoded binary data

Slide 10

Slide 10 text

Internationalization ■ RSS may have a language set for a feed, but doesn’t have a way to indicate language for items in the feed ■ Atom uses the xml:lang attribute to specify language on a per-element basis ■ Atom uses IRIs, which allow the usage of characters in identifiers outside of ASCII

Slide 11

Slide 11 text

Modularity ■ RSS vocabulary elements aren’t reusable in other XML vocabularies ■ Atom was designed to allow its vocabulary to be mixed with other XML vocabularies ■ Namespaces! Atom has one; RSS does not

Slide 12

Slide 12 text

Other things ■ RSS has no schema; Atom has a RelaxNG schema ■ RSS defines no mechanism for handling relative URIs; Atom uses xml:base ■ Various implementations in RSS leads to interoperability problems ■ No standard protocol for publishing

Slide 13

Slide 13 text

Atom was created to solve the RSS problems.

Slide 14

Slide 14 text

Atom profile ■ Atom Syndication Format ■ An XML-based web content and metadata syndication format ■ Defined by IETF RFC 4287 ■ Fixes the “problems” of RSS ■ XML namespace: http://www.w3.org/2005/Atom

Slide 15

Slide 15 text

AtomPub profile ■ Atom Publishing Protocol ■ A protocol for publishing and editing web resources using HTTP and XML ■ Defined by IETF RFC 5023 ■ Uses Atom as it’s XML syntax ■ XML namespace: http://www.w3.org/2007/app

Slide 16

Slide 16 text

AtomPub basics ■ Each resource has a unique identifier ■ The resources are well-connected ■ Resources share the same interface ■ There is no state; requests are atomic ■ Follows a resource-oriented architecture

Slide 17

Slide 17 text

Some terms… ■ Entry ■ Feed/Collection ■ Category Document ■ Service Document

Slide 18

Slide 18 text

Content types… ■ Entry: application/atom+xml;type=entry ■ Feed/Collection: application/atom+xml ■ Category Document: application/atomcat+xml ■ Service Document: application/atomsvc+xml

Slide 19

Slide 19 text

Designing an AtomPub service

Slide 20

Slide 20 text

What will our service do? ■ Expose users ■ Expose content ■ Allow users to manipulate content

Slide 21

Slide 21 text

1. Define our URIs

Slide 22

Slide 22 text

Users ■ /user ■ /user/ramsey

Slide 23

Slide 23 text

Content ■ /content ■ /content/1234

Slide 24

Slide 24 text

2. Define the relationships

Slide 25

Slide 25 text

■ user ⇛ content: one to many ■ content ⇛ user: one to one

Slide 26

Slide 26 text

3. Define the interface

Slide 27

Slide 27 text

Methods GET PUT POST DELETE Cut & Paste Copy Paste Over Paste After Cut

Slide 28

Slide 28 text

■ Retrieve user collection: GET /user ■ Create a new user: POST /user ■ Modify an existing user: PUT /user/ramsey ■ Delete a user: DELETE /user/ramsey

Slide 29

Slide 29 text

■ Retrieve content: GET /content ■ Create new content: POST /content ■ Modify content: PUT /content/1234 ■ Remove content: DELETE /content/1234

Slide 30

Slide 30 text

■ Service discovery: GET / ■ Retrieve content for a particular user: GET /user/ramsey/content

Slide 31

Slide 31 text

Let’s see it in action

Slide 32

Slide 32 text

GET / HTTP/1.1 Host: atom.example.org HTTP/1.x 200 OK Date: Mon, 21 Sep 2009 16:33:45 GMT Content-Type: application/atomsvc+xml

Slide 33

Slide 33 text

Our Content Store Users application/atom+xml;type=entry Content application/atom+xml;type=entry

Slide 34

Slide 34 text

GET /user HTTP/1.1 Host: atom.example.org HTTP/1.x 200 OK Date: Mon, 21 Sep 2009 16:34:26 GMT Content-Type: application/atom+xml

Slide 35

Slide 35 text

Users 2009-09-21T05:21:19Z tag:example.org,2009-09:user Users application/atom+xml;type=entry ...

Slide 36

Slide 36 text

Manipulate a user

Slide 37

Slide 37 text

GET /user/ramsey HTTP/1.1 Host: atom.example.org HTTP/1.x 200 OK Date: Mon, 21 Sep 2009 16:34:26 GMT Content-Type: application/atom+xml;type=entry

Slide 38

Slide 38 text

ramsey ramsey tag:example.org,2008:user/ramsey 2009-09-21T13:45:00Z 2008-05-23T16:23:34Z
Ben Ramsey 123-456-7890

Slide 39

Slide 39 text

Modify the entry locally

Slide 40

Slide 40 text

ramsey ramsey tag:example.org,2008:user/ramsey 2009-09-22T09:14:58Z 2008-05-23T16:23:34Z
Ben Ramsey Schematic 123-456-7890

Slide 41

Slide 41 text

PUT /user/ramsey HTTP/1.1 Host: atom.example.org Content-Type: application/atom+xml;type=entry {body here} HTTP/1.x 200 OK Date: Mon, 22 Sep 2009 09:14:59 GMT Content-Type: application/atom+xml;type=entry

Slide 42

Slide 42 text

Add some content

Slide 43

Slide 43 text

First, a few notes ■ You need authentication! ■ Perhaps you need encryption ■ You definitely need validation ■ And I’m not going to tell you how

Slide 44

Slide 44 text

That’s out of scope.

Slide 45

Slide 45 text

:-)

Slide 46

Slide 46 text

But I’ll show you the basics…

Slide 47

Slide 47 text

Our Content Store Users application/atom+xml;type=entry Content application/atom+xml;type=entry

Slide 48

Slide 48 text

Get the categories

Slide 49

Slide 49 text

GET /cat/content HTTP/1.1 Host: atom.example.org HTTP/1.x 200 OK Date: Mon, 22 Sep 2009 09:39:26 GMT Content-Type: application/atomcat+xml

Slide 50

Slide 50 text

Slide 51

Slide 51 text

Create the entry document locally

Slide 52

Slide 52 text

Perfect Mark Phelps tag:example.org,2009:content/perfect 2009-09-22T20:12:08Z TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWF IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGl ...

Slide 53

Slide 53 text

POST /content HTTP/1.1 Host: atom.example.org Content-Type: application/atom+xml;type=entry {body here} HTTP/1.x 202 Accepted Date: Mon, 22 Sep 2009 09:14:59 GMT {body contains status indicator}

Slide 54

Slide 54 text

Authentication? ■ Atom doesn’t specify a preference ■ WSSE Username Token ■ OAuth ■ Basic authentication ■ ???

Slide 55

Slide 55 text

Did I miss anything?

Slide 56

Slide 56 text

Where’s the PHP code? Oh, yeah.

Slide 57

Slide 57 text

■ Requires lots of parser code; either DOM or XMLReader/XMLWriter ■ No good AtomPub library that I like ■ Apache Abdera is good, but it’s in Java ■ I’m porting it to PHP: http://github.com/ramsey/AbderaPHP

Slide 58

Slide 58 text

Wrapping up…

Slide 59

Slide 59 text

■ You can extend Atom with other XML vocabularies (Dublin Core, etc.) ■ XML Digital Signature or XML Encryption may be used, or encrypt as a bag of bits ■ Use your preferred auth method ■ Use HTTP in a RESTful fashion ■ Use DOM or XMLReader/XMLWriter to parse Atom documents

Slide 60

Slide 60 text

Questions? ■ My website is benramsey.com ■ @ramsey on Twitter ■ Rate this talk at joind.in ■ Read the Atom specs at tools.ietf.org/html/rfc4287 tools.ietf.org/html/rfc5023 ■ My company is Schematic schematic.com

Slide 61

Slide 61 text

Web Service Design with AtomPub Copyright © Ben Ramsey. Some rights reserved. This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License. For uses not covered under this license, please contact the author.