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/10/13 13:14:07 UTC

svn commit: r1708329 - /olingo/site/trunk/content/doc/odata4/tutorials/media/tutorial_media.mdtext

Author: chrish
Date: Tue Oct 13 11:14:07 2015
New Revision: 1708329

URL: http://svn.apache.org/viewvc?rev=1708329&view=rev
Log:
CMS commit to olingo by chrish

Modified:
    olingo/site/trunk/content/doc/odata4/tutorials/media/tutorial_media.mdtext

Modified: olingo/site/trunk/content/doc/odata4/tutorials/media/tutorial_media.mdtext
URL: http://svn.apache.org/viewvc/olingo/site/trunk/content/doc/odata4/tutorials/media/tutorial_media.mdtext?rev=1708329&r1=1708328&r2=1708329&view=diff
==============================================================================
--- olingo/site/trunk/content/doc/odata4/tutorials/media/tutorial_media.mdtext (original)
+++ olingo/site/trunk/content/doc/odata4/tutorials/media/tutorial_media.mdtext Tue Oct 13 11:14:07 2015
@@ -51,8 +51,8 @@ Afterwards do a Deploy and run: it shoul
 
 In this tutorial we will implement a media entity set. The idea is to store advertisments and a related image. The metadata document have to be extended by the following elements:
 
-	::::xml
-	<EntityType Name="Advertisment" HasStream="true">
+    ::::xml
+    <EntityType Name="Advertisment" HasStream="true">
         <Key>
             <PropertyRef Name="ID"/>
         </Key>
@@ -60,8 +60,8 @@ In this tutorial we will implement a med
         <Property Name="Name" Type="Edm.String"/>
         <Property Name="AirDate" Type="Edm.DateTimeOffset"/>
     </EntityType>
-
-	<EntityContainer Name="Container">
+    
+    <EntityContainer Name="Container">
         <EntitySet Name="Advertisments" EntityType="OData.Demo.Advertisment"/>
     </EntityContainer>
 
@@ -79,74 +79,74 @@ If you have read the prevois tutorials,
 
 We start with method `DemoEdmProvider.getEntityType`
 
-	::::java
-	public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) {
-		CsdlEntityType entityType = null;
-
-		if (entityTypeName.equals(ET_PRODUCT_FQN)) {
-			// Definition of entity type Product
-			// ...
-		} else if (entityTypeName.equals(ET_CATEGORY_FQN)) {
-			// Definition of entity type Category
-			// ...
-		} else if(entityTypeName.equals(ET_ADVERTISMENT_FQN)) {
-			CsdlProperty id = new CsdlProperty().setName("ID")
-  											  .setType(EdmPrimitiveTypeKind.Guid.getFullQualifiedName());
-			CsdlProperty name = new CsdlProperty().setName("Name")
-        										  .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName());
-			CsdlProperty airDate = new CsdlProperty().setName("AirDate")
-	 											    .setType(EdmPrimitiveTypeKind.DateTimeOffset.getFullQualifiedName());
-
-			CsdlPropertyRef propertyRef = new CsdlPropertyRef();
-			propertyRef.setName("ID");
-
-			entityType = new CsdlEntityType();
-			entityType.setName(ET_ADVERTISMENT_NAME);
-			entityType.setProperties(Arrays.asList(id, name, airDate));
-			entityType.setKey(Collections.singletonList(propertyRef));
-			entityType.setHasStream(true);	// <- Enable the media entity stream
-		}
-
-		return entityType;
-	}
+    ::::java
+    public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) {
+        CsdlEntityType entityType = null;
+    
+        if (entityTypeName.equals(ET_PRODUCT_FQN)) {
+            // Definition of entity type Product
+            // ...
+        } else if (entityTypeName.equals(ET_CATEGORY_FQN)) {
+            // Definition of entity type Category
+            // ...
+        } else if(entityTypeName.equals(ET_ADVERTISMENT_FQN)) {
+            CsdlProperty id = new CsdlProperty().setName("ID")
+                                    .setType(EdmPrimitiveTypeKind.Guid.getFullQualifiedName());
+            CsdlProperty name = new CsdlProperty().setName("Name")
+                                    .setType(EdmPrimitiveTypeKind.String.getFullQualifiedName());
+            CsdlProperty airDate = new CsdlProperty().setName("AirDate")
+                                    .setType(EdmPrimitiveTypeKind.DateTimeOffset.getFullQualifiedName());
+    
+            CsdlPropertyRef propertyRef = new CsdlPropertyRef();
+            propertyRef.setName("ID");
+    
+            entityType = new CsdlEntityType();
+            entityType.setName(ET_ADVERTISMENT_NAME);
+            entityType.setProperties(Arrays.asList(id, name, airDate));
+            entityType.setKey(Collections.singletonList(propertyRef));
+            entityType.setHasStream(true);    // <- Enable the media entity stream
+        }
+    
+        return entityType;
+    }
 
 Further we  have to create a new entity set. Add the following snipped to `DemoEdmProvider.getEntitySet`
 
