Search in sources :

Example 1 with RateLimitException

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

the class PushServiceSocket method makeServiceRequest.

private String makeServiceRequest(String urlFragment, String method, String body) throws NonSuccessfulResponseCodeException, PushNetworkException {
    Response response = getServiceConnection(urlFragment, method, body);
    int responseCode;
    String responseMessage;
    String responseBody;
    try {
        responseCode = response.code();
        responseMessage = response.message();
        responseBody = response.body().string();
    } catch (IOException ioe) {
        throw new PushNetworkException(ioe);
    }
    switch(responseCode) {
        case 413:
            throw new RateLimitException("Rate limit exceeded: " + responseCode);
        case 401:
        case 403:
            throw new AuthorizationFailedException("Authorization failed!");
        case 404:
            throw new NotFoundException("Not found");
        case 409:
            MismatchedDevices mismatchedDevices;
            try {
                mismatchedDevices = JsonUtil.fromJson(responseBody, MismatchedDevices.class);
            } catch (JsonProcessingException e) {
                Log.w(TAG, e);
                throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
            } catch (IOException e) {
                throw new PushNetworkException(e);
            }
            throw new MismatchedDevicesException(mismatchedDevices);
        case 410:
            StaleDevices staleDevices;
            try {
                staleDevices = JsonUtil.fromJson(responseBody, StaleDevices.class);
            } catch (JsonProcessingException e) {
                throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
            } catch (IOException e) {
                throw new PushNetworkException(e);
            }
            throw new StaleDevicesException(staleDevices);
        case 411:
            DeviceLimit deviceLimit;
            try {
                deviceLimit = JsonUtil.fromJson(responseBody, DeviceLimit.class);
            } catch (JsonProcessingException e) {
                throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
            } catch (IOException e) {
                throw new PushNetworkException(e);
            }
            throw new DeviceLimitExceededException(deviceLimit);
        case 417:
            throw new ExpectationFailedException();
        case 423:
            RegistrationLockFailure accountLockFailure;
            try {
                accountLockFailure = JsonUtil.fromJson(responseBody, RegistrationLockFailure.class);
            } catch (JsonProcessingException e) {
                Log.w(TAG, e);
                throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
            } catch (IOException e) {
                throw new PushNetworkException(e);
            }
            throw new LockedException(accountLockFailure.length, accountLockFailure.timeRemaining);
    }
    if (responseCode != 200 && responseCode != 204) {
        throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
    }
    return responseBody;
}
Also used : PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) ExpectationFailedException(org.whispersystems.signalservice.api.push.exceptions.ExpectationFailedException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) StaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException) IOException(java.io.IOException) Response(okhttp3.Response) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) MismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with RateLimitException

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

the class PushServiceSocket method validateServiceResponse.

private Response validateServiceResponse(Response response) throws NonSuccessfulResponseCodeException, PushNetworkException, MalformedResponseException {
    int responseCode = response.code();
    String responseMessage = response.message();
    switch(responseCode) {
        case 413:
            throw new RateLimitException("Rate limit exceeded: " + responseCode);
        case 401:
        case 403:
            throw new AuthorizationFailedException(responseCode, "Authorization failed!");
        case 404:
            throw new NotFoundException("Not found");
        case 409:
            MismatchedDevices mismatchedDevices = readResponseJson(response, MismatchedDevices.class);
            throw new MismatchedDevicesException(mismatchedDevices);
        case 410:
            StaleDevices staleDevices = readResponseJson(response, StaleDevices.class);
            throw new StaleDevicesException(staleDevices);
        case 411:
            DeviceLimit deviceLimit = readResponseJson(response, DeviceLimit.class);
            throw new DeviceLimitExceededException(deviceLimit);
        case 417:
            throw new ExpectationFailedException();
        case 423:
            RegistrationLockFailure accountLockFailure = readResponseJson(response, RegistrationLockFailure.class);
            AuthCredentials credentials = accountLockFailure.backupCredentials;
            String basicStorageCredentials = credentials != null ? credentials.asBasic() : null;
            throw new LockedException(accountLockFailure.length, accountLockFailure.timeRemaining, basicStorageCredentials);
        case 428:
            ProofRequiredResponse proofRequiredResponse = readResponseJson(response, ProofRequiredResponse.class);
            String retryAfterRaw = response.header("Retry-After");
            long retryAfter = Util.parseInt(retryAfterRaw, -1);
            throw new ProofRequiredException(proofRequiredResponse, retryAfter);
        case 499:
            throw new DeprecatedVersionException();
        case 508:
            throw new ServerRejectedException();
    }
    if (responseCode != 200 && responseCode != 202 && responseCode != 204) {
        throw new NonSuccessfulResponseCodeException(responseCode, "Bad response: " + responseCode + " " + responseMessage);
    }
    return response;
}
Also used : RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) DeprecatedVersionException(org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException) ExpectationFailedException(org.whispersystems.signalservice.api.push.exceptions.ExpectationFailedException) GroupNotFoundException(org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) GroupStaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.GroupStaleDevicesException) StaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException) ProofRequiredException(org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException) ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) GroupMismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.GroupMismatchedDevicesException) MismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException)

Example 3 with RateLimitException

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

the class DefaultErrorMapper method parseError.

