Skip to content

Commit ed57b56

Browse files
Javadocs, refactors, anonUserName NullPointer fix
1 parent 3cd7ed5 commit ed57b56

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed

src/main/java/org/icatproject/topcat/IcatClient.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ public class DatafilesResponse {
2727
public final List<Long> ids = new ArrayList<>();
2828
public final Set<String> missing = new HashSet<>();
2929
public long totalSize = 0L;
30+
31+
/**
32+
* Submit a query for Datafiles, then appends the ids, increments the size, and
33+
* records any missing file locations.
34+
*
35+
* @param query Query to submit
36+
* @throws TopcatException If the query returns not authorized, or not found.
37+
*/
38+
public void submitDatafilesQuery(String query)
39+
throws TopcatException {
40+
JsonArray jsonArray = submitQuery(query);
41+
for (JsonObject jsonObject : jsonArray.getValuesAs(JsonObject.class)) {
42+
JsonObject datafile = jsonObject.getJsonObject("Datafile");
43+
ids.add(datafile.getJsonNumber("id").longValueExact());
44+
missing.remove(datafile.getString("location"));
45+
totalSize += datafile.getJsonNumber("fileSize").longValueExact();
46+
}
47+
}
3048
}
3149

3250
private Logger logger = LoggerFactory.getLogger(IcatClient.class);
@@ -185,13 +203,7 @@ public DatafilesResponse getDatafiles(List<String> files) throws TopcatException
185203
String quotedFile = "'" + file + "'";
186204
int encodedFileLength = URLEncoder.encode(quotedFile, "UTF8").length();
187205
if (chunkSize + 3 + encodedFileLength > chunkLimit) {
188-
JsonArray jsonArray = submitQuery(queryPrefix + chunkedFiles + querySuffix);
189-
for (JsonObject jsonObject : jsonArray.getValuesAs(JsonObject.class)) {
190-
JsonObject datafile = jsonObject.getJsonObject("Datafile");
191-
response.ids.add(datafile.getJsonNumber("id").longValueExact());
192-
response.missing.remove(datafile.getString("location"));
193-
response.totalSize += datafile.getJsonNumber("fileSize").longValueExact();
194-
}
206+
response.submitDatafilesQuery(queryPrefix + chunkedFiles + querySuffix);
195207

196208
chunkedFiles = quotedFile;
197209
chunkSize = encodedFileLength;
@@ -202,13 +214,7 @@ public DatafilesResponse getDatafiles(List<String> files) throws TopcatException
202214
response.missing.add(file);
203215
}
204216
}
205-
JsonArray jsonArray = submitQuery(queryPrefix + chunkedFiles + querySuffix);
206-
for (JsonObject jsonObject : jsonArray.getValuesAs(JsonObject.class)) {
207-
JsonObject datafile = jsonObject.getJsonObject("Datafile");
208-
response.ids.add(datafile.getJsonNumber("id").longValueExact());
209-
response.missing.remove(datafile.getString("location"));
210-
response.totalSize += datafile.getJsonNumber("fileSize").longValueExact();
211-
}
217+
response.submitDatafilesQuery(queryPrefix + chunkedFiles + querySuffix);
212218

