You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2010/07/19 08:12:16 UTC

svn commit: r965361 - in /tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider: JSONRPCBindingInvoker.java JSONRPCReferenceBindingProvider.java JSONRPCServiceServlet.java

Author: jsdelfino
Date: Mon Jul 19 06:12:16 2010
New Revision: 965361

URL: http://svn.apache.org/viewvc?rev=965361&view=rev
Log:
Tweak the JSON-RPC binding to work without Java interfaces and recognize dynamic interfaces bound to the JSON databinding.

Modified:
    tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
    tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java
    tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java

Modified: tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
URL: http://svn.apache.org/viewvc/tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java?rev=965361&r1=965360&r2=965361&view=diff
==============================================================================
--- tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java (original)
+++ tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java Mon Jul 19 06:12:16 2010
@@ -27,6 +27,7 @@ import org.apache.http.entity.StringEnti
 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 imple
         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

Modified: tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java
URL: http://svn.apache.org/viewvc/tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java?rev=965361&r1=965360&r2=965361&view=diff
==============================================================================
--- tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java (original)
+++ tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java Mon Jul 19 06:12:16 2010
@@ -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 JSONRPCReferenceBindingProv
     }
 
     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);
     }
 

Modified: tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java
URL: http://svn.apache.org/viewvc/tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java?rev=965361&r1=965360&r2=965361&view=diff
==============================================================================
--- tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java (original)
+++ tuscany/sandbox/sebastien/java/dynamic/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java Mon Jul 19 06:12:16 2010
@@ -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 exten
                 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 exten
 
         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 exten
 
         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 exten
         
         Operation result = null;
         for (Operation o : operations) {
+        	if (o.isDynamic())
+        		return o;
             if (o.getName().equalsIgnoreCase(method)) {
                 result = o;
                 break;