From 44ec57874d5937d167bd6f75f1796464ab564787 Mon Sep 17 00:00:00 2001
From: Klaus Straubinger
Date: Tue, 9 Jun 2015 13:42:40 +0200
Subject: [PATCH] [OLINGO-663] technical service uses UUIDs as ETags
Change-Id: Ie34c22a817919afe2563a2f5d221e7d4495afda9
Signed-off-by: Christian Amend
---
.../fit/tecsvc/client/BatchClientITCase.java | 10 +++++-----
.../fit/tecsvc/http/BasicBatchITCase.java | 2 +-
.../olingo/server/tecsvc/TechnicalServlet.java | 15 ++-------------
.../server/tecsvc/data/DataProvider.java | 11 ++++++++---
.../tecsvc/processor/TechnicalProcessor.java | 18 +++++++++---------
5 files changed, 25 insertions(+), 31 deletions(-)
diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BatchClientITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BatchClientITCase.java
index 7be6ab6cd..72a4ef394 100644
--- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BatchClientITCase.java
+++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BatchClientITCase.java
@@ -170,7 +170,7 @@ public class BatchClientITCase extends AbstractTestITCase {
assertEquals(1, oDataResonse.getHeader("OData-Version").size());
assertEquals("4.0", oDataResonse.getHeader("OData-Version").toArray()[0]);
assertEquals(1, oDataResonse.getHeader("Content-Length").size());
- assertEquals("583", oDataResonse.getHeader("Content-Length").toArray()[0]);
+ assertEquals("605", oDataResonse.getHeader("Content-Length").toArray()[0]);
assertEquals("application/json;odata.metadata=minimal", oDataResonse.getContentType());
}
@@ -202,7 +202,7 @@ public class BatchClientITCase extends AbstractTestITCase {
assertEquals(1, oDataResonse.getHeader("OData-Version").size());
assertEquals("4.0", oDataResonse.getHeader("OData-Version").toArray()[0]);
assertEquals(1, oDataResonse.getHeader("Content-Length").size());
- assertEquals("583", oDataResonse.getHeader("Content-Length").toArray()[0]);
+ assertEquals("605", oDataResonse.getHeader("Content-Length").toArray()[0]);
assertEquals("application/json;odata.metadata=minimal", oDataResonse.getContentType());
}
@@ -234,7 +234,7 @@ public class BatchClientITCase extends AbstractTestITCase {
assertEquals(1, oDataResonse.getHeader("OData-Version").size());
assertEquals("4.0", oDataResonse.getHeader("OData-Version").toArray()[0]);
assertEquals(1, oDataResonse.getHeader("Content-Length").size());
- assertEquals("583", oDataResonse.getHeader("Content-Length").toArray()[0]);
+ assertEquals("605", oDataResonse.getHeader("Content-Length").toArray()[0]);
assertEquals("application/json;odata.metadata=minimal", oDataResonse.getContentType());
// Check second get request
@@ -334,7 +334,7 @@ public class BatchClientITCase extends AbstractTestITCase {
assertEquals(1, oDataResonse.getHeader("OData-Version").size());
assertEquals("4.0", oDataResonse.getHeader("OData-Version").toArray()[0]);
assertEquals(1, oDataResonse.getHeader("Content-Length").size());
- assertEquals("583", oDataResonse.getHeader("Content-Length").toArray()[0]);
+ assertEquals("605", oDataResonse.getHeader("Content-Length").toArray()[0]);
assertEquals("application/json;odata.metadata=minimal", oDataResonse.getContentType());
// Check second get request
@@ -357,7 +357,7 @@ public class BatchClientITCase extends AbstractTestITCase {
assertEquals(1, oDataResonse.getHeader("OData-Version").size());
assertEquals("4.0", oDataResonse.getHeader("OData-Version").toArray()[0]);
assertEquals(1, oDataResonse.getHeader("Content-Length").size());
- assertEquals("491", oDataResonse.getHeader("Content-Length").toArray()[0]);
+ assertEquals("513", oDataResonse.getHeader("Content-Length").toArray()[0]);
assertEquals("application/json;odata.metadata=minimal", oDataResonse.getContentType());
}
diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicBatchITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicBatchITCase.java
index c434703c0..65d883020 100644
--- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicBatchITCase.java
+++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicBatchITCase.java
@@ -60,7 +60,7 @@ public class BasicBatchITCase extends AbstractBaseTestITCase {
assertEquals("HTTP/1.1 200 OK", reader.readLine());
assertEquals("OData-Version: 4.0", reader.readLine());
assertEquals("Content-Type: application/json;odata.metadata=minimal", reader.readLine());
- assertEquals("Content-Length: 583", reader.readLine());
+ assertEquals("Content-Length: 605", reader.readLine());
blankLine(reader);
reader.close();
diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java
index 40433f33b..9c1d50286 100644
--- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java
+++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java
@@ -22,6 +22,7 @@ import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
+import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -56,7 +57,7 @@ public class TechnicalServlet extends HttpServlet {
* therefore any change must be deployed, resulting in re-loading of this class,
* giving this field a new and hopefully unique value.
*/
- private static final String metadataETag = getETag();
+ private static final String metadataETag = "W/\"" + UUID.randomUUID() + "\"";
@Override
protected void service(final HttpServletRequest request, final HttpServletResponse response)
@@ -89,16 +90,4 @@ public class TechnicalServlet extends HttpServlet {
throw new ServletException(e);
}
}
-
- private static String getETag() {
- String time = "" + System.nanoTime();
- if (time.length() > 14) {
- time = time.substring(0, 14);
- } else {
- while (time.length() < 14) {
- time = "0" + time;
- }
- }
- return "W/\"" + time + "\"";
- }
}
diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataProvider.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataProvider.java
index 911ab25a4..fe06a1c50 100644
--- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataProvider.java
+++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/data/DataProvider.java
@@ -27,6 +27,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.UUID;
import org.apache.olingo.commons.api.data.ComplexValue;
import org.apache.olingo.commons.api.data.Entity;
@@ -282,7 +283,7 @@ public class DataProvider {
public void updateETag(Entity entity) {
if (entity.getETag() != null) {
- entity.setETag("W/\"" + System.nanoTime() + "\"");
+ entity.setETag("W/\"" + UUID.randomUUID() + "\"");
}
}
@@ -407,7 +408,7 @@ public class DataProvider {
if (edmProperty.isPrimitive()) {
if (newProperty != null || !patch) {
final Object value = newProperty == null ? null : newProperty.getValue();
- property.setValue(property.getValueType(), value);
+ updatePropertyValue(property, value);
}
} else if (edmProperty.isCollection()) {
// Updating collection properties means replacing all entries with the given ones.
@@ -441,6 +442,10 @@ public class DataProvider {
}
}
+ public void updatePropertyValue(Property property, final Object value) {
+ property.setValue(property.getValueType(), value);
+ }
+
private ComplexValue createComplexValue(final EdmProperty edmProperty, final ComplexValue complexValue,
final boolean patch) throws DataProviderException {
final ComplexValue result = new ComplexValue();
@@ -486,7 +491,7 @@ public class DataProvider {
entity.getProperties().remove(entity.getProperty(MEDIA_PROPERTY_NAME));
entity.addProperty(DataCreator.createPrimitive(MEDIA_PROPERTY_NAME, media));
entity.setMediaContentType(type);
- entity.setMediaETag("W/\"" + System.nanoTime() + "\"");
+ entity.setMediaETag("W/\"" + UUID.randomUUID() + "\"");
}
public EntityCollection readFunctionEntitySet(final EdmFunction function, final List parameters)
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 c95091d03..f3012d985 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
@@ -109,7 +109,7 @@ public abstract class TechnicalProcessor implements Processor {
return entitySet;
}
-
+
/**
* Reads an entity as specified in the resource path, including navigation.
* If there is navigation and the navigation ends on an entity collection,
@@ -118,13 +118,13 @@ public abstract class TechnicalProcessor implements Processor {
protected Entity readEntity(final UriInfoResource uriInfo) throws ODataApplicationException {
return readEntity(uriInfo, false);
}
-
+
/**
* If ignoreLastNavigation is set to false see {@link #readEntity(UriInfoResource)}
* otherwise returns the second last entity (Ignores the last navigation)
* If no such entity exists throws an ODataApplicationException
*/
- protected Entity readEntity(final UriInfoResource uriInfo, final boolean ignoreLastNavigation)
+ protected Entity readEntity(final UriInfoResource uriInfo, final boolean ignoreLastNavigation)
throws ODataApplicationException {
final List resourcePaths = uriInfo.getUriResourceParts();
@@ -155,19 +155,19 @@ public abstract class TechnicalProcessor implements Processor {
if (entity == null) {
throw new ODataApplicationException("Nothing found.", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
}
-
+
int readAtMostNavigations = resourcePaths.size();
- if(ignoreLastNavigation) {
+ if (ignoreLastNavigation) {
readAtMostNavigations = 0;
- for(int i = 1; i resourcePaths = uriInfo.getUriResourceParts();
int navigationCount = 1;