Java Code Examples for org.whispersystems.signalservice.api.push.exceptions.PushNetworkException

The following examples show how to use org.whispersystems.signalservice.api.push.exceptions.PushNetworkException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public ProfileAndCredential retrieveProfile(SignalServiceAddress address,
                                            Optional<ProfileKey> profileKey,
                                            Optional<UnidentifiedAccess> unidentifiedAccess,
                                            SignalServiceProfile.RequestType requestType)
    throws NonSuccessfulResponseCodeException, PushNetworkException, VerificationFailedException
{
  Optional<UUID> uuid = address.getUuid();

  if (uuid.isPresent() && profileKey.isPresent()) {
    if (requestType == SignalServiceProfile.RequestType.PROFILE_AND_CREDENTIAL) {
      return socket.retrieveVersionedProfileAndCredential(uuid.get(), profileKey.get(), unidentifiedAccess);
    } else {
      return new ProfileAndCredential(socket.retrieveVersionedProfile(uuid.get(), profileKey.get(), unidentifiedAccess),
                                      SignalServiceProfile.RequestType.PROFILE,
                                      Optional.absent());
    }
  } else {
    return new ProfileAndCredential(socket.retrieveProfile(address, unidentifiedAccess),
                                    SignalServiceProfile.RequestType.PROFILE,
                                    Optional.absent());
  }
}
 
Example 2
public ProfileAndCredential retrieveVersionedProfileAndCredential(UUID target, ProfileKey profileKey, Optional<UnidentifiedAccess> unidentifiedAccess)
    throws NonSuccessfulResponseCodeException, PushNetworkException, VerificationFailedException
{
  ProfileKeyVersion                  profileKeyIdentifier = profileKey.getProfileKeyVersion(target);
  ProfileKeyCredentialRequestContext requestContext       = clientZkProfileOperations.createProfileKeyCredentialRequestContext(random, target, profileKey);
  ProfileKeyCredentialRequest        request              = requestContext.getRequest();

  String version           = profileKeyIdentifier.serialize();
  String credentialRequest = Hex.toStringCondensed(request.serialize());
  String subPath           = String.format("%s/%s/%s", target, version, credentialRequest);

  String response = makeServiceRequest(String.format(PROFILE_PATH, subPath), "GET", null, NO_HEADERS, unidentifiedAccess);

  try {
    SignalServiceProfile signalServiceProfile = JsonUtil.fromJson(response, SignalServiceProfile.class);

    ProfileKeyCredential profileKeyCredential = signalServiceProfile.getProfileKeyCredentialResponse() != null
                                              ? clientZkProfileOperations.receiveProfileKeyCredential(requestContext, signalServiceProfile.getProfileKeyCredentialResponse())
                                              : null;

    return new ProfileAndCredential(signalServiceProfile, SignalServiceProfile.RequestType.PROFILE_AND_CREDENTIAL, Optional.fromNullable(profileKeyCredential));
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }
}
 
Example 3
public SignalServiceProfile retrieveVersionedProfile(UUID target, ProfileKey profileKey, Optional<UnidentifiedAccess> unidentifiedAccess)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
  ProfileKeyVersion profileKeyIdentifier = profileKey.getProfileKeyVersion(target);

  String version = profileKeyIdentifier.serialize();
  String subPath = String.format("%s/%s", target, version);

  String response = makeServiceRequest(String.format(PROFILE_PATH, subPath), "GET", null, NO_HEADERS, unidentifiedAccess);

  try {
    return JsonUtil.fromJson(response, SignalServiceProfile.class);
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }
}
 
Example 4
private SignalServiceAttachmentPointer uploadAttachmentV2(SignalServiceAttachmentStream attachment, byte[] attachmentKey, PushAttachmentData attachmentData) throws NonSuccessfulResponseCodeException, PushNetworkException {
  AttachmentV2UploadAttributes       v2UploadAttributes = null;
  Optional<SignalServiceMessagePipe> localPipe          = pipe.get();

  if (localPipe.isPresent()) {
    Log.d(TAG, "Using pipe to retrieve attachment upload attributes...");
    try {
      v2UploadAttributes = localPipe.get().getAttachmentV2UploadAttributes();
    } catch (IOException e) {
      Log.w(TAG, "Failed to retrieve attachment upload attributes using pipe. Falling back...");
    }
  }

  if (v2UploadAttributes == null) {
    Log.d(TAG, "Not using pipe to retrieve attachment upload attributes...");
    v2UploadAttributes = socket.getAttachmentV2UploadAttributes();
  }

  Pair<Long, byte[]> attachmentIdAndDigest = socket.uploadAttachment(attachmentData, v2UploadAttributes);

  return new SignalServiceAttachmentPointer(0,
                                            new SignalServiceAttachmentRemoteId(attachmentIdAndDigest.first()),
                                            attachment.getContentType(),
                                            attachmentKey,
                                            Optional.of(Util.toIntExact(attachment.getLength())),
                                            attachment.getPreview(),
                                            attachment.getWidth(), attachment.getHeight(),
                                            Optional.of(attachmentIdAndDigest.second()),
                                            attachment.getFileName(),
                                            attachment.getVoiceNote(),
                                            attachment.getCaption(),
                                            attachment.getBlurHash(),
                                            attachment.getUploadTimestamp());
}
 
