use of org.briarproject.bramble.api.mailbox.InvalidMailboxIdException in project briar by briar.
the class MailboxApiImpl method getFiles.
@Override
public List<MailboxFile> getFiles(MailboxProperties properties, MailboxFolderId 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());
ArrayNode filesNode = getArray(node, "files");
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 (time < 1)
throw new ApiException();
list.add(new MailboxFile(MailboxFileId.fromString(name), time));
}
Collections.sort(list);
return list;
} catch (JacksonException | InvalidMailboxIdException e) {
throw new ApiException();
}
}
use of org.briarproject.bramble.api.mailbox.InvalidMailboxIdException in project briar by briar.
the class MailboxApiImpl method getFolders.
@Override
public List<MailboxFolderId> getFolders(MailboxProperties properties) throws IOException, ApiException {
if (!properties.isOwner())
throw new IllegalArgumentException();
Response response = sendGetRequest(properties, "/folders");
if (response.code() != 200)
throw new ApiException();
ResponseBody body = response.body();
if (body == null)
throw new ApiException();
try {
JsonNode node = mapper.readTree(body.string());
ArrayNode filesNode = getArray(node, "folders");
List<MailboxFolderId> list = new ArrayList<>();
for (JsonNode fileNode : filesNode) {
if (!fileNode.isObject())
throw new ApiException();
ObjectNode objectNode = (ObjectNode) fileNode;
JsonNode idNode = objectNode.get("id");
if (idNode == null || !idNode.isTextual()) {
throw new ApiException();
}
String id = idNode.asText();
list.add(MailboxFolderId.fromString(id));
}
return list;
} catch (JacksonException | InvalidMailboxIdException e) {
throw new ApiException();
}
}
use of org.briarproject.bramble.api.mailbox.InvalidMailboxIdException in project briar by briar.
the class MailboxApiImpl method setup.
@Override
public MailboxAuthToken setup(MailboxProperties properties) throws IOException, ApiException {
if (!properties.isOwner())
throw new IllegalArgumentException();
Request request = getRequestBuilder(properties.getAuthToken()).url(properties.getBaseUrl() + "/setup").put(EMPTY_REQUEST).build();
OkHttpClient client = httpClientProvider.get();
Response response = client.newCall(request).execute();
if (response.code() == 401)
throw new MailboxAlreadyPairedException();
if (!response.isSuccessful())
throw new ApiException();
ResponseBody body = response.body();
if (body == null)
throw new ApiException();
try {
JsonNode node = mapper.readTree(body.string());
JsonNode tokenNode = node.get("token");
if (tokenNode == null) {
throw new ApiException();
}
String ownerToken = tokenNode.textValue();
return MailboxAuthToken.fromString(ownerToken);
} catch (JacksonException | InvalidMailboxIdException e) {
throw new ApiException();
}
}
use of org.briarproject.bramble.api.mailbox.InvalidMailboxIdException in project briar by briar.
the class MailboxSettingsManagerImpl method getOwnMailboxProperties.
@Override
public MailboxProperties getOwnMailboxProperties(Transaction txn) throws DbException {
Settings s = settingsManager.getSettings(txn, SETTINGS_NAMESPACE);
String onion = s.get(SETTINGS_KEY_ONION);
String token = s.get(SETTINGS_KEY_TOKEN);
if (isNullOrEmpty(onion) || isNullOrEmpty(token))
return null;
try {
MailboxAuthToken tokenId = MailboxAuthToken.fromString(token);
return new MailboxProperties(onion, tokenId, true);
} catch (InvalidMailboxIdException e) {
throw new DbException(e);
}
}
Aggregations