Fix compilation and a runtime error.

This commit is contained in:
Kavin 2023-04-19 19:44:04 +01:00
parent d2ca77c625
commit e02a92dcdb
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 60 additions and 52 deletions

66
reqwest-jni/Cargo.lock generated
View file

@ -161,53 +161,53 @@ dependencies = [
[[package]] [[package]]
name = "futures-channel" name = "futures-channel"
version = "0.3.27" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "164713a5a0dcc3e7b4b1ed7d3b433cabc18025386f9339346e8daf15963cf7ac" checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2"
dependencies = [ dependencies = [
"futures-core", "futures-core",
] ]
[[package]] [[package]]
name = "futures-core" name = "futures-core"
version = "0.3.27" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86d7a0c1aa76363dac491de0ee99faf6941128376f1cf96f07db7603b7de69dd" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c"
[[package]] [[package]]
name = "futures-io" name = "futures-io"
version = "0.3.27" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d422fa3cbe3b40dca574ab087abb5bc98258ea57eea3fd6f1fa7162c778b91" checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964"
[[package]] [[package]]
name = "futures-macro" name = "futures-macro"
version = "0.3.27" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eb14ed937631bd8b8b8977f2c198443447a8355b6e3ca599f38c975e5a963b6" checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 1.0.109", "syn 2.0.15",
] ]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.27" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec93083a4aecafb2a80a885c9de1f0ccae9dbd32c2bb54b0c3a65690e0b8d2f2" checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e"
[[package]] [[package]]
name = "futures-task" name = "futures-task"
version = "0.3.27" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd65540d33b37b16542a0438c12e6aeead10d4ac5d05bd3f805b8f35ab592879" checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65"
[[package]] [[package]]
name = "futures-util" name = "futures-util"
version = "0.3.27" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ef6b17e481503ec85211fed8f39d1970f128935ca1f814cd32ac4a6842e84ab" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-io", "futures-io",
@ -222,9 +222,9 @@ dependencies = [
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.3.16" version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be7b54589b581f624f566bf5d8eb2bab1db736c51528720b6bd36b96b55924d" checksum = "17f8a914c2987b688368b5138aa05321db91f4090cf26118185672ad588bce21"
dependencies = [ dependencies = [
"bytes", "bytes",
"fnv", "fnv",
@ -290,9 +290,9 @@ checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "0.14.25" version = "0.14.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc5e554ff619822309ffd57d8734d77cd5ce6238bc956f037ea06c58238c9899" checksum = "ab302d72a6f11a3b910431ff93aae7e773078c769f0a3ef15fb9ec692ed147d4"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-channel", "futures-channel",
@ -347,9 +347,9 @@ dependencies = [
[[package]] [[package]]
name = "ipnet" name = "ipnet"
version = "2.7.1" version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146" checksum = "12b6ee2129af8d4fb011108c73d99a1b83a85977f23b82460c0ae2e25bb4b57f"
[[package]] [[package]]
name = "itoa" name = "itoa"
@ -390,9 +390,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.140" version = "0.2.141"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
@ -505,9 +505,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.53" version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba466839c78239c09faf015484e5cc04860f88242cff4d03eb038f04b4699b73" checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -651,15 +651,15 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.158" version = "1.0.160"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "771d4d9c4163ee138805e12c710dd365e4f44be8be0503cb1bb9eb989425d9c9" checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c"
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.94" version = "1.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea" checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@ -731,9 +731,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.10" version = "2.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aad1363ed6d37b84299588d62d3a7d95b5a5c2d9aad5c85609fda12afaa1f40" checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -757,7 +757,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.10", "syn 2.0.15",
] ]
[[package]] [[package]]
@ -802,7 +802,7 @@ checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.10", "syn 2.0.15",
] ]
[[package]] [[package]]

View file