-	@Override
-	public CsdlEntitySet getEntitySet(FullQualifiedName entityContainer, String entitySetName) {
-		CsdlEntitySet entitySet = null;
-
-		if (entityContainer.equals(CONTAINER)) {
-			if (entitySetName.equals(ES_PRODUCTS_NAME)) {
-				// Definition of entity set Products
-			} else if (entitySetName.equals(ES_CATEGORIES_NAME)) {
-				// Definition if entity set Categories
-			} else if (entitySetName.equals(ES_ADVERTISMENTS_NAME)) {
-				entitySet = new CsdlEntitySet();
-				entitySet.setName(ES_ADVERTISMENTS_NAME);
-				entitySet.setType(ET_ADVERTISMENT_FQN);
-			}
-		}
-
-		return entitySet;
-	}
+    ::::java
+    @Override
+    public CsdlEntitySet getEntitySet(FullQualifiedName entityContainer, String entitySetName) {
+        CsdlEntitySet entitySet = null;
+    
+        if (entityContainer.equals(CONTAINER)) {
+            if (entitySetName.equals(ES_PRODUCTS_NAME)) {
+                // Definition of entity set Products
+            } else if (entitySetName.equals(ES_CATEGORIES_NAME)) {
+                // Definition if entity set Categories
+            } else if (entitySetName.equals(ES_ADVERTISMENTS_NAME)) {
+                entitySet = new CsdlEntitySet();
+                entitySet.setName(ES_ADVERTISMENTS_NAME);
+                entitySet.setType(ET_ADVERTISMENT_FQN);
+            }
+        }
+    
+        return entitySet;
+    }
 
 And finally announce the entity type and entity set:
 
-	@Override
-	public List<CsdlSchema> getSchemas() {
-
-		// ...
-		entityTypes.add(getEntityType(ET_ADVERTISMENT_FQN));
-		// ...
-
-		return schemas;
-	}
-	
-	public CsdlEntityContainer getEntityContainer() {
-		// ...
-		entitySets.add(getEntitySet(CONTAINER, ES_ADVERTISMENTS_NAME));
-	}
+    @Override
+    public List<CsdlSchema> getSchemas() { 
+        // ...
+        entityTypes.add(getEntityType(ET_ADVERTISMENT_FQN));
+        // ...
+    
+        return schemas;
+    }
+    	
+    public CsdlEntityContainer getEntityContainer() {
+        // ...
+        entitySets.add(getEntitySet(CONTAINER, ES_ADVERTISMENTS_NAME));
+    }
 
 ### Enable the data store to handle media entities
 
@@ -154,44 +154,44 @@ In this tutorial, we will keep things si
 
 To read the content to a media entity, we simple return the value of the property *$value*.
 
-	::::java
-	 private static final String MEDIA_PROPERTY_NAME = "$value";
-
-	public byte[] readMedia(final Entity entity) {
-		return (byte[]) entity.getProperty(MEDIA_PROPERTY_NAME).asPrimitive();
-	}
+    ::::java
+    private static final String MEDIA_PROPERTY_NAME = "$value";
+    
+    public byte[] readMedia(final Entity entity) {
+        return (byte[]) entity.getProperty(MEDIA_PROPERTY_NAME).asPrimitive();
+    }
 
 If we update the content of a media entity, we must also set the the Content Type of the content.
 
