summaryrefslogtreecommitdiffstats
path: root/site/trunk/site-publish/documentation-2x/sca-java-bindingrest.html
blob: 5c7fa11f94a08114898a43e2cf78e8163e88717e (plain)
1
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
    <HEAD>
           <META name="description" content="Apache Tuscany">
   <META name="keywords" content="apache, apache tuscany, tuscany, service, services, fabric, soa, service oriented architecture,  sca, service component architecture, das, sdo, csa, ruby, opensource">

        <LINK type="text/css" rel="stylesheet" href="http://tuscany.apache.org/stylesheets/default.css">
        <LINK rel="SHORTCUT ICON" href="https://cwiki.apache.org/confluence/display/TUSCANYxDOCx2x/$images/favicon.ico">   
        <TITLE>SCA Java binding.rest : Apache Tuscany</TITLE>
    <META http-equiv="Content-Type" content="text/html;charset=UTF-8"></HEAD>

   <STYLE>
    .spacetree * ul {
       padding-left:0px;
       margin-left: 0px;
    }
    .spacetree * li {
       margin-left: 5px;
       padding-left:5px;
    }
   </STYLE>

    <BODY onload="init()">
            <!-- topNav -->
    <TABLE border="0" cellpadding="2" cellspacing="0" width="100%">
      <TR class="topBar">
        <TD align="left" valign="middle" class="topBarDiv" align="left" nowrap="">
          <A href="https://cwiki.apache.org/geronimo"> Home</A> &gt;&nbsp;<A href="index.html" title="Apache Tuscany Docs 2.x">Apache Tuscany Docs 2.x</A>&nbsp;&gt;&nbsp;<A href="index.html" title="Index">Index</A>&nbsp;&gt;&nbsp;<A href="sca-java-extensions-guide.html" title="SCA Java Extensions Guide">SCA Java Extensions Guide</A>&nbsp;&gt;&nbsp;<A href="" title="SCA Java binding.rest">SCA Java binding.rest</A>
        </TD>
        <TD align="right" valign="middle" nowrap="">
          <FORM name="search" action="http://www.google.com/search" method="get">
            <INPUT type="hidden" name="ie" value="UTF-8">
            <INPUT type="hidden" name="oe" value="UTF-8">
            <INPUT type="hidden" name="domains" value="">
            <INPUT type="hidden" name="sitesearch" value="">
            <INPUT type="text" name="q" maxlength="255" value="">        
            <INPUT type="submit" name="btnG" value="Google Search">
          </FORM>
        </TD>
      </TR> 
    </TABLE>

        
    <TABLE border="0" cellpadding="2" cellspacing="0" width="100%">
        <TR class="topBar">
            <TD align="left" valign="middle" class="topBarDiv" nowrap="true" width="100%">
                &nbsp;<A href="index.html" title="Apache Tuscany Docs 2.x">Apache Tuscany Docs 2.x</A>&nbsp;&gt;&nbsp;<A href="index.html" title="Index">Index</A>&nbsp;&gt;&nbsp;<A href="sca-java-extensions-guide.html" title="SCA Java Extensions Guide">SCA Java Extensions Guide</A>&nbsp;&gt;&nbsp;<A href="" title="SCA Java binding.rest">SCA Java binding.rest</A>
            </TD>

            <TD align="right" valign="middle" class="topBarDiv" align="left" nowrap="true">
            <A href="http://tuscany.apache.org/">Tuscany Home</A> | <A href="http://mail-archives.apache.org/mod_mbox/tuscany-user">User List</A> | <A href="http://mail-archives.apache.org/mod_mbox/tuscany-dev">Dev List</A> | <A href="http://issues.apache.org/jira/browse/Tuscany">Issue Tracker</A>&nbsp;&nbsp;
            </TD>
        </TR>
    </TABLE>

        <TABLE border="0" cellpadding="0" width="100%" bgcolor="#FFFFFF">
            <TR>
                <TD valign="top" align="left" width="22%" bgcolor="#F9F9F9" class="noprint">
                        <DIV class="tabletitle">Table of Contents</DIV>
        <DIV class="spacetree">
            
            
            </DIV>
                </TD>
                <TD align="left" valign="top" width="78%">
                        <!-- pageContent -->
    <DIV id="PageContent">
      <DIV class="pageheader" style="padding: 6px 0px 0px 0px;">
        <!-- We'll enable this once we figure out how to access (and save) the logo resource -->
        <!--img src="http://geronimo.apache.org/images/confluence_logo.gif" style="float: left; margin: 4px 4px 4px 10px;" border="0"-->
        <DIV style="margin: 0px 10px 0px 10px" class="smalltext">Apache Tuscany Docs 2.x</DIV>
        <DIV style="margin: 0px 10px 8px 10px" class="pagetitle">SCA Java binding.rest</DIV>

        <DIV class="greynavbar" align="right" style="padding: 2px 10px; margin: 0px;">
