Search in sources :

Example 1 with JsHttpRequest

use of im.actor.runtime.js.http.JsHttpRequest 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 2 with JsHttpRequest

use of im.actor.runtime.js.http.JsHttpRequest in project actor-platform by actorapp.

the class JsSmallAvatarFileCache method onAvatarUrlLoaded.

private void onAvatarUrlLoaded(final long id, String url) {
    JsHttpRequest request = JsHttpRequest.create();
    request.open("GET", url);
    request.setOnLoadHandler(new JsHttpRequestHandler() {

        @Override
        public void onStateChanged(JsHttpRequest request) {
            if (request.getReadyState() == 4) {
                if (request.getStatus() == 200) {
                    JsBlob blob = request.getResponseBlob();
                    onAvatarDownloaded(id, blob);
                } else {
                    // TODO: Implement better
                    cachedImages.put(id, null);
                }
            }
        }
    });
    request.send();
}
Also used : JsHttpRequestHandler(im.actor.runtime.js.http.JsHttpRequestHandler) JsHttpRequest(im.actor.runtime.js.http.JsHttpRequest) JsBlob(im.actor.runtime.js.fs.JsBlob)

Aggregations

JsHttpRequest (im.actor.runtime.js.http.JsHttpRequest)2 Uint8Array (com.google.gwt.typedarrays.shared.Uint8Array)1 HTTPError (im.actor.runtime.http.HTTPError)1 HTTPResponse (im.actor.runtime.http.HTTPResponse)1 JsBlob (im.actor.runtime.js.fs.JsBlob)1 JsHttpRequestHandler (im.actor.runtime.js.http.JsHttpRequestHandler)1 Promise (im.actor.runtime.promise.Promise)1