| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
<html><head><title>Simple Atom Server Example</title></head> |
| 44 |
|
| 45 |
|
| 46 |
<body> |
| 47 |
<h1>Simple Atom Server Example</h1> |
| 48 |
<p>This example demonstrates a simple |
| 49 |
Atom server that partially conforms to the |
| 50 |
<a href="http://tools.ietf.org/wg/atompub/">Atom Publishing Format and Protocol</a>. |
| 51 |
</p> |
| 52 |
<p>Atom Entries, Atom Media Link Entries, and media are persisted to the |
| 53 |
file system. The <a href="https://github.com/rometools/rome">ROME</a> Java library provides support for the parsing |
| 54 |
and serializing of Atom Feed and Entry documents.</p> |
| 55 |
|
| 56 |
<h2>Contents</h2> |
| 57 |
<p>The example consists of four web resources implemented by the following: |
| 58 |
</p><dl> |
| 59 |
<dt><code>com.sun.jersey.samples.atomserver.resources.ServiceResource</code></dt> |
| 60 |
<dd>This Java class returns the Service Document describing the Workspace that |
| 61 |
contains the Atom Collection that creates, reads, updates, and deletes |
| 62 |
Atom Entries. |
| 63 |
</dd> |
| 64 |
<dt><code>com.sun.jersey.samples.atomserver.resources.FeedResource</code></dt> |
| 65 |
<dd>This Java class provides the Feed Document describing the Atom Entries |
| 66 |
in the feed. It also provides support to create Atom Entries and |
| 67 |
Atom Media Link Entries. |
| 68 |
The resource references the EntryResource and EditEntryResource resources |
| 69 |
using the Path annotation declared on the FeedResource.getEntryResource and |
| 70 |
FeedResource.getEditEntryResource methods respectively. |
| 71 |
</dd> |
| 72 |
<dt><code>com.sun.jersey.samples.atomserver.resources.EntryResource</code></dt> |
| 73 |
<dd>This Java class supports the reading of an Atom Entry or Atom Media Link Entry. |
| 74 |
The reading of the media, associated with a Media Link Entry, is supported by a sub-resource |
| 75 |
HTTP method. |
| 76 |
</dd> |
| 77 |
<dt><code>com.sun.jersey.samples.atomserver.resources.EditEntryResource</code></dt> |
| 78 |
<dd>This Java class supports the read, update, and delete of an Atom Entry or |
| 79 |
Atom Media Link Entry, and via a sub-resource HTTP method the read and update |
| 80 |
of the media. |
| 81 |
This resource extends EntryResource to inherit the reading support. |
| 82 |
</dd> |
| 83 |
</dl> |
| 84 |
<p>The mapping of the URI path space is presented in the following table:</p> |
| 85 |
<table border="1"> |
| 86 |
<tr> |
| 87 |
<th>URI path</th> |
| 88 |
<th>Resource class</th> |
| 89 |
<th>HTTP methods</th> |
| 90 |
</tr> |
| 91 |
<tr> |
| 92 |
<td>/service</td> |
| 93 |
<td>ServiceResource</td> |
| 94 |
<td>GET</td> |
| 95 |
</tr> |
| 96 |
<tr> |
| 97 |
<td>/collection/</td> |
| 98 |
<td>FeedResource</td> |
| 99 |
<td>GET, POST</td> |
| 100 |
</tr> |
| 101 |
<tr> |
| 102 |
<td>/collection/{entry}</td> |
| 103 |
<td>EntryResource</td> |
| 104 |
<td>GET</td> |
| 105 |
</tr> |
| 106 |
<tr> |
| 107 |
<td>/collection/{entry}/media</td> |
| 108 |
<td>EntryResource</td> |
| 109 |
<td>GET</td> |
| 110 |
</tr> |
| 111 |
<tr> |
| 112 |
<td>/collection/edit/{entry}</td> |
| 113 |
<td>EditEntryResource</td> |
| 114 |
<td>PUT, DELETE</td> |
| 115 |
</tr> |
| 116 |
<tr> |
| 117 |
<td>/collection/edit/{entry}/media</td> |
| 118 |
<td>EditEntryResource</td> |
| 119 |
<td>PUT</td> |
| 120 |
</tr> |
| 121 |
</table> |
| 122 |
|
| 123 |
<h2>File System Structure of Persisted Atom Feed, Atom Entries, and Media</h2> |
| 124 |
<p>The Atom Feed/Entries and media are persisted to a file system. |
| 125 |
The structure of the file system is as follows: |
| 126 |
</p><dl> |
| 127 |
<dt><code>collection</code></dt> |
| 128 |
<dd>The directory where all Atom Entries and media are stored. |
| 129 |
This directory is created in the current working directory where |
| 130 |
the Atom Server is run.</dd> |
| 131 |
<dt><code>collection/feed.xml</code></dt> |
| 132 |
<dd>The persisted Atom Feed document.</dd> |
| 133 |
<dt><code>collection/<uuid>/entry.xml</code></dt> |
| 134 |
<dd>Each Atom Entry document for the Collection is stored under a unique directory |
| 135 |
(a UUID) from which the Atom Entry document is persisted. The UUID |
| 136 |
corresponds to the <code>atom:id</code> of the Atom Entry.</dd> |
| 137 |
<dt><code>collection/<uuid>/media</code></dt> |
| 138 |
<dd>The media (if present) of an Atom Media Link Entry.</dd> |
| 139 |
</dl> |
| 140 |
<p>To remove the Atom Feed document and all the Atom (or Atom Media Link) |
| 141 |
Entries delete the <code>collection</code> directory.</p> |
| 142 |
|
| 143 |
<h2>Running the Example</h2> |
| 144 |
<p>Run the example as follows:</p> |
| 145 |
<blockquote><pre> |
| 146 |
mvn clean compile exec:java</pre></blockquote> |
| 147 |
<p>This deploys the Atom server using |
| 148 |
<a href="http://grizzly.java.net/">Grizzly</a> |
| 149 |
</p> |
| 150 |
<p>A <a href="http://wadl.java.net/#spec">WADL description</a> may be accessed at the URL:</p> |
| 151 |
<blockquote><code><a href="http://localhost:9998/atom/application.wadl">http://localhost:9998/atom/application.wadl</a></code></blockquote> |
| 152 |
<p>Following steps are using <a href="http://curl.haxx.se/">cURL</a> command line tool: |
| 153 |
<p>Get the service document:</p> |
| 154 |
<blockquote><pre>curl http://127.0.0.1:9998/atom/service</pre></blockquote> |
| 155 |
<p>This returns the following sevice document:</p> |
| 156 |
<blockquote><pre><service xmlns="http://purl.org/atom/app#" |
| 157 |
xmlns:atom="http://www.w3.org/2005/Atom"> |
| 158 |
<workspace> |
| 159 |
<atom:title>Service</atom:title> |
| 160 |
<collection href="http://localhost:9998/atom/collection"> |
| 161 |
<atom:title>Entries</atom:title> |
| 162 |
<categories> |
| 163 |
<atom:category term="storage" scheme="urn:storage" label="storage" /> |
| 164 |
</categories> |
| 165 |
</collection> |
| 166 |
</workspace> |
| 167 |
</service></pre></blockquote> |
| 168 |
<p>The service document contains one workspace with one collection.</p> |
| 169 |
<p>Create a media link entry:</p> |
| 170 |
<blockquote><pre>curl -i -X POST --data "Something is rotten in the state of Denmark" -HContent-type:text/plain http://127.0.0.1:9998/atom/collection</pre></blockquote> |
| 171 |
<p>Get the Atom Feed document:</p> |
| 172 |
<blockquote><pre>curl http://127.0.0.1:9998/atom/collection</pre></blockquote> |
| 173 |
<p>which returns the following:</p> |
| 174 |
<blockquote><pre> <?xml version="1.0" encoding="UTF-8"?> |
| 175 |
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/"> |
| 176 |
<title>Feed</title> |
| 177 |
<link rel="self" href="http://127.0.0.1:9998/atom/collection" /> |
| 178 |
<entry> |
| 179 |
<title>Media Entry</title> |
| 180 |
<link rel="self" href="http://127.0.0.1:9998/atom/collection/bdac4727-cbc3-4c37-ad1e-206672bc48dd" /> |
| 181 |
<link rel="edit" href="http://127.0.0.1:9998/atom/collection/edit/bdac4727-cbc3-4c37-ad1e-206672bc48dd" /> |
| 182 |
<link rel="edit-media" href="http://127.0.0.1:9998/atom/collection/edit/bdac4727-cbc3-4c37-ad1e-206672bc48dd/media" /> |
| 183 |
<id>bdac4727-cbc3-4c37-ad1e-206672bc48dd</id> |
| 184 |
<updated>2011-03-23T05:51:28Z</updated> |
| 185 |
<content type="TEXT" src="<b>http://127.0.0.1:9998/atom/collection/bdac4727-cbc3-4c37-ad1e-206672bc48dd/media</b>" /> |
| 186 |
</entry> |
| 187 |
</feed> |
| 188 |
</pre></blockquote> |
| 189 |
<p>The Atom Feed document contains one Atom Media Link Entry. The |
| 190 |
<code>atom:content</code> contains a link to the media. (Note that the UUIDs |
| 191 |
and hence the URIs will be different than those shown in this example. Substitute the UUID |
| 192 |
in the example for the one returned rather than copying and |
| 193 |
pasting these commands directly.) |
| 194 |
The Atom Entry document, which will be the same as the <code>entry</code> |
| 195 |
element in the Atom Feed document, can be retrived from the URI of the |
| 196 |
<code>link</code> element with the <code>self</code> or <code>edit</code> value |
| 197 |
for the <code>rel</code> attribute. The <code>id</code> element contains the |
| 198 |
unique ID of the entry, which is a UUID. |
| 199 |
</p><p>Get the media:</p> |
| 200 |
<blockquote><pre>curl http://127.0.0.1:9998/atom/collection/bdac4727-cbc3-4c37-ad1e-206672bc48dd/media</pre></blockquote> |
| 201 |
<p>which returns:</p> |
| 202 |
<blockquote><pre>Something is rotten in the state of Denmark</pre></blockquote> |
| 203 |
<p>Updating the media:</p> |
| 204 |
<blockquote><pre>curl -X PUT --data "Hamlet said: Something is rotten in the state of Denmark" -HContent-type:text/plain http://127.0.0.1:9998/atom/collection/edit/bdac4727-cbc3-4c37-ad1e-206672bc48dd/media</pre></blockquote> |
| 205 |
<p>The entry now has an updated time when the entry is retrieved:</p> |
| 206 |
<blockquote><pre>curl http://127.0.0.1:9998/atom/collection/bdac4727-cbc3-4c37-ad1e-206672bc48dd</pre></blockquote> |
| 207 |
<p>which returns:</p> |
| 208 |
<blockquote><pre><entry xmlns="http://www.w3.org/2005/Atom"> |
| 209 |
<title>Media Entry</title> |
| 210 |
<link rel="self" href="http://localhost:9998/atom/collection/bdac4727-cbc3-4c37-ad1e-206672bc48dd" /> |
| 211 |
<link rel="edit-media" href="http://localhost:9998/atom/collection/edit/bdac4727-cbc3-4c37-ad1e-206672bc48dd/media" /> |
| 212 |
<link rel="edit" href="http://localhost:9998/atom/collection/edit/bdac4727-cbc3-4c37-ad1e-206672bc48dd/" /> |
| 213 |
<id>bdac4727-cbc3-4c37-ad1e-206672bc48dd</id> |
| 214 |
<updated><b>2011-03-23T06:12:46Z</b></updated> |
| 215 |
<content type="TEXT" src="http://localhost:9998/atom/collection/bdac4727-cbc3-4c37-ad1e-206672bc48dd/media" /> |
| 216 |
</entry></pre></blockquote> |
| 217 |
|
| 218 |
<p>Delete the Atom Entry:</p> |
| 219 |
<blockquote><pre>curl -X DELETE http://localhost:9998/atom/collection/edit/bdac4727-cbc3-4c37-ad1e-206672bc48dd</pre></blockquote> |
| 220 |
<p>The <code>edit</code> link is used to delete the entry. Retrieving |
| 221 |
the Atom Feed document:</p> |
| 222 |
<blockquote><pre>curl http://localhost:9998/atom/collection</pre></blockquote> |
| 223 |
<p>returns a feed with no entries:</p> |
| 224 |
<blockquote><pre><?xml version="1.0" encoding="UTF-8"?> |
| 225 |
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/"> |
| 226 |
<title>Feed</title> |
| 227 |
<link rel="self" href="http://localhost:9998/atom/collection" /> |
| 228 |
</feed></pre></blockquote> |
| 229 |
</body></html> |