Search in sources :

Example 1 with MissingConfigurationException

use of org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException in project Signal-Android by WhisperSystems.

the class AttachmentDownloadJob method retrieveAttachment.

private void retrieveAttachment(long messageId, final AttachmentId attachmentId, final Attachment attachment) throws IOException, RetryLaterException {
    AttachmentDatabase database = SignalDatabase.attachments();
    File attachmentFile = database.getOrCreateTransferFile(attachmentId);
    try {
        SignalServiceMessageReceiver messageReceiver = ApplicationDependencies.getSignalServiceMessageReceiver();
        SignalServiceAttachmentPointer pointer = createAttachmentPointer(attachment);
        InputStream stream = messageReceiver.retrieveAttachment(pointer, attachmentFile, MAX_ATTACHMENT_SIZE, (total, progress) -> EventBus.getDefault().postSticky(new PartProgressEvent(attachment, PartProgressEvent.Type.NETWORK, total, progress)));
        database.insertAttachmentsForPlaceholder(messageId, attachmentId, stream);
    } catch (RangeException e) {
        Log.w(TAG, "Range exception, file size " + attachmentFile.length(), e);
        if (attachmentFile.delete()) {
            Log.i(TAG, "Deleted temp download file to recover");
            throw new RetryLaterException(e);
        } else {
            throw new IOException("Failed to delete temp download file following range exception");
        }
    } catch (InvalidPartException | NonSuccessfulResponseCodeException | InvalidMessageException | MmsException | MissingConfigurationException e) {
        Log.w(TAG, "Experienced exception while trying to download an attachment.", e);
        markFailed(messageId, attachmentId);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) InputStream(java.io.InputStream) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) PartProgressEvent(org.thoughtcrime.securesms.events.PartProgressEvent) IOException(java.io.IOException) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) MmsException(org.thoughtcrime.securesms.mms.MmsException) MissingConfigurationException(org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException) SignalServiceMessageReceiver(org.whispersystems.signalservice.api.SignalServiceMessageReceiver) RangeException(org.whispersystems.signalservice.api.push.exceptions.RangeException) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) File(java.io.File)

Example 2 with MissingConfigurationException

use of org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException in project Signal-Android by WhisperSystems.

the class PushServiceSocket method retrieveStickerManifest.

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();
}
Also used : MissingConfigurationException(org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with MissingConfigurationException

use of org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException in project Signal-Android by WhisperSystems.

the class PushServiceSocket method retrieveSticker.

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();
}
Also used : MissingConfigurationException(org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with MissingConfigurationException

use of org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException in project Signal-Android by WhisperSystems.

the class PushServiceSocket method downloadFromCdn.

private void downloadFromCdn(OutputStream outputStream, long offset, int cdnNumber, String path, long maxSizeBytes, ProgressListener listener) throws PushNetworkException, NonSuccessfulResponseCodeException, MissingConfigurationException {
    ConnectionHolder[] cdnNumberClients = cdnClientsMap.get(cdnNumber);
    if (cdnNumberClients == null) {
        throw new MissingConfigurationException("Attempted to download from unsupported CDN number: " + cdnNumber + ", Our configuration supports: " + cdnClientsMap.keySet());
    }
    ConnectionHolder connectionHolder = getRandom(cdnNumberClients, random);
    OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
    Request.Builder request = new Request.Builder().url(connectionHolder.getUrl() + "/" + path).get();
    if (connectionHolder.getHostHeader().isPresent()) {
        request.addHeader("Host", connectionHolder.getHostHeader().get());
    }
    if (offset > 0) {
        Log.i(TAG, "Starting download from CDN with offset " + offset);
        request.addHeader("Range", "bytes=" + offset + "-");
    }
    Call call = okHttpClient.newCall(request.build());
    synchronized (connections) {
        connections.add(call);
    }
    Response response = null;
    ResponseBody body = null;
    try {
        response = call.execute();
        if (response.isSuccessful()) {
            body = response.body();
            if (body == null)
                throw new PushNetworkException("No response body!");
            if (body.contentLength() > maxSizeBytes)
                throw new PushNetworkException("Response exceeds max size!");
            InputStream in = body.byteStream();
            byte[] buffer = new byte[32768];
            int read = 0;
            long totalRead = offset;
            while ((read = in.read(buffer, 0, buffer.length)) != -1) {
                outputStream.write(buffer, 0, read);
                if ((totalRead += read) > maxSizeBytes)
                    throw new PushNetworkException("Response exceeded max size!");
                if (listener != null) {
                    listener.onAttachmentProgress(body.contentLength() + offset, totalRead);
                }
            }
            return;
        } else if (response.code() == 416) {
            throw new RangeException(offset);
        }
    } catch (NonSuccessfulResponseCodeException | PushNetworkException e) {
        throw e;
    } catch (IOException e) {
        throw new PushNetworkException(e);
    } finally {
        if (body != null) {
            body.close();
        }
        synchronized (connections) {
            connections.remove(call);
        }
    }
    throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response);
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ReceiptCredentialRequest(org.signal.zkgroup.receipts.ReceiptCredentialRequest) Request(okhttp3.Request) ChangePhoneNumberRequest(org.whispersystems.signalservice.api.account.ChangePhoneNumberRequest) DiscoveryRequest(org.whispersystems.signalservice.internal.contacts.entities.DiscoveryRequest) KeyBackupRequest(org.whispersystems.signalservice.internal.contacts.entities.KeyBackupRequest) ProfileKeyCredentialRequest(org.signal.zkgroup.profiles.ProfileKeyCredentialRequest) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) CallingResponse(org.whispersystems.signalservice.api.messages.calls.CallingResponse) Response(okhttp3.Response) KeyBackupResponse(org.whispersystems.signalservice.internal.contacts.entities.KeyBackupResponse) ReceiptCredentialResponse(org.signal.zkgroup.receipts.ReceiptCredentialResponse) CredentialResponse(org.whispersystems.signalservice.api.groupsv2.CredentialResponse) StorageAuthResponse(org.whispersystems.signalservice.api.storage.StorageAuthResponse) VerifyDeviceResponse(org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse) DiscoveryResponse(org.whispersystems.signalservice.internal.contacts.entities.DiscoveryResponse) ProfileKeyCredentialResponse(org.signal.zkgroup.profiles.ProfileKeyCredentialResponse) TokenResponse(org.whispersystems.signalservice.internal.contacts.entities.TokenResponse) MissingConfigurationException(org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException) RangeException(org.whispersystems.signalservice.api.push.exceptions.RangeException)

