Search in sources :

Example 1 with HTTPResponse

use of im.actor.runtime.http.HTTPResponse 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 HTTPResponse

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

the class InviteHandler method handleIntent.

public static void handleIntent(BaseActivity activity, Intent intent) {
    if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_VIEW) && intent.getData() != null) {
        String joinGroupUrl = intent.getData().toString();
        if (joinGroupUrl != null && (joinGroupUrl.contains("join") || joinGroupUrl.contains("token"))) {
            String[] urlSplit = null;
            if (joinGroupUrl.contains("join")) {
                urlSplit = joinGroupUrl.split("/join/");
            } else if (joinGroupUrl.contains("token")) {
                urlSplit = joinGroupUrl.split("token=");
            }
            if (urlSplit != null) {
                joinGroupUrl = urlSplit[urlSplit.length - 1];
                final String token = joinGroupUrl;
                HTTP.getMethod(ActorSDK.sharedActor().getInviteDataUrl() + joinGroupUrl, 0, 0, 0).then(new Consumer<HTTPResponse>() {

                    @Override
                    public void apply(HTTPResponse httpResponse) {
                        try {
                            JSONObject data = new JSONObject(new String(httpResponse.getContent(), "UTF-8"));
                            JSONObject group = data.getJSONObject("group");
                            String title = group.getString("title");
                            if (group.has("id") && group.has("isPublic")) {
                                int gid = group.getInt("id");
                                boolean isPublic = group.getBoolean("isPublic");
                                // Check if we have this group
                                try {
                                    GroupVM groupVM = groups().get(gid);
                                    if (groupVM.isMember().get() || isPublic) {
                                        // Have this group, is member or group is public, just open it
                                        activity.startActivity(Intents.openDialog(Peer.group(gid), false, activity));
                                    } else {
                                        // Have this group, but not member, join it
                                        joinViaToken(token, title, activity);
                                    }
                                } catch (Exception e) {
                                    // Do not have this group, join it
                                    if (isPublic) {
                                        messenger().findPublicGroupById(gid).then(peer -> activity.startActivity(Intents.openDialog(peer, false, activity)));
                                    } else {
                                        joinViaToken(token, title, activity);
                                    }
                                }
                            } else {
                                joinViaToken(token, title, activity);
                            }
                        } catch (JSONException | UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }
}
Also used : GroupVM(im.actor.core.viewmodel.GroupVM) JSONObject(im.actor.runtime.json.JSONObject) HTTPResponse(im.actor.runtime.http.HTTPResponse) JSONException(im.actor.runtime.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with HTTPResponse

use of im.actor.runtime.http.HTTPResponse 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();
                // TODO: Better error?
                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 4 with HTTPResponse

use of im.actor.runtime.http.HTTPResponse 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();
                // TODO: Better error?
                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

HTTPResponse (im.actor.runtime.http.HTTPResponse)4 HTTPError (im.actor.runtime.http.HTTPError)3 Promise (im.actor.runtime.promise.Promise)3 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 GroupVM (im.actor.core.viewmodel.GroupVM)1 JsHttpRequest (im.actor.runtime.js.http.JsHttpRequest)1 JSONException (im.actor.runtime.json.JSONException)1 JSONObject (im.actor.runtime.json.JSONObject)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1