mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Rename PermanentFailureException to ApiException
This commit is contained in:
@@ -14,19 +14,19 @@ interface MailboxApi {
|
|||||||
*
|
*
|
||||||
* @param properties MailboxProperties with the setup token
|
* @param properties MailboxProperties with the setup token
|
||||||
* @return the owner token
|
* @return the owner token
|
||||||
* @throws PermanentFailureException for 401 response.
|
* @throws ApiException for 401 response.
|
||||||
*/
|
*/
|
||||||
String setup(MailboxProperties properties)
|
String setup(MailboxProperties properties)
|
||||||
throws IOException, PermanentFailureException;
|
throws IOException, ApiException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks the status of the mailbox.
|
* Checks the status of the mailbox.
|
||||||
*
|
*
|
||||||
* @return true if the status is OK, false otherwise.
|
* @return true if the status is OK, false otherwise.
|
||||||
* @throws PermanentFailureException for 401 response.
|
* @throws ApiException for 401 response.
|
||||||
*/
|
*/
|
||||||
boolean checkStatus(MailboxProperties properties)
|
boolean checkStatus(MailboxProperties properties)
|
||||||
throws IOException, PermanentFailureException;
|
throws IOException, ApiException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new contact to the mailbox.
|
* Adds a new contact to the mailbox.
|
||||||
@@ -35,7 +35,7 @@ interface MailboxApi {
|
|||||||
* (contact was already added).
|
* (contact was already added).
|
||||||
*/
|
*/
|
||||||
void addContact(MailboxProperties properties, MailboxContact contact)
|
void addContact(MailboxProperties properties, MailboxContact contact)
|
||||||
throws IOException, PermanentFailureException,
|
throws IOException, ApiException,
|
||||||
TolerableFailureException;
|
TolerableFailureException;
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
@@ -55,7 +55,7 @@ interface MailboxApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
class PermanentFailureException extends Exception {
|
class ApiException extends Exception {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class MailboxApiImpl implements MailboxApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String setup(MailboxProperties properties)
|
public String setup(MailboxProperties properties)
|
||||||
throws IOException, PermanentFailureException {
|
throws IOException, ApiException {
|
||||||
if (!properties.isOwner()) throw new IllegalArgumentException();
|
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||||
Request request = getRequestBuilder(properties.getAuthToken())
|
Request request = getRequestBuilder(properties.getAuthToken())
|
||||||
.url(properties.getOnionAddress() + "/setup")
|
.url(properties.getOnionAddress() + "/setup")
|
||||||
@@ -50,23 +50,23 @@ class MailboxApiImpl implements MailboxApi {
|
|||||||
OkHttpClient client = httpClientProvider.get();
|
OkHttpClient client = httpClientProvider.get();
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
// TODO consider throwing a special exception for the 401 case
|
// TODO consider throwing a special exception for the 401 case
|
||||||
if (response.code() == 401) throw new PermanentFailureException();
|
if (response.code() == 401) throw new ApiException();
|
||||||
if (!response.isSuccessful()) throw new PermanentFailureException();
|
if (!response.isSuccessful()) throw new ApiException();
|
||||||
ResponseBody body = response.body();
|
ResponseBody body = response.body();
|
||||||
if (body == null) throw new PermanentFailureException();
|
if (body == null) throw new ApiException();
|
||||||
try {
|
try {
|
||||||
JsonNode node = mapper.readTree(body.string());
|
JsonNode node = mapper.readTree(body.string());
|
||||||
JsonNode tokenNode = node.get("token");
|
JsonNode tokenNode = node.get("token");
|
||||||
if (tokenNode == null) {
|
if (tokenNode == null) {
|
||||||
throw new PermanentFailureException();
|
throw new ApiException();
|
||||||
}
|
}
|
||||||
String ownerToken = tokenNode.textValue();
|
String ownerToken = tokenNode.textValue();
|
||||||
if (ownerToken == null || !isValidToken(ownerToken)) {
|
if (ownerToken == null || !isValidToken(ownerToken)) {
|
||||||
throw new PermanentFailureException();
|
throw new ApiException();
|
||||||
}
|
}
|
||||||
return ownerToken;
|
return ownerToken;
|
||||||
} catch (JacksonException e) {
|
} catch (JacksonException e) {
|
||||||
throw new PermanentFailureException();
|
throw new ApiException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,20 +83,20 @@ class MailboxApiImpl implements MailboxApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean checkStatus(MailboxProperties properties)
|
public boolean checkStatus(MailboxProperties properties)
|
||||||
throws IOException, PermanentFailureException {
|
throws IOException, ApiException {
|
||||||
if (!properties.isOwner()) throw new IllegalArgumentException();
|
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||||
Request request = getRequestBuilder(properties.getAuthToken())
|
Request request = getRequestBuilder(properties.getAuthToken())
|
||||||
.url(properties.getOnionAddress() + "/status")
|
.url(properties.getOnionAddress() + "/status")
|
||||||
.build();
|
.build();
|
||||||
OkHttpClient client = httpClientProvider.get();
|
OkHttpClient client = httpClientProvider.get();
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
if (response.code() == 401) throw new PermanentFailureException();
|
if (response.code() == 401) throw new ApiException();
|
||||||
return response.isSuccessful();
|
return response.isSuccessful();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addContact(MailboxProperties properties, MailboxContact contact)
|
public void addContact(MailboxProperties properties, MailboxContact contact)
|
||||||
throws IOException, PermanentFailureException,
|
throws IOException, ApiException,
|
||||||
TolerableFailureException {
|
TolerableFailureException {
|
||||||
if (!properties.isOwner()) throw new IllegalArgumentException();
|
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||||
byte[] bodyBytes = mapper.writeValueAsBytes(contact);
|
byte[] bodyBytes = mapper.writeValueAsBytes(contact);
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package org.briarproject.bramble.mailbox;
|
|||||||
import org.briarproject.bramble.api.WeakSingletonProvider;
|
import org.briarproject.bramble.api.WeakSingletonProvider;
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||||
|
import org.briarproject.bramble.mailbox.MailboxApi.ApiException;
|
||||||
import org.briarproject.bramble.mailbox.MailboxApi.MailboxContact;
|
import org.briarproject.bramble.mailbox.MailboxApi.MailboxContact;
|
||||||
import org.briarproject.bramble.mailbox.MailboxApi.PermanentFailureException;
|
|
||||||
import org.briarproject.bramble.mailbox.MailboxApi.TolerableFailureException;
|
import org.briarproject.bramble.mailbox.MailboxApi.TolerableFailureException;
|
||||||
import org.briarproject.bramble.test.BrambleTestCase;
|
import org.briarproject.bramble.test.BrambleTestCase;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -85,50 +85,42 @@ public class MailboxApiTest extends BrambleTestCase {
|
|||||||
assertToken(request1, token);
|
assertToken(request1, token);
|
||||||
|
|
||||||
// empty body
|
// empty body
|
||||||
assertThrows(PermanentFailureException.class,
|
assertThrows(ApiException.class, () -> api.setup(properties));
|
||||||
() -> api.setup(properties));
|
|
||||||
RecordedRequest request2 = server.takeRequest();
|
RecordedRequest request2 = server.takeRequest();
|
||||||
assertEquals("/setup", request2.getPath());
|
assertEquals("/setup", request2.getPath());
|
||||||
assertEquals("PUT", request2.getMethod());
|
assertEquals("PUT", request2.getMethod());
|
||||||
assertToken(request2, token);
|
assertToken(request2, token);
|
||||||
|
|
||||||
// invalid response
|
// invalid response
|
||||||
assertThrows(PermanentFailureException.class,
|
assertThrows(ApiException.class, () -> api.setup(properties));
|
||||||
() -> api.setup(properties));
|
|
||||||
RecordedRequest request3 = server.takeRequest();
|
RecordedRequest request3 = server.takeRequest();
|
||||||
assertEquals("/setup", request3.getPath());
|
assertEquals("/setup", request3.getPath());
|
||||||
assertEquals("PUT", request3.getMethod());
|
assertEquals("PUT", request3.getMethod());
|
||||||
assertToken(request3, token);
|
assertToken(request3, token);
|
||||||
|
|
||||||
// 401 response
|
// 401 response
|
||||||
assertThrows(PermanentFailureException.class,
|
assertThrows(ApiException.class, () -> api.setup(properties2));
|
||||||
() -> api.setup(properties2));
|
|
||||||
RecordedRequest request4 = server.takeRequest();
|
RecordedRequest request4 = server.takeRequest();
|
||||||
assertEquals("/setup", request4.getPath());
|
assertEquals("/setup", request4.getPath());
|
||||||
assertEquals("PUT", request4.getMethod());
|
assertEquals("PUT", request4.getMethod());
|
||||||
assertToken(request4, token2);
|
assertToken(request4, token2);
|
||||||
|
|
||||||
// 500 response
|
// 500 response
|
||||||
assertThrows(PermanentFailureException.class,
|
assertThrows(ApiException.class, () -> api.setup(properties));
|
||||||
() -> api.setup(properties));
|
|
||||||
RecordedRequest request5 = server.takeRequest();
|
RecordedRequest request5 = server.takeRequest();
|
||||||
assertEquals("/setup", request5.getPath());
|
assertEquals("/setup", request5.getPath());
|
||||||
assertEquals("PUT", request5.getMethod());
|
assertEquals("PUT", request5.getMethod());
|
||||||
assertToken(request5, token);
|
assertToken(request5, token);
|
||||||
|
|
||||||
// invalid json dict token response
|
// invalid json dict token response
|
||||||
assertThrows(PermanentFailureException.class,
|
assertThrows(ApiException.class, () -> api.setup(properties));
|
||||||
() -> api.setup(properties)
|
|
||||||
);
|
|
||||||
RecordedRequest request6 = server.takeRequest();
|
RecordedRequest request6 = server.takeRequest();
|
||||||
assertEquals("/setup", request6.getPath());
|
assertEquals("/setup", request6.getPath());
|
||||||
assertEquals("PUT", request6.getMethod());
|
assertEquals("PUT", request6.getMethod());
|
||||||
assertToken(request6, token);
|
assertToken(request6, token);
|
||||||
|
|
||||||
// invalid non-hex string token response
|
// invalid non-hex string token response
|
||||||
assertThrows(PermanentFailureException.class,
|
assertThrows(ApiException.class, () -> api.setup(properties));
|
||||||
() -> api.setup(properties)
|
|
||||||
);
|
|
||||||
RecordedRequest request7 = server.takeRequest();
|
RecordedRequest request7 = server.takeRequest();
|
||||||
assertEquals("/setup", request7.getPath());
|
assertEquals("/setup", request7.getPath());
|
||||||
assertEquals("PUT", request7.getMethod());
|
assertEquals("PUT", request7.getMethod());
|
||||||
@@ -163,9 +155,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
|||||||
assertEquals("/status", request1.getPath());
|
assertEquals("/status", request1.getPath());
|
||||||
assertToken(request1, token);
|
assertToken(request1, token);
|
||||||
|
|
||||||
assertThrows(PermanentFailureException.class, () ->
|
assertThrows(ApiException.class, () -> api.checkStatus(properties2));
|
||||||
api.checkStatus(properties2)
|
|
||||||
);
|
|
||||||
RecordedRequest request2 = server.takeRequest();
|
RecordedRequest request2 = server.takeRequest();
|
||||||
assertEquals("/status", request2.getPath());
|
assertEquals("/status", request2.getPath());
|
||||||
assertToken(request2, token2);
|
assertToken(request2, token2);
|
||||||
|
|||||||
Reference in New Issue
Block a user