From 2716b28c1d511d4d30efe108b6aaa5ba3368f267 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 3 May 2018 23:20:42 +0900 Subject: [PATCH] ing --- .../commons/rpc/protocol/RPCClientCodec.java | 11 +++ .../protocol/RPCClientNotificationCodec.java | 8 ++ .../rpc/protocol/RPCClientResponseCodec.java | 14 ++++ .../protocol/json/JSONClientNotification.java | 11 +++ .../rpc/protocol/json/JSONClientRequest.java | 23 ++++++ .../rpc/protocol/json/JSONClientResponse.java | 19 +++++ .../rpc/protocol/json/JSONRPCClientCodec.java | 38 ++++++++++ .../json/JSONRPCClientNotificationCodec.java | 68 +++++++++++++++++ .../json/JSONRPCClientResponseCodec.java | 75 +++++++++++++++++++ .../rpc/protocol/json/JSONRPCCodec.java | 24 ++++++ .../rpc/protocol/json/JSONRPCServerCodec.java | 13 +--- 11 files changed, 293 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/RPCClientCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/RPCClientNotificationCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/RPCClientResponseCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientNotification.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientRequest.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientResponse.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientNotificationCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientResponseCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCCodec.java diff --git a/src/main/java/com/loafle/commons/rpc/protocol/RPCClientCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/RPCClientCodec.java new file mode 100644 index 0000000..cbf7c78 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/RPCClientCodec.java @@ -0,0 +1,11 @@ +package com.loafle.commons.rpc.protocol; + +import com.loafle.commons.rpc.RPCException; + +/** + * RPCClientCodec + */ +public interface RPCClientCodec { + byte[] request(String method, Object[] params, Object id) throws RPCException; + RPCClientResponseCodec response(byte[] buf) throws RPCException; +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/RPCClientNotificationCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/RPCClientNotificationCodec.java new file mode 100644 index 0000000..7186cf4 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/RPCClientNotificationCodec.java @@ -0,0 +1,8 @@ +package com.loafle.commons.rpc.protocol; + +/** + * RPCClientNotificationCodec + */ +public interface RPCClientNotificationCodec extends RPCRegistryCodec { + +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/RPCClientResponseCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/RPCClientResponseCodec.java new file mode 100644 index 0000000..b0a6ab4 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/RPCClientResponseCodec.java @@ -0,0 +1,14 @@ +package com.loafle.commons.rpc.protocol; + +import com.loafle.commons.rpc.RPCException; + +/** + * RPCClientResponseCodec + */ +public interface RPCClientResponseCodec { + boolean isNotification(); + RPCClientNotificationCodec notification() throws RPCException; + T result(Class resultType) throws RPCException; + RPCError error(); + Object id(); +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientNotification.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientNotification.java new file mode 100644 index 0000000..eebfe3a --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientNotification.java @@ -0,0 +1,11 @@ +package com.loafle.commons.rpc.protocol.json; + +import java.util.List; + +/** + * JSONClientNotification + */ +public class JSONClientNotification { + public String method; + public List params; +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientRequest.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientRequest.java new file mode 100644 index 0000000..875cbad --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientRequest.java @@ -0,0 +1,23 @@ +package com.loafle.commons.rpc.protocol.json; + +import java.util.List; + +import org.codehaus.jackson.annotate.JsonProperty; + +/** + * JSONClientRequest + */ +public class JSONClientRequest { + @JsonProperty("jsonrpc") + public String version; + public String method; + public Object[] params; + public Object id; + + JSONClientRequest(String version, String method, Object[] params, Object id) { + this.version = version; + this.method = method; + this.params = params; + this.id = id; + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientResponse.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientResponse.java new file mode 100644 index 0000000..3ed4d57 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientResponse.java @@ -0,0 +1,19 @@ +package com.loafle.commons.rpc.protocol.json; + +import com.loafle.commons.rpc.protocol.RPCError; + +import org.codehaus.jackson.annotate.JsonProperty; +import org.codehaus.jackson.annotate.JsonRawValue; + +/** + * JSONClientResponse + */ +public class JSONClientResponse { + @JsonProperty("jsonrpc") + public String version; + + @JsonRawValue + public Object result; + public RPCError error; + public Object id; +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientCodec.java new file mode 100644 index 0000000..e50b06d --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientCodec.java @@ -0,0 +1,38 @@ +package com.loafle.commons.rpc.protocol.json; + +import java.io.IOException; + +import com.loafle.commons.rpc.RPCException; +import com.loafle.commons.rpc.protocol.RPCClientCodec; +import com.loafle.commons.rpc.protocol.RPCClientResponseCodec; + +import org.codehaus.jackson.map.ObjectMapper; + +/** + * JSONRPCClientCodec + */ +public class JSONRPCClientCodec extends JSONRPCCodec implements RPCClientCodec { + public JSONRPCClientCodec() { + } + + public JSONRPCClientCodec(ObjectMapper objectMapper) { + super(objectMapper); + } + + @Override + public byte[] request(String method, Object[] params, Object id) throws RPCException { + JSONClientRequest request = new JSONClientRequest(JSONRPC.version, method, params, id); + + try { + String json = this.objectMapper.writeValueAsString(request); + return json.getBytes(); + } catch (IOException e) { + throw new RPCException("cannot marsharling", e); + } + } + + @Override + public RPCClientResponseCodec response(byte[] buf) throws RPCException { + return new JSONRPCClientResponseCodec(this.objectMapper, buf); + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientNotificationCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientNotificationCodec.java new file mode 100644 index 0000000..8c01e00 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientNotificationCodec.java @@ -0,0 +1,68 @@ +package com.loafle.commons.rpc.protocol.json; + +import java.io.IOException; +import java.lang.reflect.Type; + +import com.google.gson.internal.Primitives; +import com.loafle.commons.rpc.RPCException; +import com.loafle.commons.rpc.protocol.RPCClientNotificationCodec; + +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.JavaType; + +/** + * JSONRPCClientNotificationCodec + */ +public class JSONRPCClientNotificationCodec implements RPCClientNotificationCodec { + private ObjectMapper objectMapper; + private JSONClientNotification notification; + + public JSONRPCClientNotificationCodec(ObjectMapper objectMapper, byte[] buff) throws RPCException { + this.objectMapper = objectMapper; + try { + this.notification = this.objectMapper.readValue(buff, JSONClientNotification.class); + } catch (IOException e) { + throw new RPCException("cannot unmarsharling", e); + } + } + + @Override + public String method() { + return this.notification.method; + } + + @Override + public Object[] params(Type[] paramTypes) throws RPCException { + if (null == paramTypes && null == this.notification.params) { + return null; + } + + if ((null != paramTypes && null == this.notification.params) || null == paramTypes && null != this.notification.params) { + throw new RPCException("params is not valied"); + } + + if (paramTypes.length != this.notification.params.size()) { + throw new RPCException(String.format("The size of params[%d] and types[%d] is not same", + this.notification.params.size(), paramTypes.length)); + } + + Object[] result = new Object[paramTypes.length]; + + for (int i = 0; i < paramTypes.length; i++) { + Type paramType = paramTypes[i]; + String param = this.notification.params.get(i); + + JavaType targetType = objectMapper.getTypeFactory().constructType(paramType); + if (!Primitives.isPrimitive(paramType) && !paramType.getTypeName().equals(String.class.getName())) { + try { + result[i] = objectMapper.readValue(param, targetType); + } catch (IOException e) { + throw new IllegalArgumentException(); + } + } + result[i] = objectMapper.convertValue(param, targetType); + } + + return result; + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientResponseCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientResponseCodec.java new file mode 100644 index 0000000..6f997eb --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientResponseCodec.java @@ -0,0 +1,75 @@ +package com.loafle.commons.rpc.protocol.json; + +import java.io.IOException; + +import com.google.gson.internal.Primitives; +import com.loafle.commons.rpc.RPCException; +import com.loafle.commons.rpc.protocol.RPCClientNotificationCodec; +import com.loafle.commons.rpc.protocol.RPCClientResponseCodec; +import com.loafle.commons.rpc.protocol.RPCError; + +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.JavaType; + +/** + * JSONRPCClientResponseCodec + */ +public class JSONRPCClientResponseCodec implements RPCClientResponseCodec { + private ObjectMapper objectMapper; + private JSONClientResponse response; + + public JSONRPCClientResponseCodec(ObjectMapper objectMapper, byte[] buff) throws RPCException { + this.objectMapper = objectMapper; + try { + this.response = this.objectMapper.readValue(buff, JSONClientResponse.class); + } catch (IOException e) { + throw new RPCException("cannot unmarsharling", e); + } + } + + @Override + public boolean isNotification() { + return null == this.response.id && null == this.response.error && null != this.response.result; + } + + @Override + public RPCClientNotificationCodec notification() throws RPCException { + try { + String json = objectMapper.writeValueAsString(this.response.result); + return new JSONRPCClientNotificationCodec(objectMapper, json.getBytes()); + } catch (IOException e) { + throw new RPCException("cannot marsharling", e); + } + } + + @Override + public T result(Class resultType) throws RPCException { + String json = null; + + try { + json = objectMapper.writeValueAsString(this.response.result); + } catch (IOException e) { + throw new RPCException("cannot marsharling", e); + } + + try { + JavaType targetType = objectMapper.getTypeFactory().constructType(resultType); + if (!Primitives.isPrimitive(resultType) && !resultType.equals(String.class)) { + return objectMapper.readValue(json, targetType); + } + return objectMapper.convertValue(json, targetType); + } catch (IOException e) { + throw new RPCException("cannot unmarsharling", e); + } + } + + @Override + public RPCError error() { + return this.response.error; + } + + @Override + public Object id() { + return this.response.id; + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCCodec.java new file mode 100644 index 0000000..982b980 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCCodec.java @@ -0,0 +1,24 @@ +package com.loafle.commons.rpc.protocol.json; + +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; + +/** + * JSONRPCCodec + */ +public abstract class JSONRPCCodec { + protected ObjectMapper objectMapper; + + public JSONRPCCodec() { + this.objectMapper = new ObjectMapper(); + this.objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + } + + public JSONRPCCodec(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } + + public void setObjectMapper(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java index 42b6eb8..e5a8cea 100644 --- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java @@ -6,26 +6,17 @@ import com.loafle.commons.rpc.RPCException; import com.loafle.commons.rpc.protocol.RPCServerCodec; import com.loafle.commons.rpc.protocol.RPCServerRequestCodec; -import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; /** * JSONRPCServerCodec */ -public class JSONRPCServerCodec implements RPCServerCodec { - private ObjectMapper objectMapper; - +public class JSONRPCServerCodec extends JSONRPCCodec implements RPCServerCodec { public JSONRPCServerCodec() { - this.objectMapper = new ObjectMapper(); - this.objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); } public JSONRPCServerCodec(ObjectMapper objectMapper) { - this.objectMapper = objectMapper; - } - - public void setObjectMapper(ObjectMapper objectMapper) { - this.objectMapper = objectMapper; + super(objectMapper); } public RPCServerRequestCodec request(byte[] buff) throws RPCException {