add getHttpResponse to Rpc

This commit is contained in:
adbenitez
2023-05-01 14:22:39 -04:00
parent cbec549918
commit 0b85cd2b9e
3 changed files with 42 additions and 5 deletions
@@ -0,0 +1,33 @@
package com.b44t.messenger.rpc;
import android.util.Base64;
public class HttpResponse {
// base64-encoded response body.
private String blob;
// MIME type, e.g. "text/plain" or "text/html".
private String mimetype;
// Encoding, e.g. "utf-8".
private String encoding;
public HttpResponse(String blob, String mimetype, String encoding) {
this.blob = blob;
this.mimetype = mimetype;
this.encoding = encoding;
}
public byte[] getBlob() {
if (blob == null) {
return null;
}
return Base64.decode(blob, Base64.NO_WRAP | Base64.NO_PADDING);
}
public String getMimetype() {
return mimetype;
}
public String getEncoding() {
return encoding;
}
}
+4
View File
@@ -96,6 +96,10 @@ public class Rpc {
return gson.fromJson(getResult("get_system_info"), mapType);
}
public HttpResponse getHttpResponse(int accountId, String url) throws RpcException {
return gson.fromJson(getResult("get_http_response", accountId, url), HttpResponse.class);
}
private static class Request {
public String jsonrpc = "2.0";
@@ -9,7 +9,8 @@ import android.webkit.WebSettings;
import androidx.appcompat.app.AlertDialog;
import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcHttpResponse;
import com.b44t.messenger.rpc.HttpResponse;
import com.b44t.messenger.rpc.Rpc;
import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.util.DynamicTheme;
@@ -25,6 +26,7 @@ public class FullMsgActivity extends WebViewActivity
public static final String IS_CONTACT_REQUEST = "is_contact_request";
private int msgId;
private DcContext dcContext;
private Rpc rpc;
private boolean loadRemoteContent = false;
private boolean isContactRequest;
@@ -60,6 +62,7 @@ public class FullMsgActivity extends WebViewActivity
webView.getSettings().setAllowFileAccess(false);
dcContext = DcHelper.getContext(this);
rpc = DcHelper.getRpc(this);
msgId = getIntent().getIntExtra(MSG_ID_EXTRA, 0);
String title = dcContext.getMsg(msgId).getSubject();
if (title.isEmpty()) title = getString(R.string.chat_input_placeholder);
@@ -178,10 +181,7 @@ public class FullMsgActivity extends WebViewActivity
if (url == null) {
throw new Exception("no url specified");
}
DcHttpResponse httpResponse = dcContext.getHttpResponse(url);
if (httpResponse == null) {
throw new Exception(dcContext.getLastError());
}
HttpResponse httpResponse = rpc.getHttpResponse(dcContext.getAccountId(), url);
String mimeType = httpResponse.getMimetype();
if (mimeType == null) {
mimeType = "application/octet-stream";