Example 5
public void retrieveAttachment(int cdnNumber, SignalServiceAttachmentRemoteId cdnPath, File destination, long maxSizeBytes, ProgressListener listener)
    throws NonSuccessfulResponseCodeException, PushNetworkException, MissingConfigurationException {
  final String path;
  if (cdnPath.getV2().isPresent()) {
    path = String.format(Locale.US, ATTACHMENT_ID_DOWNLOAD_PATH, cdnPath.getV2().get());
  } else {
    path = String.format(Locale.US, ATTACHMENT_KEY_DOWNLOAD_PATH, cdnPath.getV3().get());
  }
  downloadFromCdn(destination, cdnNumber, path, maxSizeBytes, listener);
}
 
Example 6
public byte[] retrieveSticker(byte[] packId, int stickerId)
    throws NonSuccessfulResponseCodeException, PushNetworkException {
  String                hexPackId = Hex.toStringCondensed(packId);
  ByteArrayOutputStream output    = new ByteArrayOutputStream();

  try {
    downloadFromCdn(output, 0, 0, String.format(Locale.US, STICKER_PATH, hexPackId, stickerId), 1024 * 1024, null);
  } catch (MissingConfigurationException e) {
    throw new AssertionError(e);
  }

  return output.toByteArray();
}
 
Example 7
public byte[] retrieveStickerManifest(byte[] packId)
    throws NonSuccessfulResponseCodeException, PushNetworkException {
  String                hexPackId = Hex.toStringCondensed(packId);
  ByteArrayOutputStream output    = new ByteArrayOutputStream();

  try {
    downloadFromCdn(output, 0, 0, String.format(STICKER_MANIFEST_PATH, hexPackId), 1024 * 1024, null);
  } catch (MissingConfigurationException e) {
    throw new AssertionError(e);
  }

  return output.toByteArray();
}
 
Example 8
public SignalServiceProfile retrieveProfile(SignalServiceAddress target, Optional<UnidentifiedAccess> unidentifiedAccess)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
  String response = makeServiceRequest(String.format(PROFILE_PATH, target.getIdentifier()), "GET", null, NO_HEADERS, unidentifiedAccess);

  try {
    return JsonUtil.fromJson(response, SignalServiceProfile.class);
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }
}
 
Example 9
public SignalServiceProfile retrieveProfileByUsername(String username, Optional<UnidentifiedAccess> unidentifiedAccess)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
  String response = makeServiceRequest(String.format(PROFILE_USERNAME_PATH, username), "GET", null, NO_HEADERS, unidentifiedAccess);

  try {
    return JsonUtil.fromJson(response, SignalServiceProfile.class);
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }
}
 
Example 10
public void retrieveProfileAvatar(String path, File destination, long maxSizeBytes)
    throws NonSuccessfulResponseCodeException, PushNetworkException {
  try {
    downloadFromCdn(destination, 0, path, maxSizeBytes, null);
  } catch (MissingConfigurationException e) {
    throw new AssertionError(e);
  }
}
 
Example 11
public void setProfileName(String name) throws NonSuccessfulResponseCodeException, PushNetworkException {
  if (FeatureFlags.DISALLOW_OLD_PROFILE_SETTING) {
    throw new AssertionError();
  }

  makeServiceRequest(String.format(PROFILE_PATH, "name/" + (name == null ? "" : URLEncoder.encode(name))), "PUT", "");
}
 
