Repository: olingo-odata2
Updated Branches:
  refs/heads/master bd0a12777 -> 2636dd5f7


[OLINGO-516] Support for $value in Function Import + Support for byte[]
array return types in Function Imports.

byte array returned from Function Imports will be serialized into HTTP
response with mime type as application/octect-stream. Currently there is
no means to override the mime type.

Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/2636dd5f
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/2636dd5f
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/2636dd5f

Branch: refs/heads/master
Commit: 2636dd5f79186003b9d1087ed03586efd7687f24
Parents: bd0a127
Author: Chandan V A <[email protected]>
Authored: Sat Dec 20 18:14:19 2014 +0530
Committer: Chandan V A <[email protected]>
Committed: Sat Dec 20 18:14:19 2014 +0530

----------------------------------------------------------------------
 .../core/ODataJPAProcessorDefault.java          |  58 +++++++++----------
 .../core/ODataJPAResponseBuilderDefault.java    |   9 ++-
 .../processor/ref/util/CustomerImageLoader.java |  31 ++++++++++
 .../jpa-ref/src/main/resources/Customer_1.png   | Bin 0 -> 8429 bytes
 .../jpa-ref/src/main/resources/Customer_2.png   | Bin 0 -> 12212 bytes
 .../ref/extension/CustomerImageProcessor.java   |  17 ++++++
 .../SalesOrderProcessingExtension.java          |   1 +
 7 files changed, 84 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2636dd5f/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAProcessorDefault.java
----------------------------------------------------------------------
diff --git 
a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAProcessorDefault.java
 
b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAProcessorDefault.java
index 079aecc..6b1f40b 100644
--- 
a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAProcessorDefault.java
+++ 
b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAProcessorDefault.java
@@ -58,9 +58,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse readEntitySet(final GetEntitySetUriInfo 
uriParserResultView, final String contentType)
       throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     List<Object> jpaEntities = jpaProcessor.process(uriParserResultView);
 
     ODataResponse oDataResponse =
@@ -74,9 +74,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse readEntity(final GetEntityUriInfo uriParserResultView, 
final String contentType)
       throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     Object jpaEntity = jpaProcessor.process(uriParserResultView);
 
     ODataResponse oDataResponse =
@@ -88,9 +88,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse countEntitySet(final GetEntitySetCountUriInfo 
uriParserResultView, final String contentType)
       throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     long jpaEntityCount = jpaProcessor.process(uriParserResultView);
 
     ODataResponse oDataResponse = responseBuilder.build(jpaEntityCount);
@@ -101,9 +101,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse existsEntity(final GetEntityCountUriInfo uriInfo, final 
String contentType)
       throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     long jpaEntityCount = jpaProcessor.process(uriInfo);
 
     ODataResponse oDataResponse = responseBuilder.build(jpaEntityCount);
@@ -114,9 +114,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse createEntity(final PostUriInfo uriParserResultView, 
final InputStream content,
       final String requestContentType, final String contentType) throws 
ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     Object createdJpaEntity = jpaProcessor.process(uriParserResultView, 
content, requestContentType);
 
     ODataResponse oDataResponse =
@@ -128,9 +128,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse updateEntity(final PutMergePatchUriInfo 
uriParserResultView, final InputStream content,
       final String requestContentType, final boolean merge, final String 
contentType) throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     Object jpaEntity = jpaProcessor.process(uriParserResultView, content, 
requestContentType);
 
     ODataResponse oDataResponse = responseBuilder.build(uriParserResultView, 
jpaEntity);
@@ -141,9 +141,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse deleteEntity(final DeleteUriInfo uriParserResultView, 
final String contentType)
       throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     Object deletedObj = jpaProcessor.process(uriParserResultView, contentType);
 
     ODataResponse oDataResponse = responseBuilder.build(uriParserResultView, 
deletedObj);
@@ -153,9 +153,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse executeFunctionImport(final GetFunctionImportUriInfo 
uriParserResultView,
       final String contentType) throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     List<Object> resultEntity = jpaProcessor.process(uriParserResultView);
 
     ODataResponse oDataResponse =
