Java Code Examples for javax.naming.ldap.LdapContext#modifyAttributes()

The following examples show how to use javax.naming.ldap.LdapContext#modifyAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TriggerUtils.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Defines the Administration point and administrative role for the TriggerExecution specific point
 * @param apCtx The administrative point context
 * @throws NamingException If the operation failed
 */
public static void defineTriggerExecutionSpecificPoint( LdapContext apCtx ) throws NamingException
{
    Attributes ap = apCtx.getAttributes( "", new String[] { SchemaConstants.ADMINISTRATIVE_ROLE_AT } );
    Attribute administrativeRole = ap.get( SchemaConstants.ADMINISTRATIVE_ROLE_AT );
    
    if ( administrativeRole == null
        || !AttributeUtils.containsValueCaseIgnore( administrativeRole, SchemaConstants.TRIGGER_EXECUTION_SPECIFIC_AREA ) )
    {
        Attributes changes = new BasicAttributes( SchemaConstants.ADMINISTRATIVE_ROLE_AT,
            SchemaConstants.TRIGGER_EXECUTION_SPECIFIC_AREA, true );
        apCtx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
    }
}
 
Example 2
Source File: TriggerUtils.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Load an prescriptive trigger specification
 * 
 * @param apCtx The administrative point context
 * @param subentryCN The subentry CN
 * @param triggerSpec The trigger specification
 * @throws NamingException If the operation failed
 */
public static void loadPrescriptiveTriggerSpecification(
    LdapContext apCtx,
    String subentryCN,
    String triggerSpec ) throws NamingException
{
    Attributes changes = new BasicAttributes( SchemaConstants.PRESCRIPTIVE_TRIGGER_SPECIFICATION_AT, triggerSpec, true );
    apCtx.modifyAttributes( "cn=" + subentryCN, DirContext.ADD_ATTRIBUTE, changes );
}
 
Example 3
Source File: TriggerUtils.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
     * Load the trigger specification entry
     * 
     * @param ctx The context
     * @param triggerSpec The trigger specification
     * @throws NamingException If the operation failed
     */
public static void loadEntryTriggerSpecification(
        LdapContext ctx,
        String triggerSpec ) throws NamingException
    {
        Attributes changes = new BasicAttributes( SchemaConstants.ENTRY_TRIGGER_SPECIFICATION_AT, triggerSpec, true );
        ctx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
    }
 
Example 4
Source File: JNDIProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
@LdapOperation
@ModifyOperation
public final void deleteStringAttributeValue( final String entryDN, final String attributeName, final String attributeValue )
        throws ChaiUnavailableException, ChaiOperationException
{
    activityPreCheck();
    getInputValidator().deleteStringAttributeValue( entryDN, attributeName, attributeValue );

    // Create a BasicAttribute for the object.
    final BasicAttribute attributeToReplace = new BasicAttribute( attributeName, attributeValue );

    // Create the ModificationItem
    final ModificationItem[] modificationItem = new ModificationItem[1];

    // Populate the ModificationItem object with the flag & the attribute to replace.
    modificationItem[0] = new ModificationItem( DirContext.REMOVE_ATTRIBUTE, attributeToReplace );

    // Modify the Attributes.
    final LdapContext ldapConnection = getLdapConnection();
    try
    {
        ldapConnection.modifyAttributes( addJndiEscape( entryDN ), modificationItem );
    }
    catch ( NamingException e )
    {
        convertNamingException( e );
    }
}
 
Example 5
Source File: JNDIProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
@LdapOperation
@ModifyOperation
public final void replaceStringAttribute( final String entryDN, final String attributeName, final String oldValue, final String newValue )
        throws ChaiUnavailableException, ChaiOperationException
{
    activityPreCheck();
    getInputValidator().replaceStringAttribute( entryDN, attributeName, oldValue, newValue );

    // Create the ModificationItem
    final ModificationItem[] mods = new ModificationItem[2];

    // Mark the flag to remover the existing attribute.
    mods[0] = new ModificationItem( DirContext.REMOVE_ATTRIBUTE, new BasicAttribute( attributeName, oldValue ) );

    // Mark the flag to add the new attribute
    mods[1] = new ModificationItem( DirContext.ADD_ATTRIBUTE, new BasicAttribute( attributeName, newValue ) );

    // get ldap connection
    final LdapContext ldapConnection = getLdapConnection();

    // Modify the Attributes.
    try
    {
        ldapConnection.modifyAttributes( addJndiEscape( entryDN ), mods );
    }
    catch ( NamingException e )
    {
        convertNamingException( e );
    }
}
 
