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;
}
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);
}
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;
}
}
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);
}
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);
}
Aggregations