-	::::java
-	private List<Entity> advertisments;
-	
-	public void updateMedia(final Entity entity, final String mediaContentType, final byte[] data) {
-		entity.getProperties().remove(entity.getProperty(MEDIA_PROPERTY_NAME));
-		entity.addProperty(new Property(null, MEDIA_PROPERTY_NAME, ValueType.PRIMITIVE, data));
-		entity.setMediaContentType(mediaContentType);
-	}
+    ::::java
+    private List<Entity> advertisments;
+    	
+    public void updateMedia(final Entity entity, final String mediaContentType, final byte[] data) {
+        entity.getProperties().remove(entity.getProperty(MEDIA_PROPERTY_NAME));
+        entity.addProperty(new Property(null, MEDIA_PROPERTY_NAME, ValueType.PRIMITIVE, data));
+        entity.setMediaContentType(mediaContentType);
+    }
 
 If a client creates a new media entity, the body of the requet contains the content of the media entity instead the regualr properties! So the other regular properties defaults to `null`. The Content Type of the  media content must also be set.
 
-	::::java
-	public Entity createMediaEntity(final EdmEntityType edmEntityType, final String mediaContentType, final byte[] data) {
-		Entity entity = null;
-
-		if(edmEntityType.getName().equals(DemoEdmProvider.ET_ADVERTISMENT_NAME)) {
-			entity = new Entity();
-			entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, UUID.randomUUID()));
-			entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, null));
-			entity.addProperty(new Property(null, "AirDate", ValueType.PRIMITIVE, null));
-
-			entity.setMediaContentType(mediaContentType);
-			entity.addProperty(new Property(null, MEDIA_PROPERTY_NAME, ValueType.PRIMITIVE, data));
-
-			advertisments.add(entity);
-		}
-
-		return entity;
-	}
+    ::::java
+    public Entity createMediaEntity(final EdmEntityType edmEntityType, final String mediaContentType, final byte[] data) {
+        Entity entity = null;
+    
+        if(edmEntityType.getName().equals(DemoEdmProvider.ET_ADVERTISMENT_NAME)) {
+            entity = new Entity();
+            entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, UUID.randomUUID()));
+            entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, null));
+            entity.addProperty(new Property(null, "AirDate", ValueType.PRIMITIVE, null));
+    
+            entity.setMediaContentType(mediaContentType);
+            entity.addProperty(new Property(null, MEDIA_PROPERTY_NAME, ValueType.PRIMITIVE, data));
+    
+            advertisments.add(entity);
+        }
+    
+        return entity;
+    }
 
 ### Implement the interface `MediaEntityProcessor`
 
