Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Content-Length response header #156

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
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
Loading