Java Code Examples for org.apache.directory.api.ldap.model.entry.Attribute#getString()

The following examples show how to use org.apache.directory.api.ldap.model.entry.Attribute#getString() . 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: SchemaEntityFactory.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Process the FQCN attribute
 * 
 * @param entry The entry to read
 * @param objectType The type of schema object
 * @return The schema object FQCN
 * @throws LdapInvalidAttributeValueException If the attribute does not contain a valid value
 */
private String getFqcn( Entry entry, String objectType ) throws LdapInvalidAttributeValueException
{
    // The FQCN
    Attribute mFqcn = entry.get( MetaSchemaConstants.M_FQCN_AT );

    if ( mFqcn == null )
    {
        String msg = I18n.err( I18n.ERR_16034_ENTRY_WITHOUT_VALID_AT, objectType, MetaSchemaConstants.M_FQCN_AT );
        
        if ( LOG.isWarnEnabled() )
        {
            LOG.warn( msg );
        }
        
        throw new IllegalArgumentException( msg );
    }

    return mFqcn.getString();
}
 
Example 2
Source File: ApacheLdapProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String readStringAttribute( final String entryDN, final String attribute )
        throws ChaiOperationException, ChaiUnavailableException, IllegalStateException
{
    activityPreCheck();
    getInputValidator().readStringAttribute( entryDN, attribute );

    try
    {
        final EntryCursor entries = connection.search(
                entryDN,
                ChaiConstant.FILTER_OBJECTCLASS_ANY,
                org.apache.directory.api.ldap.model.message.SearchScope.OBJECT,
                attribute
        );
        final Entry entry = entries.iterator().next();
        final Attribute attr = entry.get( attribute );
        return attr == null ? null : attr.getString();

    }
    catch ( LdapException e )
    {
        throw ChaiOperationException.forErrorMessage( e.getMessage() );
    }
}
 
Example 3
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Method wraps ldap client to return attribute value by name within a given entry and returns as a string.
 *
 * @param entry         contains the target ldap entry.
 * @param attributeName name of ldap attribute to retrieve.
 * @return value contained in a string variable.
 * @throws LdapInvalidAttributeValueException When we weren't able to get the attribute from the entry
 */
protected String getAttribute( Entry entry, String attributeName ) throws LdapInvalidAttributeValueException
{
    if ( entry != null )
    {
        Attribute attr = entry.get( attributeName );

        if ( attr != null )
        {
            return attr.getString();
        }
        else
        {
            return null;
        }
    }
    else
    {
        return null;
    }
}
 
Example 4
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method getString()
 */
@Test
public void testGetString() throws LdapInvalidAttributeValueException
{
    Attribute attr1 = new DefaultAttribute( atDC );

    assertEquals( 1, attr1.add( ( String ) null ) );

    Attribute attr2 = new DefaultAttribute( atDC );

    attr2.add( "a" );
    assertEquals( "a", attr2.getString() );

    Attribute attr3 = new DefaultAttribute( atPwd );

    attr3.add( BYTES1, BYTES2 );

    try
    {
        attr3.getString();
        fail();
    }
    catch ( LdapInvalidAttributeValueException ivae )
    {
        assertTrue( true );
    }
}
 
Example 5
Source File: LdifAnonymizerTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnonymizeModify() throws Exception
{
    String ldif = 
        "dn: [email protected],ou=Email,ou=Services,o=acme,dc=com\n" +
        "changetype: modify\n" +
        "replace: cn\n" +
        "cn::QUNNRSBJbmMuIExlZ2FsIFRlYW0=\n" +
        "-";
    LdifAnonymizer anonymizer = new LdifAnonymizer( schemaManager );
    anonymizer.addNamingContext( "o=acm,dc=com" );
    String result = anonymizer.anonymize( ldif );
    
    List<LdifEntry> entries = ldifReader.parseLdif( result );
    
    assertEquals( 1, entries.size() );
    
    LdifEntry entry = entries.get( 0 );
    assertTrue( entry.isChangeModify() );
    assertEquals( 1, entry.getModifications().size() );
    
    Modification modification = entry.getModifications().get( 0 );
    assertEquals( ModificationOperation.REPLACE_ATTRIBUTE, modification.getOperation() );

    Attribute attribute = modification.getAttribute();
    assertEquals( "cn", attribute.getUpId() );
    assertEquals( 1, attribute.size() );
    
    String value = attribute.getString();
    
    // We can only test the length and the fact the values are not equal (as the value has been anonymized)
    assertEquals( "AAAAAAAAAAAAAAAAAAAA".length(), value.length() );
    assertEquals( "AAAAAAAAAAAAAAAAAAAA", value );
}
 
