| 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>Optimistic Concurrency Example</title></head> |
| 44 |
|
| 45 |
|
| 46 |
<body> |
| 47 |
<h1>Optimistic Concurrency Example</h1> |
| 48 |
<p>This example demonstrates how to apply optimistic concurrency |
| 49 |
to a web resource. |
| 50 |
<a href="http://en.wikipedia.org/wiki/Optimistic_concurrency_control">Optimistic |
| 51 |
concurrency control</a> prevents the locking of a web resource that will be updated, |
| 52 |
ensuring that other users can concurrently access or update that web resource. The clients must resolve any conflict |
| 53 |
reported by the server. This ensures that clients cannot overwrite |
| 54 |
each other's changes to that web resource. |
| 55 |
</p> |
| 56 |
<p>This example follows a design like the one in the |
| 57 |
<a href="http://code.google.com/apis/gdata/reference.html#Optimistic-concurrency">Google Data API</a> |
| 58 |
for the update of Atom entries. |
| 59 |
</p> |
| 60 |
|
| 61 |
<h2>Contents</h2> |
| 62 |
<p>The example consists of two web resources implemented by the following: |
| 63 |
</p><dl> |
| 64 |
<dt><code>com.sun.jersey.samples.optimisticconcurrency.resources.ItemResource</code></dt> |
| 65 |
<dd>This resource class provides meta-data information on the URIs to content resources |
| 66 |
that can be read and updated. JAXB is used to represent the meta-data as an XML document. |
| 67 |
The resource references the ItemContentResource resource using the |
| 68 |
Path annotation declared on the ItemResource.getItemContentResource method. |
| 69 |
</dd> |
| 70 |
<dt><code>com.sun.jersey.samples.optimisticconcurrency.resources.ItemContentResource</code></dt> |
| 71 |
<dd>This resource class provides read and update ccess to the content. In this example, the |
| 72 |
content is stored in memory as a byte array and is not persisted to a |
| 73 |
file system or database. |
| 74 |
Update occurs using a sub-resource that implements the PUT method. |
| 75 |
</dd> |
| 76 |
</dl> |
| 77 |
<p>The mapping of the URI path space is presented in the following table:</p> |
| 78 |
<table border="1"> |
| 79 |
<tr> |
| 80 |
<th>URI path</th> |
| 81 |
<th>Resource class</th> |
| 82 |
<th>HTTP methods</th> |
| 83 |
</tr> |
| 84 |
<tr> |
| 85 |
<td>/item</td> |
| 86 |
<td>ItemResource</td> |
| 87 |
<td>GET</td> |
| 88 |
</tr> |
| 89 |
<tr> |
| 90 |
<td>/item/content</td> |
| 91 |
<td>ItemContentResource</td> |
| 92 |
<td>GET</td> |
| 93 |
</tr> |
| 94 |
<tr> |
| 95 |
<td>/item/content/{version}</td> |
| 96 |
<td>ItemContentResource</td> |
| 97 |
<td>PUT</td> |
| 98 |
</tr> |
| 99 |
</table> |
| 100 |
<h2>Running the Example</h2> |
| 101 |
<p>Run the example as follows:</p> |
| 102 |
<blockquote><pre> |
| 103 |
mvn clean compile exec:java</pre></blockquote> |
| 104 |
<p>This deploys the Optimistic Concurrency example using |
| 105 |
<a href="http://grizzly.java.net/">Grizzly</a> |
| 106 |
</p> |
| 107 |
<p>A <a href="http://wadl.java.net/#spec">WADL description</a> may be accessed at the URL:</p> |
| 108 |
<blockquote><code><a href="http://127.0.0.1:9998/occ/application.wadl">http://127.0.0.1:9998/occ/application.wadl</a></code></blockquote> |
| 109 |
<p>Following steps are using <a href="http://curl.haxx.se/">cURL</a> command line tool: |
| 110 |
<p>Get the item meta-data:</p> |
| 111 |
<blockquote><pre>curl http://127.0.0.1:9998/occ/item</pre></blockquote> |
| 112 |
<p>This returns the following XML document:</p> |
| 113 |
<blockquote><pre><item> |
| 114 |
<mediaType>text/plain</mediaType> |
| 115 |
<updateUri>http://127.0.0.1:9998/occ/item/content/0</updateUri> |
| 116 |
<uri>http://127.0.0.1:9998/occ/item/content</uri> |
| 117 |
</item></pre></blockquote> |
| 118 |
<p>The URI to obtain the content is within the <code>uri</code> element.</p> |
| 119 |
<p>Get the content:</p> |
| 120 |
<blockquote><pre>curl http://127.0.0.1:9998/occ/item/content</pre></blockquote> |
| 121 |
<p>This will return the following text:</p> |
| 122 |
<blockquote><pre>Today is the first day of the REST of my life</pre></blockquote> |
| 123 |
<p>Update the content:</p> |
| 124 |
<blockquote><pre>curl -i -X PUT --data "All play and no REST makes me a dull boy" -HContent-type:text/plain http://127.0.0.1:9998/occ/item/content/0</pre></blockquote> |
| 125 |
<p>The content is updated by PUTing to the URI within the <code>updateUri</code> |
| 126 |
element of the XML document. |
| 127 |
</p> |
| 128 |
<p>Get the item meta-data:</p> |
| 129 |
<blockquote><pre>curl http://127.0.0.1:9998/occ/item</pre></blockquote> |
| 130 |
<p>This returns the following XML document:</p> |
| 131 |
<blockquote><pre><?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 132 |
<item> |
| 133 |
<mediaType>text/plain</mediaType> |
| 134 |
<updateUri><b>http://127.0.0.1:9998/occ/item/content/1</b></updateUri> |
| 135 |
<uri>http://127.0.0.1:9998/occ/item/content</uri> |
| 136 |
</item></pre></blockquote> |
| 137 |
<p>The update URI has now changed. |
| 138 |
Clients updating using the previous URI receive a <code>409 Conflict</code> |
| 139 |
in response. |
| 140 |
</p> |
| 141 |
<p>Repeating the initial update fails:</p> |
| 142 |
<blockquote><pre>curl -i -X PUT --data "All play and no REST makes me a dull boy" -HContent-type:text/plain http://127.0.0.1:9998/occ/item/content/0</pre></blockquote> |
| 143 |
<p>With: |
| 144 |
<blockquote><pre>HTTP/1.1 409 Conflict |
| 145 |
server: grizzly/1.8.1 |
| 146 |
Content-Type: text/plain |
| 147 |
Transfer-Encoding: chunked |
| 148 |
Date: Tue, 19 Aug 2008 09:52:51 GMT |
| 149 |
|
| 150 |
Conflict</pre></blockquote> |
| 151 |
</body></html> |