diff options
author | coreyg <coreyg@13f79535-47bb-0310-9956-ffa450edef68> | 2014-11-21 09:30:19 +0000 |
---|---|---|
committer | coreyg <coreyg@13f79535-47bb-0310-9956-ffa450edef68> | 2014-11-21 09:30:19 +0000 |
commit | 200a40b332420f94992eb39a6d0ea1cf1490ffc4 (patch) | |
tree | 1c76ef1d254c7bfc91128a313d319bd60da9d01e /site/trunk/site-publish/documentation-2x/sca-java-bindingrest.html | |
parent | 85f53ab6ab4f664d63774c94b672a875c9e52b8a (diff) |
Adding tuscany's website to their svn repo for svnpubsub
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1640879 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r-- | site/trunk/site-publish/documentation-2x/sca-java-bindingrest.html | 515 |
1 files changed, 515 insertions, 0 deletions
diff --git a/site/trunk/site-publish/documentation-2x/sca-java-bindingrest.html b/site/trunk/site-publish/documentation-2x/sca-java-bindingrest.html new file mode 100644 index 0000000000..5c7fa11f94 --- /dev/null +++ b/site/trunk/site-publish/documentation-2x/sca-java-bindingrest.html @@ -0,0 +1,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> > <A href="index.html" title="Apache Tuscany Docs 2.x">Apache Tuscany Docs 2.x</A> > <A href="index.html" title="Index">Index</A> > <A href="sca-java-extensions-guide.html" title="SCA Java Extensions Guide">SCA Java Extensions Guide</A> > <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%"> + <A href="index.html" title="Apache Tuscany Docs 2.x">Apache Tuscany Docs 2.x</A> > <A href="index.html" title="Index">Index</A> > <A href="sca-java-extensions-guide.html" title="SCA Java Extensions Guide">SCA Java Extensions Guide</A> > <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> + </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> + + <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> + + <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> + + <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><binding.rest> Introduction</H3> + +<P>The Tuscany Java SCA runtime supports Representational State Transfer (REST) services invocations via the <binding.rest> 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"> + <component name=<SPAN class="code-quote">"Catalog"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"services.store.FruitsCatalogImpl"</SPAN>/> + <service name=<SPAN class="code-quote">"Catalog"</SPAN>> + <tuscany:binding.<SPAN class="code-keyword">rest</SPAN> uri=<SPAN class="code-quote">"http:<SPAN class="code-comment">//localhost:8085/Catalog"</SPAN>> +</SPAN> <tuscany:wireFormat.json /> + <tuscany:operationSelector.jaxrs /> + </tuscany:binding.<SPAN class="code-keyword">rest</SPAN>> + </service> + </component> +</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">"<html><body><p>This is the service GET method, item="</SPAN> + id + <SPAN class="code-quote">"</body></html>"</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">"{id}"</SPAN>) +@PathParam(<SPAN class="code-quote">"id"</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"> + <component name=<SPAN class="code-quote">"Catalog"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"services.store.FruitsCatalogImpl"</SPAN>/> + <property name=<SPAN class="code-quote">"currencyCode"</SPAN>>USD</property> + <service name=<SPAN class="code-quote">"Catalog"</SPAN>> + <tuscany:binding.<SPAN class="code-keyword">rest</SPAN> uri=<SPAN class="code-quote">"http:<SPAN class="code-comment">//localhost:8085/Catalog"</SPAN>> +</SPAN> <tuscany:wireFormat.json /> + <tuscany:operationSelector.jaxrs /> + </tuscany:binding.<SPAN class="code-keyword">rest</SPAN>> + </service> + <reference name=<SPAN class="code-quote">"currencyConverter"</SPAN> target=<SPAN class="code-quote">"CurrencyConverter"</SPAN>/> + </component> +</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><base service URI> ?<B>method=</B><operation name>&<B>parm1=</B><value>&<B>parm2=</B><value></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&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&msgArray=Hello RPC1&msgArray=Hello RPC2"</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">"msg"</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">"msgArray"</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"> + <component name=<SPAN class="code-quote">"Catalog"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"services.store.FruitsCatalogImpl"</SPAN>/> + <property name=<SPAN class="code-quote">"currencyCode"</SPAN>>USD</property> + <service name=<SPAN class="code-quote">"Catalog"</SPAN>> + <tuscany:binding.<SPAN class="code-keyword">rest</SPAN> uri=<SPAN class="code-quote">"http:<SPAN class="code-comment">//localhost:8085/Catalog"</SPAN>> +</SPAN> <tuscany:wireFormat.json /> + <tuscany:response> + <tuscany:operationSelector.rpc /> + </tuscany:response> + </tuscany:binding.<SPAN class="code-keyword">rest</SPAN>> + </service> + <reference name=<SPAN class="code-quote">"currencyConverter"</SPAN> target=<SPAN class="code-quote">"CurrencyConverter"</SPAN>/> + </component> +</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"> +<binding...> + <wireFormat.json> +</binding...> +</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"> +<binding...> + <wireFormat.dynamic> +</binding...> +</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"> + <component name=<SPAN class="code-quote">"CustomerService"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"services.customer.CustomerServiceImpl"</SPAN>/> + <service name=<SPAN class="code-quote">"CustomerService"</SPAN>> + <tuscany:binding.<SPAN class="code-keyword">rest</SPAN> uri=<SPAN class="code-quote">"http:<SPAN class="code-comment">//localhost:8085/Customer"</SPAN>> +</SPAN> <tuscany:wireFormat.xml /> + <tuscany:operationSelector.jaxrs /> + <tuscany:http-headers> + <tuscany:header name=<SPAN class="code-quote">"Cache-Control"</SPAN> value=<SPAN class="code-quote">"no-cache"</SPAN>/> + <tuscany:header name=<SPAN class="code-quote">"Expires"</SPAN> value=<SPAN class="code-quote">"-1"</SPAN>/> + <tuscany:header name=<SPAN class="code-quote">"X-Tuscany"</SPAN> value=<SPAN class="code-quote">"tuscany"</SPAN>/> + </tuscany:http-headers> + </tuscany:binding.<SPAN class="code-keyword">rest</SPAN>> + </service> + </component> +</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"> +<composite xmlns=<SPAN class="code-quote">"http:<SPAN class="code-comment">//docs.oasis-open.org/ns/opencsa/sca/200912"</SPAN> +</SPAN> xmlns:tuscany=<SPAN class="code-quote">"http:<SPAN class="code-comment">//tuscany.apache.org/xmlns/sca/1.1"</SPAN> +</SPAN> targetNamespace=<SPAN class="code-quote">"http:<SPAN class="code-comment">//store"</SPAN> +</SPAN> name=<SPAN class="code-quote">"store"</SPAN>> + + <component name=<SPAN class="code-quote">"Catalog"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"services.store.FruitsCatalogImpl"</SPAN>/> + <property name=<SPAN class="code-quote">"currencyCode"</SPAN>>USD</property> + <service name=<SPAN class="code-quote">"Catalog"</SPAN>> + <tuscany:binding.<SPAN class="code-keyword">rest</SPAN> uri=<SPAN class="code-quote">"http:<SPAN class="code-comment">//localhost:8085/Catalog"</SPAN>> +</SPAN> <tuscany:wireFormat.json /> + <tuscany:operationSelector.jaxrs /> + </tuscany:binding.<SPAN class="code-keyword">rest</SPAN>> + </service> + <reference name=<SPAN class="code-quote">"currencyConverter"</SPAN> target=<SPAN class="code-quote">"CurrencyConverter"</SPAN>/> + </component> + + <component name=<SPAN class="code-quote">"CurrencyConverter"</SPAN>> + <implementation.java class=<SPAN class="code-quote">"services.store.CurrencyConverterImpl"</SPAN>/> + </component> + +</composite> +</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">"{id}"</SPAN>) + Item getItemById(@PathParam(<SPAN class="code-quote">"id"</SPAN>) <SPAN class="code-object">String</SPAN> itemId); + + @POST + void addItem(Item item); + + @PUT + void updateItem(Item item); + + @DELETE + @Path(<SPAN class="code-quote">"{id}"</SPAN>) + void deleteItem(@PathParam(<SPAN class="code-quote">"id"</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">"COMPOSITE"</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">"USD"</SPAN>; + + @Reference + <SPAN class="code-keyword">public</SPAN> CurrencyConverter currencyConverter; + + <SPAN class="code-keyword">private</SPAN> Map<<SPAN class="code-object">String</SPAN>, Item> catalog = <SPAN class="code-keyword">new</SPAN> HashMap<<SPAN class="code-object">String</SPAN>, Item>(); + + @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">"Apple"</SPAN>, <SPAN class="code-keyword">new</SPAN> Item(<SPAN class="code-quote">"Apple"</SPAN>, currencySymbol + currencyConverter.getConversion(<SPAN class="code-quote">"USD"</SPAN>, currencyCode, 2.99))); + catalog.put(<SPAN class="code-quote">"Orange"</SPAN>, <SPAN class="code-keyword">new</SPAN> Item(<SPAN class="code-quote">"Orange"</SPAN>, currencySymbol + currencyConverter.getConversion(<SPAN class="code-quote">"USD"</SPAN>, currencyCode, 3.55))); + catalog.put(<SPAN class="code-quote">"Pear"</SPAN>, <SPAN class="code-keyword">new</SPAN> Item(<SPAN class="code-quote">"Pear"</SPAN>, currencySymbol + currencyConverter.getConversion(<SPAN class="code-quote">"USD"</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"> + + <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> + + <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> + --> + + </TD> + <TD align="right" valign="middle" class="footer"> + <A href="http://cwiki.apache.org/GMOxPMGT/geronimo-privacy-policy.html">Privacy Policy</A> - + Copyright © 2003-2010, The Apache Software Foundation, Licensed under <A href="http://www.apache.org/licenses/LICENSE-2.0">ASL 2.0.</A> + </TD> + </TR> + </TABLE> + + </BODY> +</HTML>
\ No newline at end of file |