Skip to content

Commit a61508d

Browse files
authored
Merge pull request #156 from paulmillar/development/add-content-length-for-single-files
Include Content-Length response header
2 parents 1e32fb4 + d846a4f commit a61508d

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/main/java/org/icatproject/ids/DataSelection.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.HashSet;
88
import java.util.List;
99
import java.util.Map;
10+
import java.util.OptionalLong;
1011
import java.util.Set;
1112

1213
import jakarta.json.Json;
@@ -53,6 +54,7 @@ public class DataSelection {
5354
private Set<Long> emptyDatasets;
5455
private boolean dsWanted;
5556
private boolean dfWanted;
57+
private long length;
5658

5759

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

321+
public OptionalLong getFileLength() {
322+
if (!dfWanted || mustZip()) {
323+
return OptionalLong.empty();
324+
}
325+
return OptionalLong.of(length);
326+
}
318327
}

src/main/java/org/icatproject/ids/IdsBean.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashSet;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.OptionalLong;
2324
import java.util.Set;
2425
import java.util.SortedMap;
2526
import java.util.SortedSet;
@@ -50,6 +51,7 @@
5051
import jakarta.json.JsonReader;
5152
import jakarta.json.JsonValue;
5253
import jakarta.json.stream.JsonGenerator;
54+
import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH;
5355
import jakarta.ws.rs.core.Response;
5456
import jakarta.ws.rs.core.StreamingOutput;
5557

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

863866
Lock lock = null;
864867
try {
@@ -909,11 +912,14 @@ public Response getData(String sessionId, String investigationIds, String datase
909912
}
910913
}
911914

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

0 commit comments

Comments
 (0)