Example 12
public Optional<String> setProfileAvatar(ProfileAvatarData profileAvatar)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
  if (FeatureFlags.DISALLOW_OLD_PROFILE_SETTING) {
    throw new AssertionError();
  }

  String                        response       = makeServiceRequest(String.format(PROFILE_PATH, "form/avatar"), "GET", null);
  ProfileAvatarUploadAttributes formAttributes;

  try {
    formAttributes = JsonUtil.fromJson(response, ProfileAvatarUploadAttributes.class);
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }

  if (profileAvatar != null) {
    uploadToCdn0(AVATAR_UPLOAD_PATH, formAttributes.getAcl(), formAttributes.getKey(),
                formAttributes.getPolicy(), formAttributes.getAlgorithm(),
                formAttributes.getCredential(), formAttributes.getDate(),
                formAttributes.getSignature(), profileAvatar.getData(),
                profileAvatar.getContentType(), profileAvatar.getDataLength(),
                profileAvatar.getOutputStreamFactory(), null, null);

    return Optional.of(formAttributes.getKey());
  }

  return Optional.absent();
}
 
Example 13
/**
 * @return The avatar URL path, if one was written.
 */
public Optional<String> writeProfile(SignalServiceProfileWrite signalServiceProfileWrite, ProfileAvatarData profileAvatar)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
  String                        requestBody    = JsonUtil.toJson(signalServiceProfileWrite);
  ProfileAvatarUploadAttributes formAttributes;

  String response = makeServiceRequest(String.format(PROFILE_PATH, ""), "PUT", requestBody);

  if (signalServiceProfileWrite.hasAvatar() && profileAvatar != null) {
     try {
      formAttributes = JsonUtil.fromJson(response, ProfileAvatarUploadAttributes.class);
    } catch (IOException e) {
      Log.w(TAG, e);
      throw new NonSuccessfulResponseCodeException("Unable to parse entity");
    }

    uploadToCdn0(AVATAR_UPLOAD_PATH, formAttributes.getAcl(), formAttributes.getKey(),
                formAttributes.getPolicy(), formAttributes.getAlgorithm(),
                formAttributes.getCredential(), formAttributes.getDate(),
                formAttributes.getSignature(), profileAvatar.getData(),
                profileAvatar.getContentType(), profileAvatar.getDataLength(),
                profileAvatar.getOutputStreamFactory(), null, null);

     return Optional.of(formAttributes.getKey());
  }

  return Optional.absent();
}
 
Example 14
public List<ContactTokenDetails> retrieveDirectory(Set<String> contactTokens)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
  try {
    ContactTokenList        contactTokenList = new ContactTokenList(new LinkedList<>(contactTokens));
    String                  response         = makeServiceRequest(DIRECTORY_TOKENS_PATH, "PUT", JsonUtil.toJson(contactTokenList));
    ContactTokenDetailsList activeTokens     = JsonUtil.fromJson(response, ContactTokenDetailsList.class);

    return activeTokens.getContacts();
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }
}
 
Example 15
public AttachmentV2UploadAttributes getAttachmentV2UploadAttributes() throws NonSuccessfulResponseCodeException, PushNetworkException {
  String response = makeServiceRequest(ATTACHMENT_V2_PATH, "GET", null);
  try {
    return JsonUtil.fromJson(response, AttachmentV2UploadAttributes.class);
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }
}
 
Example 16
public AttachmentV3UploadAttributes getAttachmentV3UploadAttributes() throws NonSuccessfulResponseCodeException, PushNetworkException {
  String response = makeServiceRequest(ATTACHMENT_V3_PATH, "GET", null);
  try {
    return JsonUtil.fromJson(response, AttachmentV3UploadAttributes.class);
  } catch (IOException e) {
    Log.w(TAG, e);
    throw new NonSuccessfulResponseCodeException("Unable to parse entity");
  }
}
 
Example 17
@Override
public boolean onShouldRetryThrowable(Exception exception) {
    if (exception instanceof NonSuccessfulResponseCodeException) return false;
    if (exception instanceof PushNetworkException) return true;

    return false;
}
 
Example 18
private String makeServiceRequest(String urlFragment, String method, String jsonBody, Map<String, String> headers, ResponseCodeHandler responseCodeHandler, Optional<UnidentifiedAccess> unidentifiedAccessKey)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
  ResponseBody responseBody = makeServiceBodyRequest(urlFragment, method, jsonRequestBody(jsonBody), headers, responseCodeHandler, unidentifiedAccessKey);
  try {
    return responseBody.string();
  } catch (IOException e) {
    throw new PushNetworkException(e);
  }
}
 
