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 03:59:25 UTC

[incubator-servicecomb-java-chassis] branch master updated (94f034a -> d9233ab)

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 94f034a  SCB-474 fix pr comment
     new e1c218d  SCB-494 file part support temp file feature
     new ba59c2e  SCB-494 delete temp file after download finished
     new d9233ab  SCB-494 add download temp file demo, now only finished download server, so now can only disable signature and https, and use browser to test download

The 3 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:
 .../demo/springmvc/server/DownloadSchema.java      | 44 +++++++++++++++++++++-
 .../foundation/common/part/AbstractPart.java       |  3 +-
 .../foundation/common/part/FilePart.java           | 24 +++++++++++-
 .../foundation/common/part/FilePartForSend.java    |  9 +++--
 .../foundation/common/part/TestFilePart.java       | 17 ++++++++-
 .../VertxServerResponseToHttpServletResponse.java  | 12 ++++++
 ...stVertxServerResponseToHttpServletResponse.java | 28 ++++++++++++++
 7 files changed, 128 insertions(+), 9 deletions(-)
 copy core/src/main/java/org/apache/servicecomb/core/ProducerProvider.java => foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePartForSend.java (80%)

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

[incubator-servicecomb-java-chassis] 01/03: SCB-494 file part support temp file feature

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 e1c218d71fda68e281513243ab2dfc533aec7a8f
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Apr 18 10:07:13 2018 +0800

    SCB-494 file part support temp file feature
---
 .../foundation/common/part/AbstractPart.java       |  3 +-
 .../foundation/common/part/FilePart.java           | 24 +++++++++++++-
 .../part/{FilePart.java => FilePartForSend.java}   | 37 +++-------------------
 .../foundation/common/part/TestFilePart.java       | 17 +++++++++-
 4 files changed, 45 insertions(+), 36 deletions(-)

diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/AbstractPart.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/AbstractPart.java
index 4027f8f..653d0f9 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/AbstractPart.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/AbstractPart.java
@@ -60,9 +60,10 @@ public class AbstractPart implements Part {
     return submittedFileName;
   }
 