<!-- -->         
            <A href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=20644360">
            <IMG src="http://geronimo.apache.org/images/icons/notep_16.gif" height="16" width="16" border="0" align="absmiddle" title="Edit Page"></A>
            <A href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=20644360">Edit Page</A>
          &nbsp;
          <A href="https://cwiki.apache.org/confluence/pages/listpages.action?key=TUSCANYxDOCx2x">
            <IMG src="http://geronimo.apache.org/images/icons/browse_space.gif" height="16" width="16" border="0" align="absmiddle" title="Browse Space"></A>
            <A href="https://cwiki.apache.org/confluence/pages/listpages.action?key=TUSCANYxDOCx2x">Browse Space</A>
          &nbsp;
          <A href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=TUSCANYxDOCx2x&fromPageId=20644360">
            <IMG src="http://geronimo.apache.org/images/icons/add_page_16.gif" height="16" width="16" border="0" align="absmiddle" title="Add Page"></A>
          <A href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=TUSCANYxDOCx2x&fromPageId=20644360">Add Page</A>
          &nbsp;
          <A href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=TUSCANYxDOCx2x&fromPageId=20644360">
            <IMG src="http://geronimo.apache.org/images/icons/add_blogentry_16.gif" height="16" width="16" border="0" align="absmiddle" title="Add News"></A>
          <A href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=TUSCANYxDOCx2x&fromPageId=20644360">Add News</A>
<!-- -->	 
        </DIV>
      </DIV>

      <DIV class="pagecontent">
        <DIV class="wiki-content">
          <H3><A name="SCAJavabinding.rest-%3Cbinding.rest%3EIntroduction"></A>&lt;binding.rest&gt; Introduction</H3>

<P>The Tuscany Java SCA runtime supports Representational State Transfer (REST) services invocations via the &lt;binding.rest&gt; extension. Tuscany REST binding leverage JAX-RS Standards based annotations to map business operations to HTTP operations such as POST, GET, PUT and DELETE and utilizes Tuscany Databindings to provide support for different wire formats such as JSON, XML, Binary, etc shielding the application developer from contaminating his business logic with code to handle payload production/transformation details.</P>

<H3><A name="SCAJavabinding.rest-UsingtheTuscanyRESTbinding"></A>Using the Tuscany REST binding</H3>

<P>The primary use of the REST binding is to provide business services over HTTP in a distributed fashion. The simplest way to use the REST binding is to declare a business service that can be shared over the web and provide an HTTP address where one can access the service. This service is declared in an SCA composite file.</P>

<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">	
    &lt;component name=<SPAN class="code-quote">&quot;Catalog&quot;</SPAN>&gt;
	&lt;implementation.java class=<SPAN class="code-quote">&quot;services.store.FruitsCatalogImpl&quot;</SPAN>/&gt; 
        &lt;service name=<SPAN class="code-quote">&quot;Catalog&quot;</SPAN>&gt;
	    &lt;tuscany:binding.<SPAN class="code-keyword">rest</SPAN> uri=<SPAN class="code-quote">&quot;http:<SPAN class="code-comment">//localhost:8085/Catalog&quot;</SPAN>&gt;
