use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project mollyim-android by mollyim.
the class UpdateApkJob method onRun.
@Override
public void onRun() throws IOException, PackageManager.NameNotFoundException {
if (!TextSecurePreferences.isUpdateApkEnabled(context)) {
return;
}
boolean includeBeta = TextSecurePreferences.isUpdateApkIncludeBetaEnabled(context);
Log.i(TAG, "Checking for APK update [stable" + (includeBeta ? ", beta" : "") + "]...");
OkHttpClient client = new OkHttpClient.Builder().socketFactory(Network.getSocketFactory()).dns(Network.getDns()).build();
Request request = new Request.Builder().url(String.format("%s/index-v1.json", BuildConfig.FDROID_UPDATE_URL)).build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("Bad response: " + response.message());
}
if (response.body() == null) {
throw new IOException("Missing body!");
}
RepoIndex repoIndex = JsonUtils.fromJson(response.body().bytes(), RepoIndex.class);
if (repoIndex.packages == null || repoIndex.packages.appReleases == null) {
return;
}
List<UpdateDescriptor> releases = repoIndex.packages.appReleases;
UpdateDescriptor updateDescriptor = releases.stream().filter(r -> r.versionName != null).filter(r -> includeBeta || !r.versionName.contains("beta")).sorted().findFirst().orElse(null);
if (updateDescriptor == null) {
return;
}
byte[] digest = Hex.fromStringCondensed(updateDescriptor.getDigest());
Log.i(TAG, "Got descriptor: " + updateDescriptor);
if (updateDescriptor.getVersionCode() > getVersionCode()) {
Uri uri = Uri.parse(BuildConfig.FDROID_UPDATE_URL).buildUpon().appendPath(updateDescriptor.getApkName()).build();
DownloadStatus downloadStatus = getDownloadStatus(uri, digest);
Log.i(TAG, "Download status: " + downloadStatus.getStatus());
if (downloadStatus.getStatus() == DownloadStatus.Status.COMPLETE) {
Log.i(TAG, "Download status complete, notifying...");
handleDownloadNotify(downloadStatus.getDownloadId());
} else if (downloadStatus.getStatus() == DownloadStatus.Status.MISSING) {
Log.i(TAG, "Download status missing, starting download...");
handleDownloadStart(uri, updateDescriptor.getVersionName(), digest);
}
}
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project mollyim-android by mollyim.
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 com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project mollyim-android by mollyim.
the class PushServiceSocket method getResumeInfo.
private ResumeInfo getResumeInfo(String resumableUrl, long contentLength) throws IOException {
ConnectionHolder connectionHolder = getRandom(cdnClientsMap.get(2), random);
OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
final long offset;
final String contentRange;
Request.Builder request = new Request.Builder().url(buildConfiguredUrl(connectionHolder, resumableUrl)).put(RequestBody.create(null, "")).addHeader("Content-Range", String.format(Locale.US, "bytes */%d", contentLength));
if (connectionHolder.getHostHeader().isPresent()) {
request.header("host", connectionHolder.getHostHeader().get());
}
Call call = okHttpClient.newCall(request.build());
synchronized (connections) {
connections.add(call);
}
try {
Response response;
try {
response = call.execute();
} catch (IOException e) {
throw new PushNetworkException(e);
}
if (response.isSuccessful()) {
offset = contentLength;
contentRange = null;
} else if (response.code() == 308) {
String rangeCompleted = response.header("Range");
if (rangeCompleted == null) {
offset = 0;
} else {
offset = Long.parseLong(rangeCompleted.split("-")[1]) + 1;
}
contentRange = String.format(Locale.US, "bytes %d-%d/%d", offset, contentLength - 1, contentLength);
} else if (response.code() == 404) {
throw new ResumeLocationInvalidException();
} else {
throw new NonSuccessfulResumableUploadResponseCodeException(response.code(), "Response: " + response);
}
} finally {
synchronized (connections) {
connections.remove(call);
}
}
return new ResumeInfo(contentRange, offset);
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project mollyim-android by mollyim.
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);
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project mollyim-android by mollyim.
the class PushServiceSocket method uploadToCdn0.
private byte[] uploadToCdn0(String path, String acl, String key, String policy, String algorithm, String credential, String date, String signature, InputStream data, String contentType, long length, OutputStreamFactory outputStreamFactory, ProgressListener progressListener, CancelationSignal cancelationSignal) throws PushNetworkException, NonSuccessfulResponseCodeException {
ConnectionHolder connectionHolder = getRandom(cdnClientsMap.get(0), random);
OkHttpClient okHttpClient = connectionHolder.getClient().newBuilder().connectTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS).build();
DigestingRequestBody file = new DigestingRequestBody(data, outputStreamFactory, contentType, length, progressListener, cancelationSignal, 0);
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("acl", acl).addFormDataPart("key", key).addFormDataPart("policy", policy).addFormDataPart("Content-Type", contentType).addFormDataPart("x-amz-algorithm", algorithm).addFormDataPart("x-amz-credential", credential).addFormDataPart("x-amz-date", date).addFormDataPart("x-amz-signature", signature).addFormDataPart("file", "file", file).build();
Request.Builder request = new Request.Builder().url(connectionHolder.getUrl() + "/" + path).post(requestBody);
if (connectionHolder.getHostHeader().isPresent()) {
request.addHeader("Host", connectionHolder.getHostHeader().get());
}
Call call = okHttpClient.newCall(request.build());
synchronized (connections) {
connections.add(call);
}
try {
Response response;
try {
response = call.execute();
} catch (IOException e) {
throw new PushNetworkException(e);
}
if (response.isSuccessful())
return file.getTransmittedDigest();
else
throw new NonSuccessfulResponseCodeException(response.code(), "Response: " + response);
} finally {
synchronized (connections) {
connections.remove(call);
}
}
}
Aggregations