From 85bbcf3dc8fd6c0d4243bf32e1e25e042db87eb3 Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 18 Mar 2022 17:44:35 +0100 Subject: [PATCH] taskid 88773 #8023 Bitstream download filename lose non-latin characters --- .../rest/utils/HttpHeadersInitializer.java | 4 +- .../app/rest/BitstreamRestControllerIT.java | 48 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java index 22ae8ad3d9..ffe2acc2e0 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java @@ -9,6 +9,7 @@ package org.dspace.app.rest.utils; import static java.util.Objects.isNull; import static java.util.Objects.nonNull; +import static javax.mail.internet.MimeUtility.encodeText; import java.io.IOException; import java.util.Arrays; @@ -163,7 +164,8 @@ public class HttpHeadersInitializer { } httpHeaders.put(CONTENT_DISPOSITION, Collections.singletonList(String.format(CONTENT_DISPOSITION_FORMAT, - disposition, fileName))); + disposition, + encodeText(fileName)))); log.debug("Content-Disposition : {}", disposition); // Content phase diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/BitstreamRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/BitstreamRestControllerIT.java index f07aae876f..efb7d1776d 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/BitstreamRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/BitstreamRestControllerIT.java @@ -8,6 +8,7 @@ package org.dspace.app.rest; import static java.util.UUID.randomUUID; +import static javax.mail.internet.MimeUtility.encodeText; import static org.apache.commons.codec.CharEncoding.UTF_8; import static org.apache.commons.collections.CollectionUtils.isEmpty; import static org.apache.commons.io.IOUtils.toInputStream; @@ -293,6 +294,53 @@ public class BitstreamRestControllerIT extends AbstractControllerIntegrationTest checkNumberOfStatsRecords(bitstream, 0); } + @Test + public void testBitstreamName() throws Exception { + + context.turnOffAuthorisationSystem(); + + //** GIVEN ** + //1. A community-collection structure with one parent community and one collection + + parentCommunity = CommunityBuilder + .createCommunity(context) + .build(); + + Collection collection = CollectionBuilder + .createCollection(context, parentCommunity) + .build(); + + //2. A public item with a bitstream + + String bitstreamContent = "0123456789"; + String bitstreamName = "ภาษาไทย"; + + try (InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) { + + Item item = ItemBuilder + .createItem(context, collection) + .build(); + + bitstream = BitstreamBuilder + .createBitstream(context, item, is) + .withName(bitstreamName) + .build(); + } + + context.restoreAuthSystemState(); + + //** WHEN ** + //We download the bitstream + getClient().perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")) + //** THEN ** + .andExpect(status().isOk()) + //We expect the content disposition to have the encoded bitstream name + .andExpect(header().string( + "Content-Disposition", + "attachment;filename=\"" + encodeText(bitstreamName) + "\"" + )); + } + @Test public void testBitstreamNotFound() throws Exception { getClient().perform(get("/api/core/bitstreams/" + UUID.randomUUID() + "/content"))