Code sample: Creating your first application

The following code sample demonstrates how to find an existing user account on your BlackBerry® Enterprise Server. To run the code sample, you must specify a user account and other account information in the main() method.

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.List;

import javax.xml.ws.BindingProvider;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

import com.rim.bes.bas.baa.BAACore;
import com.rim.bes.bas.baa.BAAServiceCore;
import com.rim.bes.bas.baa.BAAServiceUtil;
import com.rim.bes.bas.baa.BAAUtil;
import com.rim.bes.bas.baa.dto.FindUsersResultType;
import com.rim.bes.bas.baa.dto.FindUsersReturnStatusEnumType;
import com.rim.bes.bas.dto.BASUserSearchCriteriaServiceAttributes;
import com.rim.bes.bas.dto.BASUserSearchCriteriaServiceAttributesType;
import com.rim.bes.bas.dto.BASUserSearchResultServiceAttributesType;
import com.rim.bes.bas.dto.BASUserSearchResultSortByEnum;
import com.rim.bes.bas.dto.BASUserSearchResultSortByEnumType;
import com.rim.bes.bas.dto.UserSearchCriteria;
import com.rim.bes.bas.dto.UserSearchResult;
import com.rim.bes.bas.dto.UserSearchResultServiceAttributesType;
import com.rim.bes.basplugin.dispatcher.dto.DispatcherUserSearchResultServiceAttributesType;
import com.rim.bes.basplugin.emailcommon.dto.EmailCommonUserSearchCriteriaServiceAttributesType;
import com.rim.bes.basplugin.emailcommon.dto.EmailCommonUserSearchResultServiceAttributesType;
import com.rim.bes.basplugin.emailexchange.dto.EmailExchangeUserSearchCriteriaServiceAttributes;


public class HelloWorld {
    public static BAAServiceCore _myCoreWSService;
    public static BAACore _myCoreWSStub;
    public static BAAServiceUtil _myUtilWSService;
    public static BAAUtil _myUtilWSStub;

    public static boolean Setup() {
        String strBASURL=System.getProperty("basurl");
        URL coreUrl = null;
        URL utilUrl=null;

        try {
            coreUrl = new URL("https://" + strBASURL + "/baaws/core/ws?wsdl");
            utilUrl = new URL("https://" + strBASURL + "/baaws/core/wsutil?wsdl");
        } catch (MalformedURLException e) {
            System.err.println("Can not initialize the wsdl");
            return false;
        }

        _myCoreWSService=new BAAServiceCore(coreUrl);
        _myCoreWSStub=_myCoreWSService.getCore();
        _myUtilWSService=new BAAServiceUtil(utilUrl);
        _myUtilWSStub=_myUtilWSService.getUtil();

        String username=System.getProperty("username");
        String password=System.getProperty("password");

        //call encodeUsername
        String myEncodeUsername=_myUtilWSStub.encodeUsername(username,null,null,0,"0");

        //set http basic authentication on web service
        BindingProvider bp = (BindingProvider)_myCoreWSStub;
        bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, myEncodeUsername);
        bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);



        //set timeout to be longer
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(600000);
        httpClientPolicy.setAllowChunking(false);
        httpClientPolicy.setReceiveTimeout(600000);

        Client client = ClientProxy.getClient(_myCoreWSStub);
        HTTPConduit http = (HTTPConduit) client.getConduit();
        http.setClient(httpClientPolicy);

        client = ClientProxy.getClient(_myUtilWSStub);
        http = (HTTPConduit) client.getConduit();
        http.setClient(httpClientPolicy);

        return true;
    }
    
    public static void findUsers(String displayName, String emailAddress){
        //find user by display name
        UserSearchCriteria myUserSearchCriteria=new UserSearchCriteria();

        //search by Display Name
        BASUserSearchCriteriaServiceAttributesType myBASCriteria=new BASUserSearchCriteriaServiceAttributes();
        myBASCriteria.setDisplayName(displayName);
        myUserSearchCriteria.getUserSearchCriteriaServiceAttributes().add(myBASCriteria);
        
        //search by Email Address 
        EmailCommonUserSearchCriteriaServiceAttributesType myEmailCriteria = new EmailExchangeUserSearchCriteriaServiceAttributes();
        myEmailCriteria.setUserEmailAddress(emailAddress);
        myUserSearchCriteria.getUserSearchCriteriaServiceAttributes().add(myEmailCriteria);

        //set the sort order
        BASUserSearchResultSortByEnum mySortByEnum=new BASUserSearchResultSortByEnum();
        mySortByEnum.setEnum(BASUserSearchResultSortByEnumType.DISPLAY_NAME);

        int pageSize=5;
        String locale="en_US";
        boolean sortAscending=true;

        //call findUsers API passing in null as last result to get the first page
        FindUsersResultType myResult=_myCoreWSStub.findUsers(myUserSearchCriteria, mySortByEnum, sortAscending, locale, null, pageSize);

        //loop through pages
        while (myResult.getFindUsersReturnStatus().getCode() == FindUsersReturnStatusEnumType.SUCCESS && myResult.getUserSearchResult().size()>0) {
            List<UserSearchResult> myResults=myResult.getUserSearchResult();
            for (UserSearchResult itr:myResults) {
                for (UserSearchResultServiceAttributesType itr2: itr.getUserSearchResultServiceAttributes()) {
                    if (itr2 instanceof BASUserSearchResultServiceAttributesType) {
                        BASUserSearchResultServiceAttributesType myBASAttributes=(BASUserSearchResultServiceAttributesType) itr2;
                        System.out.println("Display Name: " + myBASAttributes.getDisplayName());
                    }
                    else if (itr2 instanceof DispatcherUserSearchResultServiceAttributesType) {
                        DispatcherUserSearchResultServiceAttributesType myAttributes=(DispatcherUserSearchResultServiceAttributesType) itr2;
                        System.out.println("Dispatcher Instance Name: " + myAttributes.getDispatcherServiceInstanceName());
                    }
                    else if (itr2 instanceof EmailCommonUserSearchResultServiceAttributesType) {
                        EmailCommonUserSearchResultServiceAttributesType myAttributes=(EmailCommonUserSearchResultServiceAttributesType) itr2;
                        System.out.println("Email address: " + myAttributes.getUserEmailAddress());
                    }
                }
                System.out.println("");

            }

            //fetch next page by passing in the last UserSearchResult object in the previous list
            myResult=_myCoreWSStub.findUsers(myUserSearchCriteria, mySortByEnum, sortAscending, locale, myResult.getUserSearchResult().get(myResult.getUserSearchResult().size()-1), pageSize);

        }

        if (myResult.getFindUsersReturnStatus().getCode()!=FindUsersReturnStatusEnumType.SUCCESS) {
            System.out.println("Error occured: " + myResult.getFindUsersReturnStatus().getMessage());
        }
    }
    

    public static void main(String[] args) throws RemoteException {

        if (!Setup()) {
            System.err.println("Error while setting up web service");
            System.exit(1);
        }
        
        //Find a user account that exists on your BlackBerry Enterprise Server. 
        //Example: displayName = "Megan Ball";
        //Example: emailAddress = Megan.Ball@rim.com
        String displayName = "<userName>";
        String emailAddress = "<emailAddress>";
        
        findUsers(displayName,emailAddress);

    }
}
Next topic: Glossary

Was this information helpful? Send us your comments.