Search in sources :

Example 6 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)

Example 7 with ServiceAlreadyExistsException

use of com.sun.identity.sm.ServiceAlreadyExistsException in project OpenAM by OpenRock.

the class PolicyManager method createPolicyTree.

/**
     *  Creates the policy tree, ou=policy, ou=services, ... 
     */
static void createPolicyTree(String configName, String configId, ServiceConfigManager scm, String org) throws NoPermissionException, PolicyException, SSOException {
    try {
        // Get the iPlanetPolicyService node            
        ServiceConfig pConfig = scm.getOrganizationConfig(org, null);
        if (pConfig == null) {
            // Since not present, create organization services node
            // i.e, ou=services, <org dn> and ou=iPlanetPolicyService,
            //  ou=services, ...
            scm.createOrganizationConfig(org, null);
            // Since it is created above, get it
            pConfig = scm.getOrganizationConfig(org, null);
        }
        // Create requested policy sub node
        // i.e., ou=<configName>, ou=iPlanetPolicyService, ou=services, ...
        pConfig.addSubConfig(configName, configId, 0, null);
    } catch (ServiceAlreadyExistsException se) {
        // do nothing
        if (debug.messageEnabled()) {
            debug.message("PolicyManager->createPolicyTree: Name: " + configName + " ID: " + configId + " Policy service already exists under org->" + org);
        }
    } catch (SMSException e) {
        // Check for permission exception
        String[] objs = { org };
        if (e.getExceptionCode() == SMSException.STATUS_NO_PERMISSION) {
            throw (new NoPermissionException(ResBundleUtils.rbName, "insufficient_access_rights", null));
        } else {
            throw (new PolicyException(ResBundleUtils.rbName, "unable_to_create_policy_for_org", objs, e));
        }
    }
}
Also used : ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) ServiceAlreadyExistsException(com.sun.identity.sm.ServiceAlreadyExistsException)

Example 8 with ServiceAlreadyExistsException

use of com.sun.identity.sm.ServiceAlreadyExistsException in project OpenAM by OpenRock.

the class SMSEnhancedFlatFileObject method create.

/**
     * Creates the configuration object. Creates the directory for the 
     * object and the attributes properties file with the given attributes.
     * 
     * @param token Ignored argument. Access check is assumed to have 
     * occurred before reaching this method. 
     * @param objName Name of the configuration object to create. Name is 
     * expected to be a dn.
     * @param attrs Map of attributes for the object.
     *
     * @throws IllegalArgumentException if the objName or attrs argument is 
     * null or empty.
     * @throws ServiceAlreadyExistsException if the configuration object 
     * already exists.
     * @throws SMSException if an IO error occurred while creating the 
     * configuration object.
     */
