You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2015/08/04 14:15:57 UTC
svn commit: r1694041 -
/olingo/site/trunk/content/doc/odata4/tutorials/readep/tutorial_readep.mdtext
Author: chrish
Date: Tue Aug 4 12:15:57 2015
New Revision: 1694041
URL: http://svn.apache.org/r1694041
Log:
CMS commit to olingo by chrish
Modified:
olingo/site/trunk/content/doc/odata4/tutorials/readep/tutorial_readep.mdtext
Modified: olingo/site/trunk/content/doc/odata4/tutorials/readep/tutorial_readep.mdtext
URL: http://svn.apache.org/viewvc/olingo/site/trunk/content/doc/odata4/tutorials/readep/tutorial_readep.mdtext?rev=1694041&r1=1694040&r2=1694041&view=diff
==============================================================================
--- olingo/site/trunk/content/doc/odata4/tutorials/readep/tutorial_readep.mdtext (original)
+++ olingo/site/trunk/content/doc/odata4/tutorials/readep/tutorial_readep.mdtext Tue Aug 4 12:15:57 2015
@@ -88,107 +88,113 @@ Hereâs the full implementation of t
:::java
package myservice.mynamespace.data;
-
+
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
+
import myservice.mynamespace.service.DemoEdmProvider;
import myservice.mynamespace.util.Util;
+
import org.apache.olingo.commons.api.data.Entity;
import org.apache.olingo.commons.api.data.EntityCollection;
import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.data.ValueType;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.commons.api.edm.EdmEntityType;
- import org.apache.olingo.commons.api.http.HttpStatusCode;
+ import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.server.api.ODataApplicationException;
import org.apache.olingo.server.api.uri.UriParameter;
-
+
public class Storage {
- private List<Entity> productList;
-
- public Storage() {
- productList = new ArrayList<Entity>();
- initSampleData();
- }
-
- /* PUBLIC FACADE */
-
- public EntityCollection readEntitySetData(EdmEntitySet edmEntitySet)throws ODataApplicationException{
- // actually, this is only required if we have more than one Entity Sets
- if(edmEntitySet.getName().equals(DemoEdmProvider.ES_PRODUCTS_NAME)){
- return getProducts();
- }
-
- return null;
- }
-
- public Entity readEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams) throws ODataApplicationException{
+ private List<Entity> productList;
- EdmEntityType edmEntityType = edmEntitySet.getEntityType();
-
- // actually, this is only required if we have more than one Entity Type
- if(edmEntityType.getName().equals(DemoEdmProvider.ET_PRODUCT_NAME)){
- return getProduct(edmEntityType, keyParams);
- }
+ public Storage() {
+ productList = new ArrayList<Entity>();
+ initSampleData();
+ }
+
+ /* PUBLIC FACADE */
- return null;
- }
+ public EntityCollection readEntitySetData(EdmEntitySet edmEntitySet)throws ODataApplicationException{
+
+ // actually, this is only required if we have more than one Entity Sets
+ if(edmEntitySet.getName().equals(DemoEdmProvider.ES_PRODUCTS_NAME)){
+ return getProducts();
+ }
+
+ return null;
+ }
- /* INTERNAL */
+ public Entity readEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams) throws ODataApplicationException{
- private EntityCollection getProducts(){
- EntityCollection retEntitySet = new EntityCollection();
+ EdmEntityType edmEntityType = edmEntitySet.getEntityType();
+
+ // actually, this is only required if we have more than one Entity Type
+ if(edmEntityType.getName().equals(DemoEdmProvider.ET_PRODUCT_NAME)){
+ return getProduct(edmEntityType, keyParams);
+ }
+
+ return null;
+ }
+
+
- for(Entity productEntity : this.productList){
- retEntitySet.getEntities().add(productEntity);
- }
+ /* INTERNAL */
- return retEntitySet;
- }
+ private EntityCollection getProducts(){
+ EntityCollection retEntitySet = new EntityCollection();
- private Entity getProduct(EdmEntityType edmEntityType, List<UriParameter> keyParams) throws ODataApplicationException{
+ for(Entity productEntity : this.productList){
+ retEntitySet.getEntities().add(productEntity);
+ }
- // the list of entities at runtime
- EntityCollection rt_entitySet = getProducts();
+ return retEntitySet;
+ }
- /* generic approach to find the requested entity */
- Entity rt_requestedEntity = Util.findEntity(edmEntityType, rt_entitySet, keyParams);
- if(rt_requestedEntity == null){
- // this variable is null if our data doesn't contain an entity for the requested key
- // Throw suitable exception
- throw new ODataApplicationException("Entity for requested key doesn't exist",
- HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
- }
+ private Entity getProduct(EdmEntityType edmEntityType, List<UriParameter> keyParams) throws ODataApplicationException{
- return rt_requestedEntity;
- }
+ // the list of entities at runtime
+ EntityCollection entitySet = getProducts();
+
+ /* generic approach to find the requested entity */
+ Entity requestedEntity = Util.findEntity(edmEntityType, entitySet, keyParams);
+
+ if(requestedEntity == null){
+ // this variable is null if our data doesn't contain an entity for the requested key
+ // Throw suitable exception
+ throw new ODataApplicationException("Entity for requested key doesn't exist",
+ HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
+ }
+
+ return requestedEntity;
+ }
+
+ /* HELPER */
+
+ private void initSampleData(){
+
+ // add some sample product entities
+ productList.add(new Entity()
+ .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1))
+ .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15"))
+ .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB")));
+
+ productList.add(new Entity()
+ .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2))
+ .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA"))
+ .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network")));
+
+ productList.add(new Entity()
+ .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3))
+ .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen"))
+ .addProperty(new Property(null, "Description", ValueType.PRIMITIVE, "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960")));
- /* HELPER */
+ }
+ }
- private void initSampleData(){
- // add some sample product entities
- productList.add(new Entity()
- .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 1))
- .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Notebook Basic 15"))
- .addProperty(new Property(null, "Description", ValueType.PRIMITIVE,
- "Notebook Basic, 1.7GHz - 15 XGA - 1024MB DDR2 SDRAM - 40GB")));
-
- productList.add(new Entity()
- .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 2))
- .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "1UMTS PDA"))
- .addProperty(new Property(null, "Description", ValueType.PRIMITIVE,
- "Ultrafast 3G UMTS/HSDPA Pocket PC, supports GSM network")));
-
- productList.add(new Entity()
- .addProperty(new Property(null, "ID", ValueType.PRIMITIVE, 3))
- .addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Ergo Screen"))
- .addProperty(new Property(null, "Description", ValueType.PRIMITIVE,
- "19 Optimum Resolution 1024 x 768 @ 85Hz, resolution 1280 x 960")));
- }
- }
The *Public Façade* contains the methods that are called from outside.
@@ -244,6 +250,7 @@ We have to create a Constructor that tak
public class DemoEntityCollectionProcessor implements EntityCollectionProcessor {
private OData odata;
+ private ServiceMetadata serviceMetadata;
private Storage storage;
public DemoEntityCollectionProcessor(Storage storage) {
@@ -366,7 +373,7 @@ Copy the following code into this class:
Integer scale = edmKeyProperty.getScale();
// get the EdmType in order to compare
EdmType edmType = edmKeyProperty.getType();
- //if(EdmType instanceof EdmPrimitiveType) // do we need this?
+ // Key properties must be instance of primitive type
EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType)edmType;
// Runtime data: the value of the current entity
@@ -449,6 +456,7 @@ The interface `org.apache.olingo.server.
Letâs have a look at the implementation.
+Create the class `DemoEntityProcessor` in package `myservice.mynamespace.service` which implements the interface `EntityProcessor`.
First we need to implement the `init()` method, in order to store the `OData` object.
Second, as described in the preparation-section, we need to create a constructor that takes and stores our `Storage` instance
@@ -659,6 +667,21 @@ The interface `org.apache.olingo.server.
We will ignore this method in the present tutorial
+Create the class `DemoPrimitiveProcessor` in package `myservice.mynamespace.service` which implements the interface `PrimitiveProcessor`
+
+We have to create a Constructor that takes the `Storage` instance and stores it as a member variable:
+
+ :::java
+ public class DemoEntityCollectionProcessor implements EntityCollectionProcessor {
+
+ private OData odata;
+ private Storage storage;
+
+ public DemoEntityCollectionProcessor(Storage storage) {
+ this.storage = storage;
+ }
+ }
+
**readPrimitive**
Again, we have the following 4 steps to follow:
@@ -680,65 +703,63 @@ Again, we have the following 4 steps to
:::java
- public void readPrimitive(ODataRequest request, ODataResponse response,
- UriInfo uriInfo, ContentType responseFormat)
- throws ODataApplicationException, SerializerException {
-
- // 1. Retrieve info from URI
- // 1.1. retrieve the info about the requested entity set
- List<UriResource> resourceParts = uriInfo.getUriResourceParts();
- // Note: only in our example we can rely that the first segment is the EntitySet
- UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
- EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
- // the key for the entity
- List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
-
- // 1.2. retrieve the requested (Edm) property
- // the last segment is the Property
- UriResourceProperty uriProperty = (UriResourceProperty) resourceParts.get(resourceParts.size() -1);
- EdmProperty edmProperty = uriProperty.getProperty();
- String edmPropertyName = edmProperty.getName();
- // in our example, we know we have only primitive types in our model
- EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();
-
- // 2. retrieve data from backend
- // 2.1. retrieve the entity data, for which the property has to be read
- Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
- if (entity == null) { // Bad request
- throw new ODataApplicationException("Entity not found",
- HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
- }
-
- // 2.2. retrieve the property data from the entity
- Property property = entity.getProperty(edmPropertyName);
- if (property == null) {
- throw new ODataApplicationException("Property not found",
- HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
- }
-
- // 3. serialize
- Object value = property.getValue();
- if (value != null) {
- // 3.1. configure the serializer
- ODataFormat format = ODataFormat.fromContentType(responseFormat);
- ODataSerializer serializer = odata.createSerializer(format);
-
- ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
- PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
- // 3.2. serialize
- SerializerResult serializerResult = serializer.primitive(edmPropertyType, property, options);
- InputStream propertyStream = serializerResult.getContent();
-
- //4. configure the response object
- response.setContent(propertyStream);
- response.setStatusCode(HttpStatusCode.OK.getStatusCode());
- response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
- }else{
- // in case there's no value for the property, we can skip the serialization
- response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
- }
- }
-
+ public void readPrimitive(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
+ throws ODataApplicationException, SerializerException {
+
+ // 1. Retrieve info from URI
+ // 1.1. retrieve the info about the requested entity set
+ List<UriResource> resourceParts = uriInfo.getUriResourceParts();
+ // Note: only in our example we can rely that the first segment is the EntitySet
+ UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
+ EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
+ // the key for the entity
+ List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
+
+ // 1.2. retrieve the requested (Edm) property
+ // the last segment is the Property
+ UriResourceProperty uriProperty = (UriResourceProperty) resourceParts.get(resourceParts.size() -1);
+ EdmProperty edmProperty = uriProperty.getProperty();
+ String edmPropertyName = edmProperty.getName();
+ // in our example, we know we have only primitive types in our model
+ EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();
+
+ // 2. retrieve data from backend
+ // 2.1. retrieve the entity data, for which the property has to be read
+ Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
+ if (entity == null) { // Bad request
+ throw new ODataApplicationException("Entity not found",
+ HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
+ }
+
+ // 2.2. retrieve the property data from the entity
+ Property property = entity.getProperty(edmPropertyName);
+ if (property == null) {
+ throw new ODataApplicationException("Property not found",
+ HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
+ }
+
+ // 3. serialize
+ Object value = property.getValue();
+ if (value != null) {
+ // 3.1. configure the serializer
+ ODataFormat format = ODataFormat.fromContentType(responseFormat);
+ ODataSerializer serializer = odata.createSerializer(format);
+
+ ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
+ PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
+ // 3.2. serialize
+ SerializerResult serializerResult = serializer.primitive(edmPropertyType, property, options);
+ InputStream propertyStream = serializerResult.getContent();
+
+ //4. configure the response object
+ response.setContent(propertyStream);
+ response.setStatusCode(HttpStatusCode.OK.getStatusCode());
+ response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
+ }else{
+ // in case there's no value for the property, we can skip the serialization
+ response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
+ }
+ }
## 4.2. Adapt the DemoServlet class
@@ -797,4 +818,4 @@ In the next tutorial ([Part 3: Write](/d
OData specification: <http://odata.org/>
-Olingo Javadoc: [http://olingo.apache.org/javadoc/odata4/index.html](/javadoc/odata4/index.html)
+Olingo Javadoc: [http://olingo.apache.org/javadoc/odata4/index.html](/javadoc/odata4/index.html)
\ No newline at end of file