Merge pull request #8221 from atmire/w2p-88773-Issue_8023_Bitstream_download_filename_lose_non-latin_characters

Bitstream download filename lose non-latin characters
This commit is contained in:
Tim Donohue
2022-03-25 14:21:58 -05:00
committed by GitHub
2 changed files with 51 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ package org.dspace.app.rest.utils;
import static java.util.Objects.isNull; import static java.util.Objects.isNull;
import static java.util.Objects.nonNull; import static java.util.Objects.nonNull;
import static javax.mail.internet.MimeUtility.encodeText;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@@ -163,7 +164,8 @@ public class HttpHeadersInitializer {
} }
httpHeaders.put(CONTENT_DISPOSITION, Collections.singletonList(String.format(CONTENT_DISPOSITION_FORMAT, httpHeaders.put(CONTENT_DISPOSITION, Collections.singletonList(String.format(CONTENT_DISPOSITION_FORMAT,
disposition, fileName))); disposition,
encodeText(fileName))));
log.debug("Content-Disposition : {}", disposition); log.debug("Content-Disposition : {}", disposition);
// Content phase // Content phase

View File

@@ -8,6 +8,7 @@
package org.dspace.app.rest; package org.dspace.app.rest;
import static java.util.UUID.randomUUID; 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.codec.CharEncoding.UTF_8;
import static org.apache.commons.collections.CollectionUtils.isEmpty; import static org.apache.commons.collections.CollectionUtils.isEmpty;
import static org.apache.commons.io.IOUtils.toInputStream; import static org.apache.commons.io.IOUtils.toInputStream;
@@ -293,6 +294,53 @@ public class BitstreamRestControllerIT extends AbstractControllerIntegrationTest
checkNumberOfStatsRecords(bitstream, 0); 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 @Test
public void testBitstreamNotFound() throws Exception { public void testBitstreamNotFound() throws Exception {
getClient().perform(get("/api/core/bitstreams/" + UUID.randomUUID() + "/content")) getClient().perform(get("/api/core/bitstreams/" + UUID.randomUUID() + "/content"))