Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

BlackBerry Web Services

Reply
Contributor
scmurdock
Posts: 15
Registered: ‎01-14-2012
My Device: Developer
My Carrier: Developer
Accepted Solution

Sample Application code for creating BWS connection

Hi- I've started using BWS (Blackberry Web Services) recently after using BAA (Blackberry Administrative API) for some time. I'd like to get a tutorial or code sample that shows initializing the connection to BWS, and then using it to invoke the getUsersDetail method. I've got the code below missing the BWS connection.

 

import java.util.List;

import com.rim.ws.enterprise.admin.*;
public class BWSTest {

	public static String locale = "en_US";	
	public static void main(String[] args){
		
		RequestMetadata requestMetadata = new RequestMetadata();
		requestMetadata.setLocale(locale);
		GetUsersDetailRequest userRequest = new GetUsersDetailRequest();
		userRequest.setMetadata(requestMetadata);
		userRequest.setLoadDevices(true);
		userRequest.setLoadAccounts(true);
		userRequest.setLoadITPolicies(true);
		userRequest.setLoadSWConfigs(true);
//		List<User> allUsers=userRequest.getUsers(); this method reflects users added as part of the request, not resulting from it
		GetUsersDetailResponse response = bws.getUsersDetail(request);

		if (response.getReturnStatus().getCode().compareTo("SUCCESS") !=0){
			System.out.println("Error occurred: "+response.getReturnStatus().getMessage() );
		}
		
		for(GetUsersDetailIndividualResponse individualResponse:response.getIndividualResponses()){
			individualResponse.getUserDetail().getDisplayName();
		}

	}

}

 

Please use plain text.
Contributor
scmurdock
Posts: 15
Registered: ‎01-14-2012
My Device: Developer
My Carrier: Developer

Re: Sample Application code for creating BWS connection

I found this code at the following link in a zip file:

 

Java BWS Sample

 

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.ws.BindingProvider;

import com.rim.ws.enterprise.admin.BWS;
import com.rim.ws.enterprise.admin.BWSService;
import com.rim.ws.enterprise.admin.BWSUtil;
import com.rim.ws.enterprise.admin.BWSUtilService;
import com.rim.ws.enterprise.admin.GetEncodedUsernameRequest;
import com.rim.ws.enterprise.admin.GetEncodedUsernameResponse;
import com.rim.ws.enterprise.admin.GetUsersRequest;
import com.rim.ws.enterprise.admin.GetUsersResponse;
import com.rim.ws.enterprise.admin.GetUsersSearchCriteria;
import com.rim.ws.enterprise.admin.GetUsersSortBy;
import com.rim.ws.enterprise.admin.RequestMetadata;
import com.rim.ws.enterprise.admin.User;

/**
 * This simple program finds users on the BlackBerry Administration Server, 
 * and displays the existing users in the Console output.
 * 
 * @author gbeukeboom
 *
 */

public class TestMain {

	public static BWSService _myBWSService;
	public static BWS _bws;
	public static BWSUtilService _myBWSUtilService;
	public static BWSUtil _bwsUtil;
	private static String _locale = "en_US"; 
	private static String _clientVersion = "5.0.3"; //The version of BAS this application was created for
	private static RequestMetadata _meta = new RequestMetadata();;


	private static boolean setup() {
		String strBASURL=System.getProperty("basurl");
		URL serviceUrl = null;
		URL utilServiceUrl=null;
		
		//The Metadata object includes information about this application, it is included with every call
		_meta.setClientVersion(_clientVersion);
		_meta.setOrganizationUid("0"); //Not used currently, set to "0"
		_meta.setLocale(_locale);

		try {
			serviceUrl = new URL("https://" + strBASURL + "/enterprise/admin/ws?wsdl");
			utilServiceUrl = new URL("https://" + strBASURL + "/enterprise/admin/util/ws?wsdl");
		} catch (MalformedURLException e) {
			System.err.println("Cannot initialize the wsdl");
			return false;
		}

		//Initialize our web service stubs that will be used for all calls
		_myBWSService = new BWSService(serviceUrl);
		_bws = _myBWSService.getBWS();
		_myBWSUtilService = new BWSUtilService(utilServiceUrl);
		_bwsUtil = _myBWSUtilService.getBWSUtil();

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

		GetEncodedUsernameRequest request=new GetEncodedUsernameRequest();
		request.setMetadata(_meta);
		request.setUsername(username);
		request.setDomain(strBASURL);

		//The BAS expects the username to be encoded, this call returns the encoded value
		GetEncodedUsernameResponse geurResponse = _bwsUtil.getEncodedUsername(request);
		
		if (geurResponse.getReturnStatus().getCode().compareTo("SUCCESS") != 0){    
			System.out.println("Error occurred: " + geurResponse.getReturnStatus().getMessage());
			return false;
		}
		
		String myEncodeUsername = geurResponse.getEncodedUsername();

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

		return true;
	}

	public static void getUsers() {

		GetUsersRequest request = new GetUsersRequest();
		request.setMetadata(_meta); //Assign our Metadata to the call

		//This criteria object could be used to specify search parameters
		GetUsersSearchCriteria searchCriteria = new GetUsersSearchCriteria();

		request.setSearchCriteria(searchCriteria);
		request.setPageSize(500);

		GetUsersSortBy sortBy = new GetUsersSortBy();
		sortBy.setEMAILADDRESS(true);
		sortBy.setValue("EMAIL_ADDRESS");
		request.setSortBy(sortBy);
		request.setSortAscending(true);

		GetUsersResponse response = _bws.getUsers(request);

		//If the result returned from the call is SUCCESS then we loop through all returned users
		//outputting their information to the console.
		if (response.getReturnStatus().getCode().compareTo("SUCCESS") != 0){    
			System.out.println("Error occurred: " + response.getReturnStatus().getMessage());
		} else if (response.getUsers() != null) {
			for (User itr: response.getUsers()) {
				System.out.println("Display name: " + itr.getDisplayName());
				System.out.println("User ID: " + itr.getUid());
				for (String emailAddress :itr.getEmailAddresses() ){
					System.out.println("Email Address: " + emailAddress);
				}
				System.out.print("\n");
			}
		}
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Starting to intiialize credentials...");
		if (!setup()){
			System.out.println("Problem occurred while setting up credentials.");
			System.exit(0);
		}
		System.out.println("Credentials initialized.\n");
		System.out.println("Starting to retrieve users...");
		getUsers();
	}

}

 

Please use plain text.