You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by lr...@apache.org on 2010/07/09 04:22:30 UTC

svn commit: r962391 - in /tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src: main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/ test/java/org/apache/tuscany/sca/binding/rest/rpc/ test/java/services/echo/

Author: lresende
Date: Fri Jul  9 02:22:29 2010
New Revision: 962391

URL: http://svn.apache.org/viewvc?rev=962391&view=rev
Log:
TUSCANY-3618 - Enhancing type mapping for RPC over GET invocation using REST binding

Modified:
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/rpc/EchoServiceTestCase.java
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/echo/Echo.java

Modified: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java?rev=962391&r1=962390&r2=962391&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/rpc/provider/RPCOperationSelectorInterceptor.java Fri Jul  9 02:22:29 2010
@@ -6,20 +6,21 @@
  * 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.    
+ * under the License.
  */
 
 package org.apache.tuscany.sca.binding.rest.operationselector.rpc.provider;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Array;
 import java.lang.reflect.Method;
 import java.net.URLDecoder;
 import java.util.ArrayList;
@@ -32,9 +33,12 @@ import javax.ws.rs.QueryParam;
 import org.apache.tuscany.sca.common.http.HTTPContext;
 import org.apache.tuscany.sca.common.http.HTTPUtil;
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.core.UtilityExtensionPoint;
+import org.apache.tuscany.sca.databinding.SimpleTypeMapper;
 import org.apache.tuscany.sca.interfacedef.InterfaceContract;
 import org.apache.tuscany.sca.interfacedef.Operation;
 import org.apache.tuscany.sca.interfacedef.java.JavaOperation;
+import org.apache.tuscany.sca.interfacedef.util.TypeInfo;
 import org.apache.tuscany.sca.invocation.Interceptor;
 import org.apache.tuscany.sca.invocation.Invoker;
 import org.apache.tuscany.sca.invocation.Message;
@@ -43,11 +47,13 @@ import org.apache.tuscany.sca.runtime.Ru
 
 /**
  * RPC operation selector Interceptor.
- * 
+ *
  * @version $Rev$ $Date$
 */
 public class RPCOperationSelectorInterceptor implements Interceptor {
     private ExtensionPointRegistry extensionPoints;
+    private SimpleTypeMapper simpleTypeMapper;
+
     private RuntimeEndpoint endpoint;
 
     private RuntimeComponentService service;
@@ -58,6 +64,10 @@ public class RPCOperationSelectorInterce
 
     public RPCOperationSelectorInterceptor(ExtensionPointRegistry extensionPoints, RuntimeEndpoint endpoint) {
         this.extensionPoints = extensionPoints;
+
+        UtilityExtensionPoint utilityExtensionPoint = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class);
+        this.simpleTypeMapper = utilityExtensionPoint.getUtility(SimpleTypeMapper.class);
+
         this.endpoint = endpoint;
 
         this.service = (RuntimeComponentService)endpoint.getService();
@@ -86,11 +96,11 @@ public class RPCOperationSelectorInterce
             if (path.startsWith("/")) {
                 path = path.substring(1);
             }
-            
+
 
             String operationName = bindingContext.getHttpRequest().getParameter("method");
             Operation operation = findOperation( operationName );
-            
+
             if (operation == null) {
                 throw new RuntimeException("Invalid Operation '" + operationName + "'" );
             }
@@ -105,32 +115,48 @@ public class RPCOperationSelectorInterce
                         QueryParam queryParam = (QueryParam) annotation;
                         String name = queryParam.value();
                         String[] values = bindingContext.getHttpRequest().getParameterValues(name);
+
                         if(values.length == 1) {
-                            messageParameters.add(values[0]);
+                            //process value, making necessary map from string to expected value
+                            Class<?> clazz = method.getParameterTypes()[i];
+                            TypeInfo typeInfo = simpleTypeMapper.getXMLType(clazz);
+                            Object v = simpleTypeMapper.toJavaObject(typeInfo.getQName(), values[0], null);
+                            messageParameters.add(v);
                         } else {
-                            messageParameters.add(values);
+                            //process value, making necessary map from string to expected value
+                            Class<?> clazz = (method.getParameterTypes()[i]).getComponentType();
+                            TypeInfo typeInfo = simpleTypeMapper.getXMLType(clazz);
+
+
+                            Object objectArray = Array.newInstance(clazz, values.length);
+                            for (int count = 0; count < values.length; ++count) {
+                                Object v = simpleTypeMapper.toJavaObject(typeInfo.getQName(), values[count], null);
+                                Array.set(objectArray, count, v);
+                            }
+
+                            messageParameters.add(objectArray);
                         }
                     }
                 }
             }
-            
+
             Object[] body = new Object[messageParameters.size()];
             messageParameters.toArray(body);
-            
+
             msg.setBody(body);
             msg.setOperation(operation);
 
             Message responseMessage = getNext().invoke(msg);