Example 6
Source File: ApacheLdapProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Map<String, String> readStringAttributes( final String entryDN, final Set<String> attributes )
        throws ChaiOperationException, ChaiUnavailableException, IllegalStateException
{
    activityPreCheck();
    getInputValidator().readStringAttributes( entryDN, attributes );

    try
    {
        final EntryCursor entries = connection.search(
                entryDN,
                ChaiConstant.FILTER_OBJECTCLASS_ANY,
                org.apache.directory.api.ldap.model.message.SearchScope.OBJECT,
                attributes.toArray( new String[attributes.size()] )
        );
        final Entry entry = entries.iterator().next();
        final Collection<Attribute> attrs = entry.getAttributes();
        final Map<String, String> returnMap = new LinkedHashMap<>();
        for ( final Attribute attr : attrs )
        {
            final String name = attr.getId();
            final String value = attr.getString();
            returnMap.put( name, value );
        }

        return returnMap;

    }
    catch ( LdapException e )
    {
        throw ChaiOperationException.forErrorMessage( e.getMessage() );
    }
}
 
Example 7
Source File: SchemaAwareLdifReaderTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
@Test
public void testLdifParserRootDSE() throws Exception
{
    String ldif =
        "version:   1\n" +
            "dn:\n" +
            "cn:: YXBwMQ==\n" +
            "objectClass: top\n" +
            "objectClass: apApplication\n" +
            "displayName: app1   \n" +
            "userPassword: test";

    try ( LdifReader reader = new LdifReader( schemaManager ) )
    {
        List<LdifEntry> entries = reader.parseLdif( ldif );

        assertNotNull( entries );

        LdifEntry entry = entries.get( 0 );
        assertTrue( entry.isLdifContent() );

        assertEquals( "", entry.getDn().getName() );

        Attribute attr = entry.get( "cn" );
        
        if ( attr.isHumanReadable() )
        {
            String cn = attr.getString();
        }
        
        assertTrue( attr.contains( "app1" ) );

        attr = entry.get( "objectclass" );
        assertTrue( attr.contains( "top" ) );
        assertTrue( attr.contains( "apApplication" ) );

        attr = entry.get( "displayname" );
        assertTrue( attr.contains( "app1" ) );

        attr = entry.get( "userPassword" );
        assertEquals( "test", attr.get().getString() );
    }
}
 
