001    /*
002     * file AuthenticationDemo.java
003     *
004     * Licensed Materials - Property of IBM
005     * Restricted Materials of IBM - you are allowed to copy, modify and 
006     * redistribute this file as part of any program that interfaces with 
007     * IBM Rational CM API.
008     *
009     * com.ibm.rational.stp.client.samples.cc.AuthenticationDemo
010     *
011     * (C) Copyright IBM Corporation 2008.  All Rights Reserved.
012     * Note to U.S. Government Users Restricted Rights:  Use, duplication or 
013     * disclosure restricted by GSA ADP  Schedule Contract with IBM Corp.
014     */
015    
016    package com.ibm.rational.stp.client.samples.cc;
017    
018    import javax.wvcm.ProviderFactory;
019    import javax.wvcm.WvcmException;
020    import javax.wvcm.ProviderFactory.Callback;
021    import javax.wvcm.WvcmException.ReasonCode;
022    
023    import com.ibm.rational.wvcm.stp.StpProvider;
024    import com.ibm.rational.wvcm.stp.cc.CcProvider;
025    
026    /**
027     * Demonstrate basic CM Server authentication, including use of the
028     * javax.wvcm.ProviderFactory.Callback authentication callback interface.
029     */
030    public class AuthenticationDemo extends CcDemoBase {
031    
032        public class DemoCallback implements Callback {
033    
034            public Authentication getAuthentication(String realm, int retryCount)
035                throws WvcmException
036            {
037                trace("Getting credentials for: " + realm);
038                if (retryCount > 0) {
039                    throw new WvcmException("Invalid credentials", ReasonCode.UNAUTHORIZED);
040                }
041                return new Authentication() {
042                    public String loginName() {
043                        return props().getLoginNameAndDomain();
044                    }
045                    public String password() {
046                        return props().getPassword();
047                    }
048                };
049            }
050        }
051    
052        /* (non-Javadoc)
053         * @see junit.framework.TestCase#setUp()
054         */
055        protected void setUp() throws Exception {
056            super.setUp();
057        }
058    
059        /* (non-Javadoc)
060         * @see junit.framework.TestCase#tearDown()
061         */
062        protected void tearDown() throws Exception {
063            super.tearDown();
064        }
065    
066        public void testCallback() throws Exception {
067    
068            // Instantiate our own authentication callback.
069            Callback callback = new DemoCallback();
070    
071            // Instantiate our own CcProvider so we can specify our own callback.
072            StpProvider stpProvider =
073                (StpProvider) ProviderFactory.createProvider(
074                    CcProvider.CC_ONLY_PROVIDER_CLASS, callback);
075            stpProvider.setServerUrl(props().getCmServerUrl());
076    
077            // Now perform an operation that requires a CM server connection.
078            // This will cause our authentication callback to be invoked.
079            CcProvider ccProvider = stpProvider.ccProvider();
080            ccProvider.doGetDefaultCcRegistryRegion(null);
081        }
082    }