Changing a password

Use methods from the ODServer class to change the password of a Content Manager OnDemand user.

About this task

The following example uses the ODServer logon method to change a user's expired password. This example also uses the ODServer changePassword method to change the specified user's password to a new password after logon.

This example demonstrates the following ODServer methods:
  • initialize
  • logoff
  • logon
  • changePassword
  • getPassword
  • terminate
This example uses the following runtime parameters:
  • New password
  • Password
  • Port
  • Server name
  • User Id
Example of changing a password:
import com.ibm.edms.od.*;

public class TcChangePassword
{
    public static void main ( String argv[] )
    {
        ODServer odServer;
        String server, userid, original_password, new_password;
 
        //----------
        // If too few parameters, display syntax and get out
        //----------
        if ( argv.length < 5 )
        {
            System.out.println( "usage: java TcChangePassword <server> <port> <userid> <password> <new password>" );
            return;
        }
 
        try
        {
            //----------
            // Set the stage
            //----------
            System.out.println( "Testcase TcChangePassword started." );
            System.out.println( "IF the  user's password has expired ..." );
            System.out.println( "  Logon to the server using the specified password" );
            System.out.println( "  Catch the password expired exception" );
            System.out.println( "  Call logon with a new password to update the expired password" );
            System.out.println( "  Logoff" );
            System.out.println( "  Logon to the server using the new password" );
            System.out.println( "  Change the password back to the original password" );
            System.out.println( "  Logoff" );
            System.out.println( "" );
            System.out.println( "IF the  user's password has not expired ..." );
            System.out.println( "  Logon to the server using the specified password" );
            System.out.println( "  Change the user's password with the changePassword(new_password) function." );
            System.out.println( "  Logoff" );
            System.out.println( "  Logon to the server using the new password" );
            System.out.println( "  Change the password back to the original password" );
            System.out.println( "  Logoff" );
            System.out.println( "" );
            System.out.println( "---------------------------------------------------" );
            System.out.println( "" );
 
            //----------
            // Create the specified server
            //----------
            server = argv[0];
            userid = argv[2];
            original_password = argv[3];
            new_password = argv[4];
 
            ODConfig odConfig = new ODConfig();
            if( odConfig != null )
            {
                odServer = new ODServer( odConfig );
                odServer.initialize( "TcChangePassword.java" );
                odServer.setPort( Integer.parseInt(argv[1]) );
 
                //----------
                // Logon to the server using the original password
                //----------
                try
                {
                    System.out.println( "Logging on to " + server + " with user " + userid + " using original password..." );
                    odServer.logon( server, userid, original_password );
                    //----------
                    // Change to the new password
                    //----------
                    System.out.println( "Changing to new password..." );
                    odServer.changePassword( new_password );
                    System.out.println( "Current ODServer.password is " + odServer.getPassword() );
                }
                catch (ODException ode)
                {
                    if( ode.getErrorId() == ODConstant.OD_ARCMSG_CLIENT_CHANGE_EXPIRED_PASSWORD )
                    {
                        System.out.println( "User " + userid + "'s password has expired." );
                        System.out.println( "Changing "+ userid + "'s password to " + new_password );
                        odServer.logon( server, userid, original_password, new_password );
                        System.out.println( "Password changed to " + odServer.getPassword() );
                    }
                    else
                    {
                        System.out.println( "Password has not expired but logon failed" );
                        throw new Exception(ode.getErrorMsg());
                    }                                                                    
                }
 
 
                System.out.println( "Logging off..." );
                odServer.logoff( );
                //----------
                // Logon to the server using the new password
                //----------
                System.out.println( "Logging on to " + server + " with user " + userid + " using new password..." );
                odServer.logon( server, userid, new_password );
 
                //----------
                // Change back to the original password and logoff
                //----------
                System.out.println( "Changing back to original password..." );
                odServer.changePassword( original_password );
                System.out.println( "Current ODServer.password is " + odServer.getPassword() );
                System.out.println( "Logging off..." );
                odServer.logoff( );
 
                //----------
                // Cleanup
                //----------
                odServer.terminate( );
                System.out.println( "" );
                System.out.println( "---------------------------------------------------" );
                System.out.println( "" );
                System.out.println( "Testcase TcChangePassword completed successfully." );
                System.out.println( "" );
            }
        }
 
        catch ( ODException e )
        {
            System.out.println( "ODException: " + e );
            System.out.println( "   id = " + e.getErrorId( ) );
            System.out.println( "  msg = " + e.getErrorMsg( ) );
            e.printStackTrace( );
        }
 
        catch ( Exception e2 )
        {
            System.out.println( "exception: " + e2 );
            e2.printStackTrace( );
        }
    }
}