Example 8
Source File: SchemaEntityFactory.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ObjectClass getObjectClass( SchemaManager schemaManager, Entry entry, Registries targetRegistries,
    String schemaName ) throws LdapException
{
    checkEntry( entry, SchemaConstants.OBJECT_CLASS );

    // The ObjectClass OID
    String oid = getOid( entry, SchemaConstants.OBJECT_CLASS, schemaManager.isStrict() );

    // Get the schema
    if ( !schemaManager.isSchemaLoaded( schemaName ) )
    {
        // The schema is not loaded. We can't create the requested ObjectClass
        String msg = I18n.err( I18n.ERR_16030_CANNOT_ADD_OC, entry.getDn().getName(), schemaName );
        
        if ( LOG.isWarnEnabled() )
        {
            LOG.warn( msg );
        }
        
        throw new LdapUnwillingToPerformException( ResultCodeEnum.UNWILLING_TO_PERFORM, msg );
    }

    Schema schema = getSchema( schemaName, targetRegistries );

    if ( schema == null )
    {
        // The schema is disabled. We still have to update the backend
        if ( LOG.isInfoEnabled() )
        {
            LOG.info( I18n.err( I18n.ERR_16031_CANNOT_ADD_OC_IN_REGISTRY, entry.getDn().getName(), schemaName ) );
        }
        
        schema = schemaManager.getLoadedSchema( schemaName );
    }

    // Create the ObjectClass instance
    ObjectClass oc = new ObjectClass( oid );

    // The Sup field
    Attribute mSuperiors = entry.get( MetaSchemaConstants.M_SUP_OBJECT_CLASS_AT );

    if ( mSuperiors != null )
    {
        oc.setSuperiorOids( getStrings( mSuperiors ) );
    }

    // The May field
    Attribute mMay = entry.get( MetaSchemaConstants.M_MAY_AT );

    if ( mMay != null )
    {
        oc.setMayAttributeTypeOids( getStrings( mMay ) );
    }

    // The Must field
    Attribute mMust = entry.get( MetaSchemaConstants.M_MUST_AT );

    if ( mMust != null )
    {
        oc.setMustAttributeTypeOids( getStrings( mMust ) );
    }

    // The objectClassType field
    Attribute mTypeObjectClass = entry.get( MetaSchemaConstants.M_TYPE_OBJECT_CLASS_AT );

    if ( mTypeObjectClass != null )
    {
        String type = mTypeObjectClass.getString();
        oc.setType( ObjectClassTypeEnum.getClassType( type ) );
    }

    // Common properties
    setSchemaObjectProperties( oc, entry, schema );

    return oc;
}
 
Example 9
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void add( AddOperationContext addContext ) throws LdapException
{
    Dn name = addContext.getDn();
    Entry entry = addContext.getEntry();

    // Not the responsibility of MyVD to verify schema
    //check( name, entry );

    // Special checks for the MetaSchema branch
    if ( name.isDescendantOf( schemaBaseDn ) )
    {
        // get the schema name
        String schemaName = getSchemaName( name );

        if ( entry.contains( OBJECT_CLASS_AT, SchemaConstants.META_SCHEMA_OC ) )
        {
            next( addContext );

            if ( schemaManager.isSchemaLoaded( schemaName ) )
            {
                // Update the OC superiors for each added ObjectClass
                computeSuperiors();
            }
        }
        else if ( entry.contains( OBJECT_CLASS_AT, SchemaConstants.META_OBJECT_CLASS_OC ) )
        {
            // This is an ObjectClass addition
            checkOcSuperior( addContext.getEntry() );

            next( addContext );

            // Update the structures now that the schema element has been added
            Schema schema = schemaManager.getLoadedSchema( schemaName );

            if ( ( schema != null ) && schema.isEnabled() )
            {
                Attribute oidAT = entry.get( MetaSchemaConstants.M_OID_AT );
                String ocOid = oidAT.getString();

                ObjectClass addedOC = schemaManager.lookupObjectClassRegistry( ocOid );
                computeSuperior( addedOC );
            }
        }
        else if ( entry.contains( OBJECT_CLASS_AT, SchemaConstants.META_ATTRIBUTE_TYPE_OC ) )
        {
            // This is an AttributeType addition
            next( addContext );
        }
        else
        {
            next( addContext );
        }

    }
    else
    {
        next( addContext );
    }
}
 
Example 10
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void add( AddOperationContext addContext ) throws LdapException
{
    Dn name = addContext.getDn();
    Entry entry = addContext.getEntry();

    // Not the responsibility of MyVD to verify schema
    //check( name, entry );

    // Special checks for the MetaSchema branch
    if ( name.isDescendantOf( schemaBaseDn ) )
    {
        // get the schema name
        String schemaName = getSchemaName( name );

        if ( entry.contains( OBJECT_CLASS_AT, SchemaConstants.META_SCHEMA_OC ) )
        {
            next( addContext );

            if ( schemaManager.isSchemaLoaded( schemaName ) )
            {
                // Update the OC superiors for each added ObjectClass
                computeSuperiors();
            }
        }
        else if ( entry.contains( OBJECT_CLASS_AT, SchemaConstants.META_OBJECT_CLASS_OC ) )
        {
            // This is an ObjectClass addition
            checkOcSuperior( addContext.getEntry() );

            next( addContext );

            // Update the structures now that the schema element has been added
            Schema schema = schemaManager.getLoadedSchema( schemaName );

            if ( ( schema != null ) && schema.isEnabled() )
            {
                Attribute oidAT = entry.get( MetaSchemaConstants.M_OID_AT );
                String ocOid = oidAT.getString();

                ObjectClass addedOC = schemaManager.lookupObjectClassRegistry( ocOid );
                computeSuperior( addedOC );
            }
        }
        else if ( entry.contains( OBJECT_CLASS_AT, SchemaConstants.META_ATTRIBUTE_TYPE_OC ) )
        {
            // This is an AttributeType addition
            next( addContext );
        }
        else
        {
            next( addContext );
        }

    }
    else
    {
        next( addContext );
    }
}
 
