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 | |
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 'sca-java-2.x/trunk/modules')
3 files changed, 68 insertions, 40 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 diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java index cd179a04c2..d1e35730b7 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java @@ -33,6 +33,7 @@ import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.apache.http.protocol.HTTP; import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.interfacedef.Interface; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.Invoker; @@ -89,6 +90,9 @@ public class JSONRPCReferenceBindingProvider implements ReferenceBindingProvider } public Invoker createInvoker(Operation operation) { + final Interface intf = reference.getInterfaceContract().getInterface(); + if (intf.isDynamic()) + return new JSONRPCBindingInvoker(endpointReference, operation, httpClient); return new JSONRPCClientInvoker(endpointReference, operation, httpClient); } diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java index 742a27e2ce..c24d90e2d0 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java @@ -37,6 +37,7 @@ import javax.servlet.http.HttpSession; import org.apache.commons.codec.binary.Base64; import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.databinding.json.JSONDataBinding; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.invocation.MessageFactory; @@ -99,6 +100,8 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { if (re.getCause() instanceof javax.security.auth.login.LoginException) { response.setHeader("WWW-Authenticate", "BASIC realm=\"" + "ldap-realm" + "\""); response.sendError(HttpServletResponse.SC_UNAUTHORIZED); + } else { + re.printStackTrace(); } } finally { HttpSession session = request.getSession(false); @@ -299,7 +302,10 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { requestMessage.getHeaders().put("RequestMessage", request); - requestMessage.setBody(args); + if (jsonOperation.getWrapper().getDataBinding().equals(JSONDataBinding.NAME)) + requestMessage.setBody(new Object[]{jsonReq.toString()}); + else + requestMessage.setBody(args); //result = wire.invoke(jsonOperation, args); Message responseMessage = null; @@ -317,15 +323,20 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { if (!responseMessage.isFault()) { //successful execution of the invocation - try { + if (jsonOperation.getWrapper().getDataBinding().equals(JSONDataBinding.NAME)) { result = responseMessage.getBody(); - JSONObject jsonResponse = new JSONObject(); - jsonResponse.put("result", result); - jsonResponse.putOpt("id", id); - //get response to send to client - return jsonResponse.toString().getBytes("UTF-8"); - } catch (Exception e) { - throw new ServiceRuntimeException("Unable to create JSON response", e); + return result.toString().getBytes("UTF-8"); + } else { + try { + result = responseMessage.getBody(); + JSONObject jsonResponse = new JSONObject(); + jsonResponse.put("result", result); + jsonResponse.putOpt("id", id); + //get response to send to client + return jsonResponse.toString().getBytes("UTF-8"); + } catch (Exception e) { + throw new ServiceRuntimeException("Unable to create JSON response", e); + } } } else { //exception thrown while executing the invocation @@ -354,6 +365,8 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { Operation result = null; for (Operation o : operations) { + if (o.isDynamic()) + return o; if (o.getName().equalsIgnoreCase(method)) { result = o; break; |