Passing an array of strings

View: New views
6 Messages — Rating Filter:   Alert me  

Passing an array of strings

by Ken Tanaka :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm using an xmlrpc-client 3.1.2 application to talk to an xmlrpc-server
3.1.2 server and want to pass an array of strings. I figure people on
this list must have done this before.

This code below is working, but could probably be written better. Does
anyone have suggestions on cleaning up the 5 lines following the comment
"OPTION A?. The two lines (commented out) following the comment "OPTION
B" are what I would have expected to work, but throw 'Exception in
thread "main" java.lang.ClassCastException: [Ljava.lang.Object;'

Thanks in advance for any suggestions.

-Ken

- Begin client code listing ----------------------------

package gov.noaa.eds.adicXmlRpcClient;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

/**
 * This client will use the adicXmlRpcServer.
 */
public class App {

    public static void main(String[] args) {
        System.out.println("Starting adicXmlRpcServer test");

        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        try {
            config.setServerURL(new
URL("http://127.0.0.1:8084/adicXmlRpcServer/xmlrpc"));
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);
        Object[] params = new Object[] {new String("testDir")};

        try {
            /* OPTION A (next 5 lines):
             * This works, but looks ugly. Is there a better way to receive
             * an ArrayList of strings from the xml-rpc server?
             */
            Object[] result = (Object[]) client.execute("DirList.ls",
params);
            ArrayList<String> dirListing = new ArrayList<String>();
            for (Object o : result) {
                dirListing.add(o.toString());
            }

            /* OPTION B (next 2 lines):
             * This doesn't work, but is the way I would like the code to
             * work. Java runtime doesn't like the cast.
             */
//            ArrayList<String> dirListing =
//                    (ArrayList<String>) client.execute("DirList.ls",
params);

            System.out.println("Listing Length=" + dirListing.size());
            System.out.println("  First 10:");
            for (int i = 0; i < 10; i++) {
                System.out.println("    " + dirListing.get(i));
            }
        } catch (XmlRpcException ex) {
            ex.printStackTrace();
        }
    }
}

- End client code listing ----------------------------

In case it helps to know the server code, I'm sending an
ArrayList<String> at the other end:

- Begin server code listing ----------------------------

/*
 * FILE: DirList.java
 */
package gov.noaa.eds.adicXmlRpc;

import java.util.ArrayList;
import java.util.Random;

/**
 * Provide directory listing functionality.
 */
public class DirList {

    /**
     * Return a directory listing.
     * Currently generates made up names.
     * @param dirName directory name for which to get a listing.
     * @return a list of filenames for dirName
     */
    public ArrayList<String> ls(String dirName) {
        Random rng = new Random();
        int listLength = 2000;
        ArrayList<String> listing = new ArrayList<String>(listLength);
        for (int i = 0; i < listLength; i++) {
            int filenameLen = 1 + rng.nextInt(40);
            StringBuffer filename = new StringBuffer("sample_");
            for (int f = 0; f < filenameLen; f++) {
               
filename.append("abcdefghijklmnopqrstuvwxyz".charAt(rng.nextInt(26)));
            }
            listing.add(filename.toString());
        }
        return listing;
    }
}

- End server code listing ----------------------------

If anyone wants I can also post the XmlRpcServlet.properties, web.xml or
maven pom.xml files, but those probably aren't needed.

Re: Passing an array of strings

by Stanislav Miklik :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

