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/04/20 01:50:24 UTC

[incubator-servicecomb-java-chassis] branch master updated (41049be -> 719a079)

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

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


    from 41049be  [SCB-488]Retry/Metrics some default behavior cause unnecessary retry and logs
     new be11d1d  SCB-483 HttpServerFilter support async beforeSendResponse
     new 8494989  SCB-483 read file faster
     new 211afe6  SCB-483 HttpServletResponseEx from vertx support sendPart
     new 34897c4  SCB-483 ServerRestArgsFilter support sendPart
     new 8679a91  SCB-483 set correct content-type for download
     new 2cd4128  SCB-483 add demo for download, now only finished download server, so now can only disable signature and https, and use browser to test download
     new 89e3668c SCB-483 delete useless code
     new 5ea3718  SCB-483 delete unnecessary type cast
     new 719a079  SCB-483 change asyncBeforeSendResponse to beforeSendResponseAsync

The 9 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../common/javassist/TestJavassistUtils.java       |   4 +-
 .../codec/protobuf/utils/ProtobufSchemaUtils.java  |   2 +-
 .../common/rest/AbstractRestInvocation.java        |  47 ++++++--
 .../rest/definition/path/PathVarParamWriter.java   |   2 +-
 .../common/rest/filter/HttpServerFilter.java       |  30 ++++-
 ...HttpServerFilterBeforeSendResponseExecutor.java |  78 ++++++++++++
 .../rest/filter/inner/ServerRestArgsFilter.java    |  18 ++-
 .../common/rest/TestAbstractRestInvocation.java    |  20 ++--
 .../rest/filter/HttpServerFilterBaseForTest.java   |  18 ++-
 .../common/rest/filter/TestHttpServerFilter.java   |  45 +++----
 ...HttpServerFilterBeforeSendResponseExecutor.java |  78 ++++++++++++
 .../filter/inner/TestServerRestArgsFilter.java     |  73 ++++++++++++
 .../demo/springmvc/server/DownloadSchema.java      |  79 ++++++++++++
 .../vertx/http/AbstractHttpServletResponse.java    |   7 ++
 .../vertx/http/HttpServletResponseEx.java          |   5 +
 .../vertx/http/StandardHttpServletResponseEx.java  |   7 ++
 .../VertxServerResponseToHttpServletResponse.java  |  52 ++++++++
 .../vertx/stream/InputStreamToReadStream.java      |   2 +-
 .../http/TestAbstractHttpServletResponse.java      |  15 +++
 .../http/TestStandardHttpServletResponseEx.java    |  24 ++++
 ...stVertxServerResponseToHttpServletResponse.java | 132 +++++++++++++++++++++
 .../metrics/core/DefaultRegistryInitializer.java   |   4 -
 22 files changed, 671 insertions(+), 71 deletions(-)
 create mode 100644 common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBeforeSendResponseExecutor.java
 copy handlers/handler-bizkeeper/src/main/java/org/apache/servicecomb/bizkeeper/ReturnNullFallbackPolicy.java => common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBaseForTest.java (71%)
 copy service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceVersion.java => common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilter.java (50%)
 create mode 100644 common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilterBeforeSendResponseExecutor.java
 create mode 100644 common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/TestServerRestArgsFilter.java
 create mode 100644 demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DownloadSchema.java

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

[incubator-servicecomb-java-chassis] 01/09: SCB-483 HttpServerFilter support async beforeSendResponse

Posted by li...@apache.org.
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 be11d1da49bb417641121ebfa2b7307a07504463
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Apr 17 15:36:26 2018 +0800

    SCB-483 HttpServerFilter support async beforeSendResponse
---
 .../common/rest/AbstractRestInvocation.java        | 46 ++++++++++---
 .../common/rest/filter/HttpServerFilter.java       | 30 +++++++--
 ...HttpServerFilterBeforeSendResponseExecutor.java | 78 ++++++++++++++++++++++
 .../common/rest/TestAbstractRestInvocation.java    | 10 +--
 .../rest/filter/HttpServerFilterBaseForTest.java}  | 22 +++---
 .../common/rest/filter/TestHttpServerFilter.java   | 57 ++++++++++++++++
 ...HttpServerFilterBeforeSendResponseExecutor.java | 78 ++++++++++++++++++++++
 7 files changed, 288 insertions(+), 33 deletions(-)

diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
index 3dfb5cc..89be43e 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
@@ -17,11 +17,13 @@
 
 package org.apache.servicecomb.common.rest;
 
+import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.concurrent.CompletableFuture;
 
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response.Status;
@@ -31,6 +33,7 @@ import org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor;
 import org.apache.servicecomb.common.rest.codec.produce.ProduceProcessorManager;
 import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
 import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
+import org.apache.servicecomb.common.rest.filter.HttpServerFilterBeforeSendResponseExecutor;
 import org.apache.servicecomb.common.rest.locator.OperationLocator;
 import org.apache.servicecomb.common.rest.locator.ServicePathManager;
 import org.apache.servicecomb.core.Const;
@@ -194,18 +197,11 @@ public abstract class AbstractRestInvocation {
       LOGGER.error("Failed to send rest response, operation:{}.",
           invocation.getMicroserviceQualifiedName(),
           e);
-    } finally {
-      requestEx.getAsyncContext().complete();
-      // if failed to locate path, then will not create invocation
-      // TODO: statistics this case
-      if (invocation != null) {
-        invocation.onFinish(response);
-      }
     }
   }
 
   @SuppressWarnings("deprecation")
