Search in sources :

Example 1 with InvalidMailboxIdException

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();
    }
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ResponseBody(okhttp3.ResponseBody) InvalidMailboxIdException(org.briarproject.bramble.api.mailbox.InvalidMailboxIdException) Response(okhttp3.Response) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with InvalidMailboxIdException

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();
    }
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ResponseBody(okhttp3.ResponseBody) MailboxFolderId(org.briarproject.bramble.api.mailbox.MailboxFolderId) InvalidMailboxIdException(org.briarproject.bramble.api.mailbox.InvalidMailboxIdException) Response(okhttp3.Response) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 3 with InvalidMailboxIdException

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();
    }
}
Also used : Response(okhttp3.Response) JacksonException(com.fasterxml.jackson.core.JacksonException) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JsonNode(com.fasterxml.jackson.databind.JsonNode) ResponseBody(okhttp3.ResponseBody) InvalidMailboxIdException(org.briarproject.bramble.api.mailbox.InvalidMailboxIdException)

Example 4 with InvalidMailboxIdException

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);
    }
}
Also used : MailboxProperties(org.briarproject.bramble.api.mailbox.MailboxProperties) MailboxAuthToken(org.briarproject.bramble.api.mailbox.MailboxAuthToken) Settings(org.briarproject.bramble.api.settings.Settings) InvalidMailboxIdException(org.briarproject.bramble.api.mailbox.InvalidMailboxIdException) DbException(org.briarproject.bramble.api.db.DbException)

Aggregations

InvalidMailboxIdException (org.briarproject.bramble.api.mailbox.InvalidMailboxIdException)4 JacksonException (com.fasterxml.jackson.core.JacksonException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Response (okhttp3.Response)3 ResponseBody (okhttp3.ResponseBody)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ArrayList (java.util.ArrayList)2 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 DbException (org.briarproject.bramble.api.db.DbException)1 MailboxAuthToken (org.briarproject.bramble.api.mailbox.MailboxAuthToken)1 MailboxFolderId (org.briarproject.bramble.api.mailbox.MailboxFolderId)1 MailboxProperties (org.briarproject.bramble.api.mailbox.MailboxProperties)1 Settings (org.briarproject.bramble.api.settings.Settings)1