Search in sources :

Example 1 with HTTPError

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()));
                }
            }
        });
    });
}
Also used : Response(com.squareup.okhttp.Response) HTTPResponse(im.actor.runtime.http.HTTPResponse) Promise(im.actor.runtime.promise.Promise) HTTPError(im.actor.runtime.http.HTTPError) Callback(com.squareup.okhttp.Callback) HTTPResponse(im.actor.runtime.http.HTTPResponse) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Example 2 with HTTPError

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());
    });
}
Also used : Promise(im.actor.runtime.promise.Promise) HTTPError(im.actor.runtime.http.HTTPError) HTTPResponse(im.actor.runtime.http.HTTPResponse) JsHttpRequest(im.actor.runtime.js.http.JsHttpRequest) Uint8Array(com.google.gwt.typedarrays.shared.Uint8Array)

Example 3 with HTTPError

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();
        }
    });
}
Also used : ModuleContext(im.actor.core.modules.ModuleContext) ActorCancellable(im.actor.runtime.actors.ActorCancellable) ResponseGetFileUploadUrl(im.actor.core.api.rpc.ResponseGetFileUploadUrl) ResponseCommitFileUpload(im.actor.core.api.rpc.ResponseCommitFileUpload) Promise(im.actor.runtime.promise.Promise) OutputFile(im.actor.runtime.files.OutputFile) RpcException(im.actor.core.network.RpcException) Storage(im.actor.runtime.Storage) ModuleActor(im.actor.core.modules.ModuleActor) RequestCommitFileUpload(im.actor.core.api.rpc.RequestCommitFileUpload) RequestGetFileUploadUrl(im.actor.core.api.rpc.RequestGetFileUploadUrl) HTTPResponse(im.actor.runtime.http.HTTPResponse) FileReference(im.actor.core.entity.FileReference) FileSystemReference(im.actor.runtime.files.FileSystemReference) InputFile(im.actor.runtime.files.InputFile) RpcCallback(im.actor.core.network.RpcCallback) ActorRef(im.actor.runtime.actors.ActorRef) Log(im.actor.runtime.Log) HTTP(im.actor.runtime.HTTP) HTTPError(im.actor.runtime.http.HTTPError) CRC32(im.actor.runtime.crypto.CRC32) RequestGetFileUploadPartUrl(im.actor.core.api.rpc.RequestGetFileUploadPartUrl) HTTPError(im.actor.runtime.http.HTTPError) RequestGetFileUploadPartUrl(im.actor.core.api.rpc.RequestGetFileUploadPartUrl)

Example 4 with HTTPError

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()));
                }
            }
        });
    });
}
Also used : Response(com.squareup.okhttp.Response) HTTPResponse(im.actor.runtime.http.HTTPResponse) Promise(im.actor.runtime.promise.Promise) HTTPError(im.actor.runtime.http.HTTPError) Callback(com.squareup.okhttp.Callback) HTTPResponse(im.actor.runtime.http.HTTPResponse) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Aggregations

HTTPError (im.actor.runtime.http.HTTPError)4 HTTPResponse (im.actor.runtime.http.HTTPResponse)4 Promise (im.actor.runtime.promise.Promise)4 Callback (com.squareup.okhttp.Callback)2 Request (com.squareup.okhttp.Request)2 Response (com.squareup.okhttp.Response)2 IOException (java.io.IOException)2 Uint8Array (com.google.gwt.typedarrays.shared.Uint8Array)1 RequestCommitFileUpload (im.actor.core.api.rpc.RequestCommitFileUpload)1 RequestGetFileUploadPartUrl (im.actor.core.api.rpc.RequestGetFileUploadPartUrl)1 RequestGetFileUploadUrl (im.actor.core.api.rpc.RequestGetFileUploadUrl)1 ResponseCommitFileUpload (im.actor.core.api.rpc.ResponseCommitFileUpload)1 ResponseGetFileUploadUrl (im.actor.core.api.rpc.ResponseGetFileUploadUrl)1 FileReference (im.actor.core.entity.FileReference)1 ModuleActor (im.actor.core.modules.ModuleActor)1 ModuleContext (im.actor.core.modules.ModuleContext)1 RpcCallback (im.actor.core.network.RpcCallback)1 RpcException (im.actor.core.network.RpcException)1 HTTP (im.actor.runtime.HTTP)1 Log (im.actor.runtime.Log)1