Commits

James Dumay  committed 89bb6df

XMLRPC-3 Collection/List/Array Conversion

  • Participants
  • Parent commits 8bae248

Comments (0)

Files changed (15)

File src/main/java/com/atlassian/xmlrpc/RPCCallMethodInterceptor.java

     public RPCCallMethodInterceptor(URL url, String serviceObject)
     {
         XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
+        clientConfig.setEnabledForExtensions(true);
+        clientConfig.setUseExtensionsNamespace(false);
+        
         clientConfig.setServerURL(url);
         this.client = new XmlRpcClient();
         this.client.setConfig(clientConfig);
         final String methodName = getMethodName(method);
         try
         {
-            return client.execute(serviceObject + "." + methodName, new Vector(Arrays.asList(convertArguments(args))));
+            Object returnValue = client.execute(serviceObject + "." + methodName, convertArguments(args));
+            return convertReturnValue(returnValue);
         }
         catch (Exception e)
         {
         return methodName;
     }
     
-    private Object[] convertArguments(Object[] args)
+    private Vector convertArguments(Object[] objects)
     {
-        ArrayList list = new ArrayList();
-        for (Object o : args)
+        ArrayList args = new ArrayList();
+        for (Object o : objects)
         {
             if (o instanceof Collection)
             {
-                Object[] array = ((Collection)o).toArray();
-                list.add(array);
+                args.add(((Collection)o).toArray());
             }
             else
             {
-                list.add(o);
+                args.add(o);
             }
         }
-        return list.toArray();
+        return new Vector(args);
+    }
+    
+    private Object convertReturnValue(Object object)
+    {
+        if (object.getClass().isArray())
+        {
+            Object[] objArray = (Object[])object;
+            return new ArrayList(Arrays.asList(objArray));
+        }
+        return object;
     }
 }

File src/main/java/com/atlassian/xmlrpc/ServiceMethod.java

+package com.atlassian.xmlrpc;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * 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.
+ */
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+@Retention(value=RetentionPolicy.RUNTIME)
+public @interface ServiceMethod
+{
+    String methodName();
+}

File src/test/java/com/atlassian/xmlrpc/ArrayListCoversionsTest.java

+package com.atlassian.xmlrpc;
+
+import com.atlassian.xmlrpc.testservices.ReturnListsService;
+import com.atlassian.xmlrpc.testservices.ReturnListsServiceImpl;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ArrayListCoversionsTest extends XmlRpcTestCase
+{
+    @Override
+    protected Class getServiceType() {
+        return ReturnListsServiceImpl.class;
+    }
+
+    @Override
+    protected String getServiceObjectName() {
+        return "listservice";
+    }
+    
+    public void testListPassedToServiceIsReturnedAsList()
+        throws Exception
+    {
+        URL url = new URL("http://localhost:" + port);
+        Binder binder = new DefaultBinder();
+        
+        ReturnListsService listService = binder.bind(ReturnListsService.class, url);
+        
+        ArrayList<String> myList = new ArrayList();
+        myList.add("Hello");
+        myList.add("World!");
+        
+        List<String> returnedList = listService.getList(myList);
+        assertEquals(myList.size(), returnedList.size());
+        
+        assertEquals("Hello", returnedList.get(0));
+        assertEquals("World!", returnedList.get(1));
+    }
+}

File src/test/java/com/atlassian/xmlrpc/DefaultBinderTest.java

+package com.atlassian.xmlrpc;
+
+import java.net.URL;
+import java.util.concurrent.Callable;
+import junit.framework.TestCase;
+
+public class DefaultBinderTest extends TestCase
+{
+    public void testThrowsExceptionIfBinderIsClass()
+        throws Exception
+    {
+        Binder binder = new DefaultBinder();
+        try
+        {
+            binder.bind(Object.class, new URL("http://localhost"));
+            fail("Should fail as binder type was class");
+        }
+        catch (BindingException e)
+        {
+            assertTrue(true);
+        }
+    }
+    
+    public void testThrowsExceptionIfServiceObjectAnnotationIsNotPresent()
+        throws Exception
+    {   
+        Binder binder = new DefaultBinder();
+        try
+        {
+            binder.bind(Callable.class, new URL("http://localhost"));
+            fail("Should fail as Callable does not have ServiceObject annotation");
+        }
+        catch (BindingException e)
+        {
+            assertTrue(true);
+        }
+    }
+}