-  protected void sendResponse(Response response) throws Exception {
+  protected void sendResponse(Response response) {
     if (response.getHeaders().getHeaderMap() != null) {
       for (Entry<String, List<Object>> entry : response.getHeaders().getHeaderMap().entrySet()) {
         for (Object value : entry.getValue()) {
@@ -220,10 +216,38 @@ public abstract class AbstractRestInvocation {
     responseEx.setAttribute(RestConst.INVOCATION_HANDLER_RESPONSE, response);
     responseEx.setAttribute(RestConst.INVOCATION_HANDLER_PROCESSOR, produceProcessor);
 
-    for (HttpServerFilter filter : httpServerFilters) {
-      filter.beforeSendResponse(invocation, responseEx);
+    executeHttpServerFilters(response);
+  }
+
+  protected void executeHttpServerFilters(Response response) {
+    HttpServerFilterBeforeSendResponseExecutor exec =
+        new HttpServerFilterBeforeSendResponseExecutor(httpServerFilters, invocation, responseEx);
+    CompletableFuture<Void> future = exec.run();
+    future.whenComplete((v, e) -> {
+      onExecuteHttpServerFiltersFinish(response, e);
+    });
+  }
+
+  protected void onExecuteHttpServerFiltersFinish(Response response, Throwable e) {
+    if (e != null) {
+      LOGGER.error("Failed to execute HttpServerFilters, operation:{}.",
+          invocation.getMicroserviceQualifiedName(),
+          e);
     }
 
-    responseEx.flushBuffer();
+    try {
+      responseEx.flushBuffer();
+    } catch (IOException flushException) {
+      LOGGER.error("Failed to flush rest response, operation:{}.",
+          invocation.getMicroserviceQualifiedName(),
+          flushException);
+    }
+
+    requestEx.getAsyncContext().complete();
+    // if failed to locate path, then will not create invocation
+    // TODO: statistics this case
+    if (invocation != null) {
+      invocation.onFinish(response);
+    }
   }
 }
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java
index af0f6b1..594c262 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java
@@ -17,6 +17,8 @@
 
 package org.apache.servicecomb.common.rest.filter;
 
+import java.util.concurrent.CompletableFuture;
+
 import org.apache.servicecomb.core.Invocation;
 import org.apache.servicecomb.core.definition.OperationMeta;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
@@ -30,10 +32,30 @@ public interface HttpServerFilter {
     return false;
   }
 
-  // if finished, then return a none null response
-  // if return a null response, then sdk will call next filter.afterReceiveRequest
+  /**
+   * @return if finished, then return a none null response<br>
+   * if return a null response, then sdk will call next filter.afterReceiveRequest
+   */
   Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx requestEx);
 
-  // invocation maybe null
-  void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx);
+  /**
+   * @param invocation maybe null
+   */
+  default CompletableFuture<Void> asyncBeforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+    CompletableFuture<Void> future = new CompletableFuture<>();
+    try {
+      beforeSendResponse(invocation, responseEx);
+      future.complete(null);
+    } catch (Throwable e) {
+      future.completeExceptionally(e);
+    }
+    return future;
+  }
+
+  /**
+   * @param invocation maybe null
+   */
+  default void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+
+  }
 }
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBeforeSendResponseExecutor.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBeforeSendResponseExecutor.java
new file mode 100644
index 0000000..70889cc
--- /dev/null
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBeforeSendResponseExecutor.java
@@ -0,0 +1,78 @@
+/*
+ * 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.servicecomb.common.rest.filter;
+
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
+
+public class HttpServerFilterBeforeSendResponseExecutor {
+  private List<HttpServerFilter> httpServerFilters;
+
+  private Invocation invocation;
+
+  private HttpServletResponseEx responseEx;
+
+  private int currentIndex;
+
+  private CompletableFuture<Void> future = new CompletableFuture<Void>();
+
+  public HttpServerFilterBeforeSendResponseExecutor(List<HttpServerFilter> httpServerFilters, Invocation invocation,
+      HttpServletResponseEx responseEx) {
+    this.httpServerFilters = httpServerFilters;
+    this.invocation = invocation;
+    this.responseEx = responseEx;
+  }
+
+  public CompletableFuture<Void> run() {
+    doRun();
+
+    return future;
+  }
+
+  protected CompletableFuture<Void> safeInvoke(HttpServerFilter httpServerFilter) {
+    try {
+      return httpServerFilter.asyncBeforeSendResponse(invocation, responseEx);
+    } catch (Throwable e) {
+      CompletableFuture<Void> eFuture = new CompletableFuture<Void>();
+      eFuture.completeExceptionally(e);
+      return eFuture;
+    }
+  }
+
+  protected void doRun() {
+    if (currentIndex == httpServerFilters.size()) {
+      future.complete(null);
+      return;
+    }
+
+    HttpServerFilter httpServerFilter = httpServerFilters.get(currentIndex);
+    currentIndex++;
+
+    CompletableFuture<Void> stepFuture = safeInvoke(httpServerFilter);
+    stepFuture.whenComplete((v, e) -> {
+      if (e == null) {
+        doRun();
+        return;
+      }
+
+      future.completeExceptionally(e);
+    });
+  }
+}
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
index 72d0b13..6d82bc6 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
@@ -29,6 +29,7 @@ import javax.xml.ws.Holder;
 import org.apache.servicecomb.common.rest.codec.produce.ProduceProcessorManager;
 import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
 import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
+import org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest;
 import org.apache.servicecomb.common.rest.locator.OperationLocator;
 import org.apache.servicecomb.common.rest.locator.ServicePathManager;
 import org.apache.servicecomb.core.Const;
@@ -403,6 +404,7 @@ public class TestAbstractRestInvocation {
       @Override
       protected void sendResponse(Response response) {
         result.value = response;
+        super.sendResponse(response);
       }
     };
     initRestInvocation();
@@ -648,12 +650,12 @@ public class TestAbstractRestInvocation {
       }
     }.getMockInstance();
 
-    HttpServerFilter filter = new MockUp<HttpServerFilter>() {
-      @Mock
-      void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+    HttpServerFilter filter = new HttpServerFilterBaseForTest() {
+      @Override
+      public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
         buffer.appendString("-filter");
       }
-    }.getMockInstance();
+    };
 
     initRestInvocation();
     List<HttpServerFilter> httpServerFilters = SPIServiceUtils.loadSortedService(HttpServerFilter.class);
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBaseForTest.java
similarity index 63%
copy from common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java
copy to common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBaseForTest.java
index af0f6b1..24ccd95 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBaseForTest.java
@@ -14,26 +14,20 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.servicecomb.common.rest.filter;
 
 import org.apache.servicecomb.core.Invocation;
-import org.apache.servicecomb.core.definition.OperationMeta;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
-import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
 import org.apache.servicecomb.swagger.invocation.Response;
 
-public interface HttpServerFilter {
-  int getOrder();
-
-  default boolean needCacheRequest(OperationMeta operationMeta) {
-    return false;
+public class HttpServerFilterBaseForTest implements HttpServerFilter {
+  @Override
+  public int getOrder() {
+    return 0;
   }
 
-  // if finished, then return a none null response
-  // if return a null response, then sdk will call next filter.afterReceiveRequest
-  Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx requestEx);
-
-  // invocation maybe null
-  void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx);
+  @Override
+  public Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx requestEx) {
+    return null;
+  }
 }
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilter.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilter.java
new file mode 100644
index 0000000..d7b0d01
--- /dev/null
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilter.java
@@ -0,0 +1,57 @@
+/*
+ * 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.servicecomb.common.rest.filter;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class TestHttpServerFilter {
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void asyncSucc() throws InterruptedException, ExecutionException {
+    HttpServerFilter filter = new HttpServerFilterBaseForTest();
+
+    CompletableFuture<Void> future = filter.asyncBeforeSendResponse(null, null);
+    Assert.assertNull(future.get());
+  }
+
+  @Test
+  public void asyncFailed() throws InterruptedException, ExecutionException {
+    HttpServerFilter filter = new HttpServerFilterBaseForTest() {
+      @Override
+      public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+        throw new Error("");
+      }
+    };
+
+    expectedException.expect(ExecutionException.class);
+    expectedException.expectCause(Matchers.instanceOf(Error.class));
+
+    CompletableFuture<Void> future = filter.asyncBeforeSendResponse(null, null);
+    future.get();
+  }
+}
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilterBeforeSendResponseExecutor.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilterBeforeSendResponseExecutor.java
new file mode 100644
index 0000000..ba565ab
--- /dev/null
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilterBeforeSendResponseExecutor.java
@@ -0,0 +1,78 @@
+/*
+ * 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.servicecomb.common.rest.filter;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import mockit.Mocked;
+
+public class TestHttpServerFilterBeforeSendResponseExecutor {
+  @Mocked
+  Invocation invocation;
+
+  @Mocked
+  HttpServletResponseEx responseEx;
+
+  List<HttpServerFilter> httpServerFilters = new ArrayList<>();
+
+  HttpServerFilterBeforeSendResponseExecutor executor =
+      new HttpServerFilterBeforeSendResponseExecutor(httpServerFilters, invocation, responseEx);
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Before
+  public void setup() {
+    httpServerFilters.add(new HttpServerFilterBaseForTest());
+  }
+
+  @Test
+  public void runSucc() throws InterruptedException, ExecutionException {
+    CompletableFuture<Void> result = executor.run();
+
+    Assert.assertNull(result.get());
+  }
+
+  @Test
+  public void runFail() throws InterruptedException, ExecutionException {
+    httpServerFilters.add(new HttpServerFilterBaseForTest() {
+      @Override
+      public CompletableFuture<Void> asyncBeforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+        throw new Error("");
+      }
+    });
+
+    CompletableFuture<Void> result = executor.run();
+
+    expectedException.expect(ExecutionException.class);
+    expectedException.expectCause(Matchers.instanceOf(Error.class));
+
+    result.get();
+  }
+}

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

[incubator-servicecomb-java-chassis] 03/09: SCB-483 HttpServletResponseEx from vertx support sendPart

Posted by li...@apache.org.
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 211afe6cc8815c08e6d8d452c09e8c75b4d08bf4
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Apr 17 23:50:39 2018 +0800

    SCB-483 HttpServletResponseEx from vertx support sendPart
---
 .../vertx/http/AbstractHttpServletResponse.java    |   7 ++
 .../vertx/http/HttpServletResponseEx.java          |   5 +
 .../vertx/http/StandardHttpServletResponseEx.java  |   7 ++
 .../VertxServerResponseToHttpServletResponse.java  |  52 ++++++++
 .../http/TestAbstractHttpServletResponse.java      |  15 +++
 .../http/TestStandardHttpServletResponseEx.java    |  24 ++++
 ...stVertxServerResponseToHttpServletResponse.java | 132 +++++++++++++++++++++
 7 files changed, 242 insertions(+)

diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/AbstractHttpServletResponse.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/AbstractHttpServletResponse.java
index a09e773..2140429 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/AbstractHttpServletResponse.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/AbstractHttpServletResponse.java
@@ -23,9 +23,11 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
+import java.util.concurrent.CompletableFuture;
 
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.Cookie;
+import javax.servlet.http.Part;
 import javax.ws.rs.core.Response.StatusType;
 
 public abstract class AbstractHttpServletResponse extends BodyBufferSupportImpl implements HttpServletResponseEx {
@@ -230,4 +232,9 @@ public abstract class AbstractHttpServletResponse extends BodyBufferSupportImpl
   public Object getAttribute(String key) {
     return this.attributes.get(key);
   }
+
+  @Override
+  public CompletableFuture<Void> sendPart(Part body) {
+    throw new Error("not supported method");
+  }
 }
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/HttpServletResponseEx.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/HttpServletResponseEx.java
index 9749f4b..a1ee34e 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/HttpServletResponseEx.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/HttpServletResponseEx.java
@@ -17,7 +17,10 @@
 
 package org.apache.servicecomb.foundation.vertx.http;
 
+import java.util.concurrent.CompletableFuture;
+
 import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.Part;
 import javax.ws.rs.core.Response.StatusType;
 
 public interface HttpServletResponseEx extends HttpServletResponse, BodyBufferSupport {
@@ -26,4 +29,6 @@ public interface HttpServletResponseEx extends HttpServletResponse, BodyBufferSu
   void setAttribute(String key, Object value);
 
   Object getAttribute(String key);
+
+  CompletableFuture<Void> sendPart(Part body);
 }
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/StandardHttpServletResponseEx.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/StandardHttpServletResponseEx.java
index c2c1408..13c0ba7 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/StandardHttpServletResponseEx.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/StandardHttpServletResponseEx.java
@@ -20,9 +20,11 @@ package org.apache.servicecomb.foundation.vertx.http;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.CompletableFuture;
 
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponseWrapper;
+import javax.servlet.http.Part;
 import javax.ws.rs.core.Response.StatusType;
 
 import org.apache.servicecomb.foundation.common.http.HttpStatus;
@@ -95,4 +97,9 @@ public class StandardHttpServletResponseEx extends HttpServletResponseWrapper im
   public Object getAttribute(String key) {
     return this.attributes.get(key);
   }
+
+  @Override
+  public CompletableFuture<Void> sendPart(Part body) {
+    throw new Error("not supported method");
+  }
 }
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerResponseToHttpServletResponse.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerResponseToHttpServletResponse.java
index ca5510c..9f5db55 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerResponseToHttpServletResponse.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerResponseToHttpServletResponse.java
@@ -18,17 +18,23 @@
 package org.apache.servicecomb.foundation.vertx.http;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Collection;
 import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
 
+import javax.servlet.http.Part;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response.StatusType;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.servicecomb.foundation.common.http.HttpStatus;
+import org.apache.servicecomb.foundation.vertx.stream.InputStreamToReadStream;
 
 import io.vertx.core.Context;
 import io.vertx.core.Vertx;
 import io.vertx.core.http.HttpServerResponse;
+import io.vertx.core.streams.Pump;
 
 public class VertxServerResponseToHttpServletResponse extends AbstractHttpServletResponse {
   private Context context;
@@ -118,4 +124,50 @@ public class VertxServerResponseToHttpServletResponse extends AbstractHttpServle
 
     serverResponse.end(bodyBuffer);
   }
+
+  @Override
+  public CompletableFuture<Void> sendPart(Part part) {
+    CompletableFuture<Void> future = new CompletableFuture<Void>();
+
+    prepareSendPartHeader(part);
+
+    try {
+      InputStream is = part.getInputStream();
+      context.runOnContext(v -> {
+        InputStreamToReadStream aa = new InputStreamToReadStream(context.owner(), is);
+        aa.exceptionHandler(t -> {
+          clearPartResource(part, is);
+          future.completeExceptionally(t);
+        });
+        aa.endHandler(V -> {
+          clearPartResource(part, is);
+          future.complete(null);
+        });
+        Pump.pump(aa, serverResponse).start();
+      });
+    } catch (IOException e) {
+      future.completeExceptionally(e);
+    }
+
+    return future;
+  }
+
+  protected void prepareSendPartHeader(Part part) {
+    if (!serverResponse.headers().contains(HttpHeaders.CONTENT_LENGTH)) {
+      serverResponse.setChunked(true);
+    }
+
+    if (!serverResponse.headers().contains(HttpHeaders.CONTENT_TYPE)) {
+      serverResponse.putHeader(HttpHeaders.CONTENT_TYPE, part.getContentType());
+    }
+
+    if (!serverResponse.headers().contains(HttpHeaders.CONTENT_DISPOSITION)) {
+      serverResponse.putHeader(HttpHeaders.CONTENT_DISPOSITION,
+          "attachment;filename=" + part.getSubmittedFileName());
+    }
+  }
+
+  protected void clearPartResource(Part part, InputStream is) {
+    IOUtils.closeQuietly(is);
+  }
 }
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestAbstractHttpServletResponse.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestAbstractHttpServletResponse.java
index 2462772..ec884ee 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestAbstractHttpServletResponse.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestAbstractHttpServletResponse.java
@@ -20,10 +20,12 @@ package org.apache.servicecomb.foundation.vertx.http;
 import java.io.IOException;
 
 import org.hamcrest.Matchers;
+import org.junit.Assert;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 
+
 public class TestAbstractHttpServletResponse {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
@@ -304,4 +306,17 @@ public class TestAbstractHttpServletResponse {
 
     response.getStatusType();
   }
+
+  @Test
+  public void attribute() {
+    response.setAttribute("k", "v");
+    Assert.assertEquals("v", response.getAttribute("k"));
+  }
+
+  @Test
+  public void sendPart() {
+    setExceptionExpected();
+
+    response.sendPart(null);
+  }
 }
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestStandardHttpServletResponseEx.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestStandardHttpServletResponseEx.java
index 82853b2..00cb71e 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestStandardHttpServletResponseEx.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestStandardHttpServletResponseEx.java
@@ -23,9 +23,12 @@ import javax.servlet.ServletOutputStream;
 import javax.servlet.WriteListener;
 import javax.servlet.http.HttpServletResponse;
 
+import org.hamcrest.Matchers;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 import io.vertx.core.buffer.Buffer;
 import mockit.Mock;
@@ -38,6 +41,14 @@ public class TestStandardHttpServletResponseEx {
 
   StandardHttpServletResponseEx responseEx;
 
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private void setExceptionExpected() {
+    expectedException.expect(Error.class);
+    expectedException.expectMessage(Matchers.is("not supported method"));
+  }
+
   @Before
   public void setup() {
     responseEx = new StandardHttpServletResponseEx(response);
@@ -114,4 +125,17 @@ public class TestStandardHttpServletResponseEx {
     responseEx.flushBuffer();
     Assert.assertEquals("body", buffer.toString());
   }
+
+  @Test
+  public void attribute() {
+    responseEx.setAttribute("k", "v");
+    Assert.assertEquals("v", responseEx.getAttribute("k"));
+  }
+
+  @Test
+  public void sendPart() {
+    setExceptionExpected();
+
+    responseEx.sendPart(null);
+  }
 }
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
index d821d14..c69d41c 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
@@ -18,7 +18,11 @@
 package org.apache.servicecomb.foundation.vertx.http;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
 
+import javax.servlet.http.Part;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response.StatusType;
 
@@ -30,9 +34,12 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 
+import io.vertx.core.AsyncResult;
 import io.vertx.core.Context;
+import io.vertx.core.Future;
 import io.vertx.core.Handler;
 import io.vertx.core.MultiMap;
+import io.vertx.core.Vertx;
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.HttpServerResponse;
 import io.vertx.core.impl.VertxImpl;
@@ -56,11 +63,16 @@ public class TestVertxServerResponseToHttpServletResponse {
   boolean runOnContextInvoked;
 
   @Mocked
+  Vertx vertx;
+
+  @Mocked
   Context context;
 
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
+  boolean chunked;
+
   @Before
   public void setup() {
     serverResponse = new MockUp<HttpServerResponse>() {
@@ -92,6 +104,12 @@ public class TestVertxServerResponseToHttpServletResponse {
       }
 
       @Mock
+      HttpServerResponse putHeader(String name, String value) {
+        headers.set(name, value);
+        return serverResponse;
+      }
+
+      @Mock
       void end() {
         flushWithBody = false;
       }
@@ -100,6 +118,17 @@ public class TestVertxServerResponseToHttpServletResponse {
       void end(Buffer chunk) {
         flushWithBody = true;
       }
+
+      @Mock
+      HttpServerResponse setChunked(boolean chunked) {
+        TestVertxServerResponseToHttpServletResponse.this.chunked = chunked;
+        return serverResponse;
+      }
+
+      @Mock
+      boolean isChunked() {
+        return chunked;
+      }
     }.getMockInstance();
 
     new Expectations(VertxImpl.class) {
@@ -115,6 +144,22 @@ public class TestVertxServerResponseToHttpServletResponse {
         runOnContextInvoked = true;
         action.handle(null);
       }
+
+      @Mock
+      Vertx owner() {
+        return vertx;
+      }
+    };
+
+    new MockUp<Vertx>(vertx) {
+      @Mock
+      <T> void executeBlocking(Handler<Future<T>> blockingCodeHandler, boolean ordered,
+          Handler<AsyncResult<T>> resultHandler) {
+        Future<T> future = Future.future();
+        future.setHandler(resultHandler);
+
+        blockingCodeHandler.handle(future);
+      }
     };
 
     response = new VertxServerResponseToHttpServletResponse(serverResponse);
@@ -246,4 +291,91 @@ public class TestVertxServerResponseToHttpServletResponse {
 
     Assert.assertTrue(flushWithBody);
   }
+
+  @Test
+  public void prepareSendPartHeader_update(@Mocked Part part) {
+    new Expectations() {
+      {
+        part.getContentType();
+        result = "type";
+        part.getSubmittedFileName();
+        result = "name";
+      }
+    };
+    response.prepareSendPartHeader(part);
+
+    Assert.assertTrue(serverResponse.isChunked());
+    Assert.assertEquals("type", response.getHeader(HttpHeaders.CONTENT_TYPE));
+    Assert.assertEquals("attachment;filename=name", response.getHeader(HttpHeaders.CONTENT_DISPOSITION));
+  }
+
+  @Test
+  public void prepareSendPartHeader_notUpdate(@Mocked Part part) {
+    headers.add(HttpHeaders.CONTENT_LENGTH, "10");
+    headers.add(HttpHeaders.CONTENT_TYPE, "type");
+    headers.add(HttpHeaders.CONTENT_DISPOSITION, "disposition");
+
+    response.prepareSendPartHeader(part);
+
+    Assert.assertFalse(serverResponse.isChunked());
+    Assert.assertEquals("type", response.getHeader(HttpHeaders.CONTENT_TYPE));
+    Assert.assertEquals("disposition", response.getHeader(HttpHeaders.CONTENT_DISPOSITION));
+  }
+
+  @Test
+  public void sendPart_openInputStreamFailed(@Mocked Part part)
+      throws IOException, InterruptedException, ExecutionException {
+    IOException ioException = new IOException("forbid open stream");
+    new Expectations() {
+      {
+        part.getInputStream();
+        result = ioException;
+      }
+    };
+
+    CompletableFuture<Void> future = response.sendPart(part);
+
+    expectedException.expect(ExecutionException.class);
+    expectedException.expectCause(Matchers.sameInstance(ioException));
+
+    future.get();
+  }
+
+  @Test
+  public void sendPart_inputStreamBreak(@Mocked Part part, @Mocked InputStream inputStream)
+      throws IOException, InterruptedException, ExecutionException {
+    IOException ioException = new IOException("forbid read");
+    new Expectations() {
+      {
+        part.getInputStream();
+        result = inputStream;
+        inputStream.read((byte[]) any);
+        result = ioException;
+      }
+    };
+
+    CompletableFuture<Void> future = response.sendPart(part);
+
+    expectedException.expect(ExecutionException.class);
+    expectedException.expectCause(Matchers.sameInstance(ioException));
+
+    future.get();
+  }
+
+  @Test
+  public void sendPart_succ(@Mocked Part part, @Mocked InputStream inputStream)
+      throws IOException, InterruptedException, ExecutionException {
+    new Expectations() {
+      {
+        part.getInputStream();
+        result = inputStream;
+        inputStream.read((byte[]) any);
+        result = -1;
+      }
+    };
+
+    CompletableFuture<Void> future = response.sendPart(part);
+
+    Assert.assertNull(future.get());
+  }
 }

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

[incubator-servicecomb-java-chassis] 06/09: SCB-483 add demo for download, now only finished download server, so now can only disable signature and https, and use browser to test download

Posted by li...@apache.org.
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 2cd41283709f9214ed73b237d6cad766c7979d63
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Apr 18 00:23:40 2018 +0800

    SCB-483 add demo for download, now only finished download server, so now can only disable signature and https, and use browser to test download
---
 .../demo/springmvc/server/DownloadSchema.java      | 79 ++++++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DownloadSchema.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DownloadSchema.java
new file mode 100644
index 0000000..0fbd6ee
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DownloadSchema.java
@@ -0,0 +1,79 @@
+/*
+ * 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.servicecomb.demo.springmvc.server;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.core.io.Resource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
+
+@RestSchema(schemaId = "download")
+@RequestMapping(path = "/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
+public class DownloadSchema {
+  @GetMapping(path = "/file")
+  public File downloadFile() throws IOException {
+    return new File(this.getClass().getClassLoader().getResource("microservice.yaml").getFile());
+  }
+
+  @GetMapping(path = "/resource")
+  @ApiResponses({
+      @ApiResponse(code = 200, response = File.class, message = ""),
+  })
+  public Resource downloadResource() throws IOException {
+    return new ByteArrayResource("abc".getBytes(StandardCharsets.UTF_8)) {
+      @Override
+      public String getFilename() {
+        return "abc.txt";
+      }
+    };
+  }
+
+  @GetMapping(path = "/entityResource")
+  @ApiResponses({
+      @ApiResponse(code = 200, response = File.class, message = ""),
+  })
+  public ResponseEntity<Resource> downloadEntityResource() throws IOException {
+    return ResponseEntity
+        .ok()
+        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=entityResource.txt")
+        .body(new ByteArrayResource("entityResource".getBytes(StandardCharsets.UTF_8)));
+  }
+
+  @GetMapping(path = "/entityInputStream")
+  @ApiResponses({
+      @ApiResponse(code = 200, response = File.class, message = ""),
+  })
+  public ResponseEntity<InputStream> downloadEntityInputStream() throws IOException {
+    return ResponseEntity
+        .ok()
+        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=entityInputStream.txt")
+        .body(new ByteArrayInputStream("entityInputStream".getBytes(StandardCharsets.UTF_8)));
+  }
+}

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

[incubator-servicecomb-java-chassis] 02/09: SCB-483 read file faster

Posted by li...@apache.org.
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 84949890643c26dc03b6b8f200f911c8b0922781
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Apr 17 23:47:51 2018 +0800

    SCB-483 read file faster
---
 .../servicecomb/foundation/vertx/stream/InputStreamToReadStream.java    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/InputStreamToReadStream.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/InputStreamToReadStream.java
index 2b94af4..2066dfe 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/InputStreamToReadStream.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/InputStreamToReadStream.java
@@ -34,7 +34,7 @@ import io.vertx.core.streams.ReadStream;
 public class InputStreamToReadStream implements ReadStream<Buffer> {
   private static final Logger LOGGER = LoggerFactory.getLogger(InputStreamToReadStream.class);
 
-  public static final int DEFAULT_READ_BUFFER_SIZE = 8192;
+  public static final int DEFAULT_READ_BUFFER_SIZE = 1024 * 1024;
 
   private Vertx vertx;
 

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

[incubator-servicecomb-java-chassis] 07/09: SCB-483 delete useless code

Posted by li...@apache.org.
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 89e3668ccb38bf5666fd19ad6bbc9b0324384491
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Apr 18 00:24:09 2018 +0800

    SCB-483 delete useless code
---
 .../apache/servicecomb/metrics/core/DefaultRegistryInitializer.java   | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java
index 64650de..5e81ee1 100644
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java
@@ -26,10 +26,6 @@ import com.netflix.spectator.api.Registry;
 import com.netflix.spectator.servo.ServoRegistry;
 
 public class DefaultRegistryInitializer implements MetricsInitializer {
-  public static final String METRICS_WINDOW_TIME = "servicecomb.metrics.window_time";
-
-  public static final int DEFAULT_METRICS_WINDOW_TIME = 5000;
-
   public static final String SERVO_POLLERS = "servo.pollers";
 
   private CompositeRegistry globalRegistry;

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

[incubator-servicecomb-java-chassis] 08/09: SCB-483 delete unnecessary type cast

Posted by li...@apache.org.
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 5ea3718cceee1d9d6b01e66abd4170fe2ba201a6
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Apr 18 00:25:25 2018 +0800

    SCB-483 delete unnecessary type cast
---
 .../org/apache/servicecomb/common/javassist/TestJavassistUtils.java   | 4 ++--
 .../apache/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java  | 2 +-
 .../servicecomb/common/rest/definition/path/PathVarParamWriter.java   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/common/common-javassist/src/test/java/org/apache/servicecomb/common/javassist/TestJavassistUtils.java b/common/common-javassist/src/test/java/org/apache/servicecomb/common/javassist/TestJavassistUtils.java
index 73c4c7f..a69eb0f 100644
--- a/common/common-javassist/src/test/java/org/apache/servicecomb/common/javassist/TestJavassistUtils.java
+++ b/common/common-javassist/src/test/java/org/apache/servicecomb/common/javassist/TestJavassistUtils.java
@@ -152,7 +152,7 @@ public class TestJavassistUtils {
   public void multiWrapper() throws Exception {
     ClassConfig classConfig = new ClassConfig();
     classConfig.setClassName("cse.ut.multi.Wrapper");
-    classConfig.addField("intField", (Type) int.class);
+    classConfig.addField("intField", int.class);
     classConfig.addField("strField", String.class);
 
     JavassistUtils.genMultiWrapperInterface(classConfig);
@@ -161,7 +161,7 @@ public class TestJavassistUtils {
 
     MultiWrapper instance = (MultiWrapper) wrapperClass.newInstance();
     instance.writeFields(new Object[] {100, "test"});
-    Object[] fieldValues = (Object[]) instance.readFields();
+    Object[] fieldValues = instance.readFields();
     Assert.assertEquals(100, fieldValues[0]);
     Assert.assertEquals("test", fieldValues[1]);
   }
diff --git a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java
index f1c985f..8fd1e89 100644
--- a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java
+++ b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java
@@ -178,7 +178,7 @@ public final class ProtobufSchemaUtils {
     return getOrCreateSchema(type, () -> {
       if (!isArgsNeedWrap(method)) {
         // 可以直接使用
-        Class<?> cls = (Class<?>) method.getParameterTypes()[0];
+        Class<?> cls = method.getParameterTypes()[0];
         Schema<?> schema = RuntimeSchema.createFrom(cls);
         return WrapSchemaFactory.createSchema(schema, WrapType.ARGS_NOT_WRAP);
       }
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/PathVarParamWriter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/PathVarParamWriter.java
index c38b038..95659e8 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/PathVarParamWriter.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/PathVarParamWriter.java
@@ -29,6 +29,6 @@ public class PathVarParamWriter extends AbstractUrlParamWriter {
 
   @Override
   public void write(StringBuilder builder, Object[] args) throws Exception {
-    builder.append((Object) getParamValue(args));
+    builder.append(getParamValue(args));
   }
 }

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

[incubator-servicecomb-java-chassis] 09/09: SCB-483 change asyncBeforeSendResponse to beforeSendResponseAsync

Posted by li...@apache.org.
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 719a079592fa3dabd1f251599462df88ef7f68e5
Author: wujimin <wu...@huawei.com>
AuthorDate: Thu Apr 19 14:29:16 2018 +0800

    SCB-483 change asyncBeforeSendResponse to beforeSendResponseAsync
---
 .../org/apache/servicecomb/common/rest/filter/HttpServerFilter.java   | 2 +-
 .../rest/filter/HttpServerFilterBeforeSendResponseExecutor.java       | 2 +-
 .../servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java    | 2 +-
 .../apache/servicecomb/common/rest/filter/TestHttpServerFilter.java   | 4 ++--
 .../rest/filter/TestHttpServerFilterBeforeSendResponseExecutor.java   | 2 +-
 .../common/rest/filter/inner/TestServerRestArgsFilter.java            | 2 +-
 6 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java
index 594c262..8e56e99 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilter.java
@@ -41,7 +41,7 @@ public interface HttpServerFilter {
   /**
    * @param invocation maybe null
    */
