Debian Codegraph @

samples/optimistic-concurrency/README.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <!--
3
4 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5
6 Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved.
7
8 The contents of this file are subject to the terms of either the GNU
9 General Public License Version 2 only ("GPL") or the Common Development
10 and Distribution License("CDDL") (collectively, the "License"). You
11 may not use this file except in compliance with the License. You can
12 obtain a copy of the License at
13 http://glassfish.java.net/public/CDDL+GPL_1_1.html
14 or packager/legal/LICENSE.txt. See the License for the specific
15 language governing permissions and limitations under the License.
16
17 When distributing the software, include this License Header Notice in each
18 file and include the License file at packager/legal/LICENSE.txt.
19
20 GPL Classpath Exception:
21 Oracle designates this particular file as subject to the "Classpath"
22 exception as provided by Oracle in the GPL Version 2 section of the License
23 file that accompanied this code.
24
25 Modifications:
26 If applicable, add the following below the License Header, with the fields
27 enclosed by brackets [] replaced by your own identifying information:
28 "Portions Copyright [year] [name of copyright owner]"
29
30 Contributor(s):
31 If you wish your version of this file to be governed by only the CDDL or
32 only the GPL Version 2, indicate your decision by adding "[Contributor]
33 elects to include this software in this distribution under the [CDDL or GPL
34 Version 2] license." If you don't indicate a single choice of license, a
35 recipient has the option to distribute your version of this file under
36 either the CDDL, the GPL Version 2 or to extend the choice of license to
37 its licensees as provided above. However, if you add GPL Version 2 code
38 and therefore, elected the GPL Version 2 license, then the option applies
39 only if the new code is made subject to such option by the copyright
40 holder.
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>&lt;item&gt;
114 &lt;mediaType&gt;text/plain&lt;/mediaType&gt;
115 &lt;updateUri&gt;http://127.0.0.1:9998/occ/item/content/0&lt;/updateUri&gt;
116 &lt;uri&gt;http://127.0.0.1:9998/occ/item/content&lt;/uri&gt;
117 &lt;/item&gt;</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>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
132 &lt;item&gt;
133 &lt;mediaType&gt;text/plain&lt;/mediaType&gt;
134 &lt;updateUri&gt;<b>http://127.0.0.1:9998/occ/item/content/1</b>&lt;/updateUri&gt;
135 &lt;uri&gt;http://127.0.0.1:9998/occ/item/content&lt;/uri&gt;
136 &lt;/item&gt;</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>