Repository: olingo-odata4
Updated Branches:
  refs/heads/master f0188a6ec -> 1fef3a131


OLINGO-1271: Making service dispatcher framework to use service root in the 
context url instead of relative url


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

Branch: refs/heads/master
Commit: 1fef3a131ea4cc334151a7f10b67cab6bb424239
Parents: f0188a6
Author: Ramesh Reddy <rare...@jboss.org>
Authored: Thu Jun 21 16:42:33 2018 -0500
Committer: Ramesh Reddy <rare...@jboss.org>
Committed: Thu Jun 21 16:42:33 2018 -0500

----------------------------------------------------------------------
 .../server/core/requests/DataRequest.java       | 25 +++++++++--
 .../server/example/TripPinServiceTest.java      | 46 ++++++++++----------
 2 files changed, 45 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1fef3a13/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
----------------------------------------------------------------------
diff --git 
a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
 
b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
index 30589b8..11a092f 100644
--- 
a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
+++ 
b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
@@ -50,6 +50,7 @@ import org.apache.olingo.commons.api.http.HttpMethod;
 import org.apache.olingo.server.api.OData;
 import org.apache.olingo.server.api.ODataApplicationException;
 import org.apache.olingo.server.api.ODataLibraryException;
+import org.apache.olingo.server.api.ODataRequest;
 import org.apache.olingo.server.api.ODataResponse;
 import org.apache.olingo.server.api.ServiceMetadata;
 import org.apache.olingo.server.api.deserializer.DeserializerException;
@@ -362,7 +363,7 @@ public class DataRequest extends ServiceRequest {
       // EntitySet based return
       final UriHelper helper = odata.createUriHelper();
       ContextURL.Builder builder = buildEntitySetContextURL(helper, 
getEntitySet(),
-          getKeyPredicates(), getUriInfo(), getNavigations(), isCollection(), 
false);
+          getKeyPredicates(), getUriInfo(), getNavigations(), isCollection(), 
false, getODataRequest());
       return builder.build();
     }
   }
@@ -502,6 +503,7 @@ public class DataRequest extends ServiceRequest {
       if (isCollection()) {
         builder.asCollection();
       }
+      setServiceRoot(builder, getODataRequest());
       return builder.build();
     }
   }
@@ -609,6 +611,7 @@ public class DataRequest extends ServiceRequest {
       } else {
         builder.navOrPropertyPath(edmProperty.getName());
       }
