Sunday, March 8, 2015

Using Oracle OIM API (simplified from OIM doc)

import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import javax.security.auth.login.LoginException;

import oracle.iam.identity.exception.UserSearchException;
import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.usermgmt.api.UserManagerConstants.AttributeName;
import oracle.iam.identity.usermgmt.vo.User;
import oracle.iam.platform.OIMClient;
import oracle.iam.platform.authz.exception.AccessDeniedException;
import oracle.iam.platform.entitymgr.vo.SearchCriteria;

public class NEASOIMClient {
    private static OIMClient oimClient;

    private void init() throws LoginException {
        System.setProperty("java.security.auth.login.config", "/mypathto/conf/authwl.conf");
        System.setProperty("APPSERVER_TYPE","wls");

        System.out.println("Creating client....");
        String ctxFactory = "weblogic.jndi.WLInitialContextFactory";
        String serverURL = "t3://127.0.0.1:14000";
        String username = "xelsysadm";
        String password = "password";
        Hashtable<String,String> env = new Hashtable<String,String>();
        env.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, ctxFactory);
        env.put(OIMClient.JAVA_NAMING_PROVIDER_URL, serverURL);
        oimClient = new OIMClient(env);
        System.out.println("Logging in");
        oimClient.login(username, password.toCharArray());
        System.out.println("Log in successful");
    }

    private void getUserLogin(String psFirstName) {
        UserManager userService = oimClient.getService(UserManager.class);
        Set<String> retAttrs = new HashSet<String>();
        retAttrs.add(AttributeName.USER_LOGIN.getId());
        SearchCriteria criteria = new SearchCriteria(AttributeName.FIRSTNAME.getId(), psFirstName, SearchCriteria.Operator.EQUAL);
        try {
            List<User> users = userService.search(criteria, retAttrs, null);
            for (int i = 0; i < users.size(); i++) {
                System.out.println("First Name : " + psFirstName + "--" + users.get(i).getLogin());
            }
        } catch (AccessDeniedException ade) {
            ade.printStackTrace();
        } catch (UserSearchException use) {
            use.printStackTrace();
        }
    }

    public static void main(String args[]) {
        try {
            NEASOIMClient oimSample = new NEASOIMClient();
            oimSample.init();
            oimSample.getUserLogin("Leo*");
            oimClient.logout();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

No comments:

Post a Comment