@@ -167,13 +167,13 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse executeFunctionImportValue(final 
GetFunctionImportUriInfo uriParserResultView,
       final String contentType) throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     List<Object> result = jpaProcessor.process(uriParserResultView);
 
     ODataResponse oDataResponse =
-        responseBuilder.build(uriParserResultView, result, contentType);
+        responseBuilder.build(uriParserResultView, result.get(0));
 
     return oDataResponse;
   }
@@ -181,9 +181,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse readEntityLink(final GetEntityLinkUriInfo 
uriParserResultView, final String contentType)
       throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     Object jpaEntity = jpaProcessor.process(uriParserResultView);
 
     ODataResponse oDataResponse =
@@ -197,7 +197,7 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
       throws ODataException {
 
     oDataJPAContext.setODataContext(getContext());
-    
+
     List<Object> jpaEntity = jpaProcessor.process(uriParserResultView);
 
     ODataResponse oDataResponse =
@@ -211,7 +211,7 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
       final String requestContentType, final String contentType) throws 
ODataException {
 
     oDataJPAContext.setODataContext(getContext());
-    
+
     jpaProcessor.process(uriParserResultView, content, requestContentType, 
contentType);
 
     return ODataResponse.newBuilder().build();
@@ -222,7 +222,7 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
       final String requestContentType, final String contentType) throws 
