David's profileWebbert SolutionsBlogLists Tools Help

Blog


    March 08

    Java Client using XFire - Part 1

    Here is the source code for the Java client using XFire.



    /**

     *

     */

    import java.util.List;

    import emp.service.*;

    import emp.data.*;

     

    /**

     * @author delliott

     *

     */

    public class SimpleAsmxXFireClient

    {

     

        /**

         * @param args

         */

        public static void main(String[] args)

        {

            GetPerson();

            GetAddress();

            GetWorkAddress();

        }

       

        /**

         *

         */

        public static void GetPerson()

        {

            Person person;

            Person newPerson;

     

            try

            {

                // Create Proxy Class

                SimpleAsmxServiceClient service = new SimpleAsmxServiceClient();

               

                // Create Person

                person = new Person();

                person.setName( "Bob the Builder" );

     

                //

                // DO NOT SEND THE ADDRESS

                //

                System.out.println( "\nDo NOT send Address to Service..." );

                newPerson = service.getSimpleAsmxServiceSoap().getPerson( person );

                Print( newPerson );

               

                //

                // SEND THE ADDRESS

                //

                System.out.println( "\nSend Address to Service..." );

                HomeAddress homeAddress = new HomeAddress();

                homeAddress.setHouseNumber("456 Long and Winding Road ");

                person.setAddress(homeAddress);

               

                newPerson = service.getSimpleAsmxServiceSoap().getPerson( person );

                Print( newPerson );

            }

            catch ( Exception ex )

            {

                System.err.println(ex);

            }

        }

     

        /**

         * @param newPerson

         */

        public static void Print( Person newPerson )

        {

            Object o;

           

            if ( newPerson != null )

            {

                System.out.println( "    Name: " + newPerson.getName() );

               

                o = newPerson.getAddress();

                if ( o != null)

                {

                    System.out.println( "    Addr: " + ( (HomeAddress)newPerson.getAddress() ).getHouseNumber() );

                }

                else

                {

                    System.out.println( "    Addr: is null"  );

                }

            }

        }

    Java Client using XFire - Part 2

      

        /**

         *

         */

        public static void GetAddress()

        {

            try

            {

                ArrayOfAddress listOfAddress;

               

                // Create Proxy Class

                SimpleAsmxServiceClient service = new SimpleAsmxServiceClient();

                listOfAddress = service.getSimpleAsmxServiceSoap().getAddressList();

               

                System.out.println( "\nStarting GetAddress()..." );

                Print( listOfAddress );

            }

            catch ( Exception ex )

            {

                System.err.println(ex);

            }   

        }

       

     

        /**

         * @param listOfAddress

         */

        public static void Print( ArrayOfAddress listOfAddress )

        {

            if ( listOfAddress != null )

            {

                List<Address> addressList = listOfAddress.getAddress();

               

                for ( int index = 0; index < addressList.size(); index++)

                {

                    System.out.println( "    House Number: " +

                        ((HomeAddress)addressList.get( index ) ).getHouseNumber() );

                }

            }

        }

     

       

        /**

         *

         */

        public static void GetWorkAddress()

        {

            try

            {

                ArrayOfHomeAddress listOfAddress;

               

                // Create Proxy Class

                SimpleAsmxServiceClient service = new SimpleAsmxServiceClient();

                listOfAddress = service.getSimpleAsmxServiceSoap().getHomeAddress();

               

                System.out.println( "\nStarting GetWorkAddress()..." );

                Print( listOfAddress );

            }

            catch ( Exception ex )

            {

                System.err.println(ex);

            }   

        }

       

        /**

         * @param listOfAddress

         */

        public static void Print( ArrayOfHomeAddress listOfAddress )

        {

            HomeAddress address;

           

            if ( listOfAddress != null )

            {

                List<HomeAddress> addressList = listOfAddress.getHomeAddress();

               

                for ( int index = 0; index < addressList.size(); index++)

                {

                    address = addressList.get( index );

                   

                        System.out.println( "    House Number: " +

                            ((WorkAddress)address).getHouseNumber() );

                        System.out.println( "    Phone Number: " +

                            ((WorkAddress)address).getPhoneNumber() );

                }

            }

        }   

    }

    Interoperability Testing

    SimpleAsmxClient

     

     

    Test 1: 

    Step 1: Run Java Client

     

    Results: 3 of the 4 Prints are successful

     

    Send Address to Service...

     

    System.Web.Services.Protocols.SoapException: Server was unable to read request. --->

    System.InvalidOperationException: There is an error in XML document (1, 337). --->

    System.InvalidOperationException:

    The specified type is abstract: name='Address', namespace='urn:emp:data', at

    <Address xmlns='urn:emp:data'>.

     

    Test 2:

    Step 1: Modify Person.java

    Change all instance of

    data.emp.Address

    To

    data.emp.HomeAddress

     

    Step 2: Run Java Client

     

    Results: 4 of the 4 Prints are successful

     

     

     

     

    SimpleAsmxXFireClient

     

     

    Test 1: 

    Step 1: Run Java Client

     

    Results: 3 of the 4 Prints are successful

      

                Starting GetWorkAddress()...

                java.lang.ClassCastException: emp.data.HomeAddress

     

     

    Test 2:

      

    Step 1: Modify SimpleAsmxService.asmx

    Change

    [WebMethod]

    public List<HomeAddress> GetHomeAddress()

    To

    // [WebMethod]

    public List<HomeAddress> GetHomeAddress()

     

    Step 2: Rebuild Java Proxy Classes

     

    Step 3: Comment out code in Java Client

    GetWorkAddress();

     

    public static void GetWorkAddress()

    {

    ...

    }

     

    public static void Print( ArrayOfHomeAddress listOfAddress )

    {

    ...

    }

     

    Step 4: Run Java Client

     

    Results: 2 of the 3 Prints are successful

    public static void GetAddress() fails to retrieve data

     

    org.codehaus.xfire.XFireRuntimeException: Could not invoke service..

    Nested exception is org.codehaus.xfire.fault.XFireFault: Could not unmarshall type.

     

    Test 3:

    Step 1: Restore original files

    ·         SimpleAsmxService.asmx

    ·         SimpleAsmxXFireClient.java

     

    Step 2: Modify SimpleAsmxService.asmx

    Uncomment the following method

     

    [WebMethod]

    public List<WorkAddress> GetMatchingTypes()

    {

    ...

    }

      

     

    Step 3: Rebuild Java Proxy Classes

     

    Step 4: Run Java Client

     

    Results: 4 of the 4 Prints are successful