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 2023/07/17 00:54:09 UTC

[servicecomb-java-chassis] branch master updated: [#3850] Fix the problem when set content-type by ResponseEntity.header() (#3852)

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/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 9bfb89681 [#3850] Fix the problem when set content-type by ResponseEntity.header() (#3852)
9bfb89681 is described below

commit 9bfb896812f47c667048090c0aec3c207d162fba
Author: nek0R1n <37...@users.noreply.github.com>
AuthorDate: Mon Jul 17 08:54:05 2023 +0800

    [#3850] Fix the problem when set content-type by ResponseEntity.header() (#3852)
---
 .../demo/springmvc/client/TestDownloadSchema.java         | 15 +++++++++++++++
 .../servicecomb/demo/springmvc/server/DownloadSchema.java | 11 +++++++++++
 .../servicecomb/foundation/vertx/http/DownloadUtils.java  |  6 +++++-
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownloadSchema.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownloadSchema.java
index 37a24116a..40214c1f5 100644
--- a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownloadSchema.java
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDownloadSchema.java
@@ -21,9 +21,13 @@ import org.apache.servicecomb.demo.CategorizedTestCase;
 import org.apache.servicecomb.demo.TestMgr;
 import org.apache.servicecomb.foundation.vertx.http.ReadStreamPart;
 import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
 import org.springframework.web.client.RestTemplate;
 
+import java.util.Collections;
+
 @Component
 public class TestDownloadSchema implements CategorizedTestCase {
   @Override
@@ -31,6 +35,7 @@ public class TestDownloadSchema implements CategorizedTestCase {
     testDownloadFileAndDeleted();
     testDownloadFileNotDeleted();
     testDownloadFileWithNull();
+    testSetContentTypeByResponseEntity();
   }
 
   private void testDownloadFileAndDeleted() throws Exception {
@@ -69,4 +74,14 @@ public class TestDownloadSchema implements CategorizedTestCase {
         .getForObject("servicecomb://springmvc/download/assertLastFileDeleted", boolean.class);
     TestMgr.check(exists, true);
   }
+
+  private void testSetContentTypeByResponseEntity() throws Exception {
+    RestTemplate restTemplate = RestTemplateBuilder.create();
+    ResponseEntity<ReadStreamPart> responseEntity = restTemplate
+            .getForEntity("servicecomb://springmvc/download/setContentTypeByResponseEntity?content=hello&contentType=customType",
+                    ReadStreamPart.class);
+    String hello = responseEntity.getBody().saveAsString().get();
+    TestMgr.check(responseEntity.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections.singletonList("customType"));
+    TestMgr.check(hello, "hello");
+  }
 }
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 c75775f24..8eed21c19 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
@@ -92,6 +92,17 @@ public class DownloadSchema {
         .body(new FilePart(null, file));
   }
 
+  @GetMapping(path = "/setContentTypeByResponseEntity")
+  public ResponseEntity<Part> setContentTypeByResponseEntity(@RequestParam("content") String content, @RequestParam("contentType") String contentType) throws IOException {
+    File file = createTempFile(content);
+
+    return ResponseEntity
+            .ok()
+            .header(HttpHeaders.CONTENT_TYPE, contentType)
+            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=tempFileEntity.txt")
+            .body(new FilePart(null, file));
+  }
+
   @GetMapping(path = "/assertLastFileDeleted")
   public boolean assertLastFileDeleted() {
     return lastFile.exists();
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/DownloadUtils.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/DownloadUtils.java
index f37712e34..e83e65e64 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/DownloadUtils.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/DownloadUtils.java
@@ -44,7 +44,11 @@ public final class DownloadUtils {
       return;
     }
     if (responseEx.getHeader(HttpHeaders.CONTENT_TYPE.toString()) == null) {
-      responseEx.setHeader(HttpHeaders.CONTENT_TYPE.toString(), part.getContentType());
+      if (responseEx.getContentType() != null) {
+        responseEx.setHeader(HttpHeaders.CONTENT_TYPE.toString(), responseEx.getContentType());
+      } else {
+        responseEx.setHeader(HttpHeaders.CONTENT_TYPE.toString(), part.getContentType());
+      }
     }
 
     if (responseEx.getHeader(javax.ws.rs.core.HttpHeaders.CONTENT_DISPOSITION) == null) {