You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/06/04 06:22:19 UTC

[incubator-servicecomb-java-chassis] 03/03: [SCB-637] impl getMethod/getContentType/getCharacterEncoding in VertxClientRequestToHttpServletRequest/VertxServerRequestToHttpServletRequest/InvocationToHttpServletRequest

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 517a9ff762f8c5f969ed7dee834a6fc88c32e340
Author: wujimin <wu...@huawei.com>
AuthorDate: Mon Jun 4 00:24:58 2018 +0800

    [SCB-637] impl getMethod/getContentType/getCharacterEncoding in VertxClientRequestToHttpServletRequest/VertxServerRequestToHttpServletRequest/InvocationToHttpServletRequest
---
 .../VertxClientRequestToHttpServletRequest.java    | 25 +++++++++++
 .../VertxServerRequestToHttpServletRequest.java    | 37 +++++++++++++---
 ...TestVertxClientRequestToHttpServletRequest.java | 51 ++++++++++++++++++++++
 ...TestVertxServerRequestToHttpServletRequest.java | 16 +++++++
 .../common/InvocationToHttpServletRequest.java     | 21 +++++++++
 .../common/TestInvocationToHttpServletRequest.java | 10 +++++
 6 files changed, 153 insertions(+), 7 deletions(-)

diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxClientRequestToHttpServletRequest.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxClientRequestToHttpServletRequest.java
index 3563d4d..72fd366 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxClientRequestToHttpServletRequest.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxClientRequestToHttpServletRequest.java
@@ -20,12 +20,18 @@ package org.apache.servicecomb.foundation.vertx.http;
 import java.util.Collections;
 import java.util.Enumeration;
 
