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:28 UTC

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

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.