mirror of
				https://gitee.com/BDWare/sdk-java
				synced 2025-11-03 21:32:15 +00:00 
			
		
		
		
	update http server
This commit is contained in:
		
							parent
							
								
									f7bbcf8208
								
							
						
					
					
						commit
						ad411e5e6c
					
				@ -5,7 +5,7 @@ plugins {
 | 
			
		||||
    id 'signing'
 | 
			
		||||
}
 | 
			
		||||
group 'org.bdware.bdcontract'
 | 
			
		||||
version '1.0.1'
 | 
			
		||||
version '1.0.2'
 | 
			
		||||
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()) {
 | 
			
		||||
            req.put("arg", "undefined");
 | 
			
		||||
        } else {
 | 
			
		||||
            if (cr.getArg().isJsonPrimitive())
 | 
			
		||||
                req.put("arg", cr.getArg().getAsString());
 | 
			
		||||
            else
 | 
			
		||||
                req.put("arg", cr.getArg().toString());
 | 
			
		||||
        }
 | 
			
		||||
        return req;
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,6 @@
 | 
			
		||||
package org.bdware.client;
 | 
			
		||||
 | 
			
		||||
import com.google.gson.Gson;
 | 
			
		||||
import com.google.gson.JsonObject;
 | 
			
		||||
import com.google.gson.JsonParser;
 | 
			
		||||
import org.zz.gmhelper.SM2KeyPair;
 | 
			
		||||
@ -12,6 +13,7 @@ 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;
 | 
			
		||||
@ -85,21 +87,34 @@ public class SmartContractHttpClient {
 | 
			
		||||
            ret.put("responseCode", 505);
 | 
			
		||||
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
 | 
			
		||||
            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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public static Map<String, Object> httpGet(String str) {
 | 
			
		||||
        Map<String, Object> ret = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            URL url = new URL(str);
 | 
			
		||||
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 | 
			
		||||
            connection.setConnectTimeout(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());
 | 
			
		||||
            InputStream input = connection.getInputStream();
 | 
			
		||||
            Scanner sc = new Scanner(input);
 | 
			
		||||
            StringBuilder sb = new StringBuilder();
 | 
			
		||||
            while (sc.hasNextLine()) {
 | 
			
		||||
@ -111,11 +126,18 @@ public class SmartContractHttpClient {
 | 
			
		||||
            ret.put("responseCode", 505);
 | 
			
		||||
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
 | 
			
		||||
            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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public JsonObject executeContract(String id, String operation, String arg) {
 | 
			
		||||
        ContractRequest cr = new ContractRequest();
 | 
			
		||||
        cr.setContractID(id);
 | 
			
		||||
@ -124,6 +146,7 @@ public class SmartContractHttpClient {
 | 
			
		||||
        cr.doSignature(keyPair);
 | 
			
		||||
        String url = getExecuteUrl(ip, port, cr);
 | 
			
		||||
        String str = (String) httpGet(url).get("response");
 | 
			
		||||
 | 
			
		||||
        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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user