Example 5 with MissingConfigurationException

use of org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException in project Signal-Android by WhisperSystems.

the class AvatarGroupsV1DownloadJob method onRun.

@Override
public void onRun() throws IOException {
    GroupDatabase database = SignalDatabase.groups();
    Optional<GroupRecord> record = database.getGroup(groupId);
    File attachment = null;
    try {
        if (record.isPresent()) {
            long avatarId = record.get().getAvatarId();
            String contentType = record.get().getAvatarContentType();
            byte[] key = record.get().getAvatarKey();
            String relay = record.get().getRelay();
            Optional<byte[]> digest = Optional.fromNullable(record.get().getAvatarDigest());
            Optional<String> fileName = Optional.absent();
            if (avatarId == -1 || key == null) {
                return;
            }
            if (digest.isPresent()) {
                Log.i(TAG, "Downloading group avatar with digest: " + Hex.toString(digest.get()));
            }
            attachment = File.createTempFile("avatar", "tmp", context.getCacheDir());
            attachment.deleteOnExit();
            SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
            SignalServiceAttachmentPointer pointer = new SignalServiceAttachmentPointer(0, new SignalServiceAttachmentRemoteId(avatarId), contentType, key, Optional.of(0), Optional.absent(), 0, 0, digest, fileName, false, false, false, Optional.absent(), Optional.absent(), System.currentTimeMillis());
            InputStream inputStream = receiver.retrieveAttachment(pointer, attachment, AvatarHelper.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE);
            AvatarHelper.setAvatar(context, record.get().getRecipientId(), inputStream);
            SignalDatabase.groups().onAvatarUpdated(groupId, true);
            inputStream.close();
        }
    } catch (NonSuccessfulResponseCodeException | InvalidMessageException | MissingConfigurationException e) {
        Log.w(TAG, e);
    } finally {
        if (attachment != null)
            attachment.delete();
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) InputStream(java.io.InputStream) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) GroupRecord(org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord) MissingConfigurationException(org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException) SignalServiceMessageReceiver(org.whispersystems.signalservice.api.SignalServiceMessageReceiver) SignalServiceAttachmentRemoteId(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) File(java.io.File)

Aggregations

MissingConfigurationException (org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException)5 InputStream (java.io.InputStream)3 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 SignalServiceMessageReceiver (org.whispersystems.signalservice.api.SignalServiceMessageReceiver)2 GroupsV2AuthorizationString (org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString)2 SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)2 RangeException (org.whispersystems.signalservice.api.push.exceptions.RangeException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 ProfileKeyCredentialRequest (org.signal.zkgroup.profiles.ProfileKeyCredentialRequest)1 ProfileKeyCredentialResponse (org.signal.zkgroup.profiles.ProfileKeyCredentialResponse)1 ReceiptCredentialRequest (org.signal.zkgroup.receipts.ReceiptCredentialRequest)1