001: /**
002: * $Id: AMHelper.java,v 1.1 2005/08/27 05:15:39 yue Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wsrp.consumer.admin.mbeans;
014:
015: import java.util.ArrayList;
016: import java.util.HashMap;
017: import java.util.Iterator;
018: import java.util.Map;
019: import java.util.Set;
020:
021: import netscape.ldap.LDAPDN;
022: import netscape.ldap.util.DN;
023:
024: import com.iplanet.am.sdk.AMConstants;
025: import com.iplanet.am.sdk.AMObject;
026: import com.iplanet.am.sdk.AMOrganization;
027: import com.iplanet.am.sdk.AMStoreConnection;
028: import com.sun.portal.admin.server.AdminServerUtil;
029:
030: public class AMHelper {
031: public static AMObject getAMObject(String dn) throws Exception {
032: AMStoreConnection sc = new AMStoreConnection(AdminServerUtil
033: .getSSOToken());
034: int objectType = sc.getAMObjectType(dn);
035: AMObject dpObj = null;
036:
037: switch (objectType) {
038: case AMObject.ORGANIZATION:
039: dpObj = sc.getOrganization(dn);
040: break;
041: case AMObject.ORGANIZATIONAL_UNIT:
042: dpObj = sc.getOrganizationalUnit(dn);
043: break;
044: case AMObject.GROUP:
045: case AMObject.STATIC_GROUP:
046: dpObj = sc.getStaticGroup(dn);
047: break;
048: case AMObject.DYNAMIC_GROUP:
049: dpObj = sc.getDynamicGroup(dn);
050: break;
051: case AMObject.ASSIGNABLE_DYNAMIC_GROUP:
052: dpObj = sc.getAssignableDynamicGroup(dn);
053: break;
054: case AMObject.ROLE:
055: dpObj = sc.getRole(dn);
056: break;
057: case AMObject.FILTERED_ROLE:
058: dpObj = sc.getFilteredRole(dn);
059: break;
060: case AMObject.USER:
061: dpObj = sc.getUser(dn);
062: break;
063: case AMObject.PEOPLE_CONTAINER:
064: dpObj = sc.getPeopleContainer(dn);
065: break;
066: case AMObject.GROUP_CONTAINER:
067: dpObj = sc.getGroupContainer(dn);
068: break;
069: default:
070: }
071:
072: return dpObj;
073: }
074:
075: public static String getOrganizationDN(String dn) throws Exception {
076: return getAMObject(dn).getOrganizationDN();
077: }
078:
079: public static AMOrganization getOrganization(String dn)
080: throws Exception {
081: AMStoreConnection sc = new AMStoreConnection(AdminServerUtil
082: .getSSOToken());
083: return sc.getOrganization(getOrganizationDN(dn));
084: }
085:
086: public static String getNamingAttribute(int objectType) {
087: String namingAttribute = "";
088:
089: try {
090: namingAttribute = AMStoreConnection
091: .getNamingAttribute(objectType);
092: } catch (Exception e) {
093: }
094:
095: return namingAttribute;
096: }
097:
098: public static String DNToName(String roleDN) {
099: String result = roleDN;
100:
101: if (DN.isDN(roleDN)) {
102: String[] components = LDAPDN.explodeDN(roleDN, true);
103: result = components[0];
104:
105: // temp workaround until localization of role names is completed
106: //
107: // when displaying the role name for one of the group admin
108: // strip off the cn= and the remainder of the name after the
109: // first occurance of "_" which is not part of actual role name.
110: // Help Desk Admin's are named 'help_desk_cn=' so for
111: // this case we must strip off the naming portion, then
112: // everything after the first '_'.
113: String HD_NAME = "help_desk_"
114: + getNamingAttribute(AMObject.GROUP) + "=";
115:
116: String suffix = "Admin";
117:
118: if (result.startsWith(HD_NAME)) {
119: result = result.substring(HD_NAME.length());
120: suffix = "Help Desk Admin";
121: int end = result.indexOf("_");
122:
123: if (end == -1) {
124: end = result.length();
125: }
126:
127: result = result.substring(0, end) + " " + suffix;
128: } else {
129: String grpNamingAttr = getNamingAttribute(AMObject.GROUP);
130: String pcNamingAttr = getNamingAttribute(AMObject.PEOPLE_CONTAINER);
131:
132: if (result.startsWith(grpNamingAttr + "=")) {
133: /*
134: * Sub groups will contain multiple occurences of '_cn'
135: * in the dn so we need to first check if this string
136: * exists in the new result string. If '_cn=' is not
137: * present, then return the portion leading up to '_ou'.
138: */
139: int start = 3;
140: int end = result.indexOf("_" + grpNamingAttr + "=");
141:
142: if (end == -1) {
143: end = result
144: .lastIndexOf("_"
145: + getNamingAttribute(AMObject.GROUP_CONTAINER));
146:
147: if (end == -1) {
148: end = result.length();
149: }
150: }
151:
152: result = result.substring(start, end) + " "
153: + suffix;
154: } else if (result.startsWith(pcNamingAttr + "=")) {
155: int start = 3;
156: result = result.substring(start);
157: int end = result.indexOf("_");
158:
159: if (end == -1) {
160: end = result.length();
161: }
162:
163: result = result.substring(0, end) + " " + suffix;
164: }
165: }
166: }
167:
168: return result;
169: }
170:
171: public static Map getAllRoles(String dn) throws Exception {
172: Set roles = getOrganization(dn).getRoles(AMConstants.SCOPE_ONE);
173: Map result = new HashMap(roles.size());
174:
175: for (Iterator i = roles.iterator(); i.hasNext();) {
176: String roleDN = (String) i.next();
177: String roleDNDisplayString = DNToName(roleDN);
178:
179: if (!roleDNDisplayString.startsWith("_WSRPRole_")) {
180: result.put(roleDN, roleDNDisplayString);
181: }
182: }
183:
184: return result;
185: }
186: }
|