Java Sample Code – Authentication (SOAP API)

This section walks through a sample Java client application. Web service client access helper classes and stubs for this example were generated using the Apache Axis WSDL2Java tool. For more information on this tool, see Getting Started with the XML API and SOAP API. The example demonstrates the following functions:

  1. Sign in to OpenAir web services with user credentials entered by the user at the console prompt.

  2. Add several user records to your OpenAir account using the information entered by the user at the console prompt.

  3. Sign out of OpenAir web services.

          import java.rmi.RemoteException;
import javax.xml.soap.SOAPElement;
import org.apache.axis.message.SOAPHeaderElement;
import java.io.*;

class Program
{
     // Instance of OpenAir web services proxy object
     private static OAirServiceSoapBindingStub m_svc;

     // Company ID for the account new users will be added to
     private static String m_strCompany;

     // Console prompt for user credentials
     private static String GetUserInput(String prompt)
     {
          {
               BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
               return reader.readLine();
          }
          catch (java.io.IOException e)
          {
               return null;
          }
     }

     // Sign in to OpenAir web services using with user credentials.
     // Returns true if successful, false if not
     private static boolean Login() throws javax.xml.soap.SOAPException, javax.xml.rpc.ServiceException
     {
          // Set up login information
          LoginParams lp = new LoginParams();
          lp.setApi_key("************");
          lp.setApi_namespace("company_namespace");
          lp.setUser( GetUserInput("Enter username: ") );
          lp.setPassword( GetUserInput("Enter password: ") );
          lp.setCompany( GetUserInput("Enter company: ") );
          m_strCompany = lp.getCompany();

          try
          {
               // Get an instance of OpenAir web services and login
               OAirServiceHandlerServiceLocator locator = new OAirServiceHandlerServiceLocator();
               m_svc = (OAirServiceSoapBindingStub)locator.getOAirService();

               LoginResult loginResult = m_svc.login(lp);
               System.out.println("Signed in, session ID = " + loginResult.getSessionId()+"\n");

               // Set up session header to include returned session ID to perform further operations
               SOAPHeaderElement header = new SOAPHeaderElement("https://my-account-domain.app.openair.com/OAirService","SessionHeader");
               SOAPElement node = header.addChildElement("sessionId");
               node.addTextNode(loginResult.getSessionId());
               m_svc.setHeader(header);
          }
          catch (java.rmi.RemoteException e)
          {
               // Catch any login problems and return
               System.out.println(e.toString());
               return false;
          }
          return true;
     }
     // Prompt for information about user to be added and Add a new user record using the information supplied
     private static void CreateUser()
     {
          System.out.println("----------------------------");
          System.out.println("Enter new user information\n");

          // Create the company object that the new user will be associated with
          oaCompany company = new oaCompany();
          company.setNickname(m_strCompany);

          // Get the new user information
          oaUser user = new oaUser();
          user.setNickname( GetUserInput("Enter username: ") );
          user.setRole_id( GetUserInput("Enter role ID: ") );
          user.setAddr_first( GetUserInput("Enter first name: ") );
          user.setAddr_last( GetUserInput("Enter last name: ") );
          user.setAddr_email( GetUserInput("Enter email: ") );
          user.setPassword( GetUserInput("Enter password: ") );
          try
          {
               // Add the user and output any errors encountered
               UpdateResult result = m_svc.createUser(user, company);
               if (result.getErrors() != null)
               {
                    for (oaBase base : result.getErrors())
                    {
                         oaError err = (oaError)base;
                         System.out.println("Error: " + err.getCode() + "\t" + err.getComment() + "\t" + err.getText());
                    }
               }
               if (result.getStatus() == "A")
                    System.out.println("User successfully added");
          }
          catch (Exception e)
          {
               System.out.println("Error while adding user:\n"+e.toString());
          }
     }
     // Application entry point
     public static void main(String[] args)
     {
          try
          {
               // Sign in to OpenAir web services and add users
               if (Login())
               {
               {
                    do
                    {
                         CreateUser();
                    } while (GetUserInput("\nAdd another (y/n)?
                    ").toLowerCase().startsWith("y"));

                    m_svc.logout();
               }
          }
          catch (Exception e)
          {
               System.out.println(e.toString());
          }
          System.out.println("\nDone");
     }
}