-  default CompletableFuture<Void> asyncBeforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+  default CompletableFuture<Void> beforeSendResponseAsync(Invocation invocation, HttpServletResponseEx responseEx) {
     CompletableFuture<Void> future = new CompletableFuture<>();
     try {
       beforeSendResponse(invocation, responseEx);
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBeforeSendResponseExecutor.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBeforeSendResponseExecutor.java
index 70889cc..b07485a 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBeforeSendResponseExecutor.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/HttpServerFilterBeforeSendResponseExecutor.java
@@ -48,7 +48,7 @@ public class HttpServerFilterBeforeSendResponseExecutor {
 
   protected CompletableFuture<Void> safeInvoke(HttpServerFilter httpServerFilter) {
     try {
-      return httpServerFilter.asyncBeforeSendResponse(invocation, responseEx);
+      return httpServerFilter.beforeSendResponseAsync(invocation, responseEx);
     } catch (Throwable e) {
       CompletableFuture<Void> eFuture = new CompletableFuture<Void>();
       eFuture.completeExceptionally(e);
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
index f740568..c806fc0 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
@@ -54,7 +54,7 @@ public class ServerRestArgsFilter implements HttpServerFilter {
   }
 
   @Override
-  public CompletableFuture<Void> asyncBeforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+  public CompletableFuture<Void> beforeSendResponseAsync(Invocation invocation, HttpServletResponseEx responseEx) {
     Response response = (Response) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_RESPONSE);
     ProduceProcessor produceProcessor =
         (ProduceProcessor) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_PROCESSOR);
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilter.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilter.java
index d7b0d01..38c1f97 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilter.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilter.java
@@ -35,7 +35,7 @@ public class TestHttpServerFilter {
   public void asyncSucc() throws InterruptedException, ExecutionException {
     HttpServerFilter filter = new HttpServerFilterBaseForTest();
 
-    CompletableFuture<Void> future = filter.asyncBeforeSendResponse(null, null);
+    CompletableFuture<Void> future = filter.beforeSendResponseAsync(null, null);
     Assert.assertNull(future.get());
   }
 
@@ -51,7 +51,7 @@ public class TestHttpServerFilter {
     expectedException.expect(ExecutionException.class);
     expectedException.expectCause(Matchers.instanceOf(Error.class));
 
-    CompletableFuture<Void> future = filter.asyncBeforeSendResponse(null, null);
+    CompletableFuture<Void> future = filter.beforeSendResponseAsync(null, null);
     future.get();
   }
 }
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilterBeforeSendResponseExecutor.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilterBeforeSendResponseExecutor.java
index ba565ab..0708f8f 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilterBeforeSendResponseExecutor.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/TestHttpServerFilterBeforeSendResponseExecutor.java
@@ -63,7 +63,7 @@ public class TestHttpServerFilterBeforeSendResponseExecutor {
   public void runFail() throws InterruptedException, ExecutionException {
     httpServerFilters.add(new HttpServerFilterBaseForTest() {
       @Override
-      public CompletableFuture<Void> asyncBeforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+      public CompletableFuture<Void> beforeSendResponseAsync(Invocation invocation, HttpServletResponseEx responseEx) {
         throw new Error("");
       }
     });
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/TestServerRestArgsFilter.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/TestServerRestArgsFilter.java
index 9f8ed6c..8a322e0 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/TestServerRestArgsFilter.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/TestServerRestArgsFilter.java
@@ -67,7 +67,7 @@ public class TestServerRestArgsFilter {
       }
     };
 
-    Assert.assertNull(filter.asyncBeforeSendResponse(invocation, responseEx));
+    Assert.assertNull(filter.beforeSendResponseAsync(invocation, responseEx));
     Assert.assertTrue(invokedSendPart);
   }
 }

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

[incubator-servicecomb-java-chassis] 05/09: SCB-483 set correct content-type for download

Posted by li...@apache.org.
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 8679a91508190986b50f9d39d2e229d8bbd8eb87
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Apr 18 15:24:35 2018 +0800

    SCB-483 set correct content-type for download
---
 .../apache/servicecomb/common/rest/AbstractRestInvocation.java |  1 -
 .../common/rest/filter/inner/ServerRestArgsFilter.java         |  2 ++
 .../servicecomb/common/rest/TestAbstractRestInvocation.java    | 10 +++-------
 3 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
index 89be43e..9a3769b 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
@@ -212,7 +212,6 @@ public abstract class AbstractRestInvocation {
       }
     }
     responseEx.setStatus(response.getStatusCode(), response.getReasonPhrase());
-    responseEx.setContentType(produceProcessor.getName() + "; charset=utf-8");
     responseEx.setAttribute(RestConst.INVOCATION_HANDLER_RESPONSE, response);
     responseEx.setAttribute(RestConst.INVOCATION_HANDLER_PROCESSOR, produceProcessor);
 
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
index f33e0af..f740568 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
@@ -67,6 +67,8 @@ public class ServerRestArgsFilter implements HttpServerFilter {
       return responseEx.sendPart((Part) body);
     }
 
+    responseEx.setContentType(produceProcessor.getName() + "; charset=utf-8");
+
     CompletableFuture<Void> future = new CompletableFuture<Void>();
     try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
       produceProcessor.encodeResponse(output, body);
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
index 6d82bc6..811ea47 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
@@ -445,7 +445,7 @@ public class TestAbstractRestInvocation {
         response.getReasonPhrase();
         result = "reason";
         response.getResult();
-        result = new Error("stop");
+        result = "result";
       }
     };
 
@@ -482,12 +482,8 @@ public class TestAbstractRestInvocation {
 
     initRestInvocation();
 
-    try {
-      restInvocation.sendResponse(response);
-      Assert.fail("must throw exception");
-    } catch (Error e) {
-      Assert.assertEquals(expected, result);
-    }
+    restInvocation.sendResponse(response);
+    Assert.assertEquals(expected, result);
   }
 
   @Test

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

[incubator-servicecomb-java-chassis] 04/09: SCB-483 ServerRestArgsFilter support sendPart

Posted by li...@apache.org.
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 34897c428e679acda74c5e77c4cec411a7f70cad
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Apr 18 00:15:59 2018 +0800

    SCB-483 ServerRestArgsFilter support sendPart
---
 .../rest/filter/inner/ServerRestArgsFilter.java    | 16 ++++-
 .../filter/inner/TestServerRestArgsFilter.java     | 73 ++++++++++++++++++++++
 2 files changed, 86 insertions(+), 3 deletions(-)

diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
index b690cbf..f33e0af 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/filter/inner/ServerRestArgsFilter.java
@@ -17,6 +17,10 @@
 
 package org.apache.servicecomb.common.rest.filter.inner;
 
+import java.util.concurrent.CompletableFuture;
+
+import javax.servlet.http.Part;
+
 import org.apache.servicecomb.common.rest.RestConst;
 import org.apache.servicecomb.common.rest.codec.RestCodec;
 import org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor;
@@ -50,7 +54,7 @@ public class ServerRestArgsFilter implements HttpServerFilter {
   }
 
   @Override
-  public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
+  public CompletableFuture<Void> asyncBeforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
     Response response = (Response) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_RESPONSE);
     ProduceProcessor produceProcessor =
         (ProduceProcessor) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_PROCESSOR);
