Search in sources :

Example 1 with ConflictException

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

Aggregations

IOException (java.io.IOException)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 ReceiptCredentialResponse (org.signal.zkgroup.receipts.ReceiptCredentialResponse)1 ChangePhoneNumberRequest (org.whispersystems.signalservice.api.account.ChangePhoneNumberRequest)1 CredentialResponse (org.whispersystems.signalservice.api.groupsv2.CredentialResponse)1 CallingResponse (org.whispersystems.signalservice.api.messages.calls.CallingResponse)1 VerifyDeviceResponse (org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse)1 AuthorizationFailedException (org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException)1 ConflictException (org.whispersystems.signalservice.api.push.exceptions.ConflictException)1 ContactManifestMismatchException (org.whispersystems.signalservice.api.push.exceptions.ContactManifestMismatchException)1 DeprecatedVersionException (org.whispersystems.signalservice.api.push.exceptions.DeprecatedVersionException)1 NoContentException (org.whispersystems.signalservice.api.push.exceptions.NoContentException)1 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)1