use of com.sun.identity.sm.ServiceAlreadyExistsException in project OpenAM by OpenRock.
the class AMServiceUtils method getOrgConfig.
/**
* Get organization config for the service
*
* @param token
* SSOToken
* @param orgDN
* DN of the org or org unit
* @param serviceName
* Service Name
* @return ServiceConfig of the organization for the service
*/
public static ServiceConfig getOrgConfig(SSOToken token, String orgDN, String serviceName) throws SSOException, AMException {
try {
ServiceConfigManager scm = new ServiceConfigManager(serviceName, token);
ServiceConfig sc = scm.getOrganizationConfig(orgDN, null);
DN theOrgDN = DN.valueOf(orgDN);
if (theOrgDN.equals(DN.valueOf(SMSEntry.getAMSdkBaseDN())) && sc != null) {
Map avPair = sc.getAttributes();
Set subConfigs = sc.getSubConfigNames();
if (avPair.isEmpty() && (subConfigs == null || subConfigs.isEmpty())) {
return null;
}
}
return sc;
} catch (ServiceNotFoundException ex) {
Object[] args = { serviceName };
String locale = AMCommonUtils.getUserLocale(token);
throw new AMException(AMSDKBundle.getString("481", args, locale), "481", args);
} catch (ServiceAlreadyExistsException ex) {
Object[] args = { serviceName };
String locale = AMCommonUtils.getUserLocale(token);
throw new AMException(AMSDKBundle.getString("479", args, locale), "479", args);
} catch (SMSException ex) {
Object[] args = { serviceName };
String locale = AMCommonUtils.getUserLocale(token);
throw new AMException(AMSDKBundle.getString("482", args, locale), "482", args);
}
}
use of com.sun.identity.sm.ServiceAlreadyExistsException in project OpenAM by OpenRock.
the class SMSEnhancedFlatFileObject method loadMapper.
/**
* Loads the dirrectory mapper, create it if it doesn't exist.
**/
synchronized void loadMapper() throws SMSException {
String fileName = mRootDir + File.separator + DIR_TREE_FILENAME;
File fileHandle = new File(fileName);
if (fileHandle.isFile()) {
if (!fileHandle.canRead()) {
String errmsg = "SMSEnhancedFlatFileObject.initialize: cannot read file " + fileName;
mDebug.error(errmsg);
throw new SMSException(errmsg);
}
parseDirectoryTreeXML(fileName);
} else {
try {
fileHandle.createNewFile();
} catch (IOException e) {
String errmsg = "SMSEnhancedFlatFileObject.initialize: " + "cannot create file, " + fileName + ". Exception " + e.getMessage();
mDebug.error("SMSEnhancedFlatFileObject.initialize", e);
throw new SMSException(errmsg);
} catch (SecurityException e) {
String errmsg = "SMSEnhancedFlatFileObject.initialize: " + "cannot create file, " + fileName + ". Exception " + e.getMessage();
mDebug.error("SMSEnhancedFlatFileObject.initialize", e);
throw new SMSException(errmsg);
}
root = new SMSFlatFileTreeNode(mRootDN);
try {
Map map = new HashMap(2);
Set set = new HashSet(4);
set.add("top");
set.add("organizationalunit");
map.put("objectclass", set);
create(null, "ou=services," + mRootDN, map);
saveDirectoryTree();
} catch (SSOException e) {
// not possible
} catch (ServiceAlreadyExistsException e) {
mDebug.message("SMSEnhancedFlatFileObject.initialize", e);
}
}
}
use of com.sun.identity.sm.ServiceAlreadyExistsException in project OpenAM by OpenRock.
the class SMSFlatFileObject method loadMapper.
/**
* Loads the name mapper, create it if it doesn't exist.
**/
protected void loadMapper() throws SMSException {
StringBuffer nameMapFilename = new StringBuffer(mRootDir);
nameMapFilename.append(File.separatorChar);
nameMapFilename.append(DEFAULT_NAMEMAP_FILENAME);
mNameMapHandle = new File(nameMapFilename.toString());
if (mNameMapHandle.isFile()) {
if (!mNameMapHandle.canRead()) {
String errmsg = "SMSFlatFileObject.initialize: cannot read file " + mNameMapHandle.getPath();
mDebug.error(errmsg);
throw new SMSException(errmsg);
}
mNameMap = loadProperties(mNameMapHandle, null);
} else {
try {
mNameMapHandle.createNewFile();
} catch (IOException e) {
String errmsg = "SMSFlatFileObject.initialize: " + "cannot create file, " + nameMapFilename + ". Exception " + e.getMessage();
mDebug.error(errmsg);
throw new SMSException(errmsg);
} catch (SecurityException e) {
String errmsg = "SMSFlatFileObject.initialize: " + "cannot create file " + nameMapFilename + ". Exception " + e.getMessage();
mDebug.error(errmsg);
throw new SMSException(errmsg);
}
mNameMap = new CaseInsensitiveProperties();
// create root dn if this is a new directory.
try {
create(null, mRootDN, new HashMap());
if (mDebug.messageEnabled()) {
mDebug.message("SMSFlatFileObject.initialize: " + "created SMS object for " + mRootDN);
}
} catch (SSOException e) {
// not possible
} catch (ServiceAlreadyExistsException e) {
if (mDebug.messageEnabled()) {
mDebug.message("SMSFlatFileObject.initialize: " + mRootDN + " already exists");
}
}
// also create ou=services this is a new directory.
try {
create(null, "ou=services," + mRootDN, new HashMap());
if (mDebug.messageEnabled()) {
mDebug.message("SMSFlatFileObject.initialize: " + "created SMS object for ou=services," + mRootDN);
}
} catch (SSOException e) {
// not possible
} catch (ServiceAlreadyExistsException e) {
if (mDebug.messageEnabled()) {
mDebug.message("SMSFlatFileObject.initialize: " + "ou=services," + mRootDN + " already exists");
}
}
}
}
use of com.sun.identity.sm.ServiceAlreadyExistsException in project OpenAM by OpenRock.
the class AgentsRepo method create.
/*
* (non-Javadoc)
*
* @see com.sun.identity.idm.IdRepo#create(com.iplanet.sso.SSOToken,
* com.sun.identity.idm.IdType, java.lang.String, java.util.Map)
*/
public String create(SSOToken token, IdType type, String agentName, Map attrMap) throws IdRepoException, SSOException {
if (agentName.startsWith("\"")) {
agentName = "\\" + agentName;
}
if (debug.messageEnabled()) {
debug.message("AgentsRepo.create() called: " + type + ": " + agentName);
}
if (initializationException != null) {
debug.error("AgentsRepo.create: " + "Realm " + realmName + " does not exist.");
throw (initializationException);
}
if (attrMap == null || attrMap.isEmpty()) {
if (debug.messageEnabled()) {
debug.message("AgentsRepo.create(): Attribute Map is empty ");
}
throw new IdRepoException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.ILLEGAL_ARGUMENTS, null);
}
String agentType = null;
ServiceConfig aTypeConfig = null;
if (attrMap != null && !attrMap.isEmpty()) {
if ((attrMap.keySet()).contains(IdConstants.AGENT_TYPE)) {
Set aTypeSet = (HashSet) attrMap.get(IdConstants.AGENT_TYPE);
if ((aTypeSet != null) && (!aTypeSet.isEmpty())) {
agentType = (String) aTypeSet.iterator().next();
attrMap.remove(IdConstants.AGENT_TYPE);
} else {
debug.error("AgentsRepo.create():Unable to create agents." + " Agent Type " + aTypeSet + " is empty");
throw new IdRepoException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.ILLEGAL_ARGUMENTS, null);
}
} else {
if (type.equals(IdType.AGENTONLY) || type.equals(IdType.AGENT)) {
agentType = "2.2_Agent";
} else {
debug.error("AgentsRepo.create():Unable to create agents." + " Agent Type " + agentType + " is empty");
throw new IdRepoException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.ILLEGAL_ARGUMENTS, null);
}
}
}
try {
Set<String> vals = (Set) attrMap.get("userpassword");
if ((vals != null) && !AgentConfiguration.AGENT_TYPE_OAUTH2.equals(agentType)) {
Set<String> hashedVals = new HashSet<String>(vals.size());
for (String val : vals) {
// If the password is already a hashed value, leave as is.
if (val.startsWith(hashAlgStr)) {
hashedVals.add(val);
} else {
hashedVals.add(hashAlgStr + Hash.hash(val));
}
}
attrMap.remove("userpassword");
attrMap.put("userpassword", hashedVals);
}
if (type.equals(IdType.AGENTONLY) || type.equals(IdType.AGENT)) {
ServiceConfig orgConfig = getOrgConfig(token);
if (!orgConfig.getSubConfigNames().contains(agentName)) {
/*
* While migrating 2.2 agents to new ones, look for the
* attribute 'entrydn' and remove this 'entrydn' while
* creating the agent, as it gets added in a
* getAttributes() call explicitly to the result set and
* returned. Reason:
* When queried with this entrydn/dn the lower level
* api/ ldapjdk does not return this operational attribute.
*/
if (attrMap.containsKey("entrydn")) {
attrMap.remove("entrydn");
}
orgConfig.addSubConfig(agentName, agentType, 0, attrMap);
aTypeConfig = orgConfig.getSubConfig(agentName);
} else {
// Agent already found, throw an exception
throw IdRepoDuplicateObjectException.identityOfTypeAlreadyExists(agentName, type.getName());
}
} else if (type.equals(IdType.AGENTGROUP)) {
ServiceConfig agentGroupConfig = getAgentGroupConfig(token);
if (agentGroupConfig == null) {
agentGroupConfig = createAgentGroupConfig(token);
}
if (!agentGroupConfig.getSubConfigNames().contains(agentName)) {
agentGroupConfig.addSubConfig(agentName, agentType, 0, attrMap);
aTypeConfig = agentGroupConfig.getSubConfig(agentName);
} else {
// Agent already found, throw an exception
throw IdRepoDuplicateObjectException.identityOfTypeAlreadyExists(agentName, type.getName());
}
}
} catch (ServiceAlreadyExistsException saee) {
debug.error("AgentsRepo.create():Unable to create agents ", saee);
throw IdRepoDuplicateObjectException.identityOfTypeAlreadyExists(agentName, type.getName());
} catch (SMSException smse) {
debug.error("AgentsRepo.create():Unable to create agents ", smse);
Object[] args = { NAME };
throw new IdRepoException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.UNABLE_CREATE_AGENT, args);
}
return (aTypeConfig.getDN());
}
use of com.sun.identity.sm.ServiceAlreadyExistsException in project OpenAM by OpenRock.
the class PolicyManager method addPolicy.
/**
* Adds a policy to the data store.
*
* @param policy policy object to be added to the organization
*
* @throws SSOException invalid or expired single-sign-on token
* @throws NoPermissionException user does not have sufficient
* privileges to add policy
* @throws InvalidFormatException the data in the policy object
* has been corrupted or does not have a valid format
* @throws NameAlreadyExistsException a policy with the same
* name already exists
* @throws PolicyException for any other abnormal condition
*
* @supported.api
*/
public void addPolicy(Policy policy) throws SSOException, NameAlreadyExistsException, NoPermissionException, InvalidFormatException, PolicyException {
String realmName = getOrganizationDN();
//TODO: handle non DNs/
realmName = LDAPUtils.formatToRFC(realmName);
String subjectRealm = policy.getSubjectRealm();
String[] realmNames = { realmName, subjectRealm };
if ((subjectRealm != null) && !subjectRealm.equals(realmName)) {
if (debug.messageEnabled()) {
debug.message("Can not add policy in realm :" + realmName + ", policy has realm subjects " + " from realm : " + subjectRealm);
}
throw (new InvalidFormatException(ResBundleUtils.rbName, "policy_realm_does_not_match", realmNames, null, realmName, PolicyException.POLICY));
}
validateForResourcePrefix(policy);
validateReferrals(policy);
String testCreatedBy = policy.getCreatedBy();
//testCreatedBy is set if we are doing policy replaced.
if ((testCreatedBy == null) || (testCreatedBy.length() == 0)) {
Date creationDate = new Date();
policy.setCreatedBy(token.getPrincipal().getName());
policy.setCreationDate(creationDate.getTime());
policy.setLastModifiedBy(token.getPrincipal().getName());
policy.setLastModifiedDate(creationDate.getTime());
}
// Construct the named policy
String policyXml = policy.toXML();
Map attrs = new HashMap();
Set set = new HashSet();
set.add(policyXml);
attrs.put(POLICY_XML, set);
// Get(create if necessary) ou=policies entry
ServiceConfig namedPolicy = createOrGetPolicyConfig(NAMED_POLICY, NAMED_POLICY, scm, org);
try {
//create the policy entry
namedPolicy.addSubConfig(policy.getName(), NAMED_POLICY_ID, 0, attrs);
if (isMigratedToEntitlementService()) {
PrivilegeIndexStore pis = PrivilegeIndexStore.getInstance(adminSubject, realmName);
Set<IPrivilege> privileges = PrivilegeUtils.policyToPrivileges(policy);
pis.add(privileges);
policyCache.sendPolicyChangeNotification(null, policy, ServiceListener.ADDED);
} else {
// do the addition in resources tree
//rm.addPolicyToResourceTree(policy);
rim.addPolicyToResourceTree(svtm, token, policy);
}
} catch (EntitlementException e) {
String[] objs = { policy.getName(), org };
throw (new PolicyException(ResBundleUtils.rbName, "unable_to_add_policy", objs, e));
} catch (ServiceAlreadyExistsException e) {
String[] objs = { policy.getName(), org };
if (PolicyUtils.logStatus) {
PolicyUtils.logErrorMessage("POLICY_ALREADY_EXISTS_IN_REALM", objs, token);
}
throw (new NameAlreadyExistsException(ResBundleUtils.rbName, "policy_already_exists_in_org", objs, policy.getName(), PolicyException.POLICY));
} catch (SMSException se) {
String[] objs = { policy.getName(), org };
if (PolicyUtils.logStatus) {
PolicyUtils.logErrorMessage("UNABLE_TO_ADD_POLICY", objs, token);
}
debug.error("SMS error in add policy: " + policy.getName() + " for org: " + org, se);
// Check for permission exception
if (se.getExceptionCode() == SMSException.STATUS_NO_PERMISSION) {
throw (new NoPermissionException(ResBundleUtils.rbName, "insufficient_access_rights", null));
} else {
// Throw generic policy exception
throw (new PolicyException(ResBundleUtils.rbName, "unable_to_add_policy", objs, se));
}
}
if (PolicyUtils.logStatus) {
String[] objs = { policy.getName(), org };
PolicyUtils.logAccessMessage("POLICY_CREATE_SUCCESS", objs, token);
}
}
Aggregations