@ -1,8 +1,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use jni::JNIEnv; use jni::JNIEnv;
use jni::objects::{JClass, JMap, JObject, JString}; use jni::objects::{JByteArray, JClass, JMap, JObject, JString};
use jni::sys::{jbyteArray, jobject}; use jni::sys::jobject;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use reqwest::{Client, Method, Url}; use reqwest::{Client, Method, Url};
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
@ -17,18 +17,18 @@ static CLIENT: Lazy<Client> = Lazy::new(||
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch( pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch(
env: JNIEnv, mut env: JNIEnv,
_: JClass, _: JClass,
url: JString, url: JString,
method: JString, method: JString,
body: jbyteArray, body: JByteArray,
headers: JObject, headers: JObject,
) -> jobject { ) -> jobject {
// set method, url, body, headers // set method, url, body, headers
let method = Method::from_bytes(env.get_string(method).unwrap().to_bytes()).unwrap(); let method = Method::from_bytes(env.get_string(&method).unwrap().to_bytes()).unwrap();
let url = &env.get_string(url).unwrap(); let url = &env.get_string(&url).unwrap();
let url = url.to_str(); let url = url.to_str();
if url.is_err() { if url.is_err() {
@ -38,14 +38,15 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch(
let url = Url::parse(url.unwrap()).unwrap(); let url = Url::parse(url.unwrap()).unwrap();
let body = env.convert_byte_array(body).unwrap_or_default(); let body = env.convert_byte_array(body).unwrap_or_default();
let headers: JMap = JMap::from_env(&env, headers).unwrap(); let java_headers: JMap = JMap::from_env(&mut env, &headers).unwrap();
let headers = headers.iter().unwrap().fold(HashMap::new(), |mut headers, (key, value)| { let mut java_headers = java_headers.iter(&mut env).unwrap();
let mut headers = HashMap::new();
while let Some((key, value)) = java_headers.next(&mut env).unwrap() {
headers.insert( headers.insert(
env.get_string(JString::from(key)).unwrap().to_str().unwrap().to_string(), env.get_string(&JString::from(key)).unwrap().to_str().unwrap().to_string(),
env.get_string(JString::from(value)).unwrap().to_str().unwrap().to_string(), env.get_string(&JString::from(value)).unwrap().to_str().unwrap().to_string(),
); );
headers }
});
let request = CLIENT.request(method, url); let request = CLIENT.request(method, url);
@ -61,19 +62,27 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch(
// send request // send request
let response = RUNTIME.block_on(async { let response = RUNTIME.block_on(async {
request.send().await.unwrap() request.send().await
}); });
if let Err(error) = response {
let error = error.to_string();
env.throw_new("java/lang/RuntimeException", error).unwrap();
return JObject::null().into_raw();
}
let response = response.unwrap();
// get response // get response
let status = response.status().as_u16() as i32; let status = response.status().as_u16() as i32;
let headers = env.new_object("java/util/HashMap", "()V", &[]).unwrap(); let headers = env.new_object("java/util/HashMap", "()V", &[]).unwrap();
let headers: JMap = JMap::from_env(&env, headers).unwrap(); let headers: JMap = JMap::from_env(&mut env, &headers).unwrap();
response.headers().iter().for_each(|(key, value)| { response.headers().iter().for_each(|(key, value)| {
let key = env.new_string(key.as_str()).unwrap(); let key = env.new_string(key.as_str()).unwrap();
let value = env.new_string(value.to_str().unwrap()).unwrap(); let value = env.new_string(value.to_str().unwrap()).unwrap();
headers.put(JObject::from(key), JObject::from(value)).unwrap(); headers.put(&mut env, &JObject::from(key), &JObject::from(value)).unwrap();
}); });
let final_url = response.url().to_string(); let final_url = response.url().to_string();
@ -85,14 +94,13 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch(
let body = env.byte_array_from_slice(&body).unwrap(); let body = env.byte_array_from_slice(&body).unwrap();
let body = unsafe { JObject::from_raw(body) };
// return response // return response
let response = env.new_object("rocks/kavin/reqwest4j/Response", "(ILjava/util/Map;[BLjava/lang/String;)V", &[ let response = env.new_object("rocks/kavin/reqwest4j/Response", "(ILjava/util/Map;[BLjava/lang/String;)V", &[
status.into(), status.into(),
headers.into(), (&headers).into(),
body.into(), (&body).into(),
final_url.into(), (&final_url).into(),
]).unwrap(); ]).unwrap();
response.into_raw() response.into_raw()