You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by sk...@apache.org on 2014/05/19 15:28:07 UTC

[18/50] [abbrv] git commit: [OLINGO-266] TDD for metadata & uri handling

[OLINGO-266] TDD for metadata & uri handling


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

Branch: refs/heads/master
Commit: 0c32f1a56f7e8e19671b31152eea386428bf22a3
Parents: 181bd26
Author: Stephan Klevenz <st...@sap.com>
Authored: Fri May 16 12:21:03 2014 +0200
Committer: Stephan Klevenz <st...@sap.com>
Committed: Mon May 19 14:27:05 2014 +0200

----------------------------------------------------------------------
 .../server/core/ODataHttpHandlerImpl.java       |  55 ++++++----
 .../apache/olingo/server/core/ODataRequest.java |  38 ++++++-
 .../server/core/ODataHttpHandlerImplTest.java   | 101 +++++++++++++++++++
 .../olingo/server/tecsvc/TechnicalServlet.java  |  18 ++++
 .../olingo/server/core/ODataHandlerTest.java    |  32 ++++--
 5 files changed, 214 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/0c32f1a5/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java
index 4664b96..2efba90 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java
@@ -92,14 +92,16 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
     }
   }
 
-  private ODataRequest createODataRequest(final HttpServletRequest request) {
+  private ODataRequest createODataRequest(final HttpServletRequest httpRequest) {
     try {
       ODataRequest odRequest = new ODataRequest();
 
-      odRequest.setBody(request.getInputStream());
-      odRequest.setHeaders(extractHeaders(request));
-      odRequest.setQueryParameters(extractQueryParameters(request.getQueryString()));
-      odRequest.setMethod(HttpMethod.valueOf(request.getMethod()));
+      odRequest.setBody(httpRequest.getInputStream());
+      odRequest.setHeaders(extractHeaders(httpRequest));
+      odRequest.setMethod(HttpMethod.valueOf(httpRequest.getMethod()));
+
+      // request uri string
+      fillRequestUri(odRequest, httpRequest, 0);
 
       return odRequest;
     } catch (Exception e) {
@@ -107,20 +109,37 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler {
     }
   }
 
-  private Map<String, String> extractQueryParameters(final String queryString) {
-    Map<String, String> queryParametersMap = new HashMap<String, String>();
-    if (queryString != null) {
-      List<String> queryParameters = Arrays.asList(Decoder.decode(queryString).split("\\&"));
-      for (String param : queryParameters) {
-        int indexOfEqualSign = param.indexOf("=");
-        if (indexOfEqualSign < 0) {
-          queryParametersMap.put(param, "");
-        } else {
-          queryParametersMap.put(param.substring(0, indexOfEqualSign), param.substring(indexOfEqualSign + 1));
-        }
-      }
+  static void fillRequestUri(ODataRequest odRequest, final HttpServletRequest httpRequest, int split) {
+
+    String rawRequestUri = httpRequest.getRequestURL().toString();
+
+    String rawODataPath;
+    if (!"".equals(httpRequest.getServletPath())) {
+      int beginIndex;
+      beginIndex = rawRequestUri.indexOf(httpRequest.getServletPath());
+      beginIndex += httpRequest.getServletPath().length();
+      rawODataPath = rawRequestUri.substring(beginIndex);
+    } else if (!"".equals(httpRequest.getContextPath())) {
+      int beginIndex;
+      beginIndex = rawRequestUri.indexOf(httpRequest.getContextPath());
+      beginIndex += httpRequest.getContextPath().length();
+      rawODataPath = rawRequestUri.substring(beginIndex);
+    } else {
+      rawODataPath = httpRequest.getRequestURI();
     }
-    return queryParametersMap;
+
+    for (int i = 0; i < split; i++) {
+      int e = rawODataPath.indexOf("/", 1);
+      rawODataPath = rawODataPath.substring(e);
+    }
+
+    String rawBaseUri = rawRequestUri.substring(0, rawRequestUri.length() - rawODataPath.length());
+
+    odRequest.setRawQueryPath(httpRequest.getQueryString());
+    odRequest.setRawRequestUri(rawRequestUri
+        + (httpRequest.getQueryString() == null ? "" : "?" + httpRequest.getQueryString()));
+    odRequest.setRawODataPath(rawODataPath);
+    odRequest.setRawBaseUri(rawBaseUri);
   }
 
   private Map<String, List<String>> extractHeaders(final HttpServletRequest req) {

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/0c32f1a5/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataRequest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataRequest.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataRequest.java
index 5499a79..ce2f4b3 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataRequest.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataRequest.java
@@ -30,7 +30,10 @@ public class ODataRequest {
   private HttpMethod method;
   private Map<String, List<String>> headers = new HashMap<String, List<String>>();
   private InputStream body;
-  private Map<String, String> queryParameters;
+  private String rawQueryPath;
+  private String rawRequestUri;
+  private String rawODataPath;
+  private String rawBaseUri;
 
   public HttpMethod getMethod() {
     return method;
@@ -56,11 +59,36 @@ public class ODataRequest {
     this.body = body;
   }
 
-  public Map<String, String> getQueryParameters() {
-    return queryParameters;
+  public String getRawQueryPath() {
+    return rawQueryPath;
   }
 
-  public void setQueryParameters(final Map<String, String> queryParameters) {
-    this.queryParameters = queryParameters;
+  public void setRawQueryPath(String rawQueryPath) {
+    this.rawQueryPath = rawQueryPath;
+  }
+
+  public String getRawBaseUri() {
+    return rawBaseUri;
+  }
+
+  public String getRawRequestUri() {
+    return rawRequestUri;
+  }
+
+  public String getRawODataPath() {
+    return rawODataPath;
+  }
+
+  public void setRawRequestUri(String rawRequestUri) {
+    this.rawRequestUri = rawRequestUri;
+  }
+
+  public void setRawODataPath(String rawODataPath) {
+    this.rawODataPath = rawODataPath;
+    
+  }
+
+  public void setRawBaseUri(String rawBaseUri) {
+    this.rawBaseUri = rawBaseUri;
   }
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/0c32f1a5/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataHttpHandlerImplTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataHttpHandlerImplTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataHttpHandlerImplTest.java
new file mode 100644
index 0000000..0d82a0c
--- /dev/null
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataHttpHandlerImplTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.core;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ODataHttpHandlerImplTest {
+
+  private final Logger LOG = LoggerFactory.getLogger(ODataHttpHandlerImplTest.class);
+
+  @Test
+  public void fillRequestGeneric() {
+
+    //@formatter:off (Eclipse formatter)
+    //CHECKSTYLE:OFF (Maven checkstyle)
+    String [][] uris = {
+        /* 0: host                    1: cp         2: sp       3: sr          4: od       5: qp        6: spl  */
+        {  "http://localhost",          "",           "",         "",          "/",         "",         "0"},  
+        {  "http://localhost",          "",           "",         "",          "/od",       "",         "0"},  
+        {  "http://localhost",          "",           "",         "",          "/od/",      "",         "0"},  
+
+        {  "http://localhost",          "/cp",        "",         "",          "/",         "",         "0"},  
+        {  "http://localhost",          "/cp",        "",         "",          "/od",       "",         "0"},  
+        {  "http://localhost",          "",           "/sp",      "",          "/",         "",         "0"},  
+        {  "http://localhost",          "",           "/sp",      "",          "/od",       "",         "0"},  
+        {  "http://localhost",          "",           "",         "/sr",       "/",         "",         "1"},  
+        {  "http://localhost",          "",           "",         "/sr",       "/od",       "",         "1"},  
+        {  "http://localhost",          "",           "",         "/sr/sr",    "/",         "",         "2"},  
+        {  "http://localhost",          "",           "",         "/sr/sr",    "/od",       "",         "2"},  
+
+        {  "http://localhost",          "/cp",        "/sp",      "",          "/",         "",         "0"},  
+        {  "http://localhost",          "/cp",        "/sp",      "",          "/od",       "",         "0"},  
+        {  "http://localhost",          "/cp",        "",         "/sr",       "/",         "",         "1"},  
+        {  "http://localhost",          "/cp",        "",         "/sr",       "/od",       "",         "1"},  
+        {  "http://localhost",          "",           "/sp",      "/sr",       "/",         "",         "1"},  
+        {  "http://localhost",          "",           "/sp",      "/sr",       "/od",       "",         "1"},  
+        {  "http://localhost",          "/cp",        "/sp",      "/sr",       "/",         "",         "1"},  
+        {  "http://localhost",          "/cp",        "/sp",      "/sr",       "/od",       "",         "1"},  
+        
+        {  "http://localhost",          "",           "",         "",          "/",         "qp",       "0"},  
+        {  "http://localhost",          "/cp",        "/sp",      "/sr",       "/od",       "qp",       "1"},  
+        
+        {  "http://localhost:8080",     "/c%20p",     "/s%20p",   "/s%20r",    "/o%20d",    "p+q",      "1"},                     
+    };
+    //@formatter:on
+    // CHECKSTYLE:on
+
+    for (String[] p : uris) {
+      HttpServletRequest hr = mock(HttpServletRequest.class);
+
+      String requestUrl = p[0] + p[1] + p[2] + p[3] + p[4];
+      String requestUri = p[1] + p[2] + p[3] + p[4];
+      String queryString = p[5].isEmpty() ? null : p[5];
+
+      LOG.debug(requestUrl + (queryString == null ? "" : "?" + queryString));
+
+      when(hr.getRequestURL()).thenReturn(new StringBuffer(requestUrl));
+      when(hr.getRequestURI()).thenReturn(requestUri);
+      when(hr.getQueryString()).thenReturn(queryString);
+      when(hr.getContextPath()).thenReturn(p[1]);
+      when(hr.getServletPath()).thenReturn(p[2]);
+
+      ODataRequest odr = new ODataRequest();
+      ODataHttpHandlerImpl.fillRequestUri(odr, hr, Integer.parseInt(p[6]));
+
+      String rawBaseUri = p[0] + p[1] + p[2] + p[3];
+      String rawODataPath = p[4];
+      String rawQueryPath = "".equals(p[5]) ? null : p[5];
+      String rawRequestUri = requestUrl + (queryString == null ? "" : "?" + queryString);
+
+      assertEquals(rawBaseUri, odr.getRawBaseUri());
+      assertEquals(rawODataPath, odr.getRawODataPath());
+      assertEquals(rawQueryPath, odr.getRawQueryPath());
+      assertEquals(rawRequestUri, odr.getRawRequestUri());
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/0c32f1a5/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java
----------------------------------------------------------------------
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 c7bb0e0..70e3bdf 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
@@ -48,4 +48,22 @@ public class TechnicalServlet extends HttpServlet {
     ODataHttpHandler handler = server.createHandler(edm);
     handler.process(req, resp);
   }
+
+//  public void bla(HttpServletRequest hr, HttpServletResponse hres) {
+//    ODataServer s = ODataServer.newInstance();
+//
+//    ODataRequest r = s.createRequest(hr);
+//
+//    Edm edm = server.createEdm(new EdmTechProvider());
+//    ODataUriParser p = s.createUriParser(edm);
+// 
+//    ODataUriInfo i = p.parse(r);
+//
+//    ODataDispatcher d = s.createDispatcher(proc);
+//    
+//    ODataResponse res = d.dispatch();
+//    
+//    s.sendResponse(res, hres);
+//  }
+
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/0c32f1a5/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 aa472c4..fe2f48c 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
@@ -45,23 +45,41 @@ public class ODataHandlerTest {
   @Test
   public void testServiceDocumentDefault() throws Exception {
     ODataRequest request = new ODataRequest();
-    
+
     request.setMethod(HttpMethod.GET);
-    
+
     ODataResponse response = handler.process(request);
-    
+
     assertNotNull(response);
     assertEquals(200, response.getStatusCode());
     assertEquals("application/json", response.getHeaders().get("Content-Type"));
-    
-    
+
     assertNotNull(response.getContent());
     String doc = IOUtils.toString(response.getContent());
-   
+
     assertTrue(doc.contains("\"@odata.context\" : \"http://root/$metadata\""));
     assertTrue(doc.contains("\"value\" :"));
+  }
+
+  @Test
+  public void testMetadataDefault() throws Exception {
+    ODataRequest request = new ODataRequest();
+
+    request.setMethod(HttpMethod.GET);
+//    request.setUrl("http://localhost/odata/$metadata");
+
+    ODataResponse response = handler.process(request);
+
+    assertNotNull(response);
+    assertEquals(200, response.getStatusCode());
+    assertEquals("application/xml", response.getHeaders().get("Content-Type"));
+
+    assertNotNull(response.getContent());
+    String doc = IOUtils.toString(response.getContent());
     
-    // TODO
+    assertTrue(doc.contains("<edmx:Edmx Version=\"4.0\">"));
+
   }
 
+  
 }