Skip to content

Commit

Permalink
[#3850] Fix the problem when set content-type by ResponseEntity.heade…
Browse files Browse the repository at this point in the history
…r() (#3856)

(cherry picked from commit c57f5cf)
  • Loading branch information
Sayaka617 authored Jul 17, 2023
1 parent 45910ef commit 6712e96
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@
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
public void testRestTransport() throws Exception {
testDownloadFileAndDeleted();
testDownloadFileNotDeleted();
testDownloadFileWithNull();
testSetContentTypeByResponseEntity();
}

private void testDownloadFileAndDeleted() throws Exception {
Expand Down Expand Up @@ -69,4 +74,14 @@ private void testDownloadFileNotDeleted() throws Exception {
.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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public ResponseEntity<Part> notDeleteAfterFinished(@RequestParam("content") Stri
.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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public static void prepareDownloadHeader(HttpServletResponseEx responseEx, Part
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) {
Expand Down

0 comments on commit 6712e96

Please sign in to comment.