Use MailboxId instead of String for type-safety

This commit is contained in:
Torsten Grote
2022-01-24 13:50:58 -03:00
parent 61ea7ff8de
commit f057f0859b
10 changed files with 125 additions and 65 deletions

View File

@@ -3,6 +3,7 @@ package org.briarproject.bramble.mailbox;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.mailbox.MailboxId;
import org.briarproject.bramble.api.mailbox.MailboxProperties;
import java.io.File;
@@ -21,7 +22,7 @@ interface MailboxApi {
* @return the owner token
* @throws ApiException for 401 response.
*/
String setup(MailboxProperties properties)
MailboxId setup(MailboxProperties properties)
throws IOException, ApiException;
/**
@@ -66,7 +67,7 @@ interface MailboxApi {
* The owner can add files to the contacts' inboxes
* and the contacts can add files to their own outbox.
*/
void addFile(MailboxProperties properties, String folderId,
void addFile(MailboxProperties properties, MailboxId folderId,
File file) throws IOException, ApiException;
/**
@@ -74,7 +75,7 @@ interface MailboxApi {
* <p>
* Returns 200 OK with the list of files in JSON.
*/
List<MailboxFile> getFiles(MailboxProperties properties, String folderId)
List<MailboxFile> getFiles(MailboxProperties properties, MailboxId folderId)
throws IOException, ApiException;
/**
@@ -85,8 +86,8 @@ interface MailboxApi {
*
* @param file the empty file the response bytes will be written into.
*/
void getFile(MailboxProperties properties, String folderId,
String fileId, File file) throws IOException, ApiException;
void getFile(MailboxProperties properties, MailboxId folderId,
MailboxId fileId, File file) throws IOException, ApiException;
/**
* Used by owner and contacts to delete files.
@@ -96,8 +97,8 @@ interface MailboxApi {
* @throws TolerableFailureException on 404 response,
* because file was most likely deleted already.
*/
void deleteFile(MailboxProperties properties, String folderId,
String fileId)
void deleteFile(MailboxProperties properties, MailboxId folderId,
MailboxId fileId)
throws IOException, ApiException, TolerableFailureException;
/**
@@ -105,22 +106,22 @@ interface MailboxApi {
* for the owner to download.
*
* @return a list of folder names
* to be used with {@link #getFiles(MailboxProperties, String)}.
* to be used with {@link #getFiles(MailboxProperties, MailboxId)}.
* @throws IllegalArgumentException if used by non-owner.
*/
List<String> getFolders(MailboxProperties properties)
List<MailboxId> getFolders(MailboxProperties properties)
throws IOException, ApiException;
@Immutable
@JsonSerialize
class MailboxContact {
public final int contactId;
public final String token, inboxId, outboxId;
public final MailboxId token, inboxId, outboxId;
MailboxContact(ContactId contactId,
String token,
String inboxId,
String outboxId) {
MailboxId token,
MailboxId inboxId,
MailboxId outboxId) {
this.contactId = contactId.getInt();
this.token = token;
this.inboxId = inboxId;
@@ -128,11 +129,12 @@ interface MailboxApi {
}
}
@JsonSerialize
class MailboxFile {
public final String name;
public final MailboxId name;
public final long time;
public MailboxFile(String name, long time) {
public MailboxFile(MailboxId name, long time) {
this.name = name;
this.time = time;
}

View File

@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import org.briarproject.bramble.api.WeakSingletonProvider;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.mailbox.MailboxId;
import org.briarproject.bramble.api.mailbox.MailboxProperties;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@@ -50,7 +51,7 @@ class MailboxApiImpl implements MailboxApi {
}
@Override
public String setup(MailboxProperties properties)
public MailboxId setup(MailboxProperties properties)
throws IOException, ApiException {
if (!properties.isOwner()) throw new IllegalArgumentException();
Request request = getRequestBuilder(properties.getAuthToken())
@@ -74,12 +75,14 @@ class MailboxApiImpl implements MailboxApi {
if (ownerToken == null || !isValidToken(ownerToken)) {
throw new ApiException();
}
return ownerToken;
return MailboxId.fromString(ownerToken);
} catch (JacksonException e) {
throw new ApiException();
}
}
// TODO find a batter way to validate (regex?)
// that doesn't do hex string conversion twice
private boolean isValidToken(String token) {
if (token.length() != 64) return false;
try {
@@ -161,7 +164,7 @@ class MailboxApiImpl implements MailboxApi {
/* File Management (owner and contacts) */
@Override
public void addFile(MailboxProperties properties, String folderId,
public void addFile(MailboxProperties properties, MailboxId folderId,
File file) throws IOException, ApiException {
String path = "/files/" + folderId;
RequestBody body = RequestBody.create(FILE, file);
@@ -171,7 +174,7 @@ class MailboxApiImpl implements MailboxApi {
@Override
public List<MailboxFile> getFiles(MailboxProperties properties,
String folderId) throws IOException, ApiException {
MailboxId folderId) throws IOException, ApiException {
String path = "/files/" + folderId;
Response response = sendGetRequest(properties, path);
if (response.code() != 200) throw new ApiException();
@@ -200,7 +203,7 @@ class MailboxApiImpl implements MailboxApi {
long time = timeNode.asLong();
if (!isValidToken(name)) throw new ApiException();
if (time < 1) throw new ApiException();
list.add(new MailboxFile(name, time));
list.add(new MailboxFile(MailboxId.fromString(name), time));
}
return list;
} catch (JacksonException e) {
@@ -209,8 +212,8 @@ class MailboxApiImpl implements MailboxApi {
}
@Override
public void getFile(MailboxProperties properties, String folderId,
String fileId, File file) throws IOException, ApiException {
public void getFile(MailboxProperties properties, MailboxId folderId,
MailboxId fileId, File file) throws IOException, ApiException {
String path = "/files/" + folderId + "/" + fileId;
Response response = sendGetRequest(properties, path);
if (response.code() != 200) throw new ApiException();
@@ -222,8 +225,8 @@ class MailboxApiImpl implements MailboxApi {
}
@Override
public void deleteFile(MailboxProperties properties, String folderId,
String fileId)
public void deleteFile(MailboxProperties properties, MailboxId folderId,
MailboxId fileId)
throws IOException, ApiException, TolerableFailureException {
String path = "/files/" + folderId + "/" + fileId;
Request request = getRequestBuilder(properties.getAuthToken())
@@ -237,7 +240,7 @@ class MailboxApiImpl implements MailboxApi {
}
@Override
public List<String> getFolders(MailboxProperties properties)
public List<MailboxId> getFolders(MailboxProperties properties)
throws IOException, ApiException {
if (!properties.isOwner()) throw new IllegalArgumentException();
Response response = sendGetRequest(properties, "/folders");
@@ -251,7 +254,7 @@ class MailboxApiImpl implements MailboxApi {
if (filesNode == null || !filesNode.isArray()) {
throw new ApiException();
}
List<String> list = new ArrayList<>();
List<MailboxId> list = new ArrayList<>();
for (JsonNode fileNode : filesNode) {
if (!fileNode.isObject()) throw new ApiException();
ObjectNode objectNode = (ObjectNode) fileNode;
@@ -261,7 +264,7 @@ class MailboxApiImpl implements MailboxApi {
}
String id = idNode.asText();
if (!isValidToken(id)) throw new ApiException();
list.add(id);
list.add(MailboxId.fromString(id));
}
return list;
} catch (JacksonException e) {
@@ -290,7 +293,7 @@ class MailboxApiImpl implements MailboxApi {
return client.newCall(request).execute();
}
private Request.Builder getRequestBuilder(String token) {
private Request.Builder getRequestBuilder(MailboxId token) {
return new Request.Builder()
.addHeader("Authorization", "Bearer " + token);
}

View File

@@ -3,6 +3,7 @@ package org.briarproject.bramble.mailbox;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.mailbox.MailboxId;
import org.briarproject.bramble.api.mailbox.MailboxProperties;
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager;
import org.briarproject.bramble.api.mailbox.MailboxStatus;
@@ -43,7 +44,8 @@ class MailboxSettingsManagerImpl implements MailboxSettingsManager {
String onion = s.get(SETTINGS_KEY_ONION);
String token = s.get(SETTINGS_KEY_TOKEN);
if (isNullOrEmpty(onion) || isNullOrEmpty(token)) return null;
return new MailboxProperties(onion, token, true);
MailboxId tokenId = MailboxId.fromString(token);
return new MailboxProperties(onion, tokenId, true);
}
@Override
@@ -51,7 +53,7 @@ class MailboxSettingsManagerImpl implements MailboxSettingsManager {
throws DbException {
Settings s = new Settings();
s.put(SETTINGS_KEY_ONION, p.getOnionAddress());
s.put(SETTINGS_KEY_TOKEN, p.getAuthToken());
s.put(SETTINGS_KEY_TOKEN, p.getAuthToken().toString());
settingsManager.mergeSettings(txn, s, SETTINGS_NAMESPACE);
}