Skip to content

Commit

Permalink
Merge branch 'DSpace:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
msevigny authored Jan 12, 2024
2 parents 7a91607 + 7e8a7be commit 4194095
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ private boolean performCheck(Context context, String query) throws SQLException
return true;
}
} catch (SearchServiceException e) {
log.error("Failed getting getting community/collection admin status for "
log.error("Failed getting community/collection admin status for "
+ context.getCurrentUser().getEmail() + " The search error is: " + e.getMessage()
+ " The search resourceType filter was: " + query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 @@ -167,6 +168,12 @@ public ResponseEntity retrieve(@PathVariable UUID uuid, HttpServletResponse resp
//Send the data
if (httpHeadersInitializer.isValid()) {
HttpHeaders httpHeaders = httpHeadersInitializer.initialiseHeaders();

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 @@ -206,6 +206,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 @@ -232,7 +244,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

0 comments on commit 4194095

Please sign in to comment.