diff --git a/src/main/java/org/whispersystems/textsecuregcm/federation/FederatedClient.java b/src/main/java/org/whispersystems/textsecuregcm/federation/FederatedClient.java index e12947e7f..b8909c2dd 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/federation/FederatedClient.java +++ b/src/main/java/org/whispersystems/textsecuregcm/federation/FederatedClient.java @@ -35,11 +35,7 @@ import org.whispersystems.textsecuregcm.entities.AccountCount; import org.whispersystems.textsecuregcm.entities.AttachmentUri; import org.whispersystems.textsecuregcm.entities.ClientContact; import org.whispersystems.textsecuregcm.entities.ClientContacts; -import org.whispersystems.textsecuregcm.entities.IncomingMessage; import org.whispersystems.textsecuregcm.entities.IncomingMessageList; -import org.whispersystems.textsecuregcm.entities.MessageResponse; -import org.whispersystems.textsecuregcm.entities.PreKey; -import org.whispersystems.textsecuregcm.entities.RelayMessage; import org.whispersystems.textsecuregcm.entities.UnstructuredPreKeyList; import org.whispersystems.textsecuregcm.util.Base64; @@ -69,7 +65,6 @@ public class FederatedClient { private static final String USER_COUNT_PATH = "/v1/federation/user_count"; private static final String USER_TOKENS_PATH = "/v1/federation/user_tokens/%d"; private static final String RELAY_MESSAGE_PATH = "/v1/federation/messages/%s/%s"; - private static final String PREKEY_PATH = "/v1/federation/key/%s"; private static final String PREKEY_PATH_DEVICE = "/v1/federation/key/%s/%s"; private static final String ATTACHMENT_URI_PATH = "/v1/federation/attachment/%d"; @@ -96,34 +91,36 @@ public class FederatedClient { WebResource resource = client.resource(peer.getUrl()) .path(String.format(ATTACHMENT_URI_PATH, attachmentId)); - return resource.accept(MediaType.APPLICATION_JSON) - .header("Authorization", authorizationHeader) - .get(AttachmentUri.class) - .getLocation(); + ClientResponse response = resource.accept(MediaType.APPLICATION_JSON) + .header("Authorization", authorizationHeader) + .get(ClientResponse.class); + + if (response.getStatus() < 200 || response.getStatus() >= 300) { + throw new WebApplicationException(clientResponseToResponse(response)); + } + + return response.getEntity(AttachmentUri.class).getLocation(); + } catch (UniformInterfaceException | ClientHandlerException e) { logger.warn("Bad URI", e); throw new IOException(e); } } - public Optional getKey(String destination) { - try { - WebResource resource = client.resource(peer.getUrl()).path(String.format(PREKEY_PATH, destination)); - return Optional.of(resource.accept(MediaType.APPLICATION_JSON) - .header("Authorization", authorizationHeader) - .get(PreKey.class)); - } catch (UniformInterfaceException | ClientHandlerException e) { - logger.warn("PreKey", e); - return Optional.absent(); - } - } - public Optional getKeys(String destination, String device) { try { WebResource resource = client.resource(peer.getUrl()).path(String.format(PREKEY_PATH_DEVICE, destination, device)); - return Optional.of(resource.accept(MediaType.APPLICATION_JSON) - .header("Authorization", authorizationHeader) - .get(UnstructuredPreKeyList.class)); + + ClientResponse response = resource.accept(MediaType.APPLICATION_JSON) + .header("Authorization", authorizationHeader) + .get(ClientResponse.class); + + if (response.getStatus() < 200 || response.getStatus() >= 300) { + throw new WebApplicationException(clientResponseToResponse(response)); + } + + return Optional.of(response.getEntity(UnstructuredPreKeyList.class)); + } catch (UniformInterfaceException | ClientHandlerException e) { logger.warn("PreKey", e); return Optional.absent();