AFAIK, you are right, option A is the way how it works (see:
http://ws.apache.org/xmlrpc/faq.html#arrays)
My only advice, make small tooling, eg.

   public static List decodeList(Object element) {
      if (element == null) {
         return null;
      }
      if (element instanceof List) {
         return (List) element;
      }
      if (element.getClass().isArray()) {
         int length = Array.getLength(element);
         LinkedList result = new LinkedList();
         for (int i = 0; i < length; i++) {
            result.add (Array.get(element, i));
         }
         return result;
      }
      return null;
   }

With such method you can have option B.

Best regards
Stano

On Wed, Jul 8, 2009 at 23:37, Ken Tanaka <Ken.Tanaka@...> wrote:

> I'm using an xmlrpc-client 3.1.2 application to talk to an xmlrpc-server
> 3.1.2 server and want to pass an array of strings. I figure people on this
> list must have done this before.
>
> This code below is working, but could probably be written better. Does
> anyone have suggestions on cleaning up the 5 lines following the comment
> "OPTION A?. The two lines (commented out) following the comment "OPTION B"
> are what I would have expected to work, but throw 'Exception in thread
> "main" java.lang.ClassCastException: [Ljava.lang.Object;'
>
> Thanks in advance for any suggestions.
>
> -Ken
>
> - Begin client code listing ----------------------------
>
> package gov.noaa.eds.adicXmlRpcClient;
>
> import java.net.MalformedURLException;
> import java.net.URL;
> import java.util.ArrayList;
> import org.apache.xmlrpc.XmlRpcException;
> import org.apache.xmlrpc.client.XmlRpcClient;
> import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
>
> /**
> * This client will use the adicXmlRpcServer.
> */
> public class App {
>
>   public static void main(String[] args) {
>       System.out.println("Starting adicXmlRpcServer test");
>
>       XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
>       try {
>           config.setServerURL(new URL("
> http://127.0.0.1:8084/adicXmlRpcServer/xmlrpc"));
>       } catch (MalformedURLException ex) {
>           ex.printStackTrace();
>       }
>       XmlRpcClient client = new XmlRpcClient();
>       client.setConfig(config);
>       Object[] params = new Object[] {new String("testDir")};
>
>       try {
>           /* OPTION A (next 5 lines):
>            * This works, but looks ugly. Is there a better way to receive
>            * an ArrayList of strings from the xml-rpc server?
>            */
>           Object[] result = (Object[]) client.execute("DirList.ls",
> params);
>           ArrayList<String> dirListing = new ArrayList<String>();
>           for (Object o : result) {
>               dirListing.add(o.toString());
>           }
>
>           /* OPTION B (next 2 lines):
>            * This doesn't work, but is the way I would like the code to
>            * work. Java runtime doesn't like the cast.
>            */
> //            ArrayList<String> dirListing =
> //                    (ArrayList<String>) client.execute("DirList.ls",
> params);
>
>           System.out.println("Listing Length=" + dirListing.size());
>           System.out.println("  First 10:");
>           for (int i = 0; i < 10; i++) {
>               System.out.println("    " + dirListing.get(i));
>           }
>       } catch (XmlRpcException ex) {
>           ex.printStackTrace();
>       }
>   }
> }
>
> - End client code listing ----------------------------
>
> In case it helps to know the server code, I'm sending an ArrayList<String>
> at the other end:
>
> - Begin server code listing ----------------------------
>
> /*
> * FILE: DirList.java
> */
> package gov.noaa.eds.adicXmlRpc;
>
> import java.util.ArrayList;
> import java.util.Random;
>
> /**
> * Provide directory listing functionality.
> */
> public class DirList {
>
>   /**
>    * Return a directory listing.
>    * Currently generates made up names.
>    * @param dirName directory name for which to get a listing.
>    * @return a list of filenames for dirName
>    */
>   public ArrayList<String> ls(String dirName) {
>       Random rng = new Random();
>       int listLength = 2000;
>       ArrayList<String> listing = new ArrayList<String>(listLength);
>       for (int i = 0; i < listLength; i++) {
>           int filenameLen = 1 + rng.nextInt(40);
>           StringBuffer filename = new StringBuffer("sample_");
>           for (int f = 0; f < filenameLen; f++) {
>
> filename.append("abcdefghijklmnopqrstuvwxyz".charAt(rng.nextInt(26)));
>           }
>           listing.add(filename.toString());
>       }
>       return listing;
>   }
> }
>
> - End server code listing ----------------------------
>
> If anyone wants I can also post the XmlRpcServlet.properties, web.xml or
> maven pom.xml files, but those probably aren't needed.
>

Re: Passing an array of strings

by Ken Tanaka :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Stano,
Thanks for the quick response. I did a lot of web searches but somehow
missed the FAQ right there on the Apache web site.

I'll try out the decodeList since that will be cleaner. Your suggestion
would also be a nice addition to the FAQ.

-Ken

Stanislav Miklik wrote:

> Hi,
>
> AFAIK, you are right, option A is the way how it works (see:
> http://ws.apache.org/xmlrpc/faq.html#arrays)
> My only advice, make small tooling, eg.
>
>    public static List decodeList(Object element) {
>       if (element == null) {
>          return null;
>       }
>       if (element instanceof List) {
>          return (List) element;
>       }
>       if (element.getClass().isArray()) {
>          int length = Array.getLength(element);
>          LinkedList result = new LinkedList();
>          for (int i = 0; i < length; i++) {
>             result.add (Array.get(element, i));
>          }
>          return result;
>       }
>       return null;
>    }
>
> With such method you can have option B.
>
> Best regards
> Stano
>
> On Wed, Jul 8, 2009 at 23:37, Ken Tanaka <Ken.Tanaka@...> wrote:
>
>  
>> I'm using an xmlrpc-client 3.1.2 application to talk to an xmlrpc-server
>> 3.1.2 server and want to pass an array of strings. I figure people on this
>> list must have done this before.
>>    
...


Re: Passing an array of strings

by Ken Tanaka :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Being new to XML RPC, I didn't see as many examples of code on the web
as I would have liked, especially using current libraries, and I found
no complete examples that were compiled with maven, so I put up my
resulting example in the Apache wiki

http://wiki.apache.org/ws/XmlRpcExampleStringArray

Maybe the more experienced programmers can take a look at it and make
suggestions or fix shortcomings directly (part of why I chose the wiki).
The page could then be a useful example if you ever want to help teach
someone XML RPC.

-Ken

Stanislav Miklik wrote:

> Hi,
>
> AFAIK, you are right, option A is the way how it works (see:
> http://ws.apache.org/xmlrpc/faq.html#arrays)
> My only advice, make small tooling, eg.
>
>    public static List decodeList(Object element) {
>       if (element == null) {
>          return null;
>       }
>       if (element instanceof List) {
>          return (List) element;
>       }
>       if (element.getClass().isArray()) {
>          int length = Array.getLength(element);
>          LinkedList result = new LinkedList();
>          for (int i = 0; i < length; i++) {
>             result.add (Array.get(element, i));
>          }
>          return result;
>       }
>       return null;
>    }
>
> With such method you can have option B.
>
> Best regards
> Stano
>
> On Wed, Jul 8, 2009 at 23:37, Ken Tanaka <Ken.Tanaka@...> wrote:
>
>  
>> I'm using an xmlrpc-client 3.1.2 application to talk to an xmlrpc-server
>> 3.1.2 server and want to pass an array of strings. I figure people on this
>> list must have done this before.
>>    
...

Re: Passing an array of strings

by Jochen Wiedmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for taking this work. As the essence is contained in the
App.java listing, I suggest moving this to the top and somehow
highlighting the crucial three or so lines of code.


On Mon, Jul 13, 2009 at 5:27 PM, Ken Tanaka<Ken.Tanaka@...> wrote:

> Being new to XML RPC, I didn't see as many examples of code on the web as I
> would have liked, especially using current libraries, and I found no
> complete examples that were compiled with maven, so I put up my resulting
> example in the Apache wiki
>
> http://wiki.apache.org/ws/XmlRpcExampleStringArray
>
> Maybe the more experienced programmers can take a look at it and make
> suggestions or fix shortcomings directly (part of why I chose the wiki). The
> page could then be a useful example if you ever want to help teach someone
> XML RPC.
>
> -Ken
>
> Stanislav Miklik wrote:
>>
>> Hi,
>>
>> AFAIK, you are right, option A is the way how it works (see:
>> http://ws.apache.org/xmlrpc/faq.html#arrays)
>> My only advice, make small tooling, eg.
>>
>>   public static List decodeList(Object element) {
>>      if (element == null) {
>>         return null;
>>      }
>>      if (element instanceof List) {
>>         return (List) element;
>>      }
>>      if (element.getClass().isArray()) {
>>         int length = Array.getLength(element);
>>         LinkedList result = new LinkedList();
>>         for (int i = 0; i < length; i++) {
>>            result.add (Array.get(element, i));
>>         }
>>         return result;
>>      }
>>      return null;
>>   }
>>
>> With such method you can have option B.
>>
>> Best regards
>> Stano
>>
>> On Wed, Jul 8, 2009 at 23:37, Ken Tanaka <Ken.Tanaka@...> wrote:
>>
>>
>>>
>>> I'm using an xmlrpc-client 3.1.2 application to talk to an xmlrpc-server
>>> 3.1.2 server and want to pass an array of strings. I figure people on
>>> this
>>> list must have done this before.
>>>
>
> ...
>



--
Don't trust a government that doesn't trust you.

Re: Passing an array of strings

by Ken Tanaka :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's a good suggestion, I was thinking of the data flow from server to
client, but users are more likely to care about the client side where
the request originates. Also, receiving seems to be a bit trickier than
sending with the XML RPC library.

I'll move the App.java earlier into the page.

Jochen Wiedmann wrote:

> Thanks for taking this work. As the essence is contained in the
> App.java listing, I suggest moving this to the top and somehow
> highlighting the crucial three or so lines of code.
>
>
> On Mon, Jul 13, 2009 at 5:27 PM, Ken Tanaka<Ken.Tanaka@...> wrote:
>  
>> Being new to XML RPC, I didn't see as many examples of code on the web as I
>> would have liked, especially using current libraries, and I found no
>> complete examples that were compiled with maven, so I put up my resulting
>> example in the Apache wiki
>>
>> http://wiki.apache.org/ws/XmlRpcExampleStringArray
>>
>> Maybe the more experienced programmers can take a look at it and make
>> suggestions or fix shortcomings directly (part of why I chose the wiki). The
>> page could then be a useful example if you ever want to help teach someone
>> XML RPC.
>>
>> -Ken
>>
>> Stanislav Miklik wrote:
>>    
>>> Hi,
>>>
>>> AFAIK, you are right, option A is the way how it works (see:
>>> http://ws.apache.org/xmlrpc/faq.html#arrays)
>>> My only advice, make small tooling, eg.
>>>
>>>   public static List decodeList(Object element) {
>>>      if (element == null) {
>>>         return null;
>>>      }
>>>      if (element instanceof List) {
>>>         return (List) element;
>>>      }
>>>      if (element.getClass().isArray()) {
>>>         int length = Array.getLength(element);
>>>         LinkedList result = new LinkedList();
>>>         for (int i = 0; i < length; i++) {
>>>            result.add (Array.get(element, i));
>>>         }
>>>         return result;
>>>      }
>>>      return null;
>>>   }
>>>
>>> With such method you can have option B.
>>>
>>> Best regards
>>> Stano
>>>
>>> On Wed, Jul 8, 2009 at 23:37, Ken Tanaka <Ken.Tanaka@...> wrote:
>>>
>>>
>>>      
>>>> I'm using an xmlrpc-client 3.1.2 application to talk to an xmlrpc-server
>>>> 3.1.2 server and want to pass an array of strings. I figure people on
>>>> this
>>>> list must have done this before.
>>>>
>>>>        
>> ...
>>
>>    
>
>
>
>