Using a client library

A client library is used to create a web service request to WebSphere Commerce. You can use a WebSphere Commerce services client library (for example, MemberFacadeClient) in a standard Java application.

Before you begin

About this task

A client library's use depends on how it is invoked:
  • Using the GetData tag to perform a service request; for example, creating a Get request by using a JSP, applicable to Management Center and Store clients.
  • Using the BusinessObjectDocumentAction; for example, creating Change/Process request from the Management Center.
  • Using the ComponentServiceAction; for example, creating a Change/Process request from the Store.
  • Directly using the client library; for example, a J2SE application.

To use a client library:

Procedure

  1. Add the client library JAR file path to the build path of your Java application.
  2. Initialize the client library, as seen in the following code sample:
    private  MemberFacadeClient iClient = null;
    
    /**
     * Sets the initializes the client based on the <code>CallbackHandler</code>
     * @param aCallbackHandler {@link CallbackHandler}
     */
    private void initializeClient(CallbackHandler aCallbackHandler) {
      iClient = new MemberFacadeClient(iBusinessContext, aCallbackHandler);
    }
    
  3. Use the client library, as seen in the following code sample for a Get service call:
    //Initialize the client with site admin user permissions
    initializeClient(new SampleCallbackHandlerImpl(SITE_ADMIN_LOGON_ID, SITE_ADMIN_PWD));
    
    //Create the BOD                
    GetType getType = AbstractBusinessObjectDocumentFacadeClient.createGetVerb(
      SelectionCriteriaHelper.STR_XPATH_LANG, 
      "{" + MemberFacadeConstants.SELF + "=true;" + SelectionCriteriaHelper.STR_ACCESS_PROFILE_PARAMETER 
      + "=" + MemberFacadeConstants.ACCESS_PROFILE_SUMMARY_INFORMATION + "}/Person");
    
    //Use the client library to call the Get Person Web service
    
    ShowPersonDataAreaType showPersonDAT = iClient.getPerson(getType);
    
    // Get the PersonType from the response and use the printPerson() method to display the response data
    PersonType person = (PersonType) showPersonDAT.getPerson().get(0);
    
    // Output the logon ID
    System.out.println(person.getCredential().getLogonID());
    
    An example for calling a Process or Change service is seen in the following code sample:
    
    //Initialize the client to run as a guest user - use null as the
    callback handler
    initializeClient(null);
    Map parameters = new HashMap();
            
    parameters.put("logonId", new String[]{ "myLogonId" } );
    parameters.put("logonPassword", new String[]{ "myPassw0rd" });
    parameters.put("lastName", new String[]{"myLastName"});
    parameters.put("city",        new String[]{"Toronto"});
    
    try{
            Map result = iClient.registerPerson(parameters);
            String [] strUserId = (String[]) result.get("userId");
            System.out.println("========= New userId: " +
    strUserId[0]);
    } catch (PersonException e) {
            List listErrors = e.getClientErrors();
            if (listErrors != null) {
                    for (int i=0; i < listErrors.size(); i++) {
                            ClientError clientError = (ClientError)
    listErrors.get(i);
                            System.out.println("Message: " +
    clientError.getLocalizedMessage(Locale.ENGLISH));
                            System.out.println("Error key: " +
    clientError.getErrorKey());
                            System.out.println("Error code: " +
    clientError.getErrorCode());
                    }
            }
    }
    
  4. Process the response as appropriate for your Java application.