Add method for listing files from mailbox

This commit is contained in:
Torsten Grote
2022-01-21 11:31:01 -03:00
parent 173af62dec
commit 76599a8d04
3 changed files with 174 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ import org.briarproject.bramble.api.mailbox.MailboxProperties;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import javax.annotation.concurrent.Immutable;
@@ -68,6 +69,14 @@ interface MailboxApi {
void addFile(MailboxProperties properties, String folderId,
File file) throws IOException, ApiException;
/**
* Used by owner and contacts to list their files to retrieve.
* <p>
* Returns 200 OK with the list of files in JSON.
*/
List<MailboxFile> getFiles(MailboxProperties properties, String folderId)
throws IOException, ApiException;
@Immutable
@JsonSerialize
class MailboxContact {
@@ -85,6 +94,16 @@ interface MailboxApi {
}
}
class MailboxFile {
public final String name;
public final long time;
public MailboxFile(String name, long time) {
this.name = name;
this.time = time;
}
}
@Immutable
class ApiException extends Exception {
}

View File

@@ -3,6 +3,7 @@ package org.briarproject.bramble.mailbox;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.briarproject.bramble.api.WeakSingletonProvider;
import org.briarproject.bramble.api.contact.ContactId;
@@ -165,6 +166,46 @@ class MailboxApiImpl implements MailboxApi {
Response response = sendPostRequest(properties, path, body);
if (response.code() != 200) throw new ApiException();
}
@Override
public List<MailboxFile> getFiles(MailboxProperties properties,
String folderId) throws IOException, ApiException {
String path = "/files/" + folderId;
Response response = sendGetRequest(properties, path);
if (response.code() != 200) throw new ApiException();
ResponseBody body = response.body();
if (body == null) throw new ApiException();
try {
JsonNode node = mapper.readTree(body.string());
JsonNode filesNode = node.get("files");
if (filesNode == null || !filesNode.isArray()) {
throw new ApiException();
}
List<MailboxFile> list = new ArrayList<>();
for (JsonNode fileNode : filesNode) {
if (!fileNode.isObject()) throw new ApiException();
ObjectNode objectNode = (ObjectNode) fileNode;
JsonNode nameNode = objectNode.get("name");
JsonNode timeNode = objectNode.get("time");
if (nameNode == null || !nameNode.isTextual()) {
throw new ApiException();
}
if (timeNode == null || !timeNode.isNumber()) {
throw new ApiException();
}
String name = nameNode.asText();
long time = timeNode.asLong();
if (!isValidToken(name)) throw new ApiException();
if (time < 1) throw new ApiException();
list.add(new MailboxFile(name, time));
}
return list;
} catch (JacksonException e) {
throw new ApiException();
}
}
/* Helper Functions */
private Response sendGetRequest(MailboxProperties properties, String path)