-  public void setSubmittedFileName(String submittedFileName) {
+  public AbstractPart setSubmittedFileName(String submittedFileName) {
     this.submittedFileName = submittedFileName;
     updateContentType();
+    return this;
   }
 
   private void updateContentType() {
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java
index 2b2a4da..ef29047 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java
@@ -24,9 +24,11 @@ import java.io.InputStream;
 
 import org.apache.commons.io.FileUtils;
 
-public class FilePart extends AbstractPart {
+public class FilePart extends AbstractPart implements FilePartForSend {
   private File file;
 
+  private boolean deleteAfterFinished;
+
   public FilePart(String name, String file) {
     this(name, new File(file));
   }
@@ -51,4 +53,24 @@ public class FilePart extends AbstractPart {
   public void write(String fileName) throws IOException {
     FileUtils.copyFile(file, new File(fileName));
   }
+
+  @Override
+  public void delete() throws IOException {
+    file.delete();
+  }
+
+  @Override
+  public boolean isDeleteAfterFinished() {
+    return deleteAfterFinished;
+  }
+
+  public FilePart setDeleteAfterFinished(boolean deleteAfterFinished) {
+    this.deleteAfterFinished = deleteAfterFinished;
+    return this;
+  }
+
+  @Override
+  public String getAbsolutePath() {
+    return file.getAbsolutePath();
+  }
 }
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePartForSend.java
similarity index 53%
copy from foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java
copy to foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePartForSend.java
index 2b2a4da..f86e696 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePart.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/part/FilePartForSend.java
@@ -14,41 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.servicecomb.foundation.common.part;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-
-import org.apache.commons.io.FileUtils;
-
-public class FilePart extends AbstractPart {
-  private File file;
-
-  public FilePart(String name, String file) {
-    this(name, new File(file));
-  }
-
-  public FilePart(String name, File file) {
-    this.name = name;
-    this.file = file;
-    this.setSubmittedFileName(this.file.getName());
-  }
-
-  @Override
-  public InputStream getInputStream() throws IOException {
-    return new FileInputStream(file);
-  }
+import javax.servlet.http.Part;
 
-  @Override
-  public long getSize() {
-    return file.length();
-  }
+public interface FilePartForSend extends Part {
+  boolean isDeleteAfterFinished();
 
-  @Override
-  public void write(String fileName) throws IOException {
-    FileUtils.copyFile(file, new File(fileName));
-  }
+  String getAbsolutePath();
 }
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/part/TestFilePart.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/part/TestFilePart.java
index 45f912d..d06eda8 100644
--- a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/part/TestFilePart.java
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/part/TestFilePart.java
@@ -71,6 +71,21 @@ public class TestFilePart {
 
     part.write(destFile.getPath());
     Assert.assertEquals(content, FileUtils.readFileToString(destFile));
-    destFile.delete();
+
+    FilePart destPart = new FilePart(null, destFile);
+    destPart.delete();
+    Assert.assertFalse(destFile.exists());
+  }
+
+  @Test
+  public void deleteAfterFinished() {
+    Assert.assertFalse(part.isDeleteAfterFinished());
+
+    Assert.assertTrue(part.setDeleteAfterFinished(true).isDeleteAfterFinished());
+  }
+
+  @Test
+  public void getAbsolutePath() {
+    Assert.assertEquals(file.getAbsolutePath(), part.getAbsolutePath());
   }
 }

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

[incubator-servicecomb-java-chassis] 02/03: SCB-494 delete temp file after download finished

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 ba59c2e25d094ea2a27ab04fca98f9cfcb2f95a0
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Apr 18 10:08:12 2018 +0800

    SCB-494 delete temp file after download finished
---
 .../VertxServerResponseToHttpServletResponse.java  | 12 ++++++++++
 ...stVertxServerResponseToHttpServletResponse.java | 28 ++++++++++++++++++++++
 2 files changed, 40 insertions(+)

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 9f5db55..d467058 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
@@ -29,7 +29,10 @@ 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.common.part.FilePartForSend;
 import org.apache.servicecomb.foundation.vertx.stream.InputStreamToReadStream;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import io.vertx.core.Context;
 import io.vertx.core.Vertx;
@@ -37,6 +40,8 @@ import io.vertx.core.http.HttpServerResponse;
 import io.vertx.core.streams.Pump;
 
 public class VertxServerResponseToHttpServletResponse extends AbstractHttpServletResponse {
+  private static final Logger LOGGER = LoggerFactory.getLogger(VertxServerResponseToHttpServletResponse.class);
+
   private Context context;
 
   private HttpServerResponse serverResponse;
@@ -169,5 +174,12 @@ public class VertxServerResponseToHttpServletResponse extends AbstractHttpServle
 
   protected void clearPartResource(Part part, InputStream is) {
     IOUtils.closeQuietly(is);
+    if (FilePartForSend.class.isInstance(part) && ((FilePartForSend) part).isDeleteAfterFinished()) {
+      try {
+        part.delete();
+      } catch (IOException e) {
+        LOGGER.error("Failed to delete temp file: {}.", ((FilePartForSend) part).getAbsolutePath(), e);
+      }
+    }
   }
 }
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 c69d41c..fd6202c 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
@@ -17,8 +17,10 @@
 
 package org.apache.servicecomb.foundation.vertx.http;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 
@@ -26,7 +28,9 @@ import javax.servlet.http.Part;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response.StatusType;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.servicecomb.foundation.common.http.HttpStatus;
+import org.apache.servicecomb.foundation.common.part.FilePart;
 import org.hamcrest.Matchers;
 import org.junit.Assert;
 import org.junit.Before;
@@ -378,4 +382,28 @@ public class TestVertxServerResponseToHttpServletResponse {
 
     Assert.assertNull(future.get());
   }
+
+  @Test
+  public void clearPartResource_deleteFile() throws IOException {
+    File file = new File("target", UUID.randomUUID().toString() + ".txt");
+    FileUtils.write(file, "content");
+    FilePart part = new FilePart(null, file).setDeleteAfterFinished(true);
+
+    Assert.assertTrue(file.exists());
+    response.clearPartResource(part, part.getInputStream());
+    Assert.assertFalse(file.exists());
+  }
+
+  @Test
+  public void clearPartResource_notDeleteFile() throws IOException {
+    File file = new File("target", UUID.randomUUID().toString() + ".txt");
+    FileUtils.write(file, "content");
+    FilePart part = new FilePart(null, file);
+
+    Assert.assertTrue(file.exists());
+    response.clearPartResource(part, part.getInputStream());
+    Assert.assertTrue(file.exists());
+
+    file.delete();
+  }
 }

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

[incubator-servicecomb-java-chassis] 03/03: SCB-494 add download temp file demo, 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 d9233abae64c1bdfbb16b6dbf8f98e2b24b51376
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Apr 18 10:08:43 2018 +0800

    SCB-494 add download temp file demo, now only finished download server, so now can only disable signature and https, and use browser to test download
---
 .../demo/springmvc/server/DownloadSchema.java      | 44 +++++++++++++++++++++-
 1 file changed, 42 insertions(+), 2 deletions(-)

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 0fbd6ee..7125511 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
@@ -21,12 +21,16 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
+import java.util.UUID;
 
+import javax.servlet.http.Part;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.servicecomb.foundation.common.part.FilePart;
 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;
@@ -35,8 +39,44 @@ import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
 
 @RestSchema(schemaId = "download")
-@RequestMapping(path = "/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
+@RequestMapping(path = "/download")
 public class DownloadSchema {
+  File tempDir = new File("downloadTemp");
+
+  public DownloadSchema() throws IOException {
+    FileUtils.forceMkdir(tempDir);
+  }
+
+  // content is file name
+  protected File createTempFile() throws IOException {
+    String name = "download-" + UUID.randomUUID().toString() + ".txt";
+    File file = new File(tempDir, name);
+    FileUtils.write(file, name);
+    return file;
+  }
+
+  // customize HttpHeaders.CONTENT_DISPOSITION to be "attachment;filename=tempFileEntity.txt"
+  @GetMapping(path = "/tempFileEntity")
+  public ResponseEntity<Part> downloadTempFileEntity() throws IOException {
+    File file = createTempFile();
+
+    return ResponseEntity
+        .ok()
+        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=tempFileEntity.txt")
+        .body(new FilePart(null, file)
+            .setDeleteAfterFinished(true));
+  }
+
+  // generate HttpHeaders.CONTENT_DISPOSITION to be "attachment;filename=tempFilePart.txt" automatically
+  @GetMapping(path = "/tempFilePart")
+  public Part downloadTempFilePart() throws IOException {
+    File file = createTempFile();
+
+    return new FilePart(null, file)
+        .setDeleteAfterFinished(true)
+        .setSubmittedFileName("tempFilePart.txt");
+  }
+
   @GetMapping(path = "/file")
   public File downloadFile() throws IOException {
     return new File(this.getClass().getClassLoader().getResource("microservice.yaml").getFile());

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