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 06:05:39 UTC
[servicecomb-java-chassis] branch 2.8.x updated: [#3850] Fix the problem when set content-type by ResponseEntity.header() (#3856)
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch 2.8.x
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/2.8.x by this push:
new 6712e96fd [#3850] Fix the problem when set content-type by ResponseEntity.header() (#3856)
6712e96fd is described below
commit 6712e96fd03b4425eda6a60ccc5dc6735758b602
Author: nek0R1n <37...@users.noreply.github.com>
AuthorDate: Mon Jul 17 14:05:33 2023 +0800
[#3850] Fix the problem when set content-type by ResponseEntity.header() (#3856)
(cherry picked from commit c57f5cf4e19fb1b6573feed48e78da80b8165799)
---
.../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) {