File src/test/java/com/atlassian/xmlrpc/HelloWorldService.java

-package com.atlassian.xmlrpc;
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * 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.
- */
-
-/**
- *
- * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
- */
-@ServiceObject(objectName="HelloWorld")
-public interface HelloWorldService
-{
-    String say(String name);
-}

File src/test/java/com/atlassian/xmlrpc/HelloWorldServiceImpl.java

-package com.atlassian.xmlrpc;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * 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.
- */
-
-/**
- * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
- */
-public class HelloWorldServiceImpl implements HelloWorldService
-{
-    public String say(String name)
-    {
-        return "Hello " + name;
-    }
-}

File src/test/java/com/atlassian/xmlrpc/ServiceMethodTest.java

+package com.atlassian.xmlrpc;
+
+import com.atlassian.xmlrpc.testservices.WetherReportServiceImpl;
+import com.atlassian.xmlrpc.testservices.WeatherReportService;
+import java.net.URL;
+
+public class ServiceMethodTest extends XmlRpcTestCase 
+{
+    @Override
+    protected String getServiceObjectName() {
+        return "WeatherReport";
+    }
+
+    @Override
+    protected Class getServiceType() {
+        return WetherReportServiceImpl.class;
+    }
+
+    public void testServiceMethodAnnotation() throws Exception
+    {
+        URL url = new URL("http://localhost:" + port);
+        Binder binder = new DefaultBinder();
+        WeatherReportService service = binder.bind(WeatherReportService.class, url);
+        String result = service.getReport();
+        assertEquals("Fine and sunny", result);
+    }
+}

File src/test/java/com/atlassian/xmlrpc/SimpleServiceTest.java

  * under the License.
  */
 
+import com.atlassian.xmlrpc.testservices.HelloWorldServiceImpl;
+import com.atlassian.xmlrpc.testservices.HelloWorldService;
 import java.net.URL;
-import java.util.concurrent.Callable;
-import org.apache.xmlrpc.webserver.WebServer;
-import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
-import org.apache.xmlrpc.server.PropertyHandlerMapping;
-import org.apache.xmlrpc.server.XmlRpcServer;
-import junit.framework.TestCase;
 
 
 /**
  * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
  */
