Skip to content
This repository was archived by the owner on Jan 2, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
# JsonRpc
# JsonRpc (fork)

This fork adds support for gwtjsonrpc.

Here is an example:
```java
public class Client {
public static void main(String [] args) throws MalformedURLException {
String url = "http://localhost:8080/proj/MyJsonServiceImpl";
HttpJsonRpcClientTransport transport = new HttpJsonRpcClientTransport(new URL(url));
GwtJsonRpcInvoker invoker = new GwtJsonRpcInvoker();
MyJsonService service = invoker.get(transport, MyJsonService.class);

service.sayHello("Hello from the Client", new AsyncCallback<String>() {
@Override
public void onSuccess(String result) {
System.out.println("Server says:" + result);

}
@Override
public void onFailure(Throwable caught) {
System.out.println(":(");
}
});
}
}
```

You need to add @RpcImpl(version= Version.V2_0) to your interface, and @AllowCrossSiteRequest to your methods.


[![Build Status](https://travis-ci.org/RitwikSaikia/jsonrpc.png?branch=master)](https://travis-ci.org/RitwikSaikia/jsonrpc)

JSON-RPC is a Java library implementing a very light weight client/server functionality of JSON-RPC protocol.
Server/Client API is designed in such a way that, you don't have to worry about the details of the protocol.
Just register the implementation classes in the server side, and create remote proxy objects at the client sides on the fly.

The API works well in **Android/Google App Engine/Javascript** applications.
The API works well in **Android/Google App Engine/GWT/Javascript** applications.
<br /><br /><br /><br /><br />

# Usage
Expand Down
47 changes: 30 additions & 17 deletions jsonrpc-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.json.rpc</groupId>
<artifactId>jsonrpc</artifactId>
<name>JsonRpc</name>
<version>1.0</version>
<version>1.0a</version>

<properties>
<java.version>1.5</java.version>
Expand All @@ -13,6 +13,12 @@
</properties>

<dependencies>
<dependency>
<groupId>gwtjsonrpc</groupId>
<artifactId>gwtjsonrpc</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down Expand Up @@ -241,20 +247,27 @@
<tag>jsonrpc-1.0</tag>
</scm>

<repositories>
<repository>
<id>ritwik-mvn-repo</id>
<url>http://ritwik.net/mvn/releases/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<repositories>
<repository>
<id>galaxynexus</id>
<url>http://192.168.1.6:8090/nexus/content/repositories/galaxynexus</url>
</repository>
<repository>
<id>thirdparty</id>
<url>http://192.168.1.6:8090/nexus/content/repositories/thirdparty/</url>
</repository>

<repository>
<id>gson</id>
<url>http://google-gson.googlecode.com/svn/mavenrepo</url>
</repository>
</repositories>

<distributionManagement>
<repository>
<id>galaxynexus</id>
<name>galaxynexus</name>
<url>http://192.168.1.6:8090/nexus/content/repositories/galaxynexus</url>
</repository>
</distributionManagement>
</project>
153 changes: 153 additions & 0 deletions jsonrpc-java/src/main/java/org/json/rpc/client/GwtJsonRpcInvoker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright (C) 2011 ritwik.net
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.json.rpc.client;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gwtjsonrpc.common.AsyncCallback;

import org.json.rpc.commons.GsonTypeChecker;
import org.json.rpc.commons.JsonRpcClientException;
import org.json.rpc.commons.JsonRpcRemoteException;
import org.json.rpc.commons.TypeChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.StringReader;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.util.Random;

public final class GwtJsonRpcInvoker {

private static final Logger LOG = LoggerFactory.getLogger(GwtJsonRpcInvoker.class);

private final Random rand = new Random();

private final TypeChecker typeChecker;

public GwtJsonRpcInvoker() {
this(new GsonTypeChecker());
}

public GwtJsonRpcInvoker(TypeChecker typeChecker) {
this.typeChecker = typeChecker;
}

public <T> T get(final JsonRpcClientTransport transport, final Class<T>... classes) {
for (Class<T> clazz : classes) {
typeChecker.isValidInterface(clazz);
}
return (T) Proxy.newProxyInstance(GwtJsonRpcInvoker.class.getClassLoader(), classes, new InvocationHandler() {

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return GwtJsonRpcInvoker.this.invoke(transport, method, args);
}
});
}


private Object invoke(JsonRpcClientTransport transport, Method method, Object[] args) throws Throwable {
int id = rand.nextInt(Integer.MAX_VALUE);
String methodName = method.getName();

Gson gson = new Gson();

AsyncCallback callback = null;
for (Object obj: args) {
if (obj instanceof AsyncCallback) {
callback = (AsyncCallback) obj;
}
}

JsonObject req = new JsonObject();
req.addProperty("id", id);
req.addProperty("method", methodName);
req.addProperty("jsonrpc","2.0");
JsonArray params = new JsonArray();
if (args != null) {
for (Object o : args) {
if (!(o instanceof AsyncCallback))
params.add(gson.toJsonTree(o));
}
}

req.add("params", params);

String requestData = req.toString();
LOG.debug("JSON-RPC >> {}", requestData);
String responseData;
try {
responseData = transport.call(requestData);
} catch (Exception e) {
throw new JsonRpcClientException("unable to get data from transport", e);
}
LOG.debug("JSON-RPC << {}", responseData);

JsonParser parser = new JsonParser();
JsonObject resp = (JsonObject) parser.parse(new StringReader(responseData));

JsonElement result = resp.get("result");
JsonElement error = resp.get("error");

if (error != null && !error.isJsonNull()) {
if (error.isJsonPrimitive()) {
if (callback!=null)
callback.onFailure(new JsonRpcRemoteException(error.getAsString()));
else
throw new JsonRpcRemoteException(error.getAsString());
} else if (error.isJsonObject()) {
JsonObject o = error.getAsJsonObject();
Integer code = (o.has("code") ? o.get("code").getAsInt() : null);
String message = (o.has("message") ? o.get("message").getAsString() : null);
String data = (o.has("data") ? (o.get("data") instanceof JsonObject ? o.get("data").toString() : o.get("data").getAsString()) : null);
if (callback!=null)
callback.onFailure(new JsonRpcRemoteException(code, message, data));
else
throw new JsonRpcRemoteException(code, message, data);
} else {
if (callback !=null)
callback.onFailure(new JsonRpcRemoteException("unknown error, data = " + error.toString()));
else
throw new JsonRpcRemoteException("unknown error, data = " + error.toString());
}
}

if (method.getReturnType() == void.class) {
if (callback!=null) {
Class last = null;

for (Method m: callback.getClass().getMethods()) {
if (m.getName().equals("onSuccess")) {
for (Class clazz : m.getParameterTypes()) {
last = clazz;
}
}
}
if (last!=null) {
callback.onSuccess(gson.fromJson(result.toString(), last));
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ private String post(URL url, Map<String, String> headers, String data)
}

connection.addRequestProperty("Accept-Encoding", "gzip");
connection.addRequestProperty("Content-Type","application/json; charset=utf-8");
connection.addRequestProperty("Accept","application/json");

connection.setRequestMethod("POST");
connection.setDoOutput(true);
Expand Down
Loading