diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-24 01:10:24 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-24 01:10:24 +0000 |
commit | f34c108a6d08a7b1b49fb850aad8febd1330a0cc (patch) | |
tree | d7743bd241c96f0bedf8c21b71fd299ebe71cbe8 /sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java | |
parent | 3f1c188f3f5fc65904e8234ba56e8d64904459e1 (diff) |
Tweak the JSON-RPC binding to work without Java interfaces and recognize dynamic interfaces bound to the JSON databinding.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@988365 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r-- | sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java | 73 |
1 files changed, 42 insertions, 31 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java index b5ea6b1727..14ff2ab5aa 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java @@ -27,6 +27,7 @@ import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding; +import org.apache.tuscany.sca.databinding.json.JSONDataBinding; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; @@ -57,48 +58,58 @@ public class JSONRPCBindingInvoker implements Invoker { HttpPost post = null; HttpResponse response = null; try { - - JSONObject jsonRequest = null;; String requestId = "1"; - Object[] args = null; - try { - // Extract the method - jsonRequest = new JSONObject(); - jsonRequest.putOpt("method", "Service" + "." + msg.getOperation().getName()); - - // Extract the arguments - args = msg.getBody(); - JSONArray array = new JSONArray(); - for (int i = 0; i < args.length; i++) { - array.put(args[i]); - } - jsonRequest.putOpt("params", array); - jsonRequest.put("id", requestId); + post = new HttpPost(uri); - } catch (Exception e) { - throw new RuntimeException("Unable to parse JSON parameter", e); - } + String req; + if (!msg.getOperation().getWrapper().getDataBinding().equals(JSONDataBinding.NAME)) { + - post = new HttpPost(uri); - String req = jsonRequest.toString(); - StringEntity entity = new StringEntity(req, "application/json; charset\"UTF-8\""); + JSONObject jsonRequest = null;; + Object[] args = null; + try { + // Extract the method + jsonRequest = new JSONObject(); + jsonRequest.putOpt("method", "Service" + "." + msg.getOperation().getName()); + + // Extract the arguments + args = msg.getBody(); + JSONArray array = new JSONArray(); + for (int i = 0; i < args.length; i++) { + array.put(args[i]); + } + jsonRequest.putOpt("params", array); + jsonRequest.put("id", requestId); + + } catch (Exception e) { + throw new RuntimeException("Unable to parse JSON parameter", e); + } + req = jsonRequest.toString(); + } else { + req = (String)((Object[])msg.getBody())[0]; + } + StringEntity entity = new StringEntity(req, "UTF-8"); post.setEntity(entity); response = httpClient.execute(post); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { //success - JSONObject jsonResponse = null; try { - - jsonResponse = new JSONObject(EntityUtils.toString(response.getEntity())); - - //check requestId - if (! jsonResponse.getString("id").equalsIgnoreCase(requestId)) { - throw new RuntimeException("Invalid response id:" + requestId ); + String entityResponse = EntityUtils.toString(response.getEntity()); + if (!msg.getOperation().getWrapper().getDataBinding().equals(JSONDataBinding.NAME)) { + JSONObject jsonResponse = new JSONObject(entityResponse); + + //check requestId + if (! jsonResponse.getString("id").equalsIgnoreCase(requestId)) { + throw new RuntimeException("Invalid response id:" + requestId ); + } + + msg.setBody(jsonResponse.get("result")); + } else { + msg.setBody(entityResponse); } - - msg.setBody(jsonResponse.get("result")); + } catch (Exception e) { //FIXME Exceptions are not handled correctly here // They should be reported to the client JavaScript as proper |