Skip to content

Commit

Permalink
Include Content-Length response header
Browse files Browse the repository at this point in the history
Motivation:

The 'Content-Length' response header is expected by many clients.  While
it is not possible to specify the content length for on-the-fly
compression, it is possible when sending an individual file.

Modification:

Update DataSelection to optionally return the content length.

Update the 'getData' Response to include the `Content-Length` header
when the request targets a single file that is not placed in a zip
archive.  If the content is dynamically generated (e.g., a zip file)
then no `Content-Length` header is provided.

Result:

IDS now includes the `Content-Length` response header when this is
possible

Signed-off-by: Paul Millar <paul.millar@desy.de>
  • Loading branch information
paulmillar committed Feb 12, 2024
1 parent 1e32fb4 commit d846a4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/main/java/org/icatproject/ids/DataSelection.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.OptionalLong;
import java.util.Set;

import jakarta.json.Json;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class DataSelection {
private Set<Long> emptyDatasets;
private boolean dsWanted;
private boolean dfWanted;
private long length;


public enum Returns {
Expand Down Expand Up @@ -135,6 +137,7 @@ private void resolveDatasetIds()
dsInfos.put(dsid, new DsInfoImpl(ds));
if (dfWanted) {
Datafile df = (Datafile) icat.get(userSessionId, "Datafile", dfid);
length += df.getFileSize();
String location = IdsBean.getLocation(dfid, df.getLocation());
dfInfos.add(
new DfInfoImpl(dfid, df.getName(), location, df.getCreateId(), df.getModId(), dsid));
Expand Down Expand Up @@ -315,4 +318,10 @@ public Set<Long> getEmptyDatasets() {
return emptyDatasets;
}

public OptionalLong getFileLength() {
if (!dfWanted || mustZip()) {
return OptionalLong.empty();
}
return OptionalLong.of(length);
}
}
12 changes: 9 additions & 3 deletions src/main/java/org/icatproject/ids/IdsBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.OptionalLong;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
Expand Down Expand Up @@ -50,6 +51,7 @@
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonGenerator;
import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.StreamingOutput;

Expand Down Expand Up @@ -859,6 +861,7 @@ public Response getData(String sessionId, String investigationIds, String datase
// Do it
Map<Long, DsInfo> dsInfos = dataSelection.getDsInfo();
Set<DfInfoImpl> dfInfos = dataSelection.getDfInfo();
var length = zip ? OptionalLong.empty() : dataSelection.getFileLength();

Lock lock = null;
try {
Expand Down Expand Up @@ -909,11 +912,14 @@ public Response getData(String sessionId, String investigationIds, String datase
}
}

return Response.status(offset == 0 ? HttpURLConnection.HTTP_OK : HttpURLConnection.HTTP_PARTIAL)
var response = Response.status(offset == 0 ? HttpURLConnection.HTTP_OK : HttpURLConnection.HTTP_PARTIAL)
.entity(new SO(dataSelection.getDsInfo(), dataSelection.getDfInfo(), offset, finalZip, compress, lock,
transferId, ip, start))
.header("Content-Disposition", "attachment; filename=\"" + name + "\"").header("Accept-Ranges", "bytes")
.build();
.header("Content-Disposition", "attachment; filename=\"" + name + "\"").header("Accept-Ranges", "bytes");
length.stream()
.map(l -> Math.max(0L, l - offset))
.forEach(l -> response.header(CONTENT_LENGTH, l));
return response.build();
} catch (AlreadyLockedException e) {
logger.debug("Could not acquire lock, getData failed");
throw new DataNotOnlineException("Data is busy");
Expand Down

0 comments on commit d846a4f

Please sign in to comment.