diff --git a/contrib/json-simple-1.1.1.jar b/contrib/json-simple-1.1.1.jar new file mode 100644 index 0000000..66347a6 Binary files /dev/null and b/contrib/json-simple-1.1.1.jar differ diff --git a/src/org/hwo/rpc/json/JSONRpcService.java b/src/org/hwo/rpc/json/JSONRpcService.java new file mode 100644 index 0000000..4580f36 --- /dev/null +++ b/src/org/hwo/rpc/json/JSONRpcService.java @@ -0,0 +1,78 @@ +package org.hwo.rpc.json; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.lang.reflect.Array; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.json.simple.JSONValue; + +public class JSONRpcService implements InvocationHandler{ + + private String url; + private Integer nextid; + + public JSONRpcService(String url){ + this.url = url; + this.nextid = 0; + } + + public T createProxy(Class iface){ + T proxy = (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[]{ iface }, this); + return proxy; + } + + @Override + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + + HashMap req = new HashMap(); + req.put("id", this.nextid++ ); + req.put("method", method.getName()); + req.put("params", Arrays.asList(args)); + + String reqString = JSONValue.toJSONString(req); + + System.err.println(reqString); + + URL url = new URL(this.url); + URLConnection conn = url.openConnection(); + conn.setDoInput(true); + conn.setDoOutput(true); + OutputStream out = conn.getOutputStream(); + out.write(reqString.getBytes()); + out.flush(); + out.close(); + + BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream())); + StringBuilder sb = new StringBuilder(); + String line; + + while ((line = reader.readLine())!=null) { + sb.append(line); + } + + System.err.println("Response: " + sb.toString()); + + Map response = (Map)JSONValue.parse(sb.toString()); + + if (response.containsKey("error") && (response.get("error") != null)) { + throw new RuntimeException("JSON-RPC: " + response.get("error")); + } + + return response.get("result"); + } + + +} diff --git a/src/org/hwo/rpc/json/JSONRpcServiceTest.java b/src/org/hwo/rpc/json/JSONRpcServiceTest.java new file mode 100644 index 0000000..88af385 --- /dev/null +++ b/src/org/hwo/rpc/json/JSONRpcServiceTest.java @@ -0,0 +1,77 @@ +package org.hwo.rpc.json; + +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.List; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +public class JSONRpcServiceTest { + + public interface HeloAPI { + public List helo(String client); + } + + public static void main(String[] args) { + + + TrustManager[] trustAllCerts = new TrustManager[]{ + new X509TrustManager() { + + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) + throws CertificateException { + + } + + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) + throws CertificateException { + + } + } + }; + + // Install the all-trusting trust manager + try { + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + } catch (Exception e) { + } + + HostnameVerifier allHostsValid = new HostnameVerifier() { + + @Override + public boolean verify(String arg0, SSLSession arg1) { + return true; + } + }; + + // Install the all-trusting host verifier + HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); + + + + JSONRpcService service = new JSONRpcService("https://ssl.lupus-nobilis.de/dev/timer/timer.py"); + + HeloAPI helo = service.createProxy(HeloAPI.class); + + List ident = helo.helo("me"); + System.err.println("Server: " + ident.get(0) + " Version: " + ident.get(1)); + + + + + } +}