@Override
public Throwable parseError(int status, String body, Function<String, String> getHeader) {
    if (customErrorMappers.containsKey(status)) {
        try {
            return customErrorMappers.get(status).parseError(status, body, getHeader);
        } catch (MalformedResponseException e) {
            return e;
        }
    }
    switch(status) {
        case 401:
        case 403:
            return new AuthorizationFailedException(status, "Authorization failed!");
        case 402:
            return new CaptchaRequiredException();
        case 404:
            return new NotFoundException("Not found");
        case 409:
            try {
                return new MismatchedDevicesException(JsonUtil.fromJsonResponse(body, MismatchedDevices.class));
            } catch (MalformedResponseException e) {
                return e;
            }
        case 410:
            try {
                return new StaleDevicesException(JsonUtil.fromJsonResponse(body, StaleDevices.class));
            } catch (MalformedResponseException e) {
                return e;
            }
        case 411:
            try {
                return new DeviceLimitExceededException(JsonUtil.fromJsonResponse(body, DeviceLimit.class));
            } catch (MalformedResponseException e) {
                return e;
            }
        case 413:
            return new RateLimitException("Rate limit exceeded: " + status);
        case 417:
            return new ExpectationFailedException();
        case 423:
            PushServiceSocket.RegistrationLockFailure accountLockFailure;
            try {
                accountLockFailure = JsonUtil.fromJsonResponse(body, PushServiceSocket.RegistrationLockFailure.class);
            } catch (MalformedResponseException e) {
                return e;
            }
            AuthCredentials credentials = accountLockFailure.backupCredentials;
            String basicStorageCredentials = credentials != null ? credentials.asBasic() : null;
            return new LockedException(accountLockFailure.length, accountLockFailure.timeRemaining, basicStorageCredentials);
        case 428:
            ProofRequiredResponse proofRequiredResponse;
            try {
                proofRequiredResponse = JsonUtil.fromJsonResponse(body, ProofRequiredResponse.class);
            } catch (MalformedResponseException e) {
                return e;
            }
            String retryAfterRaw = getHeader.apply("Retry-After");
            long retryAfter = Util.parseInt(retryAfterRaw, -1);
            return new ProofRequiredException(proofRequiredResponse, retryAfter);
        case 499:
            return new DeprecatedVersionException();
        case 508:
            return new ServerRejectedException();
    }
    if (status != 200 && status != 202 && status != 204) {
        return new NonSuccessfulResponseCodeException(status, "Bad response: " + status);
    }
    return null;
}
Also used : LockedException(org.whispersystems.signalservice.internal.push.LockedException) RateLimitException(org.whispersystems.signalservice.api.push.exceptions.RateLimitException) DeprecatedVersionException(org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException) ExpectationFailedException(org.whispersystems.signalservice.api.push.exceptions.ExpectationFailedException) DeviceLimit(org.whispersystems.signalservice.internal.push.DeviceLimit) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) StaleDevicesException(org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException) ProofRequiredException(org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException) ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) MalformedResponseException(org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException) MismatchedDevices(org.whispersystems.signalservice.internal.push.MismatchedDevices) StaleDevices(org.whispersystems.signalservice.internal.push.StaleDevices) DeviceLimitExceededException(org.whispersystems.signalservice.internal.push.DeviceLimitExceededException) AuthCredentials(org.whispersystems.signalservice.internal.push.AuthCredentials) AuthorizationFailedException(org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException) MismatchedDevicesException(org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException) PushServiceSocket(org.whispersystems.signalservice.internal.push.PushServiceSocket) CaptchaRequiredException(org.whispersystems.signalservice.api.push.exceptions.CaptchaRequiredException) ProofRequiredResponse(org.whispersystems.signalservice.internal.push.ProofRequiredResponse)

Example 4 with RateLimitException

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

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

AuthorizationFailedException (org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException)5 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)5 RateLimitException (org.whispersystems.signalservice.api.push.exceptions.RateLimitException)5 NotFoundException (org.whispersystems.signalservice.api.push.exceptions.NotFoundException)4 IOException (java.io.IOException)3 Response (okhttp3.Response)3 DeprecatedVersionException (org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException)3 ExpectationFailedException (org.whispersystems.signalservice.api.push.exceptions.ExpectationFailedException)3 PushNetworkException (org.whispersystems.signalservice.api.push.exceptions.PushNetworkException)3 MismatchedDevicesException (org.whispersystems.signalservice.internal.push.exceptions.MismatchedDevicesException)3 StaleDevicesException (org.whispersystems.signalservice.internal.push.exceptions.StaleDevicesException)3 Call (okhttp3.Call)2 OkHttpClient (okhttp3.OkHttpClient)2 Request (okhttp3.Request)2 ProfileKeyCredentialRequest (org.signal.zkgroup.profiles.ProfileKeyCredentialRequest)2 ProfileKeyCredentialResponse (org.signal.zkgroup.profiles.ProfileKeyCredentialResponse)2 ReceiptCredentialRequest (org.signal.zkgroup.receipts.ReceiptCredentialRequest)2 ReceiptCredentialResponse (org.signal.zkgroup.receipts.ReceiptCredentialResponse)2 ChangePhoneNumberRequest (org.whispersystems.signalservice.api.account.ChangePhoneNumberRequest)2 CredentialResponse (org.whispersystems.signalservice.api.groupsv2.CredentialResponse)2