Example 11
Source File: LdapLoginManager.java    From openmeetings with Apache License 2.0 4 votes vote down vote up
private static String getStringAttr(Properties config, Entry entry, String aliasCode, String defaultAlias) throws LdapInvalidAttributeValueException {
	Attribute a = getAttr(config, entry, aliasCode, defaultAlias);
	return a == null ? null : a.getString();
}
 
Example 12
Source File: LDAPApi.java    From mamute with Apache License 2.0 4 votes vote down vote up
private String getAttribute(Entry entry, String attribute) throws LdapException {
	Attribute attr = entry.get(attribute);
	return attr == null ? null : attr.getString();
}
 
Example 13
Source File: ObjectQueryService.java    From guacamole-client with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the identifier of the object represented by the given LDAP
 * entry. Multiple attributes may be declared as containing the identifier
 * of the object when present on an LDAP entry. If multiple such attributes
 * are present on the same LDAP entry, the value of the attribute with
 * highest priority is used. If multiple copies of the same attribute are
 * present on the same LDAPentry, the first value of that attribute is
 * used.
 *
 * @param entry
 *     The entry representing the Guacamole object whose unique identifier
 *     should be determined.
 *
 * @param attributes
 *     A collection of all attributes which may be used to specify the
 *     unique identifier of the Guacamole object represented by an LDAP
 *     entry, in order of decreasing priority.
 *
 * @return
 *     The identifier of the object represented by the given LDAP entry, or
 *     null if no attributes declared as containing the identifier of the
 *     object are present on the entry.
 * 
 * @throws LdapInvalidAttributeValueException
 *     If an error occurs retrieving the value of the identifier attribute.
 */
public String getIdentifier(Entry entry, Collection<String> attributes) 
        throws LdapInvalidAttributeValueException {

    // Retrieve the first value of the highest priority identifier attribute
    for (String identifierAttribute : attributes) {
        Attribute identifier = entry.get(identifierAttribute);
        if (identifier != null)
            return identifier.getString();
    }

    // No identifier attribute is present on the entry
    return null;

}
 
Example 14
Source File: ObjectQueryService.java    From guacamole-client with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the identifier of the object represented by the given LDAP
 * entry. Multiple attributes may be declared as containing the identifier
 * of the object when present on an LDAP entry. If multiple such attributes
 * are present on the same LDAP entry, the value of the attribute with
 * highest priority is used. If multiple copies of the same attribute are
 * present on the same LDAPentry, the first value of that attribute is
 * used.
 *
 * @param entry
 *     The entry representing the Guacamole object whose unique identifier
 *     should be determined.
 *
 * @param attributes
 *     A collection of all attributes which may be used to specify the
 *     unique identifier of the Guacamole object represented by an LDAP
 *     entry, in order of decreasing priority.
 *
 * @return
 *     The identifier of the object represented by the given LDAP entry, or
 *     null if no attributes declared as containing the identifier of the
 *     object are present on the entry.
 * 
 * @throws LdapInvalidAttributeValueException
 *     If an error occurs retrieving the value of the identifier attribute.
 */
public String getIdentifier(Entry entry, Collection<String> attributes) 
        throws LdapInvalidAttributeValueException {

    // Retrieve the first value of the highest priority identifier attribute
    for (String identifierAttribute : attributes) {
        Attribute identifier = entry.get(identifierAttribute);
        if (identifier != null)
            return identifier.getString();
    }

    // No identifier attribute is present on the entry
    return null;

}