213219
return response;
214220
}
@@ -238,13 +244,9 @@ public long getDatasetFileCount(long datasetId) throws TopcatException {
238244
* @throws TopcatException
239245
*/
240246
public long getDatasetFileSize(long datasetId) throws TopcatException {
241-
String query = "SELECT datafile.fileSize FROM Datafile datafile WHERE datafile.dataset.id = " + datasetId;
247+
String query = "SELECT SUM(datafile.fileSize) FROM Datafile datafile WHERE datafile.dataset.id = " + datasetId;
242248
JsonArray jsonArray = submitQuery(query);
243-
long size = 0L;
244-
for (JsonNumber number : jsonArray.getValuesAs(JsonNumber.class)) {
245-
size += number.longValueExact();
246-
}
247-
return size;
249+
return jsonArray.getJsonNumber(0).longValueExact();
248250
}
249251

250252
/**
@@ -276,6 +278,11 @@ private JsonArray submitQuery(String query) throws TopcatException {
276278
/**
277279
* Gets a single Entity of the specified type, without any other conditions.
278280
*
281+
* NOTE: This function is written and intended for getting Investigation,
282+
* Dataset or Datafile entities as part of the tests. It does not handle casing of
283+
* entities containing multiple words, or querying for a specific instance of an
284+
* entity.
285+
*
279286
* @param entityType Type of ICAT Entity to get
280287
* @return A single ICAT Entity of the specified type as a JsonObject
281288
* @throws TopcatException
@@ -429,7 +436,8 @@ public int getQueuePriority(String userName) throws TopcatException {
429436
}
430437
}
431438

432-
if (!userName.startsWith(Properties.getInstance().getProperty("anonUserName"))) {
439+
String anonUserName = Properties.getInstance().getProperty("anonUserName");
440+
if (anonUserName == null || !userName.startsWith(anonUserName)) {
433441
// The anonymous cart username will end with the user's sessionId so cannot do .equals
434442
return priorityMap.getAuthenticatedPriority();
435443
} else {

src/main/java/org/icatproject/topcat/PriorityMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public PriorityMap() {
3535
Properties properties = Properties.getInstance();
3636

3737
anonUserName = properties.getProperty("anonUserName", "");
38+
if (anonUserName.equals("")) {
39+
logger.warn("anonUserName not defined, cannot distinguish anonymous and authenticated users so "
40+
+ "authenticated priority will be used as default level");
41+
}
3842
anonDownloadEnabled = Boolean.parseBoolean(properties.getProperty("anonDownloadEnabled", "true"));
3943
String defaultString;
4044
if (anonDownloadEnabled) {

src/main/java/org/icatproject/topcat/web/rest/UserResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,8 @@ public Response queueAllowed(@QueryParam("sessionId") String sessionId,
983983
* Download. Defaults to facilityName_visitId.
984984
* @param email Optional email to notify upon completion
985985
* @param files ICAT Datafile.locations to download
986-
* @return Array of Download ids
986+
* @return The resultant downloadId and an array of any locations which could not
987+
* be found
987988
* @throws TopcatException
988989
* @throws UnsupportedEncodingException
989990
*/

src/test/java/org/icatproject/topcat/IcatClientTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.icatproject.topcat.httpclient.HttpClient;
1313
import org.icatproject.topcat.httpclient.Response;
1414
import org.icatproject.topcat.domain.*;
15+
import org.icatproject.topcat.exceptions.TopcatException;
16+
1517
import java.net.URLEncoder;
1618

1719
import org.icatproject.topcat.repository.CacheRepository;
@@ -238,6 +240,20 @@ public void testCheckUserFound() throws Exception {
238240
}
239241
}
240242

243+
@Test
244+
public void testGetDatasetFileCount() throws TopcatException {
245+
IcatClient icatClient = new IcatClient("https://localhost:8181", sessionId);
246+
long datasetId = icatClient.getEntity("Dataset").getJsonNumber("id").longValueExact();
247+
assertNotEquals(0, icatClient.getDatasetFileCount(datasetId));
248+
}
249+
250+
@Test
251+
public void testGetDatasetFileSize() throws TopcatException {
252+
IcatClient icatClient = new IcatClient("https://localhost:8181", sessionId);
253+
long datasetId = icatClient.getEntity("Dataset").getJsonNumber("id").longValueExact();
254+
assertNotEquals(0, icatClient.getDatasetFileSize(datasetId));
255+
}
256+
241257
/*
242258
* @Test public void testGetSize() throws Exception { IcatClient icatClient =
243259
* new IcatClient("https://localhost:8181", sessionId);

0 commit comments

Comments
 (0)