public void create(SSOToken token, String objName, Map attrs) throws SMSException, SSOException {
    if ((objName == null) || (objName.length() == 0) || (attrs == null)) {
        throw new IllegalArgumentException("SMSEnhancedFlatFileObject.create: " + "One or more arguments is null or empty");
    }
    String attributeFileName = null;
    mRWLock.readRequest();
    try {
        if (root.isExists(mRootDir, objName)) {
            String errmsg = "SMSEnhancedFlatFileObject.create: object " + objName;
            mDebug.error(errmsg);
            throw new ServiceAlreadyExistsException(errmsg);
        }
    } finally {
        mRWLock.readDone();
    }
    // Now Create the object.
    mRWLock.writeRequest();
    if (root.isExists(mRootDir, objName)) {
        String errmsg = "SMSEnhancedFlatFileObject.create: object " + objName;
        mDebug.error(errmsg);
        throw new ServiceAlreadyExistsException(errmsg);
    }
    SMSFlatFileTreeNode node = new SMSFlatFileTreeNode(objName);
    String filepath = node.getAttributeFilename(mRootDir);
    try {
        /*
             * Put attrs into in properties format, replacing any percent's 
             * with %25 and commas with %2C in the values. 
             */
        Set sunserviceids = null;
        Set sunxmlkeyvals = null;
        // there's no need for case insensitive properties here since 
        // we are not reading from it. 
        Properties props = new Properties();
        Set keys = attrs.keySet();
        if (keys != null) {
            for (Iterator i = keys.iterator(); i.hasNext(); ) {
                String key = (String) i.next();
                Set vals = (Set) attrs.get(key);
                if (key.equalsIgnoreCase(SMSEntry.ATTR_SERVICE_ID)) {
                    sunserviceids = vals;
                } else if (key.equalsIgnoreCase(SMSEntry.ATTR_XML_KEYVAL)) {
                    sunxmlkeyvals = vals;
                }
                props.put(key, toValString(vals));
            }
        }
        try {
            File attrFile = new File(filepath);
            try {
                if (!attrFile.createNewFile()) {
                    String errmsg = "SMSEnhancedFlatFileObject.create: object " + objName + ": Could not create file " + filepath;
                    mDebug.error(errmsg);
                    throw new SMSException(errmsg);
                }
            } catch (IOException e) {
                String errmsg = "SMSEnhancedFlatFileObject.create: object " + objName + " IOException encountered when creating file " + filepath + ". Exception: " + e.getMessage();
                mDebug.error("SMSEnhancedFlatFileObject.create", e);
                throw new SMSException(errmsg);
            }
            // write the attributes properties file.
            saveProperties(props, attrFile, objName);
            createSunServiceIdFiles(node, sunserviceids);
            createSunXmlKeyValFiles(node, sunxmlkeyvals);
            if (!root.addChild(node)) {
                throw new SMSException("parent not found for node name=" + objName);
            }
            saveDirectoryTree();
        } catch (SMSException e) {
            File attrFile = new File(filepath);
            try {
                attrFile.delete();
            } catch (SecurityException se) {
            //ignored
            }
            throw e;
        }
    } finally {
        mRWLock.writeDone();
    }
}
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) Iterator(java.util.Iterator) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) ServiceAlreadyExistsException(com.sun.identity.sm.ServiceAlreadyExistsException)

Example 9 with ServiceAlreadyExistsException

use of com.sun.identity.sm.ServiceAlreadyExistsException in project OpenAM by OpenRock.

the class SMSFlatFileObject method create.

/**
     * Creates the configuration object. Creates the directory for the 
     * object and the attributes properties file with the given attributes.
     * 
     * @param token Ignored argument. Access check is assumed to have 
     * occurred before reaching this method. 
     * @param objName Name of the configuration object to create. Name is 
     * expected to be a dn.
     * @param attrs Map of attributes for the object.
     *
     * @throws IllegalArgumentException if the objName or attrs argument is 
     * null or empty.
     * @throws ServiceAlreadyExistsException if the configuration object 
     * already exists.
     * @throws SMSException if an IO error occurred while creating the 
     * configuration object.
     */
