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
The following commit(s) were added to refs/heads/master by this push: new cd87c96 [SCB-529] producer download file from byte[] cd87c96 is described below commit cd87c96310795e1e56a69a2d23cbfe3b2f65b28f Author: wujimin <wuji...@huawei.com> AuthorDate: Sat May 5 11:07:14 2018 +0800 [SCB-529] producer download file from byte[] --- .../demo/springmvc/client/DownloadSchemaIntf.java | 2 + .../demo/springmvc/client/TestDownload.java | 3 ++ .../demo/springmvc/server/DownloadSchema.java | 12 ++++++ .../converter/impl/part/BytesToPartConverter.java | 47 ++++++++++++++++++++++ .../impl/part/TestBytesToPartConverter.java | 34 +++++++++------- 5 files changed, 84 insertions(+), 14 deletions(-) diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/DownloadSchemaIntf.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/DownloadSchemaIntf.java index 48b9a0f..93e67ee 100644 --- a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/DownloadSchemaIntf.java +++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/DownloadSchemaIntf.java @@ -33,5 +33,7 @@ public interface DownloadSchemaIntf { ReadStreamPart entityInputStream(String content); + ReadStreamPart bytes(String content); + ReadStreamPart netInputStream(String content); } diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownload.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownload.java index 6472171..c782b68 100644 --- a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownload.java +++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownload.java @@ -117,6 +117,9 @@ public class TestDownload { futures.add(checkFile(intf.entityInputStream(content))); futures.add(checkFuture(templateGet("entityInputStream").saveAsString())); + futures.add(checkFile(intf.bytes(content))); + futures.add(checkFuture(templateGet("bytes").saveAsString())); + futures.add(checkFile(intf.netInputStream(content))); futures.add(checkFuture(templateGet("netInputStream").saveAsString())); 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 index c6124d2..5631663 100644 --- 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 @@ -157,6 +157,18 @@ public class DownloadSchema { .body(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))); } + @GetMapping(path = "/bytes") + @ApiResponses({ + @ApiResponse(code = 200, response = File.class, message = ""), + }) + public ResponseEntity<byte[]> bytes(String content) throws IOException { + return ResponseEntity + .ok() + .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=bytes.txt") + .body(content.getBytes(StandardCharsets.UTF_8)); + } + @GetMapping(path = "/netInputStream") @ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/converter/impl/part/BytesToPartConverter.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/converter/impl/part/BytesToPartConverter.java new file mode 100644 index 0000000..4426504 --- /dev/null +++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/converter/impl/part/BytesToPartConverter.java @@ -0,0 +1,47 @@ +/* + * 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.swagger.invocation.converter.impl.part; + +import java.io.ByteArrayInputStream; +import java.lang.reflect.Type; + +import javax.servlet.http.Part; + +import org.apache.servicecomb.foundation.common.part.InputStreamPart; +import org.apache.servicecomb.swagger.invocation.converter.CustomizedConverter; +import org.springframework.stereotype.Component; + +@Component +public class BytesToPartConverter implements CustomizedConverter { + @Override + public Type getSrcType() { + return byte[].class; + } + + @Override + public Type getTargetType() { + return Part.class; + } + + @Override + public Object convert(Object value) { + // not set name, because not easy to get parameter name in this place + // org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl not depend on the name + return new InputStreamPart(null, new ByteArrayInputStream((byte[]) value)); + } +} diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/DownloadSchemaIntf.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/converter/impl/part/TestBytesToPartConverter.java similarity index 54% copy from demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/DownloadSchemaIntf.java copy to swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/converter/impl/part/TestBytesToPartConverter.java index 48b9a0f..19fcfa2 100644 --- a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/DownloadSchemaIntf.java +++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/converter/impl/part/TestBytesToPartConverter.java @@ -14,24 +14,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.servicecomb.demo.springmvc.client; +package org.apache.servicecomb.swagger.invocation.converter.impl.part; -import org.apache.servicecomb.foundation.vertx.http.ReadStreamPart; +import javax.servlet.http.Part; -public interface DownloadSchemaIntf { - ReadStreamPart tempFileEntity(String content); +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; - ReadStreamPart tempFilePart(String content); +public class TestBytesToPartConverter { + BytesToPartConverter converter = new BytesToPartConverter(); - ReadStreamPart file(String content); + @Test + public void getSrcType() { + Assert.assertSame(byte[].class, converter.getSrcType()); + } - ReadStreamPart chineseAndSpaceFile(String content); + @Test + public void getTargetType() { + Assert.assertEquals(Part.class.getName(), converter.getTargetType().getTypeName()); + } - ReadStreamPart resource(String content); - - ReadStreamPart entityResource(String content); - - ReadStreamPart entityInputStream(String content); - - ReadStreamPart netInputStream(String content); + @Test + public void convert() { + Object part = converter.convert(new byte[] {}); + Assert.assertThat(part, Matchers.instanceOf(Part.class)); + } } -- To stop receiving notification emails like this one, please contact liu...@apache.org.