mirror of
https://gitee.com/BDWare/sdk-java
synced 2025-01-10 01:44:19 +00:00
update http server
This commit is contained in:
parent
f7bbcf8208
commit
ad411e5e6c
@ -5,7 +5,7 @@ plugins {
|
|||||||
id 'signing'
|
id 'signing'
|
||||||
}
|
}
|
||||||
group 'org.bdware.bdcontract'
|
group 'org.bdware.bdcontract'
|
||||||
version '1.0.1'
|
version '1.0.2'
|
||||||
sourceCompatibility = 1.8
|
sourceCompatibility = 1.8
|
||||||
|
|
||||||
|
|
||||||
|
13
src/main/sdk/org/bdware/client/ContractResult.java
Normal file
13
src/main/sdk/org/bdware/client/ContractResult.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package org.bdware.client;
|
||||||
|
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
|
||||||
|
public class ContractResult {
|
||||||
|
public Status status;
|
||||||
|
public JsonElement result;
|
||||||
|
|
||||||
|
public enum Status {
|
||||||
|
Success, Exception, Error, Executing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -227,6 +227,9 @@ public class SmartContractClient extends SmartContractHandler {
|
|||||||
if (null == cr.getArg()) {
|
if (null == cr.getArg()) {
|
||||||
req.put("arg", "undefined");
|
req.put("arg", "undefined");
|
||||||
} else {
|
} else {
|
||||||
|
if (cr.getArg().isJsonPrimitive())
|
||||||
|
req.put("arg", cr.getArg().getAsString());
|
||||||
|
else
|
||||||
req.put("arg", cr.getArg().toString());
|
req.put("arg", cr.getArg().toString());
|
||||||
}
|
}
|
||||||
return req;
|
return req;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package org.bdware.client;
|
package org.bdware.client;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import org.zz.gmhelper.SM2KeyPair;
|
import org.zz.gmhelper.SM2KeyPair;
|
||||||
@ -12,6 +13,7 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
public class SmartContractHttpClient {
|
public class SmartContractHttpClient {
|
||||||
private final SM2KeyPair keyPair;
|
private final SM2KeyPair keyPair;
|
||||||
@ -85,21 +87,34 @@ public class SmartContractHttpClient {
|
|||||||
ret.put("responseCode", 505);
|
ret.put("responseCode", 505);
|
||||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||||
e.printStackTrace(new PrintStream(bo));
|
e.printStackTrace(new PrintStream(bo));
|
||||||
ret.put("response", bo.toString());
|
ContractResult cr = new ContractResult();
|
||||||
|
JsonObject jo = new JsonObject();
|
||||||
|
jo.addProperty("msg", bo.toString());
|
||||||
|
jo.addProperty("code", 1);
|
||||||
|
cr.result = jo;
|
||||||
|
cr.status = ContractResult.Status.Exception;
|
||||||
|
ret.put("response", new Gson().toJson(cr));
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Map<String, Object> httpGet(String str) {
|
public static Map<String, Object> httpGet(String str) {
|
||||||
Map<String, Object> ret = new HashMap<>();
|
Map<String, Object> ret = new HashMap<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
URL url = new URL(str);
|
URL url = new URL(str);
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setConnectTimeout(40000);
|
connection.setConnectTimeout(40000);
|
||||||
connection.setReadTimeout(40000);
|
connection.setReadTimeout(40000);
|
||||||
|
Callable<InputStream> callable = new Callable<InputStream>() {
|
||||||
|
@Override
|
||||||
|
public InputStream call() throws Exception {
|
||||||
|
return connection.getInputStream();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
InputStream input = TimeoutUtil.runWithTimeout(callable, 2000);
|
||||||
|
assert input != null;
|
||||||
ret.put("responseCode", connection.getResponseCode());
|
ret.put("responseCode", connection.getResponseCode());
|
||||||
InputStream input = connection.getInputStream();
|
|
||||||
Scanner sc = new Scanner(input);
|
Scanner sc = new Scanner(input);
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
while (sc.hasNextLine()) {
|
while (sc.hasNextLine()) {
|
||||||
@ -111,11 +126,18 @@ public class SmartContractHttpClient {
|
|||||||
ret.put("responseCode", 505);
|
ret.put("responseCode", 505);
|
||||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||||
e.printStackTrace(new PrintStream(bo));
|
e.printStackTrace(new PrintStream(bo));
|
||||||
ret.put("response", bo.toString());
|
ContractResult cr = new ContractResult();
|
||||||
|
JsonObject jo = new JsonObject();
|
||||||
|
jo.addProperty("msg", bo.toString());
|
||||||
|
jo.addProperty("code", 1);
|
||||||
|
cr.result = jo;
|
||||||
|
cr.status = ContractResult.Status.Exception;
|
||||||
|
ret.put("response", new Gson().toJson(cr));
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public JsonObject executeContract(String id, String operation, String arg) {
|
public JsonObject executeContract(String id, String operation, String arg) {
|
||||||
ContractRequest cr = new ContractRequest();
|
ContractRequest cr = new ContractRequest();
|
||||||
cr.setContractID(id);
|
cr.setContractID(id);
|
||||||
@ -124,6 +146,7 @@ public class SmartContractHttpClient {
|
|||||||
cr.doSignature(keyPair);
|
cr.doSignature(keyPair);
|
||||||
String url = getExecuteUrl(ip, port, cr);
|
String url = getExecuteUrl(ip, port, cr);
|
||||||
String str = (String) httpGet(url).get("response");
|
String str = (String) httpGet(url).get("response");
|
||||||
|
|
||||||
return JsonParser.parseString(str).getAsJsonObject();
|
return JsonParser.parseString(str).getAsJsonObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
39
src/main/sdk/org/bdware/client/TimeoutUtil.java
Normal file
39
src/main/sdk/org/bdware/client/TimeoutUtil.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package org.bdware.client;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class TimeoutUtil {
|
||||||
|
static ExecutorService executor = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
|
public static Callable<Object> wrapFunction(Object callback, Object arg) {
|
||||||
|
Callable<Object> callable = new Callable<Object>() {
|
||||||
|
@Override
|
||||||
|
public Object call() throws Exception {
|
||||||
|
Class clz = Class.forName("org.bdware.sc.boundry.JavaScriptEntry");
|
||||||
|
Class scriptFunction = Class.forName("wrp.jdk.nashorn.internal.runtime.ScriptFunction");
|
||||||
|
Class sco = Class.forName("wrp.jdk.nashorn.api.scripting.ScriptObjectMirror");
|
||||||
|
Field field = sco.getDeclaredField("sobj");
|
||||||
|
field.setAccessible(true);
|
||||||
|
Method method = clz.getDeclaredMethod("executeFunction", scriptFunction, Object.class);
|
||||||
|
return method.invoke(null, field.get(callback), arg);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return callable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T runWithTimeout(Callable<T> callable, int timeout) {
|
||||||
|
T ret;
|
||||||
|
try {
|
||||||
|
ret = executor.submit(callable).get(timeout, TimeUnit.MILLISECONDS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
ret = null;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user