Introduce MailboxId sub-classes for even more type-safety

This commit is contained in:
Torsten Grote
2022-02-07 15:57:05 -03:00
parent 5c153aeb6c
commit 16b503dd7b
13 changed files with 194 additions and 104 deletions

View File

@@ -0,0 +1,8 @@
package org.briarproject.bramble.api.mailbox;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@NotNullByDefault
public class InvalidMailboxIdException extends Exception {
}

View File

@@ -0,0 +1,24 @@
package org.briarproject.bramble.api.mailbox;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe
@NotNullByDefault
public class MailboxAuthToken extends MailboxId {
public MailboxAuthToken(byte[] id) {
super(id);
}
/**
* Creates a {@link MailboxAuthToken} from the given string.
*
* @throws InvalidMailboxIdException if token is not valid.
*/
public static MailboxAuthToken fromString(@Nullable String token)
throws InvalidMailboxIdException {
return new MailboxAuthToken(bytesFromString(token));
}
}

View File

@@ -0,0 +1,24 @@
package org.briarproject.bramble.api.mailbox;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe
@NotNullByDefault
public class MailboxFileId extends MailboxId {
public MailboxFileId(byte[] id) {
super(id);
}
/**
* Creates a {@link MailboxFileId} from the given string.
*
* @throws IllegalArgumentException if token is not valid.
*/
public static MailboxFileId fromString(@Nullable String token)
throws InvalidMailboxIdException {
return new MailboxFileId(bytesFromString(token));
}
}

View File

@@ -0,0 +1,24 @@
package org.briarproject.bramble.api.mailbox;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe
@NotNullByDefault
public class MailboxFolderId extends MailboxId {
public MailboxFolderId(byte[] id) {
super(id);
}
/**
* Creates a {@link MailboxFolderId} from the given string.
*
* @throws IllegalArgumentException if token is not valid.
*/
public static MailboxFolderId fromString(@Nullable String token)
throws InvalidMailboxIdException {
return new MailboxFolderId(bytesFromString(token));
}
}

View File

@@ -7,6 +7,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.Locale;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
import static org.briarproject.bramble.util.StringUtils.fromHexString;
@@ -14,20 +15,26 @@ import static org.briarproject.bramble.util.StringUtils.toHexString;
@ThreadSafe
@NotNullByDefault
public class MailboxId extends UniqueId {
public MailboxId(byte[] id) {
public abstract class MailboxId extends UniqueId {
MailboxId(byte[] id) {
super(id);
}
/**
* Creates a {@link MailboxId} from the given string.
* Returns valid {@link MailboxId} bytes from the given string.
*
* @throws IllegalArgumentException if token is not valid.
* @throws InvalidMailboxIdException if token is not valid.
*/
public static MailboxId fromString(String token) {
if (token.length() != 64) throw new IllegalArgumentException();
return new MailboxId(fromHexString(token));
static byte[] bytesFromString(@Nullable String token)
throws InvalidMailboxIdException {
if (token == null || token.length() != 64) {
throw new InvalidMailboxIdException();
}
try {
return fromHexString(token);
} catch (IllegalArgumentException e) {
throw new InvalidMailboxIdException();
}
}
/**
@@ -39,9 +46,4 @@ public class MailboxId extends UniqueId {
public String toString() {
return toHexString(getBytes()).toLowerCase(Locale.US);
}
@Override
public boolean equals(Object o) {
return o instanceof MailboxId && super.equals(o);
}
}

View File

@@ -9,10 +9,10 @@ import javax.annotation.concurrent.Immutable;
public class MailboxProperties {
private final String onionAddress;
private final MailboxId authToken;
private final MailboxAuthToken authToken;
private final boolean owner;
public MailboxProperties(String onionAddress, MailboxId authToken,
public MailboxProperties(String onionAddress, MailboxAuthToken authToken,
boolean owner) {
this.onionAddress = onionAddress;
this.authToken = authToken;
@@ -23,7 +23,7 @@ public class MailboxProperties {
return onionAddress;
}
public MailboxId getAuthToken() {
public MailboxAuthToken getAuthToken() {
return authToken;
}

View File

@@ -16,7 +16,6 @@ import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorId;
import org.briarproject.bramble.api.identity.Identity;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.mailbox.MailboxId;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.sync.ClientId;
@@ -215,10 +214,6 @@ public class TestUtils {
getAgreementPublicKey(), verified);
}
public static MailboxId getMailboxId() {
return new MailboxId(getRandomId());
}
public static void writeBytes(File file, byte[] bytes)
throws IOException {
FileOutputStream outputStream = new FileOutputStream(file);