Example 19
Response makeRequest(ClientSet clientSet, String authorization, List<String> cookies, String path, String method, String body)
    throws PushNetworkException, NonSuccessfulResponseCodeException
{
  ConnectionHolder connectionHolder = getRandom(clientsFor(clientSet), random);

  return makeRequest(connectionHolder, authorization, cookies, path, method, body);
}
 
Example 20
/**
 * Converts {@link IOException} on body byte reading to {@link PushNetworkException}.
 */
private static byte[] readBodyBytes(ResponseBody response) throws PushNetworkException {
  try {
    return response.bytes();
  } catch (IOException e) {
    throw new PushNetworkException(e);
  }
}
 
Example 21
public void putNewGroupsV2Group(Group group, GroupsV2AuthorizationString authorization)
    throws NonSuccessfulResponseCodeException, PushNetworkException
{
    makeStorageRequest(authorization.toString(),
                       GROUPSV2_GROUP,
                       "PUT",
                       protobufRequestBody(group),
                       GROUPS_V2_PUT_RESPONSE_HANDLER);
}
 
Example 22
public Group getGroupsV2Group(GroupsV2AuthorizationString authorization)
    throws NonSuccessfulResponseCodeException, PushNetworkException, InvalidProtocolBufferException
{
    ResponseBody response = makeStorageRequest(authorization.toString(),
                                               GROUPSV2_GROUP,
                                               "GET",
                                               null,
                                               GROUPS_V2_GET_CURRENT_HANDLER);

  return Group.parseFrom(readBodyBytes(response));
}
 
Example 23
public AvatarUploadAttributes getGroupsV2AvatarUploadForm(String authorization)
    throws NonSuccessfulResponseCodeException, PushNetworkException, InvalidProtocolBufferException
{
    ResponseBody response = makeStorageRequest(authorization,
                                               GROUPSV2_AVATAR_REQUEST,
                                               "GET",
                                               null,
                                               NO_HANDLER);

  return AvatarUploadAttributes.parseFrom(readBodyBytes(response));
}
 
Example 24
public GroupChange patchGroupsV2Group(GroupChange.Actions groupChange, String authorization)
    throws NonSuccessfulResponseCodeException, PushNetworkException, InvalidProtocolBufferException
{
  ResponseBody response = makeStorageRequest(authorization,
                                             GROUPSV2_GROUP,
                                             "PATCH",
                                             protobufRequestBody(groupChange),
                                             GROUPS_V2_PATCH_RESPONSE_HANDLER);

  return GroupChange.parseFrom(readBodyBytes(response));
}
 
Example 25
public GroupChanges getGroupsV2GroupHistory(int fromVersion, GroupsV2AuthorizationString authorization)
  throws NonSuccessfulResponseCodeException, PushNetworkException, InvalidProtocolBufferException
{
  ResponseBody response = makeStorageRequest(authorization.toString(),
                                             String.format(Locale.US, GROUPSV2_GROUP_CHANGES, fromVersion),
                                             "GET",
                                             null,
                                             GROUPS_V2_GET_LOGS_HANDLER);

  return GroupChanges.parseFrom(readBodyBytes(response));
}
 
Example 26
@Override
public boolean onShouldRetry(@NonNull Exception exception) {
  if (exception instanceof NonSuccessfulResponseCodeException) return false;
  if (exception instanceof PushNetworkException)               return true;

  return false;
}
 
Example 27
@Override
public void onRun() throws IOException {
  BackgroundMessageRetriever retriever = ApplicationDependencies.getBackgroundMessageRetriever();
  boolean                    result    = retriever.retrieveMessages(context, new RestStrategy());

  if (result) {
    Log.i(TAG, "Successfully pulled messages.");
  } else {
    throw new PushNetworkException("Failed to pull messages.");
  }
}
 
Example 28
@Override
public boolean onShouldRetry(Exception e) {
    return e instanceof PushNetworkException;
}
 
Example 29
public void retrieveSticker(File destination, byte[] packId, int stickerId)
    throws NonSuccessfulResponseCodeException, PushNetworkException, MissingConfigurationException {
  String hexPackId = Hex.toStringCondensed(packId);
  downloadFromCdn(destination, 0, String.format(Locale.US, STICKER_PATH, hexPackId, stickerId), 1024 * 1024, null);
}
 
Example 30
@Override
public boolean onShouldRetry(Exception e) {
    ALog.e("RetrieveProfileJob", "retrieve profile fail", e);
    return e instanceof PushNetworkException;
}