</SPAN>    	        &lt;tuscany:wireFormat.json /&gt;
	        &lt;tuscany:operationSelector.jaxrs /&gt;
    	    &lt;/tuscany:binding.<SPAN class="code-keyword">rest</SPAN>&gt;
   	&lt;/service&gt;
    &lt;/component&gt;
</PRE>
</DIV></DIV>


<P>Another way of implementing a REST service is to use a collection interface that matches the actions of the HTTP protocol. In this case, the methods must be named post, get, put, and delete. Tuscany ensures that the proper method is invoked via the request and response protocol of HTTP:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">public</SPAN> class TestGetImpl {
    
    <SPAN class="code-keyword">public</SPAN> InputStream get(<SPAN class="code-object">String</SPAN> id) {
        <SPAN class="code-keyword">return</SPAN> <SPAN class="code-keyword">new</SPAN> ByteArrayInputStream((<SPAN class="code-quote">&quot;&lt;html&gt;&lt;body&gt;&lt;p&gt;This is the service GET method, item=&quot;</SPAN> + id + <SPAN class="code-quote">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</SPAN>).getBytes());

    }
}
</PRE>
</DIV></DIV>

<P>So using the common verbs of HTTP and Java object serialization, one can implement services and run them anywhere the HTTP protocol is implemented. The service developer or implementer simply creates methods for post, get, put, and delete, and a business collection such as a shopping cart, telephone directory, insurance form, or blog sites can be created. See the Tuscany module binding-rest-runtime for complete examples.</P>


<H3><A name="SCAJavabinding.rest-MappingbusinessinterfacestoHTTPOperationsusingJAXRSStandardAnnotations"></A>Mapping business interfaces to HTTP Operations using JAX-RS Standard Annotations</H3>

<P>JAX-RS offers standards based annotations that allow properly configuration of the REST service endpoint and mappings of specific HTTP operations (e.g. get, put, post, delete) to java operations. The following subset of JAX-RS annotations are currently supported :</P>

<H4><A name="SCAJavabinding.rest-URIMappings"></A>URI Mappings</H4>

<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
@Path
@Path(<SPAN class="code-quote">&quot;{id}&quot;</SPAN>)
@PathParam(<SPAN class="code-quote">&quot;id&quot;</SPAN>)
</PRE>
</DIV></DIV>

<H4><A name="SCAJavabinding.rest-OperationMappings"></A>Operation Mappings</H4>

<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
@GET
@PUT
@POST
@DELETE
</PRE>
</DIV></DIV>

<H4><A name="SCAJavabinding.rest-EnablingJAXRSannotationprocessing"></A>Enabling JAX-RS annotation processing</H4>

<P>In order to enable JAX-RS annotation processing, a component need to be configured to use JAX-RS Operation Selector.</P>

<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
        &lt;component name=<SPAN class="code-quote">&quot;Catalog&quot;</SPAN>&gt;
		&lt;implementation.java class=<SPAN class="code-quote">&quot;services.store.FruitsCatalogImpl&quot;</SPAN>/&gt; 
		&lt;property name=<SPAN class="code-quote">&quot;currencyCode&quot;</SPAN>&gt;USD&lt;/property&gt;
		&lt;service name=<SPAN class="code-quote">&quot;Catalog&quot;</SPAN>&gt;
			&lt;tuscany:binding.<SPAN class="code-keyword">rest</SPAN> uri=<SPAN class="code-quote">&quot;http:<SPAN class="code-comment">//localhost:8085/Catalog&quot;</SPAN>&gt;
</SPAN>    		            &lt;tuscany:wireFormat.json /&gt;
			    &lt;tuscany:operationSelector.jaxrs /&gt;
    		        &lt;/tuscany:binding.<SPAN class="code-keyword">rest</SPAN>&gt;
   		&lt;/service&gt;
		&lt;reference name=<SPAN class="code-quote">&quot;currencyConverter&quot;</SPAN> target=<SPAN class="code-quote">&quot;CurrencyConverter&quot;</SPAN>/&gt;	
	&lt;/component&gt; 