@@ -59,13 +63,19 @@ public class ServerRestArgsFilter implements HttpServerFilter {
       body = ((InvocationException) body).getErrorData();
     }
 
+    if (Part.class.isInstance(body)) {
+      return responseEx.sendPart((Part) body);
+    }
+
+    CompletableFuture<Void> future = new CompletableFuture<Void>();
     try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
       produceProcessor.encodeResponse(output, body);
 
       responseEx.setBodyBuffer(output.getBuffer());
+      future.complete(null);
     } catch (Throwable e) {
-      throw ExceptionFactory.convertProducerException(e);
+      future.completeExceptionally(ExceptionFactory.convertProducerException(e));
     }
+    return future;
   }
-
 }
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/TestServerRestArgsFilter.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/TestServerRestArgsFilter.java
new file mode 100644
index 0000000..9f8ed6c
--- /dev/null
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/TestServerRestArgsFilter.java
@@ -0,0 +1,73 @@
+/*
+ * 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.servicecomb.common.rest.filter.inner;
+
+import java.util.concurrent.CompletableFuture;
+
+import javax.servlet.http.Part;
+
+import org.apache.servicecomb.common.rest.RestConst;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.junit.Assert;
+import org.junit.Test;
+
+import mockit.Expectations;
+import mockit.Mock;
+import mockit.MockUp;
+import mockit.Mocked;
+
+public class TestServerRestArgsFilter {
+  @Mocked
+  Invocation invocation;
+
+  @Mocked
+  HttpServletResponseEx responseEx;
+
+  @Mocked
+  Response response;
+
+  @Mocked
+  Part part;
+
+  boolean invokedSendPart;
+
+  ServerRestArgsFilter filter = new ServerRestArgsFilter();
+
+  @Test
+  public void asyncBeforeSendResponse_part() {
+    new Expectations() {
+      {
+        responseEx.getAttribute(RestConst.INVOCATION_HANDLER_RESPONSE);
+        result = response;
+        response.getResult();
+        result = part;
+      }
+    };
+    new MockUp<HttpServletResponseEx>(responseEx) {
+      @Mock
+      CompletableFuture<Void> sendPart(Part body) {
+        invokedSendPart = true;
+        return null;
+      }
+    };
+
+    Assert.assertNull(filter.asyncBeforeSendResponse(invocation, responseEx));
+    Assert.assertTrue(invokedSendPart);
+  }
+}

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