Search in sources :

Example 11 with MalformedResponseException

use of org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException in project Signal-Android by signalapp.

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 12 with MalformedResponseException

use of org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException in project Signal-Android by signalapp.

the class RemoteAttestationUtil method makeAttestationRequest.

private static ResponsePair makeAttestationRequest(PushServiceSocket socket, PushServiceSocket.ClientSet clientSet, String authorization, String enclaveName, ECKeyPair keyPair) throws IOException {
    RemoteAttestationRequest attestationRequest = new RemoteAttestationRequest(keyPair.getPublicKey().getPublicKeyBytes());
    Response response = socket.makeRequest(clientSet, authorization, new LinkedList<String>(), "/v1/attestation/" + enclaveName, "PUT", JsonUtil.toJson(attestationRequest));
    ResponseBody body = response.body();
    if (body == null) {
        throw new MalformedResponseException("Empty response!");
    }
    return new ResponsePair(body.string(), parseCookies(response));
}
Also used : MultiRemoteAttestationResponse(org.whispersystems.signalservice.internal.contacts.entities.MultiRemoteAttestationResponse) Response(okhttp3.Response) RemoteAttestationResponse(org.whispersystems.signalservice.internal.contacts.entities.RemoteAttestationResponse) RemoteAttestationRequest(org.whispersystems.signalservice.internal.contacts.entities.RemoteAttestationRequest) MalformedResponseException(org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException) ResponseBody(okhttp3.ResponseBody)

Example 13 with MalformedResponseException

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

the class PushServiceSocket method writeProfile.

/**
 * @return The avatar URL path, if one was written.
 */
public Optional<String> writeProfile(SignalServiceProfileWrite signalServiceProfileWrite, ProfileAvatarData profileAvatar) throws NonSuccessfulResponseCodeException, PushNetworkException, MalformedResponseException {
    String requestBody = JsonUtil.toJson(signalServiceProfileWrite);
    ProfileAvatarUploadAttributes formAttributes;
    String response = makeServiceRequest(String.format(PROFILE_PATH, ""), "PUT", requestBody, NO_HEADERS, PaymentsRegionException::responseCodeHandler, Optional.absent());
    if (signalServiceProfileWrite.hasAvatar() && profileAvatar != null) {
        try {
            formAttributes = JsonUtil.fromJson(response, ProfileAvatarUploadAttributes.class);
        } catch (IOException e) {
            Log.w(TAG, e);
            throw new MalformedResponseException("Unable to parse entity", e);
        }
        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();
}
Also used : PaymentsRegionException(org.whispersystems.signalservice.internal.push.exceptions.PaymentsRegionException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) IOException(java.io.IOException) MalformedResponseException(org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException)

Example 14 with MalformedResponseException

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

the class PushServiceSocket method getGroupsV2GroupHistory.

public GroupHistory getGroupsV2GroupHistory(int fromVersion, GroupsV2AuthorizationString authorization, int highestKnownEpoch, boolean includeFirstState) throws IOException {
    Response response = makeStorageRequestResponse(authorization.toString(), String.format(Locale.US, GROUPSV2_GROUP_CHANGES, fromVersion, highestKnownEpoch, includeFirstState), "GET", null, GROUPS_V2_GET_LOGS_HANDLER);
    if (response.body() == null) {
        throw new PushNetworkException("No body!");
    }
    GroupChanges groupChanges;
    try (InputStream input = response.body().byteStream()) {
        groupChanges = GroupChanges.parseFrom(input);
    } catch (IOException e) {
        throw new PushNetworkException(e);
    }
    if (response.code() == 206) {
        String contentRangeHeader = response.header("Content-Range");
        Optional<ContentRange> contentRange = ContentRange.parse(contentRangeHeader);
        if (contentRange.isPresent()) {
            Log.i(TAG, "Additional logs for group: " + contentRangeHeader);
            return new GroupHistory(groupChanges, contentRange);
        } else {
            Log.w(TAG, "Unable to parse Content-Range header: " + contentRangeHeader);
            throw new MalformedResponseException("Unable to parse content range header on 206");
        }
    }
    return new GroupHistory(groupChanges, Optional.absent());
}
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) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) GroupChanges(org.signal.storageservice.protos.groups.GroupChanges) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) MalformedResponseException(org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException)

Example 15 with MalformedResponseException

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

the class PushServiceSocket method retrieveVersionedProfile.

public ListenableFuture<SignalServiceProfile> retrieveVersionedProfile(UUID target, ProfileKey profileKey, Optional<UnidentifiedAccess> unidentifiedAccess, Locale locale) {
    ProfileKeyVersion profileKeyIdentifier = profileKey.getProfileKeyVersion(target);
    String version = profileKeyIdentifier.serialize();
    String subPath = String.format("%s/%s", target, version);
    ListenableFuture<String> response = submitServiceRequest(String.format(PROFILE_PATH, subPath), "GET", null, AcceptLanguagesUtil.getHeadersWithAcceptLanguage(locale), unidentifiedAccess);
    return FutureTransformers.map(response, body -> {
        try {
            return JsonUtil.fromJson(body, SignalServiceProfile.class);
        } catch (IOException e) {
            Log.w(TAG, e);
            throw new MalformedResponseException("Unable to parse entity", e);
        }
    });
}
Also used : ProfileKeyVersion(org.signal.zkgroup.profiles.ProfileKeyVersion) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) IOException(java.io.IOException) MalformedResponseException(org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException)

Aggregations

MalformedResponseException (org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException)18 GroupsV2AuthorizationString (org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString)10 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)8 IOException (java.io.IOException)6 Response (okhttp3.Response)6 ResponseBody (okhttp3.ResponseBody)4 ProfileKeyCredentialResponse (org.signal.zkgroup.profiles.ProfileKeyCredentialResponse)4 ReceiptCredentialResponse (org.signal.zkgroup.receipts.ReceiptCredentialResponse)4 CredentialResponse (org.whispersystems.signalservice.api.groupsv2.CredentialResponse)4 CallingResponse (org.whispersystems.signalservice.api.messages.calls.CallingResponse)4 VerifyDeviceResponse (org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse)4 PushNetworkException (org.whispersystems.signalservice.api.push.exceptions.PushNetworkException)4 StorageAuthResponse (org.whispersystems.signalservice.api.storage.StorageAuthResponse)4 DiscoveryResponse (org.whispersystems.signalservice.internal.contacts.entities.DiscoveryResponse)4 KeyBackupResponse (org.whispersystems.signalservice.internal.contacts.entities.KeyBackupResponse)4 MultiRemoteAttestationResponse (org.whispersystems.signalservice.internal.contacts.entities.MultiRemoteAttestationResponse)4 RemoteAttestationResponse (org.whispersystems.signalservice.internal.contacts.entities.RemoteAttestationResponse)4 TokenResponse (org.whispersystems.signalservice.internal.contacts.entities.TokenResponse)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2