Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public ResponseEntity retrieve(@PathVariable UUID uuid, HttpServletResponse resp
.withBufferSize(BUFFER_SIZE)
.withFileName(name)
.withChecksum(bit.getChecksum())
.withLength(bit.getSizeBytes())
.withMimetype(mimetype)
.with(request)
.with(response);
Expand Down Expand Up @@ -189,6 +190,12 @@ public ResponseEntity retrieve(@PathVariable UUID uuid, HttpServletResponse resp
if (s3DirectDownload) {
return redirectToS3DownloadUrl(httpHeaders, name, bit.getInternalId());
}

if (RequestMethod.HEAD.name().equals(request.getMethod())) {
log.debug("HEAD request - no response body");
return ResponseEntity.ok().headers(httpHeaders).build();
}

return ResponseEntity.ok().headers(httpHeaders).body(bitstreamResource);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand All @@ -33,7 +34,6 @@ public class HttpHeadersInitializer {

protected final Logger log = LoggerFactory.getLogger(this.getClass());

private static final String METHOD_HEAD = "HEAD";
private static final String MULTIPART_BOUNDARY = "MULTIPART_BYTERANGES";
private static final String CONTENT_TYPE_MULTITYPE_WITH_BOUNDARY = "multipart/byteranges; boundary=" +
MULTIPART_BOUNDARY;
Expand Down Expand Up @@ -144,6 +144,9 @@ public HttpHeaders initialiseHeaders() throws IOException {
if (checksum != null) {
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(EXPIRES, Collections.singletonList(FastHttpDateFormat.formatDate(
System.currentTimeMillis() + DEFAULT_EXPIRE_TIME)));
Expand All @@ -165,16 +168,14 @@ public HttpHeaders initialiseHeaders() throws IOException {

httpHeaders.put(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS,
Collections.singletonList(HttpHeaders.ACCEPT_RANGES));
httpHeaders.put(CONTENT_DISPOSITION, Collections.singletonList(String.format(CONTENT_DISPOSITION_FORMAT,
disposition,
encodeText(fileName))));
log.debug("Content-Disposition : {}", disposition);

// Content phase
if (METHOD_HEAD.equals(request.getMethod())) {
log.debug("HEAD request - skipping content");
return null;
// distposition may be null here if contentType is null
if (!isNullOrEmpty(disposition)) {
httpHeaders.put(CONTENT_DISPOSITION, Collections.singletonList(String.format(CONTENT_DISPOSITION_FORMAT,
disposition,
encodeText(fileName))));
}
log.debug("Content-Disposition : {}", disposition);

return httpHeaders;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ public void retrieveFullBitstream() throws Exception {
}
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 **
//We download the bitstream
getClient().perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content"))
Expand All @@ -245,7 +257,7 @@ public void retrieveFullBitstream() throws Exception {
.andExpect(status().isNotModified());

//The download and head request should also be logged as a statistics record
checkNumberOfStatsRecords(bitstream, 2);
checkNumberOfStatsRecords(bitstream, 3);
}

@Test
Expand Down