summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchRequest.java74
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchResponse.java47
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Error.java84
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java90
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Response.java41
-rw-r--r--sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Result.java34
6 files changed, 370 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchRequest.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchRequest.java
new file mode 100644
index 0000000000..ab4db234cf
--- /dev/null
+++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchRequest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class JsonRpc20BatchRequest {
+ private List<JsonRpc20Request> requests = new ArrayList<JsonRpc20Request>();
+ // The corresponding batch response
+ private JsonRpc20BatchResponse batchResponse;
+
+ public JsonRpc20BatchRequest(JSONArray array) {
+ super();
+ batchResponse = new JsonRpc20BatchResponse();
+ for (int i = 0; i < array.length(); i++) {
+ Object req = null;
+ try {
+ req = array.get(i);
+ } catch (JSONException e1) {
+ // It shouldn't happen
+ throw new IllegalArgumentException(e1);
+ }
+ if (req instanceof JSONObject) {
+ try {
+ requests.add(new JsonRpc20Request((JSONObject)req));
+ batchResponse.getResponses().add(null);
+ } catch (JSONException e) {
+ // We should return invalid request errors
+ JsonRpc20Error error =
+ new JsonRpc20Error(JSONObject.NULL, JsonRpc20Error.PARSE_ERROR, JsonRpc20Error.PARSE_ERROR_MSG,
+ req);
+ batchResponse.getResponses().add(error);
+ }
+ } else {
+ // We should return invalid request errors
+ JsonRpc20Error error =
+ new JsonRpc20Error(JSONObject.NULL, JsonRpc20Error.INVALID_REQUEST,
+ JsonRpc20Error.INVALID_REQUEST_MSG, req);
+ batchResponse.getResponses().add(error);
+ }
+ }
+ }
+
+ public List<JsonRpc20Request> getRequests() {
+ return requests;
+ }
+
+ public JsonRpc20BatchResponse getBatchResponse() {
+ return batchResponse;
+ }
+
+}
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchResponse.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20BatchResponse.java
new file mode 100644
index 0000000000..093b58d18b
--- /dev/null
+++ b/sca-java-2.x/trunk/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.json.JSONArray;
+import org.json.JSONException;
+
+public class JsonRpc20BatchResponse {
+ private List<JsonRpc20Result> results = new ArrayList<JsonRpc20Result>();
+
+ public JsonRpc20BatchResponse() {
+ super();
+ }
+
+ public List<JsonRpc20Result> getResponses() {
+ return results;
+ }
+
+ public JSONArray toJSONArray() throws JSONException {
+ JSONArray jsonArray = new JSONArray();
+ for (JsonRpc20Result result : results) {
+ jsonArray.put(result.toJSONObject());
+ }
+ return jsonArray;
+ }
+
+}
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Error.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Error.java
new file mode 100644
index 0000000000..3568847561
--- /dev/null
+++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Error.java
@@ -0,0 +1,84 @@
+/*
+ * 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.PrintWriter;
+import java.io.StringWriter;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class JsonRpc20Error extends JsonRpc20Result {
+ private final int code; // The error code
+ private final String messaege; // The error message
+ private final Object data; // The json data for the error
+
+ 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(Object id, int code, String messaege, Object data) {
+ super(id);
+ this.code = code;
+ this.messaege = messaege;
+ this.data = data;
+ }
+
+ public JsonRpc20Error(Object id, Throwable t) {
+ super(id);
+ this.code = INTERNAL_ERROR;
+ this.messaege = t.getMessage();
+ this.data = stackTrace(t);
+ }
+
+ private static String stackTrace(Throwable t) {
+ StringWriter sw = new StringWriter();
+ t.printStackTrace(new PrintWriter(sw));
+ return sw.toString();
+ }
+
+ public JSONObject toJSONObject() throws JSONException {
+ JSONObject response = new JSONObject();
+ response.put("jsonrpc", "2.0");
+ response.put("id", id);
+ JSONObject error = new JSONObject();
+ error.put("code", code);
+ error.put("message", messaege);
+ error.put("data", data);
+ response.put("error", error);
+
+ return response;
+ }
+
+}
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java
new file mode 100644
index 0000000000..82f0cf42c3
--- /dev/null
+++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java
@@ -0,0 +1,90 @@
+/*
+ * 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.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class JsonRpc20Request {
+ private final String method;
+ private final Object id;
+ private final Object[] params;
+ private final Map<String, Object> mappedParams;
+
+ public JsonRpc20Request(Object id, String method, Object[] params) {
+ super();
+ this.id = id;
+ this.method = method;
+ this.params = params;
+ this.mappedParams = null;
+ }
+
+ public JsonRpc20Request(Object id, String method, Map<String, Object> mappedParams) {
+ super();
+ this.id = id;
+ this.method = method;
+ this.params = null;
+ this.mappedParams = mappedParams;
+ }
+
+ public JsonRpc20Request(JSONObject req) throws JSONException {
+ super();
+ if (req.has("jsonrpc") && "2.0".equals(req.getString("jsonrpc"))) {
+ method = req.getString("method");
+ id = req.opt("id");
+ Object args = req.opt("params");
+ if (args instanceof JSONArray) {
+ // Positional parameters
+ JSONArray array = (JSONArray)args;
+ params = new Object[array.length()];
+ mappedParams = null;
+ for (int i = 0; i < params.length; i++) {
+ params[i] = array.get(i);
+ }
+ } else if (args instanceof JSONObject) {
+ JSONObject map = (JSONObject)args;
+ mappedParams = new HashMap<String, Object>();
+ params = null;
+ Iterator<String> keys = map.keys();
+ while (keys.hasNext()) {
+ String key = keys.next();
+ Object value = map.get(key);
+ mappedParams.put(key, value);
+ }
+ } else if (args == null) {
+ params = new Object[0];
+ mappedParams = null;
+ } else {
+ throw new IllegalArgumentException("Invalid request: params is not a JSON array or object - " + args);
+ }
+ } else {
+ throw new IllegalArgumentException("Invalid request: missing jsonrpc attribute");
+ }
+ }
+
+ public boolean isNotification() {
+ return id == null;
+ }
+}
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Response.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Response.java
new file mode 100644
index 0000000000..df6f1b9f5b
--- /dev/null
+++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Response.java
@@ -0,0 +1,41 @@
+/*
+ * 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.json.JSONException;
+import org.json.JSONObject;
+
+public class JsonRpc20Response extends JsonRpc20Result {
+ private Object result;
+
+ public JsonRpc20Response(Object id, Object result) {
+ super(id);
+ this.result = result;
+ }
+
+ public JSONObject toJSONObject() throws JSONException {
+ JSONObject response = new JSONObject();
+ response.put("jsonrpc", "2.0");
+ response.put("id", id);
+ response.put("result", result);
+ return response;
+ }
+
+}
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Result.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Result.java
new file mode 100644
index 0000000000..95bec61b4b
--- /dev/null
+++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Result.java
@@ -0,0 +1,34 @@
+/*
+ * 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.json.JSONException;
+import org.json.JSONObject;
+
+public abstract class JsonRpc20Result {
+ protected Object id;
+
+ protected JsonRpc20Result(Object id) {
+ super();
+ this.id = id;
+ }
+
+ public abstract JSONObject toJSONObject() throws JSONException;
+}