You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by sl...@apache.org on 2010/07/29 10:56:53 UTC

svn commit: r980366 - in /tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime: ./ src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/ src/test/java/test/

Author: slaws
Date: Thu Jul 29 08:56:53 2010
New Revision: 980366

URL: http://svn.apache.org/viewvc?rev=980366&view=rev
Log:
TUSCANY-3635 - delegate the conversion to/from JSON to the databinding layer which has the correct information regarding the operation parameter and return types. 

Modified:
    tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/pom.xml
    tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java
    tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java
    tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java
    tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java
    tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java

Modified: tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/pom.xml
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/pom.xml?rev=980366&r1=980365&r2=980366&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/pom.xml (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/pom.xml Thu Jul 29 08:56:53 2010
@@ -85,6 +85,13 @@
             <version>2.0-SNAPSHOT</version>
             <scope>test</scope>
         </dependency>
+        
+        <dependency>
+            <groupId>org.apache.tuscany.sca</groupId>
+            <artifactId>tuscany-databinding-json</artifactId>
+            <version>2.0-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>        
 
         <dependency>
             <groupId>org.apache.tuscany.sca</groupId>

Modified: tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java?rev=980366&r1=980365&r2=980366&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPInvoker.java Thu Jul 29 08:56:53 2010
@@ -66,12 +66,13 @@ public class JSONPInvoker implements Inv
 
     public Message doInvoke(Message msg) throws JsonGenerationException, JsonMappingException, IOException, EncoderException {
         String uri = endpoint.getBinding().getURI() + "/" + operation.getName();
-        String[] jsonArgs = objectsToJSON((Object[])msg.getBody());
+        //String[] jsonArgs = objectsToJSON((Object[])msg.getBody());
+        String[] jsonArgs = objectsToJSONStrings((Object[])msg.getBody());
 
         String responseJSON = invokeHTTPRequest(uri, jsonArgs);
 
-        Object response = jsonToObjects(responseJSON)[0];
-        msg.setBody(response);
+        //Object response = jsonToObjects(responseJSON)[0];
+        msg.setBody(responseJSON);
 
         return msg;
     }
@@ -142,6 +143,7 @@ public class JSONPInvoker implements Inv
          return responseJSON.toString();
     }
 
+/* Not required now JSON conversion is delegated to databinding   
     protected String[] objectsToJSON(Object[] msgArgs) throws JsonGenerationException, JsonMappingException, IOException {
         String[] jsonArgs = new String[msgArgs.length];
         for (int i=0; i<msgArgs.length; i++) {
@@ -151,11 +153,22 @@ public class JSONPInvoker implements Inv
         }
         return jsonArgs;
     }
+*/
+    
+    protected String[] objectsToJSONStrings(Object[] msgArgs) throws JsonGenerationException, JsonMappingException, IOException {
+        String[] jsonArgs = new String[msgArgs.length];
+        for (int i=0; i<msgArgs.length; i++) {
+            jsonArgs[i] = msgArgs[i].toString();
+        }
+        return jsonArgs;
+    }    
 
+/* Not required now JSON conversion is delegated to databinding    
     protected Object[] jsonToObjects(String jsonRequest) throws JsonParseException, JsonMappingException, IOException {
         Class<?> c = new Object[0].getClass();
         Object[] args = (Object[])mapper.readValue("[" + jsonRequest +"]", c);
         return args;
     }
+*/
     
 }