</PRE>
</DIV></DIV>

<DIV class="panelMacro"><TABLE class="infoMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="https://cwiki.apache.org/confluence/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD>Integration with a existent JAX-RS engine might help on processing the full set of JAX-RS annotations. I have started looking into leveraging Wink resourceProcessor or related code to help on this layer.</TD></TR></TABLE></DIV>

<H3><A name="SCAJavabinding.rest-MappingRPCstylecallsoverHTTPGET"></A>Mapping RPC style calls over HTTP GET</H3>

<P>In some cases, there isn't a simple mapping of a business operation as resources, but there is still a desire to take advantage of HTTP caching and other benefits that HTTP GET type of operations provide. In order to accomplish this, binding.rest has built in functionality that allows you to map RPC style calls to HTTP GET operations.</P>



<H4><A name="SCAJavabinding.rest-ClientInvocation"></A>Client Invocation</H4>

<P>The URL schema follows a simple pattern :</P>

<UL class="alternate" type="square">
	<LI>&lt;base service URI&gt; ?<B>method=</B>&lt;operation name&gt;&amp;<B>parm1=</B>&lt;value&gt;&amp;<B>parm2=</B>&lt;value&gt;</LI>
</UL>


<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
http:<SPAN class="code-comment">//localhost:8085/EchoService?method=echo&amp;msg=Hello RPC</SPAN>
</PRE>
</DIV></DIV>

<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
http:<SPAN class="code-comment">//localhost:8085/EchoService?method=echoArrayString&amp;msgArray=Hello RPC1&amp;msgArray=Hello RPC2&quot;</SPAN>
</PRE>
</DIV></DIV>

<H4><A name="SCAJavabinding.rest-MappingQueryStringstoBusinessOperationparameters"></A>Mapping QueryStrings to Business Operation parameters</H4>

<P>Standard JAX-RS annotation @QueryParam is used to map parameters sent over HTTP GET invocations using query string to business operation parameter</P>

<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
@Remotable
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> Echo {

    <SPAN class="code-object">String</SPAN> echo(@QueryParam(<SPAN class="code-quote">&quot;msg&quot;</SPAN>) <SPAN class="code-object">String</SPAN> msg);

    <SPAN class="code-object">int</SPAN> echoInt(<SPAN class="code-object">int</SPAN> param);

    <SPAN class="code-object">boolean</SPAN> echoBoolean(<SPAN class="code-object">boolean</SPAN> param);

    <SPAN class="code-object">String</SPAN> [] echoArrayString(@QueryParam(<SPAN class="code-quote">&quot;msgArray&quot;</SPAN>) <SPAN class="code-object">String</SPAN>[] stringArray);

    <SPAN class="code-object">int</SPAN> [] echoArrayInt(<SPAN class="code-object">int</SPAN>[] intArray);

}
</PRE>
</DIV></DIV>

<H4><A name="SCAJavabinding.rest-EnablingRPCtoHTTPGETmapping"></A>Enabling RPC to HTTP GET mapping</H4>

<P>In order to enable automatically mapping, a component need to be configured to use RPC Operation Selector.</P>

<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
        &lt;component name=<SPAN class="code-quote">&quot;Catalog&quot;</SPAN>&gt;
		&lt;implementation.java class=<SPAN class="code-quote">&quot;services.store.FruitsCatalogImpl&quot;</SPAN>/&gt; 
		&lt;property name=<SPAN class="code-quote">&quot;currencyCode&quot;</SPAN>&gt;USD&lt;/property&gt;
		&lt;service name=<SPAN class="code-quote">&quot;Catalog&quot;</SPAN>&gt;
			&lt;tuscany:binding.<SPAN class="code-keyword">rest</SPAN> uri=<SPAN class="code-quote">&quot;http:<SPAN class="code-comment">//localhost:8085/Catalog&quot;</SPAN>&gt;