ODataException {
 
     oDataJPAContext.setODataContext(getContext());
-    
+
     jpaProcessor.process(uriParserResultView, content, requestContentType, 
contentType);
 
     return ODataResponse.newBuilder().build();
@@ -231,9 +231,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse deleteEntityLink(final DeleteUriInfo 
uriParserResultView, final String contentType)
       throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     jpaProcessor.process(uriParserResultView, contentType);
     return ODataResponse.newBuilder().build();
 
@@ -242,9 +242,9 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
   @Override
   public ODataResponse executeBatch(final BatchHandler handler, final String 
contentType, final InputStream content)
       throws ODataException {
-    
+
     oDataJPAContext.setODataContext(getContext());
-    
+
     ODataResponse batchResponse;
     List<BatchResponsePart> batchResponseParts = new 
ArrayList<BatchResponsePart>();
     PathInfo pathInfo = getContext().getPathInfo();
@@ -282,7 +282,7 @@ public class ODataJPAProcessorDefault extends 
ODataJPAProcessor {
 
       return BatchResponsePart.responses(responses).changeSet(true).build();
     } catch (Exception e) {
-      
+
       List<ODataResponse> errorResponses = new ArrayList<ODataResponse>(1);
       
errorResponses.add(ODataResponse.entity(e).status(HttpStatusCodes.INTERNAL_SERVER_ERROR).build());
       return 
BatchResponsePart.responses(errorResponses).changeSet(false).build();

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2636dd5f/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderDefault.java
----------------------------------------------------------------------
diff --git 
a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderDefault.java
 
b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderDefault.java
index fc0653f..edec5fb 100644
--- 
a/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderDefault.java
+++ 
b/odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderDefault.java
@@ -267,9 +267,12 @@ public final class ODataJPAResponseBuilderDefault 
implements ODataJPAResponseBui
 
       if (result != null) {
         ODataResponse response = null;
-
-        final String value = type.valueToString(result, 
EdmLiteralKind.DEFAULT, null);
-        response = EntityProvider.writeText(value);
+        if (type.getDefaultType().equals(byte[].class)) {
+          response = EntityProvider.writeBinary("application/octet-stream", 
(byte[]) result);
+        } else {
+          final String value = type.valueToString(result, 
EdmLiteralKind.DEFAULT, null);
+          response = EntityProvider.writeText(value);
+        }
 
         return ODataResponse.fromResponse(response).build();
       } else {

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2636dd5f/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/util/CustomerImageLoader.java
----------------------------------------------------------------------
diff --git 
a/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/util/CustomerImageLoader.java
 
b/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/util/CustomerImageLoader.java
new file mode 100644
index 0000000..59403a5
--- /dev/null
+++ 
b/odata2-jpa-processor/jpa-ref/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/util/CustomerImageLoader.java
@@ -0,0 +1,31 @@
+package org.apache.olingo.odata2.jpa.processor.ref.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+public class CustomerImageLoader {
+  public static byte[] loadImage(Long customerId) {
+    String name = null;
+    if (customerId == 1L) {
+      name = "/Customer_1.png";
+    } else if (customerId == 2L) {
+      name = "/Customer_2.png";
+    } else {
+      return null;
+    }
+
+    InputStream is = CustomerImageLoader.class.getResourceAsStream(name);
+    ByteArrayOutputStream os = new ByteArrayOutputStream();
+    int b = 0;
+    try {
+      while ((b = is.read()) != -1) {
+        os.write(b);
+      }
+    } catch (IOException e) {
+      return null;
+    }
+    return os.toByteArray();
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2636dd5f/odata2-jpa-processor/jpa-ref/src/main/resources/Customer_1.png
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-ref/src/main/resources/Customer_1.png 
b/odata2-jpa-processor/jpa-ref/src/main/resources/Customer_1.png
new file mode 100644
index 0000000..f817682
Binary files /dev/null and 
b/odata2-jpa-processor/jpa-ref/src/main/resources/Customer_1.png differ

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2636dd5f/odata2-jpa-processor/jpa-ref/src/main/resources/Customer_2.png
----------------------------------------------------------------------
diff --git a/odata2-jpa-processor/jpa-ref/src/main/resources/Customer_2.png 
b/odata2-jpa-processor/jpa-ref/src/main/resources/Customer_2.png
new file mode 100644
index 0000000..144ed93
Binary files /dev/null and 
b/odata2-jpa-processor/jpa-ref/src/main/resources/Customer_2.png differ

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2636dd5f/odata2-jpa-processor/jpa-web/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/extension/CustomerImageProcessor.java
----------------------------------------------------------------------
diff --git 
a/odata2-jpa-processor/jpa-web/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/extension/CustomerImageProcessor.java
 
b/odata2-jpa-processor/jpa-web/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/extension/CustomerImageProcessor.java
new file mode 100644
index 0000000..d341290
--- /dev/null
+++ 
b/odata2-jpa-processor/jpa-web/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/extension/CustomerImageProcessor.java
@@ -0,0 +1,17 @@
+package org.apache.olingo.odata2.jpa.processor.ref.extension;
+
+import org.apache.olingo.odata2.api.annotation.edm.EdmFacets;
+import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport;
+import 
org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport.ReturnType;
+import 
org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImport.ReturnType.Type;
+import org.apache.olingo.odata2.api.annotation.edm.EdmFunctionImportParameter;
+import org.apache.olingo.odata2.jpa.processor.ref.util.CustomerImageLoader;
+
+public class CustomerImageProcessor {
+
+  @EdmFunctionImport(returnType = @ReturnType(type = Type.SIMPLE))
+  public byte[] getImage(
+      @EdmFunctionImportParameter(name = "CustomerId", facets = 
@EdmFacets(nullable = false)) Long customerId) {
+    return CustomerImageLoader.loadImage(customerId);
+  }
+}

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2636dd5f/odata2-jpa-processor/jpa-web/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/extension/SalesOrderProcessingExtension.java
----------------------------------------------------------------------
diff --git 
a/odata2-jpa-processor/jpa-web/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/extension/SalesOrderProcessingExtension.java
 
b/odata2-jpa-processor/jpa-web/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/extension/SalesOrderProcessingExtension.java
index c3065ec..1553ba6 100644
--- 
a/odata2-jpa-processor/jpa-web/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/extension/SalesOrderProcessingExtension.java
+++ 
b/odata2-jpa-processor/jpa-web/src/main/java/org/apache/olingo/odata2/jpa/processor/ref/extension/SalesOrderProcessingExtension.java
@@ -63,6 +63,7 @@ public class SalesOrderProcessingExtension implements 
JPAEdmExtension {
   @Override
   public void extendWithOperation(final JPAEdmSchemaView view) {
     view.registerOperations(SalesOrderHeaderProcessor.class, null);
+    view.registerOperations(CustomerImageProcessor.class, null);
 
   }
 

Reply via email to