Search in sources :

Example 11 with PushNetworkException

use of org.whispersystems.signalservice.api.push.exceptions.PushNetworkException in project libsignal-service-java by signalapp.

the class SignalServiceMessageSender method sendMessage.

private SendMessageResponseList sendMessage(List<SignalServiceAddress> recipients, long timestamp, byte[] content) throws IOException {
    SendMessageResponseList responseList = new SendMessageResponseList();
    for (SignalServiceAddress recipient : recipients) {
        try {
            SendMessageResponse response = sendMessage(recipient, timestamp, content, false);
            responseList.addResponse(response);
        } catch (UntrustedIdentityException e) {
            Log.w(TAG, e);
            responseList.addException(e);
        } catch (UnregisteredUserException e) {
            Log.w(TAG, e);
            responseList.addException(e);
        } catch (PushNetworkException e) {
            Log.w(TAG, e);
            responseList.addException(new NetworkFailureException(recipient.getNumber(), e));
        }
    }
    return responseList;
}
Also used : SendMessageResponse(org.whispersystems.signalservice.internal.push.SendMessageResponse) SendMessageResponseList(org.whispersystems.signalservice.internal.push.SendMessageResponseList) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) NetworkFailureException(org.whispersystems.signalservice.api.push.exceptions.NetworkFailureException)

Example 12 with PushNetworkException

use of org.whispersystems.signalservice.api.push.exceptions.PushNetworkException in project libsignal-service-java by signalapp.

the class PushServiceSocket method downloadFromCdn.

private void downloadFromCdn(File destination, String path, int maxSizeBytes) throws PushNetworkException, NonSuccessfulResponseCodeException {
    ConnectionHolder connectionHolder = getRandom(cdnClients, 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());
    }
    Call call = okHttpClient.newCall(request.build());
    synchronized (connections) {
        connections.add(call);
    }
    Response response;
    try {
        response = call.execute();
        if (response.isSuccessful()) {
            ResponseBody 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();
            OutputStream out = new FileOutputStream(destination);
            byte[] buffer = new byte[4096];
            int read, totalRead = 0;
            while ((read = in.read(buffer, 0, buffer.length)) != -1) {
                out.write(buffer, 0, read);
                if ((totalRead += read) > maxSizeBytes)
                    throw new PushNetworkException("Response exceeded max size!");
            }
            return;
        }
    } catch (IOException e) {
        throw new PushNetworkException(e);
    } finally {
        synchronized (connections) {
            connections.remove(call);
        }
    }
    throw new NonSuccessfulResponseCodeException("Response: " + response);
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DigestingOutputStream(org.whispersystems.signalservice.api.crypto.DigestingOutputStream) Request(okhttp3.Request) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) FileOutputStream(java.io.FileOutputStream)

Example 13 with PushNetworkException

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

the class PushServiceSocket method makeStorageRequestResponse.

private Response makeStorageRequestResponse(String authorization, String path, String method, RequestBody body, ResponseCodeHandler responseCodeHandler) throws PushNetworkException, NonSuccessfulResponseCodeException {
    ConnectionHolder connectionHolder = getRandom(storageClients, random);
    OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
    // Log.d(TAG, "Opening URL: " + connectionHolder.getUrl());
    Request.Builder request = new Request.Builder().url(connectionHolder.getUrl() + path);
    request.method(method, body);
    if (connectionHolder.getHostHeader().isPresent()) {
        request.addHeader("Host", connectionHolder.getHostHeader().get());
    }
    if (authorization != null) {
        request.addHeader("Authorization", authorization);
    }
    Call call = okHttpClient.newCall(request.build());
    synchronized (connections) {
        connections.add(call);
    }
    Response response;
    try {
        response = call.execute();
        if (response.isSuccessful() && response.code() != 204) {
            return response;
        }
    } catch (IOException e) {
        throw new PushNetworkException(e);
    } finally {
        synchronized (connections) {
            connections.remove(call);
        }
    }
    ResponseBody responseBody = response.body();
    try {
        responseCodeHandler.handle(response.code(), responseBody);
        switch(response.code()) {
            case 204:
                throw new NoContentException("No content!");
            case 401:
            case 403:
                throw new AuthorizationFailedException(response.code(), "Authorization failed!");
            case 404:
                throw new NotFoundException("Not found");
            case 409:
                if (responseBody != null) {
                    throw new ContactManifestMismatchException(readBodyBytes(responseBody));
                } else {
                    throw new ConflictException();
                }
            case 429:
                throw new RateLimitException("Rate limit exceeded: " + response.code());
            case 499:
                throw new DeprecatedVersionException();
        }
        throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response);
    } catch (NonSuccessfulResponseCodeException | PushNetworkException e) {
        if (responseBody != null) {
            responseBody.close();
        }
        throw e;
    }
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) DeprecatedVersionException(org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException) ConflictException(org.whispersystems.signalservice.api.push.exceptions.ConflictException) 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) GroupNotFoundException(org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) IOException(java.io.IOException) NoContentException(org.whispersystems.signalservice.api.push.exceptions.NoContentException) 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) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) ContactManifestMismatchException(org.whispersystems.signalservice.api.push.exceptions.ContactManifestMismatchException)

