forked from LupusNobilis/java-org.hwo
initial commit JSON-RPC
parent
699f0d53fb
commit
14f7700c0b
Binary file not shown.
|
@ -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> T createProxy(Class<T> 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<String, Object> req = new HashMap<String, Object>();
|
||||||
|
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<String, Object> response = (Map<String, Object>)JSONValue.parse(sb.toString());
|
||||||
|
|
||||||
|
if (response.containsKey("error") && (response.get("error") != null)) {
|
||||||
|
throw new RuntimeException("JSON-RPC: " + response.get("error"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.get("result");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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<String> 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<String> ident = helo.helo("me");
|
||||||
|
System.err.println("Server: " + ident.get(0) + " Version: " + ident.get(1));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue