org.bdware.client.SmartContractHttpClient Maven / Gradle / Ivy
package org.bdware.client;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.zz.gmhelper.SM2KeyPair;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.Callable;
public class SmartContractHttpClient {
private final SM2KeyPair keyPair;
String ip;
int port;
boolean isPost;
public SmartContractHttpClient(SM2KeyPair pair, String ip, int port) {
this(pair, ip, port, "GET");
}
public SmartContractHttpClient(SM2KeyPair pair, String ip, int port, String method) {
this.ip = ip;
this.port = port;
this.keyPair = pair;
this.isPost = method.equalsIgnoreCase("POST");
}
public static String getExecuteUrl(String ip, int port, ContractRequest cr) {
try {
String str;
if (cr.getArg() == null) str = "";
else if (cr.getArg().isJsonPrimitive())
str = cr.getArg().getAsString();
else str = cr.getArg().toString();
return String.format(
"http://%s:%d/SCIDE/CMManager?action=executeContract&contractID=%s&operation=%s&arg=%s&pubkey=%s&signature=%s",
ip,
port,
cr.getContractID(),
cr.getAction(),
URLEncoder.encode(str, "utf-8"),
cr.getPublicKey(),
cr.signature);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static Map httpPost(String str, String body) {
Map ret = new HashMap<>();
try {
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
OutputStreamWriter out =
new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
out.append((body));
out.flush();
out.close();
ret.put("responseCode", connection.getResponseCode());
InputStream input = connection.getInputStream();
Scanner sc = new Scanner(input);
StringBuilder sb = new StringBuilder();
while (sc.hasNextLine()) {
sb.append(sc.nextLine()).append("\n");
}
sc.close();
ret.put("response", sb.toString());
} catch (Throwable e) {
ret.put("responseCode", 505);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo));
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;
}
public static Map httpGet(String str) {
Map ret = new HashMap<>();
try {
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(40000);
connection.setReadTimeout(40000);
Callable callable = new Callable() {
@Override
public InputStream call() throws Exception {
return connection.getInputStream();
}
};
InputStream input = TimeoutUtil.runWithTimeout(callable, 2000);
assert input != null;
ret.put("responseCode", connection.getResponseCode());
Scanner sc = new Scanner(input);
StringBuilder sb = new StringBuilder();
while (sc.hasNextLine()) {
sb.append(sc.nextLine()).append("\n");
}
sc.close();
ret.put("response", sb.toString());
} catch (Throwable e) {
ret.put("responseCode", 505);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo));
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;
}
public JsonObject executeContract(String id, String operation, String arg) {
ContractRequest cr = new ContractRequest();
cr.setContractID(id);
cr.setAction(operation);
cr.setArg(arg);
cr.doSignature(keyPair);
String url = getExecuteUrl(ip, port, cr);
String str = (String) httpGet(url).get("response");
return JsonParser.parseString(str).getAsJsonObject();
}
public String executeContractAsString(String id, String operation, String arg) {
ContractRequest cr = new ContractRequest();
cr.setContractID(id);
cr.setAction(operation);
cr.setArg(arg);
cr.doSignature(keyPair);
if (isPost) {
JsonObject jo = new JsonObject();
String url = String.format("http://%s:%d/SCIDE/CMManager", ip, port);
jo.addProperty("action", "executeContract");
jo.addProperty("contractID", cr.getContractID());
jo.addProperty("operation", cr.getAction());
jo.add("arg", cr.getArg());
jo.addProperty("pubkey", cr.getPublicKey());
jo.addProperty("signature", cr.getSignature());
String str = (String) httpPost(url, jo.toString()).get("response");
return str;
} else {
String url = getExecuteUrl(ip, port, cr);
String str = (String) httpGet(url).get("response");
return str;
}
}
public String getExecuteUrl(String id, String operation, String arg) {
ContractRequest cr = new ContractRequest();
cr.setContractID(id);
cr.setAction(operation);
cr.setArg(arg);
cr.doSignature(keyPair);
String url = getExecuteUrl(ip, port, cr);
return url;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy