четверг, 30 июля 2015 г.

Get info about user in WebSphere Application Server 8.5.5

Sometimes you should get info about user in WebSphere Application Server. WAS is connected to ldap. I have to find out the full name of a user. How to do it?

One can use the VMM - the subsystem for user management in WAS.

To compile the VMM code following jars have to be in the classpath:
  <WAS_HOME>\plugins\com.ibm.ws.runtime.jar
  <WAS_HOME>\plugins\com.ibm.ws.runtime.wim.base.jar
  <WAS_HOME>\plugins\org.eclipse.emf.commonj.sdo.jar
  <WAS_HOME>\lib\j2ee.jar

Here is the code to get the user name:

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.ibm.websphere.wim.SchemaConstants;
import com.ibm.websphere.wim.Service;
import com.ibm.websphere.wim.client.LocalServiceProvider;
import com.ibm.websphere.wim.ras.WIMTraceHelper;
import com.ibm.websphere.wim.util.SDOHelper;
import commonj.sdo.DataObject;

class VMMRealm  {
    private static Logger log = LogManager.getLogger(VMMRealm.class);
    // Virtual member manager service that is used to make API calls
    static Service service = null;
    static {
        service = locateService();
    }

 
   @SuppressWarnings("unchecked")
    public UserInfo getUserData(String login) throws UserStoreAccessException {
        UserInfo userInfo = null;
        DataObject root = null;
        try {
            root = SDOHelper.createRootDataObject();
            DataObject searchCtrl = SDOHelper.createControlDataObject(root,
                    null, SchemaConstants.DO_SEARCH_CONTROL);
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add(
                    "telephoneNumber");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add(
                    "createTimestamp");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES)
                    .add("givenName");
            searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("title");
            searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, String
                    .format("@xsi:type='PersonAccount' and uid='%s'", login));

            log.trace(printDO(root));
            root = service.search(root);
            log.trace(printDO(root));
            userInfo = new UserInfo();
            convertDataObjectToUserInfo(root, userInfo);
        } catch (Exception e) {
            throw new UserStoreAccessException("Error getting user", e);
        }
        return userInfo;
    }

    /**
     * Convert data object to user info
     *
     * @param root
     * @param info
     */
    private void convertDataObjectToUserInfo(DataObject root, UserInfo info) {
        List entities = root.getList(SchemaConstants.DO_ENTITIES);
        for (int i = 0; i < entities.size(); i++) {
            DataObject ent = (DataObject) entities.get(i);
            info.setCn(ent.getString("cn"));
            info.setUid(ent.getString("uid"));
        }
    }

    /**
     * Loop through the entities in the DataObject and print its uniqueName
     *
     * @param root
     *            input DataObject
     */
    public static void printIdentifiers(DataObject root) throws Exception {
        // Get all entities in the DataObject
        List entities = root.getList(SchemaConstants.DO_ENTITIES);
        for (int i = 0; i < entities.size(); i++) {
            DataObject ent = (DataObject) entities.get(i);
            // Get the entity Identifier
            DataObject id = ent.getDataObject(SchemaConstants.DO_IDENTIFIER);
            if (id != null) {
                String uniqueName = id
                        .getString(SchemaConstants.PROP_UNIQUE_NAME);
                log.debug("UniqueName is  -> " + uniqueName);
            } else {
                log.debug("Missing Identifier");
            }
        }
    }

    /**
     * Locates virtual member manager service in local JVM
     **/
    private static Service locateService() {
        try {
            // Local access virtual member manager Service
            return new LocalServiceProvider(null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }

    public static String printDO(DataObject obj) {
        return WIMTraceHelper.printDataObject(obj);
    }
}

To get info about the user call getUserData method.

Комментариев нет:

Отправить комментарий