-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82c18e9
commit 119a957
Showing
8 changed files
with
263 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/com/rabbitmq/tools/jsonrpc/DefaultJsonRpcMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.rabbitmq.tools.jsonrpc; | ||
|
||
import com.rabbitmq.tools.json.JSONReader; | ||
import com.rabbitmq.tools.json.JSONWriter; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* | ||
*/ | ||
public class DefaultJsonRpcMapper implements JsonRpcMapper { | ||
|
||
@Override | ||
public JsonRpcRequest parse(String requestBody, ServiceDescription description) { | ||
Map<String, Object> request = (Map<String,Object>) new JSONReader().read(requestBody); | ||
|
||
return new JsonRpcRequest( | ||
request.get("id"), request.get("version").toString(), request.get("method").toString(), | ||
((List<?>) request.get("params")).toArray() | ||
); | ||
} | ||
|
||
@Override | ||
public JsonRpcResponse parse(String responseBody) { | ||
Map<String, Object> map = (Map<String, Object>) (new JSONReader().read(responseBody)); | ||
Map<String, Object> error; | ||
JsonRpcException exception = null; | ||
if (map.containsKey("error")) { | ||
error = (Map<String, Object>) map.get("error"); | ||
exception = new JsonRpcException( | ||
new JSONWriter().write(error), | ||
(String) error.get("name"), | ||
error.get("code") == null ? 0 : (Integer) error.get("code"), | ||
(String) error.get("message"), | ||
error | ||
); | ||
} | ||
return new JsonRpcResponse(map, map.get("result"), map.get("error"), exception); | ||
} | ||
|
||
@Override | ||
public Object[] parameters(JsonRpcRequest request, Method method) { | ||
Object[] parameters = request.getParameters(); | ||
Object[] convertedParameters = new Object[parameters.length]; | ||
Class<?>[] parameterTypes = method.getParameterTypes(); | ||
for (int i = 0; i < parameters.length; i++) { | ||
convertedParameters[i] = convert(parameters[i], parameterTypes[i]); | ||
} | ||
return convertedParameters; | ||
} | ||
|
||
@Override | ||
public String write(Object input) { | ||
return new JSONWriter().write(input); | ||
} | ||
|
||
protected Object convert(Object input, Class<?> expectedClass) { | ||
return input; | ||
// if (input == null || input.getClass().equals(expectedClass)) { | ||
// return input; | ||
// } | ||
// System.err.println(input.getClass() + " " + expectedClass); | ||
// if (Long.class.equals(expectedClass) && input instanceof Integer) { | ||
// return Long.valueOf(((Integer) input).longValue()); | ||
// } else if (long.class.equals(expectedClass) && input instanceof Integer) { | ||
// return | ||
// } | ||
// return input; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/com/rabbitmq/tools/jsonrpc/JsonRpcMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.rabbitmq.tools.jsonrpc; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* | ||
*/ | ||
public interface JsonRpcMapper { | ||
|
||
JsonRpcRequest parse(String requestBody, ServiceDescription description); | ||
|
||
JsonRpcResponse parse(String responseBody); | ||
|
||
Object[] parameters(JsonRpcRequest request, Method method); | ||
|
||
String write(Object input); | ||
|
||
class JsonRpcRequest { | ||
|
||
private final Object id; | ||
private final String version; | ||
private final String method; | ||
private final Object[] parameters; | ||
|
||
public JsonRpcRequest(Object id, String version, String method, Object[] parameters) { | ||
this.id = id; | ||
this.version = version; | ||
this.method = method; | ||
this.parameters = parameters; | ||
} | ||
|
||
public Object getId() { | ||
return id; | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
|
||
public String getMethod() { | ||
return method; | ||
} | ||
|
||
public Object[] getParameters() { | ||
return parameters; | ||
} | ||
|
||
public boolean isSystem() { | ||
return method.startsWith("system."); | ||
} | ||
|
||
public boolean isSystemDescribe() { | ||
return "system.describe".equals(method); | ||
} | ||
|
||
} | ||
|
||
class JsonRpcResponse { | ||
|
||
private final Object reply; | ||
private final Object result; | ||
private final Object error; | ||
private final JsonRpcException exception; | ||
|
||
public JsonRpcResponse(Object reply, Object result, Object error, JsonRpcException exception) { | ||
this.reply = reply; | ||
this.result = result; | ||
this.error = error; | ||
this.exception = exception; | ||
} | ||
|
||
public Object getReply() { | ||
return reply; | ||
} | ||
|
||
public Object getError() { | ||
return error; | ||
} | ||
|
||
public Object getResult() { | ||
return result; | ||
} | ||
|
||
public JsonRpcException getException() { | ||
return exception; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.