+import javax.ws.rs.core.HttpHeaders;
+
+import org.apache.servicecomb.foundation.common.http.HttpUtils;
+
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.HttpClientRequest;
 
 public class VertxClientRequestToHttpServletRequest extends AbstractHttpServletRequest {
   private HttpClientRequest clientRequest;
 
+  private String characterEncoding;
+
   public VertxClientRequestToHttpServletRequest(HttpClientRequest clientRequest, Buffer bodyBuffer) {
     this.clientRequest = clientRequest;
     setBodyBuffer(bodyBuffer);
@@ -70,4 +76,23 @@ public class VertxClientRequestToHttpServletRequest extends AbstractHttpServletR
   public String getContextPath() {
     return "";
   }
+
+  @Override
+  public String getMethod() {
+    return clientRequest.method().name();
+  }
+
+  @Override
+  public String getContentType() {
+    return clientRequest.headers().get(HttpHeaders.CONTENT_TYPE);
+  }
+
+  @Override
+  public String getCharacterEncoding() {
+    if (characterEncoding == null) {
+      characterEncoding = HttpUtils.getCharsetFromContentType(getContentType());
+    }
+
+    return characterEncoding;
+  }
 }
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java
index 47e9d4d..f198587 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java
@@ -31,6 +31,7 @@ import javax.servlet.http.Cookie;
 import javax.servlet.http.Part;
 import javax.ws.rs.core.HttpHeaders;
 
+import org.apache.servicecomb.foundation.common.http.HttpUtils;
 import org.apache.servicecomb.foundation.vertx.stream.BufferInputStream;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -60,6 +61,11 @@ public class VertxServerRequestToHttpServletRequest extends AbstractHttpServletR
 
   private SocketAddress socketAddress;
 
+  // cache from convert vertx parameters to servlet parameters
+  private Map<String, String[]> parameterMap;
+
+  private String characterEncoding;
+
   public VertxServerRequestToHttpServletRequest(RoutingContext context, String path) {
     this(context);
     this.path = path;
@@ -106,19 +112,27 @@ public class VertxServerRequestToHttpServletRequest extends AbstractHttpServletR
 
   @Override
   public String[] getParameterValues(String name) {
+    if (parameterMap != null) {
+      return parameterMap.get(name);
+    }
+
     List<String> paramList = this.vertxRequest.params().getAll(name);
-    return (String[]) paramList.toArray(new String[paramList.size()]);
+    return paramList.toArray(new String[paramList.size()]);
   }
 
   @Override
   public Map<String, String[]> getParameterMap() {
-    Map<String, String[]> paramMap = new HashMap<>();
-    MultiMap map = this.vertxRequest.params();
-    for (String name : map.names()) {
-      List<String> valueList = map.getAll(name);
-      paramMap.put(name, (String[]) map.getAll(name).toArray(new String[valueList.size()]));
+    if (parameterMap == null) {
+      Map<String, String[]> paramMap = new HashMap<>();
+      MultiMap map = this.vertxRequest.params();
+      for (String name : map.names()) {
+        List<String> valueList = map.getAll(name);
+        paramMap.put(name, map.getAll(name).toArray(new String[valueList.size()]));
+      }
+      parameterMap = paramMap;
     }
-    return paramMap;
+
+    return parameterMap;
   }
 
   @Override
@@ -240,4 +254,13 @@ public class VertxServerRequestToHttpServletRequest extends AbstractHttpServletR
   public RoutingContext getContext() {
     return context;
   }
+
+  @Override
+  public String getCharacterEncoding() {
+    if (characterEncoding == null) {
+      characterEncoding = HttpUtils.getCharsetFromContentType(getContentType());
+    }
+
+    return characterEncoding;
+  }
 }
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientRequestToHttpServletRequest.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientRequestToHttpServletRequest.java
index 71908f1..271a0c08 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientRequestToHttpServletRequest.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientRequestToHttpServletRequest.java
@@ -19,6 +19,9 @@ package org.apache.servicecomb.foundation.vertx.http;
 
 import java.util.Collections;
 
+import javax.ws.rs.core.HttpHeaders;
+
+import org.apache.servicecomb.foundation.common.http.HttpUtils;
 import org.hamcrest.Matchers;
 import org.junit.Assert;
 import org.junit.Before;
@@ -27,6 +30,7 @@ import org.junit.Test;
 import io.vertx.core.MultiMap;
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.HttpClientRequest;
+import io.vertx.core.http.HttpMethod;
 import mockit.Expectations;
 import mockit.Mocked;
 
@@ -143,4 +147,51 @@ public class TestVertxClientRequestToHttpServletRequest {
   public void testGetContextPath() {
     Assert.assertEquals("", request.getContextPath());
   }
+
+  @Test
+  public void getMethod() {
+    new Expectations() {
+      {
+        clientRequest.method();
+        result = HttpMethod.GET;
+      }
+    };
+
+    Assert.assertEquals("GET", request.getMethod());
+  }
+
+  @Test
+  public void getContentType() {
+    MultiMap headers = MultiMap.caseInsensitiveMultiMap();
+    new Expectations() {
+      {
+        clientRequest.headers();
+        result = headers;
+      }
+    };
+
+    request.addHeader(HttpHeaders.CONTENT_TYPE, "ct");
+
+    Assert.assertEquals("ct", request.getContentType());
+  }
+
+  @Test
+  public void getCharacterEncoding() {
+    String contentType = "ct";
+    String characterEncoding = "ce";
+
+    MultiMap headers = MultiMap.caseInsensitiveMultiMap();
+    new Expectations(HttpUtils.class) {
+      {
+        HttpUtils.getCharsetFromContentType(contentType);
+        result = characterEncoding;
+        clientRequest.headers();
+        result = headers;
+      }
+    };
+
+    request.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
+
+    Assert.assertEquals("ce", request.getCharacterEncoding());
+  }
 }
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerRequestToHttpServletRequest.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerRequestToHttpServletRequest.java
index 2e2f0f8..df72215 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerRequestToHttpServletRequest.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerRequestToHttpServletRequest.java
@@ -29,6 +29,7 @@ import javax.servlet.http.Cookie;
 import javax.ws.rs.core.HttpHeaders;
 import javax.xml.ws.Holder;
 
+import org.apache.servicecomb.foundation.common.http.HttpUtils;
 import org.hamcrest.Matchers;
 import org.junit.Assert;
 import org.junit.Before;
@@ -182,6 +183,7 @@ public class TestVertxServerRequestToHttpServletRequest {
     Map<String, String[]> result = request.getParameterMap();
     Assert.assertThat(result.keySet(), Matchers.contains("name"));
     Assert.assertThat(result.get("name"), Matchers.arrayContaining("value"));
+    Assert.assertSame(result, request.getParameterMap());
   }
 
   @Test
@@ -436,4 +438,18 @@ public class TestVertxServerRequestToHttpServletRequest {
 
     Assert.assertSame(asyncContext, request.getAsyncContext());
   }
+
+  @Test
+  public void getCharacterEncoding() {
+    new Expectations(HttpUtils.class) {
+      {
+        vertxRequest.getHeader(HttpHeaders.CONTENT_TYPE);
+        result = "ct";
+        HttpUtils.getCharsetFromContentType("ct");
+        result = "ce";
+      }
+    };
+
+    Assert.assertEquals("ce", request.getCharacterEncoding());
+  }
 }
diff --git a/providers/provider-rest-common/src/main/java/org/apache/servicecomb/provider/rest/common/InvocationToHttpServletRequest.java b/providers/provider-rest-common/src/main/java/org/apache/servicecomb/provider/rest/common/InvocationToHttpServletRequest.java
index c3585bb..eb126d4 100644
--- a/providers/provider-rest-common/src/main/java/org/apache/servicecomb/provider/rest/common/InvocationToHttpServletRequest.java
+++ b/providers/provider-rest-common/src/main/java/org/apache/servicecomb/provider/rest/common/InvocationToHttpServletRequest.java
@@ -30,6 +30,9 @@ import org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest;
 
 import io.vertx.core.net.SocketAddress;
 
+/**
+ * when transport is not over http, mock a HttpServletRequest from Invocation
+ */
 public class InvocationToHttpServletRequest extends AbstractHttpServletRequest {
   private RestOperationMeta swaggerOperation;
 
@@ -126,4 +129,22 @@ public class InvocationToHttpServletRequest extends AbstractHttpServletRequest {
   public String getContextPath() {
     return "";
   }
+
+  /**
+   * it's a mock httpServletRequest, contentType is unknown
+   * @return contentType
+   */
+  @Override
+  public String getContentType() {
+    return null;
+  }
+
+  /**
+   * it's a mock httpServletRequest, characterEncoding is unknown
+   * @return characterEncoding
+   */
+  @Override
+  public String getCharacterEncoding() {
+    return null;
+  }
 }
diff --git a/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestInvocationToHttpServletRequest.java b/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestInvocationToHttpServletRequest.java
index b29f329..79c1c90 100644
--- a/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestInvocationToHttpServletRequest.java
+++ b/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestInvocationToHttpServletRequest.java
@@ -322,4 +322,14 @@ public class TestInvocationToHttpServletRequest {
     InvocationToHttpServletRequest request = new InvocationToHttpServletRequest(invocation);
     Assert.assertEquals("", request.getContextPath());
   }
+
+  @Test
+  public void getContentType() {
+    Assert.assertNull(request.getContentType());
+  }
+
+  @Test
+  public void getCharacterEncoding() {
+    Assert.assertNull(request.getCharacterEncoding());
+  }
 }

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.