Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
@GET
@PUT
@POST
@DELETE
Info

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.

Wire Formats

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:

...

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.

Info

Further support for declarative with possible injection of fields to cache control headers is under investigation.

Store scenarios goes REST - Catalog Services using binding.rest

Below is our Store Catalog exposed as REST services utilizing the new binding.rest.

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

  • binding.rest uri defines the Catalog service endpoint
  • wireFormat.json configure the service to use JSON as the payload
  • operationSelector.jaxrs configure the binding to use JAX-RS annotations to map the HTTP operations to business operations
Code Block
<composite	xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
		xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
		targetNamespace="http://store"
		name="store">
		
	<component name="Catalog">
		<implementation.java class="services.store.FruitsCatalogImpl"/> 
		<property name="currencyCode">USD</property>
		<service name="Catalog">
			<tuscany:binding.rest uri="http://localhost:8085/Catalog">
    		            <tuscany:wireFormat.json />
			    <tuscany:operationSelector.jaxrs />
    		        </tuscany:binding.rest>
   		</service>
		<reference name="currencyConverter" target="CurrencyConverter"/>	
	</component> 
    
	<component name="CurrencyConverter">
		<implementation.java class="services.store.CurrencyConverterImpl"/>
	</component>     

</composite>

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

Code Block
package services.store;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import org.oasisopen.sca.annotation.Remotable;


@Remotable
public interface Catalog {
    
    @GET
    Item[] getAll();
    
    @GET
    @Path("{id}")
    Item getItemById(@PathParam("id") String itemId);
    
    @POST
    void addItem(Item item);
    
    @PUT
    void updateItem(Item item);
    
    @DELETE
    @Path("{id}")
    void deleteItem(@PathParam("id") String itemId);
}

Below is the Fuit catalog implementation

Code Block
@Scope("COMPOSITE")
public class FruitsCatalogImpl implements Catalog {
    
    @Property
    public String currencyCode = "USD";
    
    @Reference
    public CurrencyConverter currencyConverter;
    
    private Map<String, Item> catalog = new HashMap<String, Item>();

    @Init
    public void init() {
        String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode);
        catalog.put("Apple", new Item("Apple",  currencySymbol + currencyConverter.getConversion("USD", currencyCode, 2.99)));
        catalog.put("Orange", new Item("Orange", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 3.55)));
        catalog.put("Pear", new Item("Pear", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 1.55)));
    }

    public Item[] getAll() {
        Item[] catalogArray = new Item[catalog.size()];
        catalog.values().toArray(catalogArray);
        return catalogArray;
    }
    
    public Item getItemById(String itemId) {
        return catalog.get(itemId);
    }
    
    public void addItem(Item item) {
        catalog.put(item.getName(),item);
    }
    
    public void updateItem(Item item) {
        if(catalog.get(item.getName()) != null) {
            catalog.put(item.getName(), item);
        }
    }
    
    public void deleteItem(String itemId) {
        if(catalog.get(itemId) != null) {
            catalog.remove(itemId);
        }        
    }
}