@@ -199,107 +199,106 @@ As you can see the [`MediaEntityProcesso
 
 The easiest part is to delete an media entity. The method `deleteMediaEntity` is delegated to the method `deleteEntity(...)`.
 
-	::::java
-	@Override
-	public void deleteMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
-			throws ODataApplicationException, ODataLibraryException {
-
-		deleteEntity(request, response, uriInfo);
-	}
+    ::::java
+    @Override
+    public void deleteMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
+            throws ODataApplicationException, ODataLibraryException {
+    
+        deleteEntity(request, response, uriInfo);
+    }
 
 Next the creation of media entites is implemented. First we fetch the addressed entity set and convert the body of the request to a byte array. Remember the whole body of the request contains the content of the media entity.
 
-	::::java
-	@Override
-	public void createMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
-			ContentType requestFormat, ContentType responseFormat)
-			throws ODataApplicationException, ODataLibraryException {
-
-		final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
-		final byte[] mediaContent = odata.createFixedFormatDeserializer().binary(request.getBody());
-		///...
+    ::::java
+    @Override
+    public void createMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
+            ContentType requestFormat, ContentType responseFormat)
+            throws ODataApplicationException, ODataLibraryException {
+    
+        final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
+        final byte[] mediaContent = odata.createFixedFormatDeserializer().binary(request.getBody());
+        ///...
 
 After that we call the data store to create the new media entity. The [OData Specification](http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/part1-protocol/odata-v4.0-errata02-os-part1-protocol-complete.html#_Toc406398337) tells us, that we have to set the location header to the edit URL of the entity. Since we do not support Prefer Headers we have to return the entity itself.
 
-	:::java
-		final Entity entity = storage.createMediaEntity(edmEntitySet.getEntityType(),
-														requestFormat.toContentTypeString(),
-														mediaContent);
-
-		final ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build();
-		final EntitySerializerOptions opts = EntitySerializerOptions.with().contextURL(contextUrl).build();
-		final SerializerResult serializerResult = odata.createSerializer(responseFormat)
-									   .entity(serviceMetadata, edmEntitySet.getEntityType(), entity, opts);
-
-		final String location = request.getRawBaseUri() + '/' 
-									+ odata.createUriHelper().buildCanonicalURL(edmEntitySet, entity);
-
-		response.setContent(serializerResult.getContent());
-		response.setStatusCode(HttpStatusCode.CREATED.getStatusCode());
-		response.setHeader(HttpHeader.LOCATION, location);
-		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
-	}
+    :::java
+        final Entity entity = storage.createMediaEntity(edmEntitySet.getEntityType(),
+        requestFormat.toContentTypeString(),mediaContent);
+        
+        final ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build();
+        final EntitySerializerOptions opts = EntitySerializerOptions.with().contextURL(contextUrl).build();
+        final SerializerResult serializerResult = odata.createSerializer(responseFormat)
+                            .entity(serviceMetadata, edmEntitySet.getEntityType(), entity, opts);
+        
+        final String location = request.getRawBaseUri() + '/' 
+                + odata.createUriHelper().buildCanonicalURL(edmEntitySet, entity);
+        
+        response.setContent(serializerResult.getContent());
+        response.setStatusCode(HttpStatusCode.CREATED.getStatusCode());
+        response.setHeader(HttpHeader.LOCATION, location);
+        response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
+    }
 
 To keep things simple, our scenario do not support navigation to media entities. Because of this, the implementation to read a media entity is quite simple. First analayse the URI and fetch the entity. Than take the content of our specical property, serialize them and return the serialized content. The serializer converts the byte array to an `InputStream`.
 
-	::::java
-	@Override
-	public void readMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
-			throws ODataApplicationException, ODataLibraryException {
-
-		// Since our scenario do not contain navigations from media entities. We can keep things simple and
-		// check only the first resource path of the URI.
-		final UriResource firstResoucePart = uriInfo.getUriResourceParts().get(0);
-		if(firstResoucePart instanceof UriResourceEntitySet) {
-			final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
-			final UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) firstResoucePart;
-
-			final Entity entity = storage.readEntityData(edmEntitySet, uriResourceEntitySet.getKeyPredicates());
-			if(entity == null) {
-			throw new ODataApplicationException("Entity not found",
-								HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
-			}
-
-			final byte[] mediaContent = storage.readMedia(entity);
-			final InputStream responseContent = odata.createFixedFormatSerializer().binary(mediaContent);
-
-			response.setStatusCode(HttpStatusCode.OK.getStatusCode());
-			response.setContent(responseContent);
-			response.setHeader(HttpHeader.CONTENT_TYPE, entity.getMediaContentType());
-		} else {
-			throw new ODataApplicationException("Not implemented",
-								HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
-		}
-	}
+    ::::java
+    @Override
+    public void readMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
+            throws ODataApplicationException, ODataLibraryException {
+    
+        // Since our scenario do not contain navigations from media entities. We can keep things simple and
+        // check only the first resource path of the URI.
+        final UriResource firstResoucePart = uriInfo.getUriResourceParts().get(0);
+        if(firstResoucePart instanceof UriResourceEntitySet) {
+            final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
+            final UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) firstResoucePart;
+    
+            final Entity entity = storage.readEntityData(edmEntitySet, uriResourceEntitySet.getKeyPredicates());
+            if(entity == null) {
+                throw new ODataApplicationException("Entity not found",
+                    HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
+            }
+    
+            final byte[] mediaContent = storage.readMedia(entity);
+            final InputStream responseContent = odata.createFixedFormatSerializer().binary(mediaContent);
+    
+            response.setStatusCode(HttpStatusCode.OK.getStatusCode());
+            response.setContent(responseContent);
+            response.setHeader(HttpHeader.CONTENT_TYPE, entity.getMediaContentType());
+        } else {
+            throw new ODataApplicationException("Not implemented",
+                HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
+        }
+    }
 
 Updating a media entity in our scenario is quite similar to read an entity. The first step is to analyse the URI, than fetch the entity from data store. Afer that we call the `updateMediaEntity` method. In our case we do not return any content. If we would return content, we must return the recently uploaded content of the media entity.
 	
-	:::java
-	@Override
-	public void updateMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
-			ContentType requestFormat, ContentType responseFormat) 
-			throws ODataApplicationException, ODataLibraryException {
-
-		final UriResource firstResoucePart = uriInfo.getUriResourceParts().get(0);
-		if (firstResoucePart instanceof UriResourceEntitySet) {
-			final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
-			final UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) firstResoucePart;
-
-			final Entity entity = storage.readEntityData(edmEntitySet, uriResourceEntitySet.getKeyPredicates());
-			if (entity == null) {
-				throw new ODataApplicationException("Entity not found",
-						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
-			}
-
-			final byte[] mediaContent = odata.createFixedFormatDeserializer().binary(request.getBody());
-			storage.updateMedia(entity, requestFormat.toContentTypeString(), mediaContent);
-
-			response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
-		} else {
-			throw new ODataApplicationException("Not implemented",
-					HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
-		}
-	}
+    :::java
+    @Override
+    public void updateMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
+            ContentType requestFormat, ContentType responseFormat) 
+            throws ODataApplicationException, ODataLibraryException {
+    
+        final UriResource firstResoucePart = uriInfo.getUriResourceParts().get(0);
+        if (firstResoucePart instanceof UriResourceEntitySet) {
+            final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
+            final UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) firstResoucePart;
+    
+            final Entity entity = storage.readEntityData(edmEntitySet, uriResourceEntitySet.getKeyPredicates());
+            if (entity == null) {
+                throw new ODataApplicationException("Entity not found",
+                    HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
+            }
+    
+            final byte[] mediaContent = odata.createFixedFormatDeserializer().binary(request.getBody());
+            storage.updateMedia(entity, requestFormat.toContentTypeString(), mediaContent);
+    
+            response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
+        } else {
+            throw new ODataApplicationException("Not implemented",
+                HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
+        }
+    }
 
 ## Run the implemented service
 
@@ -319,13 +318,13 @@ After building and deploying the project
  Content-Type: image/svg+xml]
 
 
-	::::xml
-	<?xml version="1.0" encoding="UTF-8"?>
-	<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">
-		<g stroke="darkmagenta" stroke-width="16" fill="crimson">
-			<circle cx="50" cy="50" r="42"/>
-		</g>
-	</svg>
+    ::::xml
+    <?xml version="1.0" encoding="UTF-8"?>
+        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">
+            <g stroke="darkmagenta" stroke-width="16" fill="crimson">
+                <circle cx="50" cy="50" r="42"/>
+            </g>
+    </svg>
 
 
  * Update the content of a media entity    
@@ -333,19 +332,19 @@ After building and deploying the project
  Content-Type: text/plain
 
 
-	::::text
-	Super super nice content
+    ::::text
+    Super super nice content
 
  * Update the properties of a media entity    
 **PUT** [http://localhost:8080/DemoService-Media/DemoService.svc/Advertisments(f89dee73-af9f-4cd4-b330-db93c25ff3c7)](http://localhost:8080/DemoService-Media/DemoService.svc/Advertisments(f89dee73-af9f-4cd4-b330-db93c25ff3c7))    
 Content-Type: application/json
 
 
-	::::json
-	{
-		"Name": "New Name",
-		"AirDate": "2020-06-05T23:00"
-	}
+    ::::json
+    {
+        "Name": "New Name",
+        "AirDate": "2020-06-05T23:00"
+    }