You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2014/09/30 06:33:06 UTC

git commit: [OLINGO-438] Added better support

Repository: olingo-odata4
Updated Branches:
  refs/heads/Olingo-438 [created] e26bbb282


[OLINGO-438] Added better  support


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

Branch: refs/heads/Olingo-438
Commit: e26bbb28204193997296546bd5e4b66215436984
Parents: 7e0b013
Author: Michael Bolz <mi...@sap.com>
Authored: Tue Sep 30 06:30:15 2014 +0200
Committer: Michael Bolz <mi...@sap.com>
Committed: Tue Sep 30 06:30:15 2014 +0200

----------------------------------------------------------------------
 .../server/api/processor/CountProcessor.java    | 38 ++++++++++++++
 .../apache/olingo/server/core/ODataHandler.java | 10 ++++
 .../server/core/uri/validator/UriValidator.java |  1 +
 .../tecsvc/processor/TechnicalProcessor.java    | 54 +++++++++++++++++++-
 .../olingo/server/core/ODataHandlerTest.java    | 38 ++++++++++++++
 5 files changed, 139 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e26bbb28/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CountProcessor.java
----------------------------------------------------------------------
diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CountProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CountProcessor.java
new file mode 100644
index 0000000..c7c66eb
--- /dev/null
+++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CountProcessor.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.server.api.processor;
+
+import org.apache.olingo.server.api.ODataRequest;
+import org.apache.olingo.server.api.ODataResponse;
+import org.apache.olingo.server.api.uri.UriInfo;
+
+/**
+ * Processor interface for handling a $count
+ */
+public interface CountProcessor extends Processor {
+
+  /**
+   * Read entity data from persistency and puts serialized content and status into the response.
+   *  @param request - OData request object containing raw http information.
+   *  @param response - OData response object for collecting response data
+   *  @param uriInfo - information of a parsed OData uri
+   */
+  void readCount(ODataRequest request, ODataResponse response, UriInfo uriInfo);
+  
+}

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e26bbb28/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
index 0893d09..90f791f 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java
@@ -33,6 +33,7 @@ import org.apache.olingo.server.api.ODataRequest;
 import org.apache.olingo.server.api.ODataResponse;
 import org.apache.olingo.server.api.ODataServerError;
 import org.apache.olingo.server.api.ODataTranslatedException;
+import org.apache.olingo.server.api.processor.CountProcessor;
 import org.apache.olingo.server.api.processor.DefaultProcessor;
 import org.apache.olingo.server.api.processor.EntityCollectionProcessor;
 import org.apache.olingo.server.api.processor.EntityProcessor;
@@ -227,6 +228,15 @@ public class ODataHandler {
         }
       }
       break;