</SPAN>    		            &lt;tuscany:wireFormat.json /&gt;
                            &lt;tuscany:response&gt;
                                &lt;tuscany:operationSelector.rpc /&gt;
                            &lt;/tuscany:response&gt;
    		        &lt;/tuscany:binding.<SPAN class="code-keyword">rest</SPAN>&gt;
   		&lt;/service&gt;
		&lt;reference name=<SPAN class="code-quote">&quot;currencyConverter&quot;</SPAN> target=<SPAN class="code-quote">&quot;CurrencyConverter&quot;</SPAN>/&gt;	
	&lt;/component&gt; 
</PRE>
</DIV></DIV>

<H3><A name="SCAJavabinding.rest-WireFormats"></A>Wire Formats</H3>

<P>This binding will support two styles of wire formats and will be used to control what type of payload will be generated by the service:</P>

<UL class="alternate" type="square">
	<LI>hardWired : where you hard code the wire format expectations in the composite when configuring the binding. In the example below, service will be using JSON payload.</LI>
</UL>


<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
&lt;binding...&gt;
   &lt;wireFormat.json&gt;
&lt;/binding...&gt;
</PRE>
</DIV></DIV>

<UL class="alternate" type="square">
	<LI>dynamic : based on Content-Type header for request and Accept header for response. In the case below, the request content will be parsed based on the Content-Type request header and the response payload will be based on the request Accept header.</LI>
</UL>


<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
&lt;binding...&gt;
   &lt;wireFormat.dynamic&gt;
&lt;/binding...&gt;
</PRE>
</DIV></DIV>

<DIV class="panelMacro"><TABLE class="infoMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="https://cwiki.apache.org/confluence/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD>In progress</TD></TR></TABLE></DIV>

<H3><A name="SCAJavabinding.rest-CachecontrolusingETags%2CLastModifiedandotherHTTPHeaders"></A>Cache control using ETags, Last-Modified and other HTTP Headers</H3>

<P>The HTTP specification provides a set of methods for HTTP clients and servers to interact. These methods form the foundation of the World Wide Web. Tuscany implements many of these methods a binding interface to a collection. The main methods are:</P>

<UL>
	<LI>GET - retrieves an item from a collection</LI>
	<LI>POST - creates or adds an item to a collection</LI>
	<LI>PUT - updates or replaces an item in a collection</LI>
	<LI>DELETE - removes an item in a a collection</LI>
</UL>


<P>The HTTP specification (HTTP 1.1 Chapter 13 - Caching) also provides a mechanism by which these methods may be executed conditionally. To perform conditional methods, an HTTP client puts 3 items in the HTTP request header:</P>

<UL>
	<LI>ETag - entity tag, a unique identifier to an item in a collection. Normally created and returned by the server when creating (POST) a new item.</LI>
	<LI>LastModified - an updated field. Normally a string containing a date and time of the last modification of the item.</LI>
	<LI>Predicate - a logical test (e.g. IfModified, IfUnmodified) to use with the ETag and LastModified to determine whether to act.</LI>
</UL>


<P>The complete list of predicates is given in the HTTP specification.</P>

<P>The most common use of conditional methods is to prevent two requests to the server instead of one conditional request. For example, a common scenario is to check if an item has been modified, if not changed update it with a new version, if changed do not update it. With a conditional PUT method (using the IfUnmodifed predicate and a LastModified date), this can be done in one action. Another common use is to prevent multiple GETs of an item to ensure we have a valid copy. Rather than doing a second request of a large item, one can do a conditional GET request (using an IfModified predicate and a LastModified date), and avoid the second request if our object is still valid. The server responds with either a normal response body, or status code 304 (Not Modified), or status code 412 (precondition failed).</P>

<P>Default cache control is done by using generated ETags based on response content checksum. To avoid data to be overwriten during concurrent updates, include an HTTP If-Match header that contains the original content ETag value. If you want to force an update regardless of whether someone else has updated it since you retrieved it, then use If-Match: * and don't include the ETag.</P>