-            
+
             //set Cache-Control to no-cache to avoid intermediary
             //proxy/reverse-proxy caches and always hit the server
             //that would identify if the value was current or not
             bindingContext.getHttpResponse().setHeader("Cache-Control", "no-cache");
             bindingContext.getHttpResponse().setHeader("Expires", new Date(0).toGMTString());
-            
-            
+
+
             String eTag = HTTPUtil.calculateHashETag(responseMessage.getBody().toString().getBytes("UTF-8"));
-            
+
             // Test request for predicates.
             String predicate = bindingContext.getHttpRequest().getHeader( "If-Match" );
             if (( predicate != null ) && ( !predicate.equals(eTag) )) {
@@ -142,9 +168,9 @@ public class RPCOperationSelectorInterce
                 // Match, should short circuit
                 bindingContext.getHttpResponse().sendError(HttpServletResponse.SC_NOT_MODIFIED);
             }
-            
+
             bindingContext.getHttpResponse().addHeader("ETag", eTag);
-            
+
             return responseMessage;
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -161,8 +187,8 @@ public class RPCOperationSelectorInterce
         if (method.contains(".")) {
             method = method.substring(method.lastIndexOf(".") + 1);
         }
-    
-        List<Operation> operations = endpoint.getComponentServiceInterfaceContract().getInterface().getOperations(); 
+
+        List<Operation> operations = endpoint.getComponentServiceInterfaceContract().getInterface().getOperations();
 
         Operation result = null;
         for (Operation o : operations) {
@@ -173,5 +199,5 @@ public class RPCOperationSelectorInterce
         }
 
         return result;
-    } 
+    }
 }

Modified: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/rpc/EchoServiceTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/rpc/EchoServiceTestCase.java?rev=962391&r1=962390&r2=962391&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/rpc/EchoServiceTestCase.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/rpc/EchoServiceTestCase.java Fri Jul  9 02:22:29 2010
@@ -73,7 +73,7 @@ public class EchoServiceTestCase {
     }
 
     @Test
-    public void testJSONRPCGetOperation() throws Exception {
+    public void testJSONRPCGetOperationWithString() throws Exception {
         String queryString = "?method=echo&msg=Hello RPC";
 
         WebConversation wc = new WebConversation();
@@ -86,7 +86,20 @@ public class EchoServiceTestCase {
     }
 
     @Test
-    public void testRPCGetArrayOperation() throws Exception {
+    public void testJSONRPCGetOperationWithInt() throws Exception {
+        String queryString = "?method=echoInt&param=1000";
+
+        WebConversation wc = new WebConversation();
+        WebRequest request = new GetMethodWebRequest(SERVICE_URL_JSON + queryString);
+        request.setHeaderField("Content-Type", "application/json");
+        WebResponse response = wc.getResource(request);
+
+        Assert.assertEquals(200, response.getResponseCode());
+        Assert.assertEquals("1000", response.getText());
+    }
+
+    @Test
+    public void testRPCGetArrayOperationWithString() throws Exception {
         String queryString = "?method=echoArrayString&msgArray=Hello RPC1&msgArray=Hello RPC2";
 
         WebConversation wc = new WebConversation();
@@ -98,6 +111,19 @@ public class EchoServiceTestCase {
         Assert.assertEquals("[\"Hello RPC1\",\"Hello RPC2\"]", response.getText());
     }
 
+    @Test
+    public void testRPCGetArrayOperationWithInt() throws Exception {
+        String queryString = "?method=echoArrayInt&intArray=1000&intArray=2000";
+
+        WebConversation wc = new WebConversation();
+        WebRequest request = new GetMethodWebRequest(SERVICE_URL_JSON + queryString);
+        request.setHeaderField("Content-Type", "application/json");
+        WebResponse response = wc.getResource(request);
+
+        Assert.assertEquals(200, response.getResponseCode());
+        Assert.assertEquals("[1000,2000]", response.getText());
+    }
+
 
     @Test
     public void testXMLRPCGetOperation() throws Exception {
@@ -108,8 +134,8 @@ public class EchoServiceTestCase {
         request.setHeaderField("Content-Type", "application/xml");
         WebResponse response = wc.getResource(request);
 
-        System.out.println("Expected>>" + XML_RESPONSE);
-        System.out.println("Received>>" + response.getText());
+        //System.out.println("Expected>>" + XML_RESPONSE);
+        //System.out.println("Received>>" + response.getText());
 
         Assert.assertEquals(200, response.getResponseCode());
         Assert.assertEquals(XML_RESPONSE, response.getText());

Modified: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/echo/Echo.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/echo/Echo.java?rev=962391&r1=962390&r2=962391&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/echo/Echo.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/echo/Echo.java Fri Jul  9 02:22:29 2010
@@ -36,6 +36,6 @@ public interface Echo {
 
     String [] echoArrayString(@QueryParam("msgArray") String[] stringArray);
 
-    int [] echoArrayInt(int[] intArray);
+    int [] echoArrayInt(@QueryParam("intArray") int[] intArray);
 
 }