From e5b7380c874745c989d1816b8f552504f038e1bc Mon Sep 17 00:00:00 2001 From: lresende Date: Thu, 26 Sep 2013 20:33:20 +0000 Subject: 2.0 branch for possible maintenance release git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1526672 13f79535-47bb-0310-9956-ffa450edef68 --- .../binding/jsonrpc/protocol/JsonRpc10Request.java | 70 +++++++++++++ .../jsonrpc/protocol/JsonRpc10Response.java | 50 +++++++++ .../jsonrpc/protocol/JsonRpc20BatchRequest.java | 66 ++++++++++++ .../jsonrpc/protocol/JsonRpc20BatchResponse.java | 47 +++++++++ .../binding/jsonrpc/protocol/JsonRpc20Error.java | 66 ++++++++++++ .../binding/jsonrpc/protocol/JsonRpc20Request.java | 113 +++++++++++++++++++++ .../jsonrpc/protocol/JsonRpc20Response.java | 37 +++++++ .../binding/jsonrpc/protocol/JsonRpcRequest.java | 92 +++++++++++++++++ .../binding/jsonrpc/protocol/JsonRpcResponse.java | 98 ++++++++++++++++++ 9 files changed, 639 insertions(+) create mode 100644 sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java create mode 100644 sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Response.java create mode 100644 sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchRequest.java create mode 100644 sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchResponse.java create mode 100644 sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Error.java create mode 100644 sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java create mode 100644 sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Response.java create mode 100644 sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcRequest.java create mode 100644 sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcResponse.java (limited to 'sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol') diff --git a/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java new file mode 100644 index 0000000000..547d36abaf --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.jsonrpc.protocol; + +import java.io.OutputStream; + +import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.node.ArrayNode; +import org.codehaus.jackson.node.JsonNodeFactory; +import org.codehaus.jackson.node.ObjectNode; + +/** + * http://json-rpc.org/wiki/specification + * A remote method is invoked by sending a request to a remote service. The request is a single object serialized using JSON. + *
It has three properties: + * + */ +public class JsonRpc10Request extends JsonRpcRequest { + + public JsonRpc10Request(String id, String method, Object[] params) { + super(JsonNodeFactory.instance.textNode(id), method, params); + } + + public JsonRpc10Request(ObjectNode req) { + super(req); + method = req.get("method").getTextValue(); + id = req.get("id"); + JsonNode args = req.get("params"); + if (args instanceof ArrayNode) { + // Positional parameters + ArrayNode array = (ArrayNode)args; + params = new Object[array.size()]; + for (int i = 0; i < params.length; i++) { + params[i] = array.get(i); + } + } else if (args == null) { + params = new Object[0]; + } else { + throw new IllegalArgumentException("Invalid request: params is not a JSON array - " + args); + } + } + + public void write(OutputStream os) throws Exception { + JacksonHelper.MAPPER.writeValue(os, getJsonNode()); + + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Response.java b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Response.java new file mode 100644 index 0000000000..4ca941580a --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Response.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.jsonrpc.protocol; + +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.node.ObjectNode; + +/** + * When the method invocation completes, the service must reply with a response. The response is a single object serialized using JSON. + *
+ * It has three properties: + * + * + */ +public class JsonRpc10Response extends JsonRpcResponse { + + public JsonRpc10Response(JsonNode id, JsonNode result) { + super(id, result); + } + + public JsonRpc10Response(JsonNode id, Throwable t) { + super(id, t); + } + + public JsonRpc10Response(ObjectNode response) { + super(response); + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchRequest.java b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchRequest.java new file mode 100644 index 0000000000..44627ddb08 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchRequest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.jsonrpc.protocol; + +import java.util.ArrayList; +import java.util.List; + +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.node.ArrayNode; +import org.codehaus.jackson.node.ObjectNode; + +public class JsonRpc20BatchRequest { + private List requests = new ArrayList(); + // The corresponding batch response + private JsonRpc20BatchResponse batchResponse; + + public JsonRpc20BatchRequest(ArrayNode array) { + super(); + batchResponse = new JsonRpc20BatchResponse(); + for (int i = 0; i < array.size(); i++) { + JsonNode req = array.get(i); + if (req instanceof ObjectNode) { + try { + requests.add(new JsonRpc20Request((ObjectNode)req)); + batchResponse.getResponses().add(null); + } catch (Exception e) { + // We should return invalid request errors + JsonRpc20Error error = + new JsonRpc20Error(null, JsonRpc20Error.PARSE_ERROR, JsonRpc20Error.PARSE_ERROR_MSG, req); + batchResponse.getResponses().add(error); + } + } else { + // We should return invalid request errors + JsonRpc20Error error = + new JsonRpc20Error(null, JsonRpc20Error.INVALID_REQUEST, JsonRpc20Error.INVALID_REQUEST_MSG, req); + batchResponse.getResponses().add(error); + } + } + } + + public List getRequests() { + return requests; + } + + public JsonRpc20BatchResponse getBatchResponse() { + return batchResponse; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchResponse.java b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchResponse.java new file mode 100644 index 0000000000..d571458192 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchResponse.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.jsonrpc.protocol; + +import java.util.ArrayList; +import java.util.List; + +import org.codehaus.jackson.node.ArrayNode; +import org.codehaus.jackson.node.JsonNodeFactory; + +public class JsonRpc20BatchResponse { + private List results = new ArrayList(); + + public JsonRpc20BatchResponse() { + super(); + } + + public List getResponses() { + return results; + } + + public ArrayNode toJSONArray() { + ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode(); + for (JsonRpcResponse result : results) { + jsonArray.add(result.getJsonNode()); + } + return jsonArray; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Error.java b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Error.java new file mode 100644 index 0000000000..30c61515ee --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Error.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.jsonrpc.protocol; + +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.node.JsonNodeFactory; +import org.codehaus.jackson.node.ObjectNode; + +public class JsonRpc20Error extends JsonRpcResponse { + + public static final int PARSE_ERROR = 32700; + public static final String PARSE_ERROR_MSG = + "Parse error: Invalid JSON was received by the server. " + "An error occurred on the server while parsing the JSON text."; + + public static final int INVALID_REQUEST = -32600; + public static final String INVALID_REQUEST_MSG = "Invalid Request: The JSON sent is not a valid Request object."; + + public static final int METHOD_NOT_FOUND = -32601; + public static final String METHOD_NOT_FOUND_MSG = "Method not found: The method does not exist / is not available."; + + public static final int INVALID_PARAMS = -32602; + public static final String INVALID_PARAMS_MSG = "Invalid params Invalid method parameter(s)."; + + public static final int INTERNAL_ERROR = -32603; + public static final String INTERNAL_ERROR_MSG = "Internal error Internal JSON-RPC error."; + + // -32099 to -32000 Server error Reserved for implementation-defined server-errors. + + public JsonRpc20Error(JsonNode id, Throwable t) { + super(id, t); + this.jsonNode.put("jsonrpc", "2.0"); + this.jsonNode.put("code", INTERNAL_ERROR); + } + + public JsonRpc20Error(JsonNode id, int code, String message, JsonNode data) { + super(id, JsonNodeFactory.instance.nullNode()); + this.jsonNode.put("jsonrpc", "2.0"); + ObjectNode error = JsonNodeFactory.instance.objectNode(); + error.put("code", code); + error.put("message", message); + error.put("data", data); + this.jsonNode.put("error", error); + } + + public JsonRpc20Error(ObjectNode jsonNode) { + super(jsonNode); + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java new file mode 100644 index 0000000000..22b3b7057f --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.jsonrpc.protocol; + +import java.io.OutputStream; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.node.ArrayNode; +import org.codehaus.jackson.node.JsonNodeFactory; +import org.codehaus.jackson.node.ObjectNode; +import org.json.JSONException; + +public class JsonRpc20Request extends JsonRpcRequest { + + protected ObjectNode mappedParams; + + public JsonRpc20Request(String id, String method, Object[] params) { + super(JsonNodeFactory.instance.textNode(id), method, params); + this.jsonNode.put("jsonrpc", "2.0"); + this.mappedParams = null; + } + + public JsonRpc20Request(String id, String method, ObjectNode mappedParams) { + super(JsonNodeFactory.instance.textNode(id), method, null); + this.jsonNode.put("jsonrpc", "2.0"); + this.mappedParams = mappedParams; + this.jsonNode.put("params", mappedParams); + } + + public JsonRpc20Request(ObjectNode req) throws JSONException { + super(req); + JsonNode node = req.get("jsonrpc"); + boolean v20 = node != null && "2.0".equals(node.getTextValue()); + if (!v20) { + throw new IllegalArgumentException("Invalid request: jsonrpc attribute must be \"2.0\""); + } + method = req.get("method").getTextValue(); + JsonNode idNode = req.get("id"); + if (idNode != null) { + id = idNode; + } + JsonNode args = req.get("params"); + if (args instanceof ArrayNode) { + // Positional parameters + ArrayNode array = (ArrayNode)args; + params = new Object[array.size()]; + for (int i = 0; i < params.length; i++) { + params[i] = array.get(i); + } + } else if (args instanceof ObjectNode) { + ObjectNode map = (ObjectNode)args; + mappedParams = map; + params = null; + } else if (args == null) { + params = new Object[0]; + } else { + throw new IllegalArgumentException("Invalid request: params is not a JSON array - " + args); + } + } + + public void write(OutputStream os) throws Exception { + // Construct a map to hold JSON-RPC request + final Map jsonRequest = new HashMap(); + jsonRequest.put("jsonrpc", "2.0"); + jsonRequest.put("id", id); + jsonRequest.put("method", method); + + if (mappedParams != null) { + jsonRequest.put("params", mappedParams); + } + + else { + List parameters = null; + + if (params != null) { + parameters = Arrays.asList(params); + } else { + parameters = Collections.emptyList(); + } + + jsonRequest.put("params", parameters); + } + JacksonHelper.MAPPER.writeValue(os, jsonRequest); + + } + + public ObjectNode getMappedParams() { + return mappedParams; + } +} diff --git a/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Response.java b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Response.java new file mode 100644 index 0000000000..aa433ede6c --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Response.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.jsonrpc.protocol; + +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.node.ObjectNode; + +public class JsonRpc20Response extends JsonRpcResponse { + + public JsonRpc20Response(JsonNode id, JsonNode result) { + super(id, result); + this.jsonNode.put("jsonrpc", "2.0"); + + } + + public JsonRpc20Response(ObjectNode jsonNode) { + super(jsonNode); + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcRequest.java b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcRequest.java new file mode 100644 index 0000000000..cd98a30976 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcRequest.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.jsonrpc.protocol; + +import java.io.OutputStream; + +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.node.ArrayNode; +import org.codehaus.jackson.node.JsonNodeFactory; +import org.codehaus.jackson.node.NullNode; +import org.codehaus.jackson.node.ObjectNode; + +/** + * http://json-rpc.org/wiki/specification + * A remote method is invoked by sending a request to a remote service. The request is a single object serialized using JSON. + *
It has three properties: + *
    + *
  • method - A String containing the name of the method to be invoked. + *
  • params - An Array of objects to pass as arguments to the method. + *
  • id - The request id. This can be of any type. It is used to match the response with the request that it is replying to. + *
+ */ +public abstract class JsonRpcRequest { + protected String method; + protected JsonNode id; + protected Object[] params; + + protected ObjectNode jsonNode; + + public JsonRpcRequest(ObjectNode jsonNode) { + super(); + this.jsonNode = jsonNode; + } + + public JsonRpcRequest(JsonNode id, String method, Object[] params) { + super(); + this.id = id; + this.method = method; + this.params = params; + ObjectNode req = JsonNodeFactory.instance.objectNode(); + req.put("method", method); + req.put("id", id); + if (params != null) { + ArrayNode args = JsonNodeFactory.instance.arrayNode(); + for (Object p : params) { + args.add(JsonNodeFactory.instance.POJONode(p)); + } + req.put("params", args); + } + this.jsonNode = req; + } + + public abstract void write(OutputStream os) throws Exception; + + public boolean isNotification() { + return id == null || (id instanceof NullNode); + } + + public String getMethod() { + return method; + } + + public JsonNode getId() { + return id; + } + + public Object[] getParams() { + return params; + } + + public ObjectNode getJsonNode() { + return jsonNode; + } + +} diff --git a/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcResponse.java b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcResponse.java new file mode 100644 index 0000000000..8186d89030 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcResponse.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.jsonrpc.protocol; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.Writer; + +import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.node.JsonNodeFactory; +import org.codehaus.jackson.node.ObjectNode; + +/** + * When the method invocation completes, the service must reply with a response. The response is a single object serialized using JSON. + *
+ * It has three properties: + * + *
    + *
  • result - The Object that was returned by the invoked method. This must be null in case there was an error invoking the method. + *
  • error - An Error object if there was an error invoking the method. It must be null if there was no error. + *
  • id - This must be the same id as the request it is responding to. + *
+ */ +public abstract class JsonRpcResponse { + public static final int REMOTE_EXCEPTION = 490; + + protected ObjectNode jsonNode; + + public JsonRpcResponse(ObjectNode response) { + super(); + this.jsonNode = response; + } + + public JsonRpcResponse(JsonNode id, Throwable t) { + super(); + ObjectNode response = JsonNodeFactory.instance.objectNode(); + response.put("id", id); + // response.put("result", JsonNodeFactory.instance.nullNode()); + response.put("error", mapError(t)); + this.jsonNode = response; + } + + public JsonRpcResponse(JsonNode id, JsonNode result) { + super(); + ObjectNode response = JsonNodeFactory.instance.objectNode(); + response.put("id", id); + response.put("result", result); + // response.put("error", JsonNodeFactory.instance.nullNode()); + this.jsonNode = response; + } + + private ObjectNode mapError(Throwable t) { + ObjectNode obj = JsonNodeFactory.instance.objectNode(); + // obj.put("msg", t.getMessage()); + obj.put("code", REMOTE_EXCEPTION); + obj.put("message", t.getMessage()); + ObjectNode exception = JsonNodeFactory.instance.objectNode(); + exception.put("class", t.getClass().getName()); + exception.put("message", t.getMessage()); + exception.put("stackTrace", JsonRpc20Error.stackTrace(t)); + obj.put("data", exception); + return obj; + } + + public void write(Writer writer) throws IOException { + JacksonHelper.MAPPER.writeValue(writer, jsonNode); + } + + public static String stackTrace(Throwable t) { + StringWriter sw = new StringWriter(); + t.printStackTrace(new PrintWriter(sw)); + return sw.toString(); + } + + public ObjectNode getJsonNode() { + return jsonNode; + } + +} -- cgit v1.2.3