<H4><A name="SCAJavabinding.rest-Declarativecachecontrol"></A>Declarative cache control</H4>
<P>In order to allow for better flexibility, and re-usability of the components exposed as REST services, there is an option to declare cache controls when configuring the REST Binding as show the example below :</P>

<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
	&lt;component name=<SPAN class="code-quote">&quot;CustomerService&quot;</SPAN>&gt;
		&lt;implementation.java class=<SPAN class="code-quote">&quot;services.customer.CustomerServiceImpl&quot;</SPAN>/&gt; 
		&lt;service name=<SPAN class="code-quote">&quot;CustomerService&quot;</SPAN>&gt;
			&lt;tuscany:binding.<SPAN class="code-keyword">rest</SPAN> uri=<SPAN class="code-quote">&quot;http:<SPAN class="code-comment">//localhost:8085/Customer&quot;</SPAN>&gt;
</SPAN>    		    &lt;tuscany:wireFormat.xml /&gt;
			    &lt;tuscany:operationSelector.jaxrs /&gt;
			    &lt;tuscany:http-headers&gt;
			       &lt;tuscany:header name=<SPAN class="code-quote">&quot;Cache-Control&quot;</SPAN> value=<SPAN class="code-quote">&quot;no-cache&quot;</SPAN>/&gt;
			       &lt;tuscany:header name=<SPAN class="code-quote">&quot;Expires&quot;</SPAN> value=<SPAN class="code-quote">&quot;-1&quot;</SPAN>/&gt;
			       &lt;tuscany:header name=<SPAN class="code-quote">&quot;X-Tuscany&quot;</SPAN> value=<SPAN class="code-quote">&quot;tuscany&quot;</SPAN>/&gt; 
			    &lt;/tuscany:http-headers&gt;
    		&lt;/tuscany:binding.<SPAN class="code-keyword">rest</SPAN>&gt;
   		&lt;/service&gt;
	&lt;/component&gt;
</PRE>
</DIV></DIV>

<DIV class="panelMacro"><TABLE class="infoMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="https://cwiki.apache.org/confluence/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD>This could be enhanced to enable more complex injection of fields to cache control headers.</TD></TR></TABLE></DIV>



<H3><A name="SCAJavabinding.rest-StorescenariosgoesRESTCatalogServicesusingbinding.rest"></A>Store scenarios goes REST - Catalog Services using binding.rest</H3>

<P>Below is our Store Catalog exposed as REST services utilizing the new binding.rest.</P>

<P>Let's start by looking on how the component gets defined and configured in the composite file, particularly the following details :</P>

<UL class="alternate" type="square">
	<LI>binding.rest uri defines the Catalog service endpoint</LI>
	<LI>wireFormat.json configure the service to use JSON as the payload</LI>
	<LI>operationSelector.jaxrs configure the binding to use JAX-RS annotations to map the HTTP operations to business operations</LI>
</UL>


<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
&lt;composite	xmlns=<SPAN class="code-quote">&quot;http:<SPAN class="code-comment">//docs.oasis-open.org/ns/opencsa/sca/200912&quot;</SPAN>
</SPAN>		xmlns:tuscany=<SPAN class="code-quote">&quot;http:<SPAN class="code-comment">//tuscany.apache.org/xmlns/sca/1.1&quot;</SPAN>
</SPAN>		targetNamespace=<SPAN class="code-quote">&quot;http:<SPAN class="code-comment">//store&quot;</SPAN>
</SPAN>		name=<SPAN class="code-quote">&quot;store&quot;</SPAN>&gt;
		
	&lt;component name=<SPAN class="code-quote">&quot;Catalog&quot;</SPAN>&gt;
		&lt;implementation.java class=<SPAN class="code-quote">&quot;services.store.FruitsCatalogImpl&quot;</SPAN>/&gt; 
		&lt;property name=<SPAN class="code-quote">&quot;currencyCode&quot;</SPAN>&gt;USD&lt;/property&gt;
		&lt;service name=<SPAN class="code-quote">&quot;Catalog&quot;</SPAN>&gt;
			&lt;tuscany:binding.<SPAN class="code-keyword">rest</SPAN> uri=<SPAN class="code-quote">&quot;http:<SPAN class="code-comment">//localhost:8085/Catalog&quot;</SPAN>&gt;
