Web Service Howto:

Step 1: Setup your development environment

Step 2: Authentication
  2A> Generating Java Classes from WSDL
  2B> Using these classes to authenticate
  2C> Authenticate with CompanyName/UserName/Password 

Step 3: Post Authentication: Query Organization Records
  3A> Generating Java Classes from WSDL
  3B> Using these classes to query Organization Records

Step 4: More Sample Codes
  4A> Contact API WSDL
  4B> Contact Sample code to query, read, and save a contact record


Step 1: Setup your development environment
We will be using
  • WSDL2Java
  • All needed jar files in your classpath


  • Step 2: Authentication: ( http://planplusonline02.com/cxf/SessionAuthenticatorAPI?wsdl )
        2A> Generating Java Classes from WSDL
          C:> WSDL2Java http://www.planplusonline02.com/cxf/SessionAuthenticatorAPI?wsdl
          This should generate several web service classes in your file system.
    Using WSDL2Java to generate Stub Classes
    Result Folder
        2B> Using these classes to authenticate:
    Code Sample 1: SimpleTest1 (com.cxrm.common.tutorial)
    package com.cxrm.common.tutorial;
    
    import java.io.*;
    import java.util.*;
    import com.cxrm.login.SessionAuthenticatorAPIServiceSoapBindingStub;
    
    public class SimpleTest1 {
      public static void doTest() throws Exception
      {
        java.net.URL _endpoint = new java.net.URL( "http://www.planplusonline02.com/cxf/SessionAuthenticatorAPI" );
        SessionAuthenticatorAPIServiceSoapBindingStub _stub = 
          new SessionAuthenticatorAPIServiceSoapBindingStub(_endpoint, new org.apache.axis.client.Service() );
        System.out.println( "[" + _stub.simpleAuthenticate( "******", "*************************" ) + "]" );
      }
    
      public static void main ( String[] params )
      {
        try { doTest(); } catch (Exception e) { e.printStackTrace(); }
      }
    }
    Compile, Run, and Result
    If success, the result should start with "[SESSIONID:" . Otherwise it should display the error message.
        2C> Authenticate with CompanyName/UserName/Password
    Alternatively, we can use loginAuthenticate(company_name,user_name,password) to authenticate.
    package com.cxrm.common.tutorial;
    
    import java.io.*;
    import java.util.*;
    import com.cxrm.login.SessionAuthenticatorAPIServiceSoapBindingStub;
    
    public class SimpleTest1 {
      public static void doTest() throws Exception
      {
        java.net.URL _endpoint = new java.net.URL( "http://www.planplusonline02.com/cxf/SessionAuthenticatorAPI" );
        SessionAuthenticatorAPIServiceSoapBindingStub _stub = 
          new SessionAuthenticatorAPIServiceSoapBindingStub(_endpoint, new org.apache.axis.client.Service() );
        System.out.println( "[" + _stub.loginAuthenticate( "*****", "*****", "*****" ) + "]" );
      }
    
      public static void main ( String[] params )
      {
        try { doTest(); } catch (Exception e) { e.printStackTrace(); }
      }
    }


    Step 3: Post Authentication: Query Organization Records. ( http://planplusonline02.com/cxf/OrgAPI?wsdl )
        3A> Generating Java Classes from WSDL
          C:> WSDL2Java http://www.planplusonline02.com/cxf/OrgAPI?wsdl
          This should generate several web service classes in your file system.
    Using WSDL2Java to generate Stub Classes
    Result Folder
        3B> Using these classes to query Organization Records:
    Code Sample 2: SimpleTest2 (com.cxrm.common.tutorial)
    package com.cxrm.common.tutorial;
    
    import java.io.*;
    import java.util.*;
    import com.cxrm.login.SessionAuthenticatorAPIServiceSoapBindingStub;
    import com.cxrm.sales.api.OrgAPIServiceSoapBindingStub;
    import com.cxrm.sales.api.OrganizationList;
    import com.cxrm.sales.api.Organization;
    
    public class SimpleTest2 {
    
      public static void doTest() throws Exception
      {
        java.net.URL _endpoint = new java.net.URL( "http://www.planplusonline02.com/cxf/SessionAuthenticatorAPI" );
        SessionAuthenticatorAPIServiceSoapBindingStub _stub =
            new SessionAuthenticatorAPIServiceSoapBindingStub(_endpoint, new org.apache.axis.client.Service() );
        String authResult = _stub.simpleAuthenticate( "******", "*******************" );
        System.out.println( "[" + authResult + "]" );
    
        if ( authResult.startsWith("SESSIONID:") )
        {
          String sessId = authResult.substring( 10 );
          OrgAPIServiceSoapBindingStub orgApiStub = new OrgAPIServiceSoapBindingStub( 
            new java.net.URL( "http://www.planplusonline02.com/cxf/OrgAPI" ), new org.apache.axis.client.Service() );
          OrganizationList ol = orgApiStub.searchOrganizations( sessId, "xrm" );
          Organization[] org = ol.getList();
          System.out.println( " getAllOrganizations() return row # = " + org.length );
        }
      }
    
      public static void main ( String[] params )
      {
        try { doTest(); } catch (Exception e) { e.printStackTrace(); }
      }
    }
    Compile, Run, and Result
    In this example, there are 14 records returned for search result "xrm"


    Step 4: More Sample Codes ( Using Contact API http://planplusonline02.com/cxf/PsnAPI?wsdl )
        4A> Contact API WSDL
          C:> WSDL2Java http://www.planplusonline02.com/cxf/PsnAPI?wsdl
          This should generate several web service classes in your file system.
    Using WSDL2Java to generate Stub Classes
    Result Folder
        4B> Contact Sample code to query, read, and save a contact record
    Code Sample 3: SimpleTest3 (com.cxrm.common.tutorial)
    package com.cxrm.common.tutorial;
    
    import java.io.*;
    import java.util.*;
    import com.cxrm.login.SessionAuthenticatorAPIServiceSoapBindingStub;
    import com.cxrm.sales.api.PsnAPIServiceSoapBindingStub;
    import com.cxrm.sales.api.ContactList;
    import com.cxrm.sales.api.Contact;
    
    public class SimpleTest3 {
    
      public static void doTest() throws Exception
      {
        java.net.URL _endpoint = new java.net.URL( "http://www.planplusonline02.com/cxf/SessionAuthenticatorAPI" );
        SessionAuthenticatorAPIServiceSoapBindingStub _stub =
            new SessionAuthenticatorAPIServiceSoapBindingStub(_endpoint, new org.apache.axis.client.Service() );
        String authResult = _stub.loginAuthenticate( "**", "**", "**" );
        System.out.println( "[" + authResult + "]" );
    
        if ( authResult.startsWith("SESSIONID:") )
        {
          String sessId = authResult.substring( 10 );
          PsnAPIServiceSoapBindingStub psnApiStub = new PsnAPIServiceSoapBindingStub( 
            new java.net.URL( "http://www.planplusonline02.com/cxf/PsnAPI" ), new org.apache.axis.client.Service() );
          ContactList cl = psnApiStub.searchContactsByFirstAndLast( sessId, "John", "Doe" );
          Contact[] psn = cl.getList();
          
          if ( psn.length > 0 )
          {
            Contact c = psnApiStub.getContactDetail( sessId, psn[0].getCustomerId() );
            System.out.println( c.getFirstName() + " " + c.getLastName() );
          }
    
          System.out.println( " Now attempting to create a new contact" );
          Contact newContact = new Contact();
          newContact.setFirstName( "WebService" );
          newContact.setLastName( "Test" );
          Contact result = psnApiStub.saveContactRecord( sessId, newContact );
     
          System.out.println( "\n\n --- saveContactRecord() --- " );
          if ( result != null && result.getCustomerId() > 0 )
          {
            System.out.println( " Record saved successfully. New record ID ="+ result.getCustomerId() );
          }
          else
          {
            System.out.println( " Transaction Failed. " );
            if ( result != null )
              System.out.println( " Msg = "+ result.getErrorMsg() );
          }
        }
      }
    
      public static void main ( String[] params )
      {
        try { doTest(); } catch (Exception e) { e.printStackTrace(); }
      }
    }