Add content-length to bitstream

This commit is contained in:
William Welling
2024-01-10 13:51:11 -06:00
parent 64ae49a29f
commit 6be7e4e370
3 changed files with 18 additions and 1 deletions

View File

@@ -138,6 +138,7 @@ public class BitstreamRestController {
.withBufferSize(BUFFER_SIZE) .withBufferSize(BUFFER_SIZE)
.withFileName(name) .withFileName(name)
.withChecksum(bit.getChecksum()) .withChecksum(bit.getChecksum())
.withLength(bit.getSizeBytes())
.withMimetype(mimetype) .withMimetype(mimetype)
.with(request) .with(request)
.with(response); .with(response);

View File

@@ -14,6 +14,7 @@ import static javax.mail.internet.MimeUtility.encodeText;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@@ -143,6 +144,9 @@ public class HttpHeadersInitializer {
if (checksum != null) { if (checksum != null) {
httpHeaders.put(ETAG, Collections.singletonList(checksum)); httpHeaders.put(ETAG, Collections.singletonList(checksum));
} }
if (Objects.nonNull((Long.valueOf(this.length)))) {
httpHeaders.put(HttpHeaders.CONTENT_LENGTH, Collections.singletonList(String.valueOf(this.length)));
}
httpHeaders.put(LAST_MODIFIED, Collections.singletonList(FastHttpDateFormat.formatDate(lastModified))); httpHeaders.put(LAST_MODIFIED, Collections.singletonList(FastHttpDateFormat.formatDate(lastModified)));
httpHeaders.put(EXPIRES, Collections.singletonList(FastHttpDateFormat.formatDate( httpHeaders.put(EXPIRES, Collections.singletonList(FastHttpDateFormat.formatDate(
System.currentTimeMillis() + DEFAULT_EXPIRE_TIME))); System.currentTimeMillis() + DEFAULT_EXPIRE_TIME)));

View File

@@ -206,6 +206,18 @@ public class BitstreamRestControllerIT extends AbstractControllerIntegrationTest
} }
context.restoreAuthSystemState(); context.restoreAuthSystemState();
//** WHEN **
// we want to know what we are downloading before we download it
getClient().perform(head("/api/core/bitstreams/" + bitstream.getID() + "/content"))
//** THEN **
.andExpect(status().isOk())
//The Content Length must match the full length
.andExpect(header().longValue("Content-Length", bitstreamContent.getBytes().length))
.andExpect(header().string("Content-Type", "text/plain;charset=UTF-8"))
.andExpect(header().string("ETag", "\"" + bitstream.getChecksum() + "\""))
.andExpect(content().bytes(new byte[] {}));
//** WHEN ** //** WHEN **
//We download the bitstream //We download the bitstream
getClient().perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")) getClient().perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content"))
@@ -232,7 +244,7 @@ public class BitstreamRestControllerIT extends AbstractControllerIntegrationTest
.andExpect(status().isNotModified()); .andExpect(status().isNotModified());
//The download and head request should also be logged as a statistics record //The download and head request should also be logged as a statistics record
checkNumberOfStatsRecords(bitstream, 2); checkNumberOfStatsRecords(bitstream, 3);
} }
@Test @Test