diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-10-10 07:07:31 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-10-10 07:07:31 +0000 |
commit | cd0891e54e0e4e9123f029f3a4f9689c15598337 (patch) | |
tree | 48c7e5ee175c2cb47adf4aceb619ddf66072bd06 /sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding | |
parent | 5edad53664c23e540bea5ed336bbfa3948bb1035 (diff) |
Fix the invoker to serialize the objects to json
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1180795 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding')
4 files changed, 151 insertions, 79 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java index 82eb6ea2f4..ddd9722836 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java @@ -19,8 +19,14 @@ 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.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -35,21 +41,14 @@ import org.json.JSONObject; * <li>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. * </ul> */ -public class JsonRpc10Request { - private final String method; - private final Object id; - private final Object[] params; +public class JsonRpc10Request extends JsonRpcRequest { public JsonRpc10Request(Object id, String method, Object[] params) { - super(); - this.id = id; - this.method = method; - this.params = params; + super(id, method, params); } public JsonRpc10Request(JSONObject req) throws JSONException { - super(); - + super(req); method = req.getString("method"); id = req.opt("id"); Object args = req.opt("params"); @@ -65,34 +64,25 @@ public class JsonRpc10Request { } else { throw new IllegalArgumentException("Invalid request: params is not a JSON array - " + args); } - } - public JSONObject toJSONObject() throws JSONException { - JSONObject req = new JSONObject(); - req.put("id", id); - req.put("method", method); - if (params != null) { - JSONArray args = new JSONArray(Arrays.asList(params)); - req.put("params", args); - } - return req; - } + public void write(OutputStream os) throws Exception { + // Construct a map to hold JSON-RPC request + final Map<String, Object> map = new HashMap<String, Object>(); + map.put("id", id); + map.put("method", method); - public boolean isNotification() { - return id == null || id == JSONObject.NULL; - } + List<Object> parameters = null; - public String getMethod() { - return method; - } + if (params != null) { + parameters = Arrays.asList(params); + } else { + parameters = Collections.emptyList(); + } - public Object getId() { - return id; - } + map.put("params", parameters); + JacksonHelper.MAPPER.writeValue(os, map); - public Object[] getParams() { - return params; } } 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 index e73f049545..de4862bb9a 100644 --- 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 @@ -19,54 +19,35 @@ 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.Iterator; +import java.util.List; import java.util.Map; +import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; 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 class JsonRpc20Request extends JsonRpcRequest { + + protected Map<String, Object> mappedParams; public JsonRpc20Request(Object id, String method, Object[] params) { - super(); - this.id = id; - this.method = method; - this.params = params; + super(id, method, params); this.mappedParams = null; } public JsonRpc20Request(Object id, String method, Map<String, Object> mappedParams) { - super(); - this.id = id; - this.method = method; - this.params = null; + super(id, method, null); this.mappedParams = mappedParams; } - public JSONObject toJSONObject() throws JSONException { - JSONObject req = new JSONObject(); - req.put("jsonrpc", "2.0"); - req.put("id", id); - req.put("method", method); - if (params != null) { - JSONArray args = new JSONArray(Arrays.asList(params)); - req.put("params", args); - } else { - JSONObject args = new JSONObject(mappedParams); - req.put("params", args); - } - return req; - } - public JsonRpc20Request(JSONObject req) throws JSONException { - super(); + super(req); if (req.has("jsonrpc") && "2.0".equals(req.getString("jsonrpc"))) { method = req.getString("method"); id = req.opt("id"); @@ -100,20 +81,30 @@ public class JsonRpc20Request { } } - public boolean isNotification() { - return id == null || id == JSONObject.NULL; - } + public void write(OutputStream os) throws Exception { + // Construct a map to hold JSON-RPC request + final Map<String, Object> jsonRequest = new HashMap<String, Object>(); + jsonRequest.put("jsonrpc", "2.0"); + jsonRequest.put("id", id); + jsonRequest.put("method", method); - public String getMethod() { - return method; - } + if (mappedParams != null) { + jsonRequest.put("params", mappedParams); + } - public Object getId() { - return id; - } + else { + List<Object> parameters = null; + + if (params != null) { + parameters = Arrays.asList(params); + } else { + parameters = Collections.emptyList(); + } + + jsonRequest.put("params", parameters); + } + JacksonHelper.MAPPER.writeValue(os, jsonRequest); - public Object[] getParams() { - return params; } public Map<String, Object> getMappedParams() { diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcRequest.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcRequest.java new file mode 100644 index 0000000000..8c0a71025a --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcRequest.java @@ -0,0 +1,88 @@ +/* + * 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.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; + +import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; +import org.json.JSONObject; + +/** + * 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. + * <br>It has three properties: + * <ul> + * <li>method - A String containing the name of the method to be invoked. + * <li>params - An Array of objects to pass as arguments to the method. + * <li>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. + * </ul> + */ +public abstract class JsonRpcRequest { + protected String method; + protected Object id; + protected Object[] params; + + protected JSONObject jsonObject; + + public JsonRpcRequest(Object id, String method, Object[] params) { + super(); + this.id = id; + this.method = method; + this.params = params; + } + + protected JsonRpcRequest(JSONObject jsonObject) { + super(); + this.jsonObject = jsonObject; + } + + public JSONObject toJSONObject() throws Exception { + if (jsonObject != null) { + return jsonObject; + } else { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + write(bos); + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + jsonObject = JacksonHelper.MAPPER.readValue(bis, JSONObject.class); + } + return jsonObject; + } + + public abstract void write(OutputStream os) throws Exception; + + public boolean isNotification() { + return id == null || id == JSONObject.NULL; + } + + public String getMethod() { + return method; + } + + public Object getId() { + return id; + } + + public Object[] getParams() { + return params; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcInvoker.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcInvoker.java index b3781d88de..efba2ac1ac 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcInvoker.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcInvoker.java @@ -40,6 +40,7 @@ import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding; import org.apache.tuscany.sca.binding.jsonrpc.protocol.JsonRpc10Request; import org.apache.tuscany.sca.binding.jsonrpc.protocol.JsonRpc20Request; +import org.apache.tuscany.sca.binding.jsonrpc.protocol.JsonRpcRequest; import org.apache.tuscany.sca.databinding.json.JSONDataBinding; import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; import org.apache.tuscany.sca.interfacedef.DataType; @@ -92,21 +93,23 @@ public class JsonRpcInvoker implements Invoker, DataExchangeSemantics { params = (Object[])args; } - JSONObject jsonReq = null; + JsonRpcRequest req = null; if (JSONRPCBinding.VERSION_20.equals(((JSONRPCBinding)endpointReference.getBinding()).getVersion())) { - JsonRpc20Request req = new JsonRpc20Request(requestId, msg.getOperation().getName(), params); - jsonReq = req.toJSONObject(); + req = new JsonRpc20Request(requestId, msg.getOperation().getName(), params); } else { - JsonRpc10Request req = new JsonRpc10Request(requestId, msg.getOperation().getName(), params); - jsonReq = req.toJSONObject(); + req = new JsonRpc10Request(requestId, msg.getOperation().getName(), params); } - final String json = jsonReq.toString(4); + final JsonRpcRequest json = req; // Create content producer so that we can stream the json result out ContentProducer cp = new ContentProducer() { public void writeTo(OutputStream outstream) throws IOException { // mapper.writeValue(outstream, req.toJSONObject().toString()); - outstream.write(json.getBytes("UTF-8")); + try { + json.write(outstream); + } catch (Exception e) { + throw new IOException(e); + } } }; entity = new EntityTemplate(cp); |