mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Add /status and /setup mailbox API call with tests
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package org.briarproject.bramble.mailbox;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
interface MailboxApi {
|
||||
|
||||
/**
|
||||
* Sets up the mailbox with the setup token.
|
||||
*
|
||||
* @param properties MailboxProperties with the setup token
|
||||
* @return the owner token
|
||||
* @throws PermanentFailureException for 401 response.
|
||||
*/
|
||||
String setup(MailboxProperties properties)
|
||||
throws IOException, PermanentFailureException;
|
||||
|
||||
/**
|
||||
* Checks the status of the mailbox.
|
||||
*
|
||||
* @return true if the status is OK, false otherwise.
|
||||
* @throws PermanentFailureException for 401 response.
|
||||
*/
|
||||
boolean checkStatus(MailboxProperties properties)
|
||||
throws IOException, PermanentFailureException;
|
||||
|
||||
@Immutable
|
||||
class MailboxProperties {
|
||||
final String baseUrl;
|
||||
final String token;
|
||||
final boolean isOwner;
|
||||
|
||||
MailboxProperties(String baseUrl, String token, boolean isOwner) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.token = token;
|
||||
this.isOwner = isOwner;
|
||||
}
|
||||
}
|
||||
|
||||
@Immutable
|
||||
class PermanentFailureException extends Exception {
|
||||
/**
|
||||
* If true, the failure is fatal and requires user attention.
|
||||
* The entire task queue will most likely need to stop.
|
||||
*/
|
||||
final boolean fatal;
|
||||
|
||||
PermanentFailureException(boolean fatal) {
|
||||
this.fatal = fatal;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package org.briarproject.bramble.mailbox;
|
||||
|
||||
import com.fasterxml.jackson.core.JacksonException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||
|
||||
import org.briarproject.bramble.api.WeakSingletonProvider;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
import static com.fasterxml.jackson.databind.MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES;
|
||||
import static okhttp3.internal.Util.EMPTY_REQUEST;
|
||||
|
||||
@NotNullByDefault
|
||||
class MailboxApiImpl implements MailboxApi {
|
||||
|
||||
private final WeakSingletonProvider<OkHttpClient> httpClientProvider;
|
||||
private final JsonMapper mapper = JsonMapper.builder()
|
||||
.enable(BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES)
|
||||
.build();
|
||||
|
||||
@Inject
|
||||
MailboxApiImpl(WeakSingletonProvider<OkHttpClient> httpClientProvider) {
|
||||
this.httpClientProvider = httpClientProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String setup(MailboxProperties properties)
|
||||
throws IOException, PermanentFailureException {
|
||||
if (!properties.isOwner) throw new IllegalArgumentException();
|
||||
Request request = getRequestBuilder(properties.token)
|
||||
.url(properties.baseUrl + "/setup")
|
||||
.put(EMPTY_REQUEST)
|
||||
.build();
|
||||
OkHttpClient client = httpClientProvider.get();
|
||||
Response response = client.newCall(request).execute();
|
||||
if (response.code() == 401) {
|
||||
throw new PermanentFailureException(true);
|
||||
}
|
||||
if (!response.isSuccessful()) throw new IOException();
|
||||
ResponseBody body = response.body();
|
||||
if (body == null) throw new PermanentFailureException(false);
|
||||
try {
|
||||
JsonNode node = mapper.readTree(body.string());
|
||||
JsonNode tokenNode = node.get("token");
|
||||
if (tokenNode == null) {
|
||||
throw new PermanentFailureException(false);
|
||||
}
|
||||
String ownerToken = tokenNode.textValue();
|
||||
if (ownerToken == null) {
|
||||
throw new PermanentFailureException(false);
|
||||
}
|
||||
return ownerToken;
|
||||
} catch (JacksonException e) {
|
||||
throw new PermanentFailureException(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkStatus(MailboxProperties properties)
|
||||
throws IOException, PermanentFailureException {
|
||||
if (!properties.isOwner) throw new IllegalArgumentException();
|
||||
Request request = getRequestBuilder(properties.token)
|
||||
.url(properties.baseUrl + "/status")
|
||||
.build();
|
||||
OkHttpClient client = httpClientProvider.get();
|
||||
Response response = client.newCall(request).execute();
|
||||
if (response.code() == 401) {
|
||||
throw new PermanentFailureException(true);
|
||||
}
|
||||
return response.isSuccessful();
|
||||
}
|
||||
|
||||
private Request.Builder getRequestBuilder(String token) {
|
||||
return new Request.Builder()
|
||||
.addHeader("Authorization", "Bearer " + token);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user