Example 14 with PushNetworkException

use of org.whispersystems.signalservice.api.push.exceptions.PushNetworkException 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 15 with PushNetworkException

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

the class PushServiceSocket method makeRequest.

private Response makeRequest(ConnectionHolder connectionHolder, String authorization, List<String> cookies, String path, String method, String body) throws PushNetworkException, NonSuccessfulResponseCodeException {
    OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
    Request.Builder request = new Request.Builder().url(connectionHolder.getUrl() + path);
    if (body != null) {
        request.method(method, RequestBody.create(MediaType.parse("application/json"), body));
    } else {
        request.method(method, null);
    }
    if (connectionHolder.getHostHeader().isPresent()) {
        request.addHeader("Host", connectionHolder.getHostHeader().get());
    }
    if (authorization != null) {
        request.addHeader("Authorization", authorization);
    }
    if (cookies != null && !cookies.isEmpty()) {
        request.addHeader("Cookie", Util.join(cookies, "; "));
    }
    Call call = okHttpClient.newCall(request.build());
    synchronized (connections) {
        connections.add(call);
    }
    Response response;
    try {
        response = call.execute();
        if (response.isSuccessful()) {
            return response;
        }
    } catch (IOException e) {
        throw new PushNetworkException(e);
    } finally {
        synchronized (connections) {
            connections.remove(call);
        }
    }
    switch(response.code()) {
        case 401:
        case 403:
            throw new AuthorizationFailedException(response.code(), "Authorization failed!");
        case 409:
            throw new RemoteAttestationResponseExpiredException("Remote attestation response expired");
        case 429:
            throw new RateLimitException("Rate limit exceeded: " + response.code());
    }
    throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response);
}
Also used : 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) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) 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) RemoteAttestationResponseExpiredException(org.whispersystems.signalservice.api.push.exceptions.RemoteAttestationResponseExpiredException)

Aggregations

PushNetworkException (org.whispersystems.signalservice.api.push.exceptions.PushNetworkException)22 IOException (java.io.IOException)19 Response (okhttp3.Response)13 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)13 Call (okhttp3.Call)12 OkHttpClient (okhttp3.OkHttpClient)11 Request (okhttp3.Request)11 ProfileKeyCredentialResponse (org.signal.zkgroup.profiles.ProfileKeyCredentialResponse)10 ReceiptCredentialResponse (org.signal.zkgroup.receipts.ReceiptCredentialResponse)10 CredentialResponse (org.whispersystems.signalservice.api.groupsv2.CredentialResponse)10 CallingResponse (org.whispersystems.signalservice.api.messages.calls.CallingResponse)10 VerifyDeviceResponse (org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse)10 StorageAuthResponse (org.whispersystems.signalservice.api.storage.StorageAuthResponse)10 DiscoveryResponse (org.whispersystems.signalservice.internal.contacts.entities.DiscoveryResponse)10 KeyBackupResponse (org.whispersystems.signalservice.internal.contacts.entities.KeyBackupResponse)10 TokenResponse (org.whispersystems.signalservice.internal.contacts.entities.TokenResponse)10 ProfileKeyCredentialRequest (org.signal.zkgroup.profiles.ProfileKeyCredentialRequest)8 ReceiptCredentialRequest (org.signal.zkgroup.receipts.ReceiptCredentialRequest)8 ChangePhoneNumberRequest (org.whispersystems.signalservice.api.account.ChangePhoneNumberRequest)8 DiscoveryRequest (org.whispersystems.signalservice.internal.contacts.entities.DiscoveryRequest)8