</SPAN>    		            &lt;tuscany:wireFormat.json /&gt;
			    &lt;tuscany:operationSelector.jaxrs /&gt;
    		        &lt;/tuscany:binding.<SPAN class="code-keyword">rest</SPAN>&gt;
   		&lt;/service&gt;
		&lt;reference name=<SPAN class="code-quote">&quot;currencyConverter&quot;</SPAN> target=<SPAN class="code-quote">&quot;CurrencyConverter&quot;</SPAN>/&gt;	
	&lt;/component&gt; 
    
	&lt;component name=<SPAN class="code-quote">&quot;CurrencyConverter&quot;</SPAN>&gt;
		&lt;implementation.java class=<SPAN class="code-quote">&quot;services.store.CurrencyConverterImpl&quot;</SPAN>/&gt;
	&lt;/component&gt;     

&lt;/composite&gt;
</PRE>
</DIV></DIV>


<P>Below is the Catalog Interface utilizing JAX-RS standard annotations to map HTTP operations to business operations.</P>

<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">package</SPAN> services.store;

<SPAN class="code-keyword">import</SPAN> javax.ws.rs.DELETE;
<SPAN class="code-keyword">import</SPAN> javax.ws.rs.GET;
<SPAN class="code-keyword">import</SPAN> javax.ws.rs.POST;
<SPAN class="code-keyword">import</SPAN> javax.ws.rs.PUT;
<SPAN class="code-keyword">import</SPAN> javax.ws.rs.Path;
<SPAN class="code-keyword">import</SPAN> javax.ws.rs.PathParam;

<SPAN class="code-keyword">import</SPAN> org.oasisopen.sca.annotation.Remotable;


@Remotable
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> Catalog {
    
    @GET
    Item[] getAll();
    
    @GET
    @Path(<SPAN class="code-quote">&quot;{id}&quot;</SPAN>)
    Item getItemById(@PathParam(<SPAN class="code-quote">&quot;id&quot;</SPAN>) <SPAN class="code-object">String</SPAN> itemId);
    
    @POST
    void addItem(Item item);
    
    @PUT
    void updateItem(Item item);
    
    @DELETE
    @Path(<SPAN class="code-quote">&quot;{id}&quot;</SPAN>)
    void deleteItem(@PathParam(<SPAN class="code-quote">&quot;id&quot;</SPAN>) <SPAN class="code-object">String</SPAN> itemId);
}
</PRE>
</DIV></DIV>

<P>Below is the Fuit catalog implementation</P>