+    case count:
+      if (request.getMethod().equals(HttpMethod.GET)) {
+      	CountProcessor cp = selectProcessor(CountProcessor.class);
+      	cp.readCount(request, response, uriInfo);
+      } else {
+        throw new ODataHandlerException("not implemented",
+            ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);
+      }
+    	break;
     default:
       throw new ODataHandlerException("not implemented",
           ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED);

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e26bbb28/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java
index a164894..9c03db4 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java
@@ -521,6 +521,7 @@ public class UriValidator {
     UriResource secondLastPathSegment = uriInfo.getUriResourceParts().get(secondLastPathSegmentIndex);
     switch (secondLastPathSegment.getKind()) {
     case entitySet:
+    case navigationProperty:
       idx = RowIndexForUriType.entitySetCount;
       break;
     case complexProperty:

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e26bbb28/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java
----------------------------------------------------------------------
diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java
index 77136b5..289fdea 100644
--- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java
+++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java
@@ -32,6 +32,7 @@ import org.apache.olingo.server.api.OData;
 import org.apache.olingo.server.api.ODataApplicationException;
 import org.apache.olingo.server.api.ODataRequest;
 import org.apache.olingo.server.api.ODataResponse;
+import org.apache.olingo.server.api.processor.CountProcessor;
 import org.apache.olingo.server.api.processor.EntityCollectionProcessor;
 import org.apache.olingo.server.api.processor.EntityProcessor;
 import org.apache.olingo.server.api.serializer.ODataSerializer;
@@ -43,12 +44,15 @@ import org.apache.olingo.server.api.uri.UriResource;
 import org.apache.olingo.server.api.uri.UriResourceEntitySet;
 import org.apache.olingo.server.api.uri.queryoption.ExpandOption;
 import org.apache.olingo.server.api.uri.queryoption.SelectOption;
+import org.apache.olingo.server.api.uri.queryoption.SystemQueryOption;
+import org.apache.olingo.server.api.uri.queryoption.SystemQueryOptionKind;
 import org.apache.olingo.server.tecsvc.data.DataProvider;
 
+import java.io.ByteArrayInputStream;
 import java.util.List;
 import java.util.Locale;
 
-public class TechnicalProcessor implements EntityCollectionProcessor, EntityProcessor {
+public class TechnicalProcessor implements EntityCollectionProcessor, EntityProcessor, CountProcessor {
 
   private OData odata;
   private DataProvider dataProvider;
@@ -134,9 +138,55 @@ public class TechnicalProcessor implements EntityCollectionProcessor, EntityProc
     }
   }
 
+  @Override
+  public void readCount(ODataRequest request, ODataResponse response,
+      UriInfo uriInfo) {
+    try {
+      EntitySet entitySet = null;
+      final UriInfoResource uriResource = uriInfo.asUriInfoResource();
+      final List<UriResource> resourceParts = uriResource.getUriResourceParts();
+      if(isCount(resourceParts)) {
+        int pos = resourceParts.size()-2;
+        if(pos >= 0) {
+          final UriResourceEntitySet ur =
+              (UriResourceEntitySet) uriResource.getUriResourceParts().get(pos);
+          entitySet = readEntitySetInternal(ur.getEntitySet(), true);
+        }
+      }
+
+      if (entitySet == null) {
+        response.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode());
+      } else {
+        Integer count = entitySet.getCount();
+        response.setContent(new ByteArrayInputStream(count.toString().getBytes()));
+        response.setStatusCode(HttpStatusCode.OK.getStatusCode());
+        response.setHeader(HttpHeader.CONTENT_TYPE, "text/plain");
+      }
+    } catch (final DataProvider.DataProviderException e) {
+      response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
+    }
+  }
+
+  private boolean isCount(List<UriResource> resourceParts) {
+    if(resourceParts.isEmpty()) {
+      return false;
+    }
+    UriResource part = resourceParts.get(resourceParts.size() - 1);
+    return SystemQueryOptionKind.COUNT.toString().equals(part.toString());
+  }
+
   private EntitySet readEntitySetInternal(final EdmEntitySet edmEntitySet) throws DataProvider.DataProviderException {
+    return readEntitySetInternal(edmEntitySet, false);
+  }
+
+  private EntitySet readEntitySetInternal(final EdmEntitySet edmEntitySet,
+      boolean withCount) throws DataProvider.DataProviderException {
     EntitySet entitySet = dataProvider.readAll(edmEntitySet);
-    // TODO: set count and next link
+    // TODO: set count (correctly) and next link
+    if(withCount && entitySet.getCount() == null) {
+      entitySet.setCount(entitySet.getEntities().size());
+    }
+    //
     return entitySet;
   }
 

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/e26bbb28/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java
----------------------------------------------------------------------
diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java
index 04ed434..1cbd347 100644
--- a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java
+++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java
@@ -35,11 +35,14 @@ import org.apache.olingo.commons.api.http.HttpStatusCode;
 import org.apache.olingo.server.api.OData;
 import org.apache.olingo.server.api.ODataRequest;
 import org.apache.olingo.server.api.ODataResponse;
+import org.apache.olingo.server.api.processor.CountProcessor;
 import org.apache.olingo.server.api.processor.MetadataProcessor;
 import org.apache.olingo.server.api.processor.ServiceDocumentProcessor;
+import org.apache.olingo.server.api.uri.UriInfo;
 import org.apache.olingo.server.tecsvc.provider.EdmTechProvider;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 public class ODataHandlerTest {
 
@@ -238,4 +241,39 @@ public class ODataHandlerTest {
     assertEquals(HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), response.getStatusCode());
   }
 
+  @Test
+  public void testCount() throws Exception {
+    ODataRequest request = new ODataRequest();
+
+    request.setMethod(HttpMethod.GET);
+    request.setRawODataPath("ESAllPrim/$count");
+
+    CountProcessor processor = mock(CountProcessor.class);
+    handler.register(processor);
+
+    ODataResponse response = handler.process(request);
+
+    Mockito.verify(processor).readCount(
+    		Mockito.any(ODataRequest.class), 
+    		Mockito.any(ODataResponse.class), 
+    		Mockito.any(UriInfo.class));
+  }  
+  
+  @Test
+  public void testCountWithNavigation() throws Exception {
+    ODataRequest request = new ODataRequest();
+
+    request.setMethod(HttpMethod.GET);
+    request.setRawODataPath("ESAllPrim/NavPropertyETTwoPrimMany/$count");
+
+    CountProcessor processor = mock(CountProcessor.class);
+    handler.register(processor);
+
+    ODataResponse response = handler.process(request);
+
+    Mockito.verify(processor).readCount(
+    		Mockito.any(ODataRequest.class), 
+    		Mockito.any(ODataResponse.class), 
+    		Mockito.any(UriInfo.class));
+  }  
 }