public void create(SSOToken token, String objName, Map attrs) throws SMSException, SSOException {
    if (objName == null || objName.length() == 0 || attrs == null) {
        throw new IllegalArgumentException("SMSFlatFileObject.create: " + "One or more arguments is null or empty");
    }
    String objKey = objName.toLowerCase();
    String filepath = null;
    mRWLock.readRequest();
    try {
        // Check if object already exists.
        filepath = mNameMap.getProperty(objKey);
        if (filepath != null) {
            String errmsg = "SMSFlatFileObject.create: object " + objName + " already exists in " + filepath;
            mDebug.error(errmsg);
            throw new ServiceAlreadyExistsException(errmsg);
        }
    } finally {
        mRWLock.readDone();
    }
    // Now Create the object.
    mRWLock.writeRequest();
    try {
        // recheck
        filepath = mNameMap.getProperty(objKey);
        if (filepath != null) {
            String errmsg = "SMSFlatFileObject.create: object " + objName + " already exists in " + filepath;
            mDebug.error(errmsg);
            throw new ServiceAlreadyExistsException(errmsg);
        }
        filepath = getAttrFile(objName);
        File filehandle = new File(filepath);
        File parentDir = filehandle.getParentFile();
        if (parentDir.isDirectory()) {
            String errmsg = "SMSFlatFileObject.create: object " + objName + " directory " + parentDir.getPath() + " exists before create!";
            mDebug.error(errmsg);
            throw new ServiceAlreadyExistsException(errmsg);
        }
        // Put attrs into in properties format, 
        // replacing any percent's with %25 and commas with %2C
        // in the values. 
        Set sunserviceids = null;
        Set sunxmlkeyvals = null;
        // there's no need for case insensitive properties here since 
        // we are not reading from it. 
        Properties props = new Properties();
        Set keys = attrs.keySet();
        if (keys != null) {
            for (Iterator i = keys.iterator(); i.hasNext(); ) {
                String key = (String) i.next();
                Set vals = (Set) attrs.get(key);
                if (key.equalsIgnoreCase(SMSEntry.ATTR_SERVICE_ID)) {
                    sunserviceids = vals;
                } else if (key.equalsIgnoreCase(SMSEntry.ATTR_XML_KEYVAL)) {
                    sunxmlkeyvals = vals;
                }
                props.put(key, toValString(vals));
            }
        }
        // Create directory, property file, etc. 
        try {
            // create directory
            if (!parentDir.mkdirs()) {
                String errmsg = "SMSFlatFileObject.create: object " + objName + ": Could not create directory " + parentDir.getPath();
                mDebug.error(errmsg);
                throw new SMSException(errmsg);
            }
            // create the attributes properties file.
            try {
                if (!filehandle.createNewFile()) {
                    String errmsg = "SMSFlatFileObject.create: object " + objName + ": Could not create file " + filepath;
                    mDebug.error(errmsg);
                    throw new SMSException(errmsg);
                }
            } catch (IOException e) {
                String errmsg = "SMSFlatFileObject.create: object " + objName + " IOException encountered when creating file " + filehandle.getPath() + ". Exception: " + e.getMessage();
                mDebug.error("SMSFlatFileObject.create", e);
                throw new SMSException(errmsg);
            }
            // write the attributes properties file.
            saveProperties(props, filehandle, objName);
            // schemaSubEntries method. 
            if (sunserviceids != null && !sunserviceids.isEmpty()) {
                createSunServiceIdFiles(parentDir, sunserviceids);
            }
            // create sunxmlkeyvalue files for faster search
            if (sunxmlkeyvals != null && !sunxmlkeyvals.isEmpty()) {
                createSunXmlKeyValFiles(parentDir, sunxmlkeyvals);
            }
            // add the name in the name map and save.
            mNameMap.setProperty(objKey, filepath);
            saveProperties(mNameMap, mNameMapHandle, null);
        } catch (SMSException e) {
            // If any error occurred, clean up - remove the directory 
            // and files created.
            deleteDir(parentDir);
            mNameMap.remove(objKey);
            throw e;
        }
    } finally {
        mRWLock.writeDone();
    }
}
Also used : CaseInsensitiveTreeSet(com.sun.identity.common.CaseInsensitiveTreeSet) Set(java.util.Set) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) SMSException(com.sun.identity.sm.SMSException) Iterator(java.util.Iterator) IOException(java.io.IOException) Properties(java.util.Properties) CaseInsensitiveProperties(com.sun.identity.common.CaseInsensitiveProperties) File(java.io.File) ServiceAlreadyExistsException(com.sun.identity.sm.ServiceAlreadyExistsException)

Example 10 with ServiceAlreadyExistsException

use of com.sun.identity.sm.ServiceAlreadyExistsException in project OpenAM by OpenRock.

the class SMSRepositoryMig method createSMSEntry.

private static void createSMSEntry(SMSFlatFileObject smsFlatFileObject, String dn, Iterable<Attribute> attrs) throws Exception {
    // Convert attrs from LDAPAttributeSet to a Map needed by SMSObject.
    Map<String, Set<String>> attrsMap = new HashMap<>();
    for (Attribute attribute : attrs) {
        String attrName = attribute.getAttributeDescriptionAsString();
        Set<String> attrVals = new HashSet<>();
        for (ByteString value : attribute) {
            attrVals.add(value.toString());
        }
        attrsMap.put(attrName, attrVals);
    }
    try {
        smsFlatFileObject.create(null, dn, attrsMap);
    } catch (ServiceAlreadyExistsException e) {
        System.out.println("Warning: '" + dn + "' already exists.");
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Attribute(org.forgerock.opendj.ldap.Attribute) ByteString(org.forgerock.opendj.ldap.ByteString) ByteString(org.forgerock.opendj.ldap.ByteString) HashSet(java.util.HashSet) ServiceAlreadyExistsException(com.sun.identity.sm.ServiceAlreadyExistsException)

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