Modified: tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java?rev=980366&r1=980365&r2=980366&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPReferenceBindingProvider.java Thu Jul 29 08:56:53 2010
@@ -28,9 +28,17 @@ import org.apache.tuscany.sca.provider.R
 public class JSONPReferenceBindingProvider implements ReferenceBindingProvider {
 
     private EndpointReference endpoint;
+    private InterfaceContract contract;
 
     public JSONPReferenceBindingProvider(EndpointReference endpoint) {
         this.endpoint = endpoint;
+        
+        try {
+            contract = (InterfaceContract)endpoint.getComponentReferenceInterfaceContract().clone();
+        } catch (Exception ex){
+            // we know this supports clone
+        }
+        contract.getInterface().resetDataBinding("JSON");
     }
     public Invoker createInvoker(Operation operation) {
         return new JSONPInvoker(operation, endpoint);
@@ -43,7 +51,7 @@ public class JSONPReferenceBindingProvid
     }
 
     public InterfaceContract getBindingInterfaceContract() {
-        return null;
+        return contract;
     }
 
     public boolean supportsOneWayInvocation() {

Modified: tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java?rev=980366&r1=980365&r2=980366&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServiceBindingProvider.java Thu Jul 29 08:56:53 2010
@@ -31,10 +31,18 @@ public class JSONPServiceBindingProvider
 
     private RuntimeEndpoint endpoint;
     private ServletHost servletHost;
+    private InterfaceContract contract;
 
     public JSONPServiceBindingProvider(RuntimeEndpoint endpoint, ServletHost servletHost) {
         this.endpoint = endpoint;
         this.servletHost = servletHost;
+        
+        try {
+            contract = (InterfaceContract)endpoint.getComponentServiceInterfaceContract().clone();
+        } catch (Exception ex){
+            // we know this supports clone
+        }
+        contract.getInterface().resetDataBinding("JSON");
     }
 
     public void start() {
@@ -58,7 +66,7 @@ public class JSONPServiceBindingProvider
 
     // TODO: Why are these two still on the ServiceBindingProvider interface?
     public InterfaceContract getBindingInterfaceContract() {
-        return null;
+        return contract;
     }
 
     public boolean supportsOneWayInvocation() {

Modified: tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java?rev=980366&r1=980365&r2=980366&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonp/runtime/JSONPServlet.java Thu Jul 29 08:56:53 2010
@@ -22,6 +22,7 @@ package org.apache.tuscany.sca.binding.j
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
@@ -56,16 +57,19 @@ public class JSONPServlet extends Generi
 
     @Override
     public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
-        String jsonRequest = getJSONRequest(servletRequest);
-        Object[] args = jsonToObjects(jsonRequest);
+        //String jsonRequest = getJSONRequest(servletRequest);
+        //Object[] args = jsonToObjects(jsonRequest);
+        Object[] args = getJSONRequestStringArray(servletRequest);
         Object response = invokeService(args);        
-        String jsonResponse = getJSONResponse(servletRequest, response);
+        //String jsonResponse = getJSONResponse(servletRequest, response);
+        String jsonResponse = getJSONResponseAsString(servletRequest, response);
         servletResponse.getOutputStream().println(jsonResponse);
     }
 
     /**
      * Turn the request into JSON 
      */
+/* Not required now JSON conversion is delegated to databinding       
     protected String getJSONRequest(ServletRequest servletRequest) throws IOException, JsonParseException, JsonMappingException {
         
         List<DataType> types = operation.getInputType().getLogical();
@@ -96,6 +100,43 @@ public class JSONPServlet extends Generi
 
         return "[" + jsonRequest + "]";
     }
+*/
+    
+    /**
+     * Turn the request into a string array of JSON structures. The Databinding
+     * layer will then convert each of the individual parameters into the appropriate
+     * types for the implementation interface
+     * 
+     */
+    protected Object[] getJSONRequestStringArray(ServletRequest servletRequest) throws IOException, JsonParseException, JsonMappingException {
+        
+        List<DataType> types = operation.getInputType().getLogical();
+        int typesIndex = 0;
+        
+        List<String> jsonRequestArray = new ArrayList<String>();
+        
+        for (String name : getOrderedParameterNames(servletRequest)) {
+            String jsonRequest = "";
+            if (!name.startsWith("_") && !"callback".equals(name)) {
+
+                // automatically quote string parameters so clients work in the usual javascript way               
+                if (typesIndex < types.size() && String.class.equals(types.get(typesIndex).getGenericType())) {
+                    String x = servletRequest.getParameter(name);
+                    // TODO: do this more properly
+                    if (!x.startsWith("\"")) {
+                        jsonRequest += "\"" + x + "\"";
+                    } else {
+                        jsonRequest += x;
+                    }
+                } else {               
+                    jsonRequest += servletRequest.getParameter(name);
+                }  
+                jsonRequestArray.add(jsonRequest);
+            }
+        }
+
+        return jsonRequestArray.toArray();
+    }    
 
     /**
      * Get the request parameter names in the correct order.
@@ -127,6 +168,7 @@ public class JSONPServlet extends Generi
     /**
      * Turn the response object into JSON 
      */
+/* Not required now JSON conversion is delegated to databinding   
     protected String getJSONResponse(ServletRequest servletRequest, Object response) throws IOException, JsonParseException {
         ByteArrayOutputStream os = new ByteArrayOutputStream();
         mapper.writeValue(os , response);
@@ -139,15 +181,34 @@ public class JSONPServlet extends Generi
 
         return jsonResponse;
     }
+*/
+    
+    /**
+     * The databinding layer will have converterted the return type into a JSON string so simply 
+     * add wrap it for return.
+     */
+    protected String getJSONResponseAsString(ServletRequest servletRequest, Object response) throws IOException, JsonParseException {
+        String jsonResponse = response.toString();
+
+        String callback = servletRequest.getParameter("callback");
+        if (callback != null && callback.length() > 1) {
+            jsonResponse = callback + "(" + jsonResponse + ");";
+        }
+
+        return jsonResponse;
+    }    
 
     /**
      * Turn the request JSON into objects 
      */
+/* Not required now JSON conversion is delegated to databinding       
     protected Object[] jsonToObjects(String jsonRequest) throws IOException, JsonParseException, JsonMappingException {
         Class<?> c = new Object[0].getClass();
         Object[] args = (Object[])mapper.readValue(jsonRequest, c);
         return args;
     }
+*/
+    
 
     /**
      * Send the request down the wire to invoke the service 

Modified: tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java?rev=980366&r1=980365&r2=980366&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-jsonp-runtime/src/test/java/test/BindingTestCase.java Thu Jul 29 08:56:53 2010
@@ -69,7 +69,7 @@ public class BindingTestCase {
     }
     
     @Test
-    @Ignore("TUSCANY-3635")
+    //@Ignore("TUSCANY-3635")
     public void testComplexParams() throws MalformedURLException, IOException {
         
         HelloWorldService client = node.getService(HelloWorldService.class, "HelloWorldClient");