<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
@Scope(<SPAN class="code-quote">&quot;COMPOSITE&quot;</SPAN>)
<SPAN class="code-keyword">public</SPAN> class FruitsCatalogImpl <SPAN class="code-keyword">implements</SPAN> Catalog {
    
    @Property
    <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">String</SPAN> currencyCode = <SPAN class="code-quote">&quot;USD&quot;</SPAN>;
    
    @Reference
    <SPAN class="code-keyword">public</SPAN> CurrencyConverter currencyConverter;
    
    <SPAN class="code-keyword">private</SPAN> Map&lt;<SPAN class="code-object">String</SPAN>, Item&gt; catalog = <SPAN class="code-keyword">new</SPAN> HashMap&lt;<SPAN class="code-object">String</SPAN>, Item&gt;();

    @Init
    <SPAN class="code-keyword">public</SPAN> void init() {
        <SPAN class="code-object">String</SPAN> currencySymbol = currencyConverter.getCurrencySymbol(currencyCode);
        catalog.put(<SPAN class="code-quote">&quot;Apple&quot;</SPAN>, <SPAN class="code-keyword">new</SPAN> Item(<SPAN class="code-quote">&quot;Apple&quot;</SPAN>,  currencySymbol + currencyConverter.getConversion(<SPAN class="code-quote">&quot;USD&quot;</SPAN>, currencyCode, 2.99)));
        catalog.put(<SPAN class="code-quote">&quot;Orange&quot;</SPAN>, <SPAN class="code-keyword">new</SPAN> Item(<SPAN class="code-quote">&quot;Orange&quot;</SPAN>, currencySymbol + currencyConverter.getConversion(<SPAN class="code-quote">&quot;USD&quot;</SPAN>, currencyCode, 3.55)));
        catalog.put(<SPAN class="code-quote">&quot;Pear&quot;</SPAN>, <SPAN class="code-keyword">new</SPAN> Item(<SPAN class="code-quote">&quot;Pear&quot;</SPAN>, currencySymbol + currencyConverter.getConversion(<SPAN class="code-quote">&quot;USD&quot;</SPAN>, currencyCode, 1.55)));
    }

    <SPAN class="code-keyword">public</SPAN> Item[] getAll() {
        Item[] catalogArray = <SPAN class="code-keyword">new</SPAN> Item[catalog.size()];
        catalog.values().toArray(catalogArray);
        <SPAN class="code-keyword">return</SPAN> catalogArray;
    }
    
    <SPAN class="code-keyword">public</SPAN> Item getItemById(<SPAN class="code-object">String</SPAN> itemId) {
        <SPAN class="code-keyword">return</SPAN> catalog.get(itemId);
    }
    
    <SPAN class="code-keyword">public</SPAN> void addItem(Item item) {
        catalog.put(item.getName(),item);
    }
    
    <SPAN class="code-keyword">public</SPAN> void updateItem(Item item) {
        <SPAN class="code-keyword">if</SPAN>(catalog.get(item.getName()) != <SPAN class="code-keyword">null</SPAN>) {
            catalog.put(item.getName(), item);
        }
    }
    
    <SPAN class="code-keyword">public</SPAN> void deleteItem(<SPAN class="code-object">String</SPAN> itemId) {
        <SPAN class="code-keyword">if</SPAN>(catalog.get(itemId) != <SPAN class="code-keyword">null</SPAN>) {
            catalog.remove(itemId);
        }        
    }
}
</PRE>
</DIV></DIV>

        </DIV>

              </DIV>
    </DIV>
                </TD>
            </TR>
        </TABLE>

            <!-- footer -->
    <TABLE border="0" cellpadding="2" cellspacing="0" width="100%">
        <TR>
          <TD align="left" valign="middle" class="footer">
            &nbsp;&nbsp;
            <IMG src="http://static.delicious.com/img/delicious.small.gif" height="10" width="10" alt="Delicious">
            <A href="http://delicious.com/save" onclick="window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title),'delicious','toolbar=no,width=550,height=550'); return false;">Bookmark this on Delicious</A>
            &nbsp;&nbsp;
            <IMG src="http://digg.com/img/badges/16x16-digg-guy.gif" width="16" height="16" alt="Digg!">
            <A href="" onclick="window.open('http://digg.com/submit?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&topic=programming');">Digg this</A>
            <!-- Slicker, but no text
            <script type="text/javascript">
              digg_skin = 'icon';
              digg_window = 'new';
              digg_title = 'Apache Geronimo v2.2 Documentation : SCA Java binding.rest';
              digg_topic = 'programming';
            </script>
            <script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
            -->
            &nbsp;&nbsp;
          </TD>
          <TD align="right" valign="middle" class="footer">
            <A href="http://cwiki.apache.org/GMOxPMGT/geronimo-privacy-policy.html">Privacy Policy</A>&nbsp;&nbsp;-&nbsp;&nbsp;
            Copyright &copy; 2003-2010, The Apache Software Foundation, Licensed under <A href="http://www.apache.org/licenses/LICENSE-2.0">ASL 2.0.</A>&nbsp;&nbsp;
          </TD>
        </TR>
    </TABLE> 

    </BODY>
</HTML>