You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by wu...@apache.org on 2018/06/08 12:54:26 UTC
[incubator-servicecomb-java-chassis] 04/05: [SCB-484] servlet
response support send part
This is an automated email from the ASF dual-hosted git repository.
wujimin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit ee04049d7024e10698ffc51b48dce84e2008d001
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Jun 5 12:50:40 2018 +0800
[SCB-484] servlet response support send part
---
.../vertx/http/StandardHttpServletResponseEx.java | 21 +++++++++-
.../http/TestStandardHttpServletResponseEx.java | 45 ++++++++++++++++++++--
2 files changed, 61 insertions(+), 5 deletions(-)
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 13c0ba7..2050601 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
@@ -18,6 +18,7 @@
package org.apache.servicecomb.foundation.vertx.http;
import java.io.IOException;
+import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@@ -28,7 +29,10 @@ import javax.servlet.http.Part;
import javax.ws.rs.core.Response.StatusType;
import org.apache.servicecomb.foundation.common.http.HttpStatus;
+import org.apache.servicecomb.foundation.vertx.stream.PumpFromPart;
+import io.vertx.core.Context;
+import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
public class StandardHttpServletResponseEx extends HttpServletResponseWrapper implements HttpServletResponseEx {
@@ -99,7 +103,20 @@ public class StandardHttpServletResponseEx extends HttpServletResponseWrapper im
}
@Override
- public CompletableFuture<Void> sendPart(Part body) {
- throw new Error("not supported method");
+ public CompletableFuture<Void> sendPart(Part part) {
+ DownloadUtils.prepareDownloadHeader(this, part);
+
+ OutputStream outputStream;
+ try {
+ outputStream = getOutputStream();
+ } catch (IOException e) {
+ CompletableFuture<Void> future = new CompletableFuture<>();
+ future.completeExceptionally(e);
+ return future;
+ }
+
+ // if context is null, then will switch to sync logic
+ Context context = Vertx.currentContext();
+ return new PumpFromPart(context, part).toOutputStream(outputStream, false);
}
}
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 535bd8f..ac68119 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
@@ -17,12 +17,17 @@
package org.apache.servicecomb.foundation.vertx.http;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.Part;
+import org.apache.commons.lang.RandomStringUtils;
+import org.apache.servicecomb.foundation.common.part.InputStreamPart;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
@@ -31,6 +36,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import io.vertx.core.buffer.Buffer;
+import mockit.Expectations;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
@@ -132,9 +138,42 @@ public class TestStandardHttpServletResponseEx {
}
@Test
- public void sendPart() {
- setExceptionExpected();
+ public void sendPart_succ() throws Throwable {
+ String src = RandomStringUtils.random(100);
+ InputStream inputStream = new ByteArrayInputStream(src.getBytes());
+ Part part = new InputStreamPart("name", inputStream);
+ Buffer buffer = Buffer.buffer();
+ ServletOutputStream outputStream = new MockUp<ServletOutputStream>() {
+ @Mock
+ void write(int b) {
+ buffer.appendByte((byte) b);
+ }
+ }.getMockInstance();
+
+ new Expectations() {
+ {
+ response.getOutputStream();
+ result = outputStream;
+ }
+ };
+
+ responseEx.sendPart(part).get();
+
+ Assert.assertEquals(src, buffer.toString());
+ }
+
+ @Test
+ public void sendPart_failed(@Mocked Part part) throws Throwable {
+ Error error = new Error();
+ new Expectations() {
+ {
+ response.getOutputStream();
+ result = error;
+ }
+ };
+
+ expectedException.expect(Matchers.sameInstance(error));
- responseEx.sendPart(null);
+ responseEx.sendPart(part).get();
}
}
--
To stop receiving notification emails like this one, please contact
wujimin@apache.org.