use of im.actor.runtime.http.HTTPError in project actor-platform by actorapp.
the class AndroidHttpProvider method putMethod.
@Override
public Promise<HTTPResponse> putMethod(String url, byte[] contents) {
return new Promise<>(resolver -> {
final Request request = new Request.Builder().url(url).method("PUT", RequestBody.create(MEDIA_TYPE, contents)).build();
Log.d(TAG, "Uploading part: " + request.toString());
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG, "Uploading part error: " + request.toString());
e.printStackTrace();
resolver.error(new HTTPError(0));
}
@Override
public void onResponse(Response response) throws IOException {
Log.d(TAG, "Upload part response: " + request.toString() + " -> " + response.toString());
if (response.code() >= 200 && response.code() < 300) {
resolver.result(new HTTPResponse(response.code(), null));
} else {
resolver.error(new HTTPError(response.code()));
}
}
});
});
}
use of im.actor.runtime.http.HTTPError in project actor-platform by actorapp.
the class JsHttpProvider method putMethod.
@Override
public Promise<HTTPResponse> putMethod(String url, byte[] contents) {
return new Promise<>(resolver -> {
JsHttpRequest request = JsHttpRequest.create();
request.open("PUT", url);
request.setRequestHeader("Content-Type", "application/octet-stream");
request.setOnLoadHandler(request1 -> {
if (request1.getReadyState() == 4) {
if (request1.getStatus() >= 200 && request1.getStatus() < 300) {
resolver.result(new HTTPResponse(request1.getStatus(), null));
} else {
resolver.error(new HTTPError(request1.getStatus()));
}
}
});
Uint8Array push = TypedArrays.createUint8Array(contents.length);
for (int i = 0; i < contents.length; i++) {
push.set(i, contents[i]);
}
request.send(push.buffer());
});
}
use of im.actor.runtime.http.HTTPError in project actor-platform by actorapp.
the class UploadTask method uploadPart.
private void uploadPart(final int blockIndex, final byte[] data, final int attempt) {
api(new RequestGetFileUploadPartUrl(blockIndex, blockSize, uploadConfig)).flatMap(r -> HTTP.putMethod(r.getUrl(), data)).then(r -> {
if (LOG) {
Log.d(TAG, "Block #" + blockIndex + " uploaded");
}
uploadCount--;
uploaded++;
reportProgress(uploaded / (float) blocksCount);
checkQueue();
}).failure(e -> {
if (e instanceof HTTPError) {
HTTPError httpError = (HTTPError) e;
if ((httpError.getErrorCode() >= 500 && httpError.getErrorCode() < 600) || httpError.getErrorCode() == 0) {
int retryInSecs = DEFAULT_RETRY;
if (LOG) {
Log.w(TAG, "Block #" + blockIndex + " upload error #" + httpError.getErrorCode() + " trying again in " + retryInSecs + " sec, attempt #" + (attempt + 1));
}
schedule(new Retry(blockIndex, data, attempt + 1), retryInSecs * 1000L);
return;
}
if (LOG) {
Log.w(TAG, "Block #" + blockIndex + " upload failure");
}
reportError();
}
});
}
use of im.actor.runtime.http.HTTPError in project actor-platform by actorapp.
the class AndroidHttpProvider method getMethod.
@Override
public Promise<HTTPResponse> getMethod(String url, int startOffset, int size, int totalSize) {
return new Promise<>(resolver -> {
final Request request = new Request.Builder().url(url).addHeader("Range", "bytes=" + startOffset + "-" + (startOffset + size)).build();
Log.d(TAG, "Downloading part: " + request.toString());
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG, "Downloading part error: " + request.toString());
e.printStackTrace();
resolver.error(new HTTPError(0));
}
@Override
public void onResponse(Response response) throws IOException {
Log.d(TAG, "Downloading part response: " + request.toString() + " -> " + response.toString());
if (response.code() >= 200 && response.code() < 300) {
resolver.result(new HTTPResponse(response.code(), response.body().bytes()));
} else {
resolver.error(new HTTPError(response.code()));
}
}
});
});
}
Aggregations