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());
});
}
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();
}
}
});
}
}
}
}
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()));
}
}
});
});
}
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()));
}
}
});
});
}
Aggregations