Example 6
Source File: JNDIProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
@LdapOperation
@ModifyOperation
public final void writeStringAttribute( final String entryDN, final String attributeName, final Set<String> values, final boolean overwrite )
        throws ChaiUnavailableException, ChaiOperationException
{
    activityPreCheck();
    getInputValidator().writeStringAttribute( entryDN, attributeName, values, overwrite );


    // Create the ModificationItem
    final ModificationItem[] modificationItem = new ModificationItem[values.size()];

    int loopCounter = 0;
    for ( final String value : values )
    {
        // Create a BasicAttribute for the object.
        final BasicAttribute attributeToReplace = new BasicAttribute( attributeName, value );

        // Determine the modification type, if replace, only replace on the first attribute, the rest just get added.
        final int modType = ( loopCounter == 0 && overwrite ) ? DirContext.REPLACE_ATTRIBUTE : DirContext.ADD_ATTRIBUTE;

        // Populate the ModificationItem object with the flag & the attribute to replace.
        modificationItem[loopCounter] = new ModificationItem( modType, attributeToReplace );
        loopCounter++;
    }

    // get ldap connection
    final LdapContext ldapConnection = getLdapConnection();

    // Modify the Attributes.
    try
    {
        ldapConnection.modifyAttributes( addJndiEscape( entryDN ), modificationItem );
    }
    catch ( NamingException e )
    {
        LOGGER.trace( "error during write of attribute '" + attributeName + "', error: " + e.getMessage() );
        convertNamingException( e );
    }
}
 
Example 7
Source File: EntityService.java    From cukes with Apache License 2.0 5 votes vote down vote up
public void modifyByDn(String dn, List<ModificationItem> modificationItems) {
    try {
        LdapContext context = connectionService.getContext();
        context.modifyAttributes(dn, modificationItems.toArray(new ModificationItem[modificationItems.size()]));
    } catch (NamingException e) {
        throw new CukesRuntimeException("Cannot modify entity by dn " + dn, e);
    } finally {
        connectionService.close();
    }
}
 
Example 8
Source File: JNDIProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 4 votes vote down vote up
public final void writeStringAttributes(
        final String entryDN,
        final Map<String, String> attributeValueProps,
        final boolean overwrite,
        final BasicControl[] controls
)
        throws ChaiUnavailableException, ChaiOperationException
{
    activityPreCheck();
    getInputValidator().writeStringAttributes( entryDN, attributeValueProps, overwrite );

    // Determine the modification type, if replace, only replace on the first attribute, the rest just get added.
    final int modType = overwrite ? DirContext.REPLACE_ATTRIBUTE : DirContext.ADD_ATTRIBUTE;

    // Create the ModificationItem
    final List<ModificationItem> modificationItems = new ArrayList<>();
    for ( final Map.Entry<String, String> entry : attributeValueProps.entrySet() )
    {
        // Create a BasicAttribute for the object.
        final BasicAttribute attributeToReplace = new BasicAttribute( entry.getKey(), entry.getValue() );

        // Populate the ModificationItem object with the flag & the attribute to replace.
        modificationItems.add( new ModificationItem( modType, attributeToReplace ) );
    }

    // convert to array
    final ModificationItem[] modificationItemArray = modificationItems.toArray( new ModificationItem[modificationItems.size()] );

    // get ldap connection
    final LdapContext ldapConnection = getLdapConnection();

    // Modify the Attributes.
    try
    {
        ldapConnection.modifyAttributes( addJndiEscape( entryDN ), modificationItemArray );
    }
    catch ( NamingException e )
    {
        convertNamingException( e );
    }
}