Java Sample Code
This is a sample Java console application that:
- Prompts the user to enter a client ID, username, and password.
- Uses web services to authenticate with the LMS.
- If successful authentication, creates a user session for the user. A URL to redirect the user to is output.
The console application uses Apache Axis (an implementation of the SOAP specification) for the client portion of the web service calls.
The
"com.firmwater.webservices.lms.lmsservice1.*" stub classes can be
generated by using Axis' "WSDL2Java" tool against the LMSService1 WSDL ( QA or production).
import java.io.*; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import org.apache.axis.message.SOAPHeaderElement;
import com.firmwater.webservices.lms.lmsservice1.*;
/** * Title: Login Sample * * Description: Console application illustrating login and session * management. * * Copyright: Copyright (c) 2007 * Company: Firmwater Inc. * * @version 1.0 */ public class Samples { private LMSService1SoapStub binding; static BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in)); public Samples() {} /** * Main console routine. */ public static void main(String[] args) throws ServiceException { Samples samples1 = new Samples(); samples1.run(); } /** * Helper function for retrieving user input from the console. */ private String getUserInput(String prompt) { System.out.print(prompt); try { return rdr.readLine(); } catch (IOException ex) { return null; } } /** * The login call is used to obtain a token from Firmwater LMS. * This token must be passed to all other calls to provide * authentication and is valid for a sliding 20 minutes. */ private boolean login(String licenseeLoginId, String username, String password) throws ServiceException { // initializes the binding stub (this is our main interface to // the API through which all calls are made) binding = (LMSService1SoapStub) new LMSService1Locator().getLMSService1Soap(); LmsLoginResult loginResult; try { System.out.println("LOGGING IN NOW..."); loginResult = binding.login(licenseeLoginId, username, password); } catch (Exception ex) { // determine if invalid login if (ex.getMessage().indexOf("INVALID_LOGIN") >= 0) { // invalid login System.out.println("The client ID, username, password combination is invalid."); return false; } if (ex.getMessage().indexOf("USER_DISABLED") >= 0) { // disabled account System.out.println("Your user account has been disabled."); return false; }
// unexpected error System.out.println("An unexpected error has occurred: " + ex.getMessage()); //ex.printStackTrace(); return false; } // once the client application has logged in successfully, it // uses the results of the login call to reset the endpoint // of the service binding._setProperty("javax.xml.rpc.service.endpoint.address", loginResult.getServerUrl()); // set a persistent SOAP header (to be included in all // subsequent calls that are made with the stub) that contains // the valid sessionId for our login credentials SOAPHeaderElement sh = new SOAPHeaderElement(new LMSService1Locator().getServiceName().getNamespaceURI(), "SessionHeader"); try { sh.addChildElement("sessionId").addTextNode(loginResult.getSessionId()); } catch (Exception ex) { // unexpected error System.out.println("An unexpected error has occurred: " + ex.getMessage()); //ex.printStackTrace(); return false; } binding.setHeader(sh); // return true to indicate that we are logged in, pointed at // the right url and have our security token in place. return true; } /** * The createUserSession call is used to generate an authorization * token that is later used to create a LMS session for the user. */ private String createUserSession(String licenseeLoginId, String username) throws ServiceException { // create the person object LmsPersonObject person = new LmsPersonObject(); // set licensee id and username person.setLicenseeId(licenseeLoginId); person.setUsername(username); // must set the adminstrative privilege to 'unknown' if no // change to value is desired person.setAdministrativePrivilege(UserPrivilegeType.fromString("unknown")); // right now, web services API requires department, location, // and job title if enabled for the licensee -- regardless of // whether fields are required or not (case 1677) LmsDepartmentObject dept = new LmsDepartmentObject(); dept.setLicenseeId(licenseeLoginId); dept.setDepartmentName("Development"); person.setDepartmentObject(dept);
UserSessionResult result; try { System.out.println("CREATING USER SESSION..."); result = binding.createUserSession(person, "", ""); } catch (Exception ex) { // unexpected error System.out.println("An unexpected error has occurred: " + ex.getMessage()); //ex.printStackTrace(); return null; }
// return the url to redirect the user to return result.getUrl(); } /** * Method that prompts the user for authentication information, * logs in the user and creates a user session on his/her behalf. */ private void run() throws ServiceException { // prompt for login information String licenseeLoginId = getUserInput("Enter client ID: "); String username = getUserInput("Enter username: "); String password = getUserInput("Enter password: "); // authenticate if (login(licenseeLoginId, username, password)) { getUserInput("SUCCESSFUL LOGIN! Hit the enter key to continue."); // create user session on user's behalf String redirectUrl = createUserSession(licenseeLoginId, username); if (redirectUrl != null) { System.out.println("SUCCESS! Redirect user to " + redirectUrl); } } } }
|