+      setServiceRoot(builder, getODataRequest());      
       if (isPropertyComplex()) {
         EdmComplexType complexType = ((UriResourceComplexProperty) 
uriResourceProperty).getComplexType();
         String select = helper.buildContextURLSelectList(complexType, 
getUriInfo().getExpandOption(),
@@ -711,7 +714,8 @@ public class DataRequest extends ServiceRequest {
     public ContextURL getContextURL(OData odata) throws SerializerException {
       final UriHelper helper = odata.createUriHelper();
       ContextURL.Builder builder = buildEntitySetContextURL(helper,
-          uriResourceSingleton.getSingleton(), null, getUriInfo(), 
getNavigations(), isCollection(), true);
+          uriResourceSingleton.getSingleton(), null, getUriInfo(), 
getNavigations(), isCollection(), true, 
+          getODataRequest());
       return builder.build();
     }
 
@@ -789,7 +793,7 @@ public class DataRequest extends ServiceRequest {
 
   static ContextURL.Builder buildEntitySetContextURL(UriHelper helper,
       EdmBindingTarget edmEntitySet, List<UriParameter> keyPredicates, UriInfo 
uriInfo,
-      LinkedList<UriResourceNavigation> navigations, boolean collectionReturn, 
boolean singleton)
+      LinkedList<UriResourceNavigation> navigations, boolean collectionReturn, 
boolean singleton, ODataRequest request)
       throws SerializerException {
 
     ContextURL.Builder builder =
@@ -800,6 +804,7 @@ public class DataRequest extends ServiceRequest {
       builder.suffix(collectionReturn ? null : Suffix.ENTITY);
     }
 
+    setServiceRoot(builder, request);    
     builder.selectList(select);
 
     final UriInfoResource resource = uriInfo.asUriInfoResource();
@@ -821,6 +826,20 @@ public class DataRequest extends ServiceRequest {
     return builder;
   }
 
+  private static void setServiceRoot(ContextURL.Builder builder, ODataRequest 
request) {
+    String serviceRoot = request.getRawBaseUri();
+    if (serviceRoot != null) {
+      try {
+        if (!serviceRoot.endsWith("/")) {
+          serviceRoot = serviceRoot + "/";
+        }
+        builder.serviceRoot(URI.create(serviceRoot));
+      } catch (IllegalArgumentException e) {
+        // ignore
+      }
+    }
+  }
+
   private static List<String> getPropertyPath(final List<UriResource> path) {
     List<String> result = new LinkedList<String>();
     int index = 1;

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1fef3a13/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java
----------------------------------------------------------------------
diff --git 
a/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java
 
b/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java
index 7f56463..bbbaa9e 100644
--- 
a/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java
+++ 
b/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java
@@ -145,7 +145,7 @@ public class TripPinServiceTest {
     request.setHeader("Prefer", "odata.maxpagesize=10");
     HttpResponse response = httpSend(request, 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("$metadata#People", node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#People", 
node.get("@odata.context").asText());
     assertEquals(baseURL+"/People?$skiptoken=10", 
node.get("@odata.nextLink").asText());
 
     JsonNode person = ((ArrayNode)node.get("value")).get(0);
@@ -157,7 +157,7 @@ public class TripPinServiceTest {
   public void testReadEntityWithKey() throws Exception {
     HttpResponse response = httpGET(baseURL + "/Airlines('AA')", 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("$metadata#Airlines/$entity", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#Airlines/$entity", 
node.get("@odata.context").asText());
     assertEquals("American Airlines", node.get("Name").asText());
     //assertEquals("/Airlines('AA')/Picture", 
node.get("Picture@odata.mediaReadLink").asText());
   }
@@ -211,7 +211,7 @@ public class TripPinServiceTest {
   public void testReadPrimitiveProperty() throws Exception {
     HttpResponse response = httpGET(baseURL + "/Airlines('AA')/Name", 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("../$metadata#Airlines('AA')/Name", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#Airlines('AA')/Name", 
node.get("@odata.context").asText());
     assertEquals("American Airlines", node.get("value").asText());
   }
 
@@ -225,7 +225,7 @@ public class TripPinServiceTest {
   public void testReadPrimitiveArrayProperty() throws Exception {
     HttpResponse response = httpGET(baseURL + 
"/People('russellwhyte')/Emails", 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("../$metadata#People('russellwhyte')/Emails", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#People('russellwhyte')/Emails", 
node.get("@odata.context").asText());
     assertTrue(node.get("value").isArray());
     assertEquals("russ...@example.com", 
((ArrayNode)node.get("value")).get(0).asText());
     assertEquals("russ...@contoso.com", 
((ArrayNode)node.get("value")).get(1).asText());
@@ -262,7 +262,7 @@ public class TripPinServiceTest {
   public void testReadComplexArrayProperty() throws Exception {
     HttpResponse response = httpGET(baseURL + 
"/People('russellwhyte')/AddressInfo", 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("../$metadata#People('russellwhyte')/AddressInfo", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#People('russellwhyte')/AddressInfo", 
node.get("@odata.context").asText());
     assertTrue(node.get("value").isArray());
     assertEquals("187 Suffolk Ln.", 
((ArrayNode)node.get("value")).get(0).get("Address").asText());
   }
@@ -349,7 +349,7 @@ public class TripPinServiceTest {
   public void testSingleton() throws Exception {
     HttpResponse response = httpGET(baseURL + "/Me", 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("$metadata#Me", node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#Me", node.get("@odata.context").asText());
     assertEquals("russellwhyte", node.get("UserName").asText());
   }
 
@@ -357,7 +357,7 @@ public class TripPinServiceTest {
   public void testSelectOption() throws Exception {
     HttpResponse response = httpGET(baseURL + 
"/People('russellwhyte')?$select=FirstName,LastName", 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("$metadata#People(UserName,FirstName,LastName)/$entity", 
node.get("@odata.context").asText());
+    
assertEquals(baseURL+"/$metadata#People(UserName,FirstName,LastName)/$entity", 
node.get("@odata.context").asText());
     assertEquals("Russell", node.get("FirstName").asText());
   }
 
@@ -393,7 +393,7 @@ public class TripPinServiceTest {
   public void testReadReferences() throws Exception {
     HttpResponse response = httpGET(baseURL + 
"/People('russellwhyte')/Friends/$ref", 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("../$metadata#Collection($ref)", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#Collection($ref)", 
node.get("@odata.context").asText());
     assertTrue(node.get("value").isArray());
     assertEquals("/People('scottketchum')", 
((ArrayNode)node.get("value")).get(0).get("@odata.id").asText());
   }
@@ -432,13 +432,13 @@ public class TripPinServiceTest {
     HttpResponse response = httpGET(baseURL+"/$entity?$id="+baseURL 
         + "/People('kristakemp')&$select=FirstName", 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("$metadata#People(UserName,FirstName)/$entity", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#People(UserName,FirstName)/$entity", 
node.get("@odata.context").asText());
     assertEquals("Krista", node.get("FirstName").asText());
 
     // using relative URL
     response = 
httpGET(baseURL+"/$entity?$id="+"People('kristakemp')&$select=FirstName", 200);
     node = getJSONNode(response);
-    assertEquals("$metadata#People(UserName,FirstName)/$entity", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#People(UserName,FirstName)/$entity", 
node.get("@odata.context").asText());
     assertEquals("Krista", node.get("FirstName").asText());
   }
 
@@ -499,7 +499,7 @@ public class TripPinServiceTest {
     
     HttpResponse response = httpGET(baseURL + "/People('kristakemp')", 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("$metadata#People/$entity", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#People/$entity", 
node.get("@odata.context").asText());
     assertEquals("kri...@example.com", node.get("Emails").get(0).asText());
     assertEquals("kri...@gmail.com", node.get("Emails").get(1).asText());
   }  
@@ -546,7 +546,7 @@ public class TripPinServiceTest {
     
     response = httpGET(baseURL+"/People('olingo')/Friends", 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("$metadata#People", node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#People", 
node.get("@odata.context").asText());
     assertTrue(node.get("value").isArray());
     assertEquals("scottketchum", 
((ArrayNode)node.get("value")).get(1).get("UserName").asText());
   }
@@ -565,7 +565,7 @@ public class TripPinServiceTest {
     
     response = httpGET(editUrl, 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("../$metadata#People('russellwhyte')/FirstName", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#People('russellwhyte')/FirstName", 
node.get("@odata.context").asText());
     assertEquals("Pilar Ackerman", node.get("value").asText());
   }
 
@@ -585,7 +585,7 @@ public class TripPinServiceTest {
 
     response = httpGET(editUrl, 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("../$metadata#People('russellwhyte')/Emails", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#People('russellwhyte')/Emails", 
node.get("@odata.context").asText());
     assertTrue(node.get("value").isArray());
     assertEquals("oli...@apache.com", 
((ArrayNode)node.get("value")).get(0).asText());
   }
@@ -611,7 +611,7 @@ public class TripPinServiceTest {
     HttpResponse response = httpGET(editUrl, 200);
 
     JsonNode node = getJSONNode(response);
-    assertEquals("$metadata#People", node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#People", 
node.get("@odata.context").asText());
 
     JsonNode person = ((ArrayNode)node.get("value")).get(0);
     assertEquals("scottketchum", person.get("UserName").asText());
@@ -623,7 +623,7 @@ public class TripPinServiceTest {
     HttpResponse response = httpGET(editUrl, 200);
 
     JsonNode node = getJSONNode(response);
-    assertEquals("$metadata#Photos/$entity", 
node.get("@odata.context").asText());
+    assertEquals(baseURL+"/$metadata#Photos/$entity", 
node.get("@odata.context").asText());
   }
   
   @Test
@@ -637,7 +637,7 @@ public class TripPinServiceTest {
     String editUrl = baseURL + 
"/People('russellwhyte')/Friends('scottketchum')/Trips";
     HttpResponse response = httpGET(editUrl, 200);
     JsonNode node = getJSONNode(response);
-    
assertEquals("../../$metadata#People('russellwhyte')/Friends('scottketchum')/Trips",
+    
assertEquals(baseURL+"/$metadata#People('russellwhyte')/Friends('scottketchum')/Trips",
         node.get("@odata.context").asText());
     assertTrue(node.get("value").isArray());
     assertEquals("1001", 
((ArrayNode)node.get("value")).get(0).get("TripId").asText());
@@ -648,7 +648,7 @@ public class TripPinServiceTest {
     String editUrl = baseURL + "/People('russellwhyte')/Trips(1003)";
     HttpResponse response = httpGET(editUrl, 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("../$metadata#People('russellwhyte')/Trips/$entity",
+    assertEquals(baseURL+"/$metadata#People('russellwhyte')/Trips/$entity",
         node.get("@odata.context").asText());
     assertEquals("f94e9116-8bdd-4dac-ab61-08438d0d9a71", 
node.get("ShareId").asText());
   }
@@ -665,7 +665,7 @@ public class TripPinServiceTest {
     String editUrl = baseURL + "/People('jhondoe')/Trips";
     HttpResponse response = httpGET(editUrl, 200);
     JsonNode node = getJSONNode(response);
-    assertEquals("../$metadata#People('jhondoe')/Trips",
+    assertEquals(baseURL+"/$metadata#People('jhondoe')/Trips",
         node.get("@odata.context").asText());
     assertEquals(0, ((ArrayNode)node.get("value")).size());
   }
@@ -682,7 +682,7 @@ public class TripPinServiceTest {
     String editUrl = baseURL + 
"/People('russellwhyte')/Trips(1003)/PlanItems(5)/ConfirmationCode";
     HttpResponse response = httpGET(editUrl, 200);
     JsonNode node = getJSONNode(response);
-    
assertEquals("../../../$metadata#People('russellwhyte')/Trips(1003)/PlanItems(5)/ConfirmationCode",
+    
assertEquals(baseURL+"/$metadata#People('russellwhyte')/Trips(1003)/PlanItems(5)/ConfirmationCode",
         node.get("@odata.context").asText());
     assertEquals("JH58494", node.get("value").asText());
   }
@@ -692,7 +692,7 @@ public class TripPinServiceTest {
     String editUrl = baseURL + "/People('russellwhyte')/Trips(1003)/PlanItems";
     HttpResponse response = httpGET(editUrl, 200);
     JsonNode node = getJSONNode(response);
-    
assertEquals("../../$metadata#People('russellwhyte')/Trips(1003)/PlanItems",
+    
assertEquals(baseURL+"/$metadata#People('russellwhyte')/Trips(1003)/PlanItems",
         node.get("@odata.context").asText());
     assertEquals("#Microsoft.OData.SampleService.Models.TripPin.Flight",
         ((ArrayNode) node.get("value")).get(0).get("@odata.type").asText());
@@ -705,7 +705,7 @@ public class TripPinServiceTest {
     HttpResponse response = httpGET(editUrl, 200);
 
     JsonNode node = getJSONNode(response);
-    
assertEquals("../../../$metadata#People('russellwhyte')/Trips(1003)/PlanItems/"
+    
assertEquals(baseURL+"/$metadata#People('russellwhyte')/Trips(1003)/PlanItems/"
         + "Microsoft.OData.SampleService.Models.TripPin.Event",
         node.get("@odata.context").asText());
 
@@ -719,7 +719,7 @@ public class TripPinServiceTest {
         + "Microsoft.OData.SampleService.Models.TripPin.Event";
     HttpResponse response = httpGET(editUrl, 200);
     JsonNode node = getJSONNode(response);
-    
assertEquals("../../../$metadata#People('russellwhyte')/Trips(1003)/PlanItems/"
+    
assertEquals(baseURL+"/$metadata#People('russellwhyte')/Trips(1003)/PlanItems/"
         + "Microsoft.OData.SampleService.Models.TripPin.Event/$entity",
         node.get("@odata.context").asText());
     assertEquals("#Microsoft.OData.SampleService.Models.TripPin.Event", 
node.get("@odata.type").asText());

Reply via email to