-public class SimpleServiceTest extends TestCase
+public class SimpleServiceTest extends XmlRpcTestCase
 {
-    public void testSimpleBinding()
-        throws Exception
-    {
-        final int port = 9001;
-        WebServer server = new WebServer(port);
-        PropertyHandlerMapping phm = new PropertyHandlerMapping();
-        phm.addHandler("HelloWorld", com.atlassian.xmlrpc.HelloWorldServiceImpl.class);
-        XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
-        xmlRpcServer.setHandlerMapping(phm);
-        
-        XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
-        serverConfig.setEnabledForExtensions(true);
-        serverConfig.setContentLengthOptional(false);
-
-        server.start();
-
-        try
-        {
-            URL url = new URL("http://localhost:" + port);
-            Binder binder = new DefaultBinder();
-            HelloWorldService service = binder.bind(HelloWorldService.class, url);
-            String result = service.say("James");
-            assertEquals("Hello James", result);
-        }
-        finally
-        {
-            server.shutdown();
-        }
+    @Override
+    protected String getServiceObjectName() {
+        return "HelloWorld";
     }
-    
-    public void testNamedMethodBinding()
-        throws Exception
-    {
-        final int port = 9001;
-        WebServer server = new WebServer(port);
-        PropertyHandlerMapping phm = new PropertyHandlerMapping();
-        phm.addHandler("WeatherReport", com.atlassian.xmlrpc.WetherReportServiceImpl.class);
-        XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
-        xmlRpcServer.setHandlerMapping(phm);
-        
-        XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
-        serverConfig.setEnabledForExtensions(true);
-        serverConfig.setContentLengthOptional(false);
-
-        server.start();
 
-        try
-        {
-            URL url = new URL("http://localhost:" + port);
-            Binder binder = new DefaultBinder();
-            WeatherReportService service = binder.bind(WeatherReportService.class, url);
-            String result = service.getReport();
-            assertEquals("Fine and sunny", result);
-        }
-        finally
-        {
-            server.shutdown();
-        }        
+    @Override
+    protected Class getServiceType() {
+        return HelloWorldServiceImpl.class;
     }
     
-    public void testThrowsExceptionIfBinderIsClass()
+    public void testSimpleBinding()
         throws Exception
     {
+        URL url = new URL("http://localhost:" + port);
         Binder binder = new DefaultBinder();
-        try
-        {
-            binder.bind(Object.class, new URL("http://localhost"));
-            fail("Should fail as binder type was class");
-        }
-        catch (BindingException e)
-        {
-            assertTrue(true);
-        }
-    }
-    
-    public void testThrowsExceptionIfServiceObjectAnnotationIsNotPresent()
-        throws Exception
-    {   
-        Binder binder = new DefaultBinder();
-        try
-        {
-            binder.bind(Callable.class, new URL("http://localhost"));
-            fail("Should fail as Callable does not have ServiceObject annotation");
-        }
-        catch (BindingException e)
-        {
-            assertTrue(true);
-        }
+        HelloWorldService service = binder.bind(HelloWorldService.class, url);
+        String result = service.say("James");
+        assertEquals("Hello James", result);
     }
 }

File src/test/java/com/atlassian/xmlrpc/XmlRpcTestCase.java

+package com.atlassian.xmlrpc;
+
+import junit.framework.TestCase;
+import org.apache.xmlrpc.server.PropertyHandlerMapping;
+import org.apache.xmlrpc.server.XmlRpcServer;
+import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
+import org.apache.xmlrpc.webserver.WebServer;
+
+public abstract class XmlRpcTestCase extends TestCase
+{
+    private WebServer server;
+    
+    protected final int port = 9001;
+    
+    protected abstract Class getServiceType();
+    
+    protected abstract  String getServiceObjectName();
+
+    @Override
+    protected void setUp() throws Exception 
+    {
+        super.setUp();
+        server = new WebServer(port);
+        PropertyHandlerMapping phm = new PropertyHandlerMapping();
+        phm.addHandler(getServiceObjectName(), getServiceType());
+        XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
+        xmlRpcServer.setHandlerMapping(phm);
+        
+        XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
+        serverConfig.setEnabledForExtensions(true);
+        serverConfig.setContentLengthOptional(false);
+        
+        server.start();
+    }
+
+    @Override
+    protected void tearDown()
+        throws Exception 
+    {
+        super.tearDown();
+        server.shutdown();
+    }
+}

File src/test/java/com/atlassian/xmlrpc/testservices/HelloWorldService.java

+package com.atlassian.xmlrpc.testservices;
+
+import com.atlassian.xmlrpc.*;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * 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.
+ */
+
+/**
+ *
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+@ServiceObject(objectName="HelloWorld")
+public interface HelloWorldService
+{
+    String say(String name);
+}

File src/test/java/com/atlassian/xmlrpc/testservices/HelloWorldServiceImpl.java

+package com.atlassian.xmlrpc.testservices;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * 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.
+ */
+
+/**
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+public class HelloWorldServiceImpl implements HelloWorldService
+{
+    public String say(String name)
+    {
+        return "Hello " + name;
+    }
+}

File src/test/java/com/atlassian/xmlrpc/testservices/ReturnListsService.java

+package com.atlassian.xmlrpc.testservices;
+
+import com.atlassian.xmlrpc.ServiceObject;
+import java.util.List;
+
+@ServiceObject(objectName="listservice")
+public interface ReturnListsService
+{
+    List getList(List l);
+}

File src/test/java/com/atlassian/xmlrpc/testservices/ReturnListsServiceImpl.java

+package com.atlassian.xmlrpc.testservices;
+
+import java.util.List;
+
+public class ReturnListsServiceImpl implements ReturnListsService
+{
+    public List getList(List l) {
+        return l;
+    }
+}

File src/test/java/com/atlassian/xmlrpc/testservices/WeatherReportService.java

+package com.atlassian.xmlrpc.testservices;
+
+import com.atlassian.xmlrpc.*;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * 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.
+ */
+
+
+/**
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+@ServiceObject(objectName="WeatherReport")
+public interface WeatherReportService {
+    @ServiceMethod(methodName="Report")
+    String getReport();
+}

File src/test/java/com/atlassian/xmlrpc/testservices/WetherReportServiceImpl.java

+package com.atlassian.xmlrpc.testservices;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * 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.
+ */
+
+/**
+ * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
+ */
+public class WetherReportServiceImpl
+{
+    public String Report()
+    {
+        return "Fine and sunny";
+    }
+}