Search in sources :

Example 1 with ServiceAlreadyExistsException

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);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) DN(org.forgerock.opendj.ldap.DN) Map(java.util.Map) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager) ServiceAlreadyExistsException(com.sun.identity.sm.ServiceAlreadyExistsException)

Example 2 with ServiceAlreadyExistsException

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);
        }
    }
}
Also used : CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet) CaseInsensitiveTreeSet(com.sun.identity.common.CaseInsensitiveTreeSet) Set(java.util.Set) SMSException(com.sun.identity.sm.SMSException) HashMap(java.util.HashMap) CaseInsensitiveHashMap(com.sun.identity.common.CaseInsensitiveHashMap) SSOException(com.iplanet.sso.SSOException) IOException(java.io.IOException) File(java.io.File) HashMap(java.util.HashMap) CaseInsensitiveHashMap(com.sun.identity.common.CaseInsensitiveHashMap) Map(java.util.Map) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet) ServiceAlreadyExistsException(com.sun.identity.sm.ServiceAlreadyExistsException)

Example 3 with ServiceAlreadyExistsException

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");
            }
        }
    }
}
Also used : SMSException(com.sun.identity.sm.SMSException) CaseInsensitiveProperties(com.sun.identity.common.CaseInsensitiveProperties) HashMap(java.util.HashMap) CaseInsensitiveHashMap(com.sun.identity.common.CaseInsensitiveHashMap) SSOException(com.iplanet.sso.SSOException) IOException(java.io.IOException) File(java.io.File) ServiceAlreadyExistsException(com.sun.identity.sm.ServiceAlreadyExistsException)

Example 4 with ServiceAlreadyExistsException

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());
}
Also used : NotificationSet(com.iplanet.services.comm.share.NotificationSet) Set(java.util.Set) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet) ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) IdRepoException(com.sun.identity.idm.IdRepoException) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet) ServiceAlreadyExistsException(com.sun.identity.sm.ServiceAlreadyExistsException)

Example 5 with ServiceAlreadyExistsException

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);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) PrivilegeIndexStore(com.sun.identity.entitlement.PrivilegeIndexStore) HashMap(java.util.HashMap) SMSException(com.sun.identity.sm.SMSException) Date(java.util.Date) ServiceAlreadyExistsException(com.sun.identity.sm.ServiceAlreadyExistsException) EntitlementException(com.sun.identity.entitlement.EntitlementException) ServiceConfig(com.sun.identity.sm.ServiceConfig) IPrivilege(com.sun.identity.entitlement.IPrivilege) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

ServiceAlreadyExistsException (com.sun.identity.sm.ServiceAlreadyExistsException)11 SMSException (com.sun.identity.sm.SMSException)10 Set (java.util.Set)8 ServiceConfig (com.sun.identity.sm.ServiceConfig)6 HashSet (java.util.HashSet)6 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)4 File (java.io.File)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 SSOException (com.iplanet.sso.SSOException)3 CaseInsensitiveTreeSet (com.sun.identity.common.CaseInsensitiveTreeSet)3 ServiceConfigManager (com.sun.identity.sm.ServiceConfigManager)3 Map (java.util.Map)3 CaseInsensitiveHashMap (com.sun.identity.common.CaseInsensitiveHashMap)2 CaseInsensitiveProperties (com.sun.identity.common.CaseInsensitiveProperties)2 ServiceNotFoundException (com.sun.identity.sm.ServiceNotFoundException)2 Iterator (java.util.Iterator)2 Properties (java.util.Properties)2 NotificationSet (com.iplanet.services.comm.share.NotificationSet)1 EntitlementException (com.sun.identity.entitlement.EntitlementException)1