Java Code Examples for org.apache.directory.api.ldap.model.schema.SchemaManager#getAttributeType()

The following examples show how to use org.apache.directory.api.ldap.model.schema.SchemaManager#getAttributeType() . 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: ApacheDSUtil.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
public static AttributeType addAttributeToSchema(Attribute attribute,SchemaManager schemaManager) throws LdapException {
	String newOID = generateRandomOID(schemaManager);
	MutableAttributeType at = new MutableAttributeType(newOID);
	
	// base new attributes on uid
	AttributeType uidAT = schemaManager.getAttributeType("0.9.2342.19200300.100.1.1");
	at.setNames(attribute.getId());
	at.setSyntax(uidAT.getSyntax());
	at.setSingleValued(false);
	at.setEquality(uidAT.getEquality());
	
	at.setSubstring(uidAT.getSubstring());
	at.setSchemaName(uidAT.getSchemaName());
	at.setSpecification(uidAT.getSpecification());
	at.setUsage(uidAT.getUsage());
	
	LOG.warn("Creating dynamic schema entry : '{}' {}", at.getName(), at.getOid());
	
	schemaManager.add(at);
	return at;
}
 
Example 2
Source File: ApacheDSUtil.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
public static AttributeType addBinaryAttributeToSchema(Attribute attribute,SchemaManager schemaManager) throws LdapException {
	String newOID = generateRandomOID(schemaManager);
	MutableAttributeType at = new MutableAttributeType(newOID);
	
	// base new attributes on javaSerializedData
	AttributeType uidAT = schemaManager.getAttributeType("1.3.6.1.4.1.42.2.27.4.1.8");
	at.setNames(attribute.getId());
	at.setSyntax(uidAT.getSyntax());
	at.setSingleValued(false);
	
	at.setSchemaName(uidAT.getSchemaName());
	at.setSpecification(uidAT.getSpecification());
	at.setUsage(uidAT.getUsage());
	
	LOG.warn("Creating dynamic schema entry : '{}' {}", at.getName(), at.getOid());
	
	schemaManager.add(at);
	return at;
}
 
Example 3
Source File: ApacheDSUtil.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
public static AttributeType addAttributeToSchema(Attribute attribute,SchemaManager schemaManager) throws LdapException {
	String newOID = generateRandomOID(schemaManager);
	MutableAttributeType at = new MutableAttributeType(newOID);
	
	// base new attributes on uid
	AttributeType uidAT = schemaManager.getAttributeType("0.9.2342.19200300.100.1.1");
	at.setNames(attribute.getId());
	at.setSyntax(uidAT.getSyntax());
	at.setSingleValued(false);
	at.setEquality(uidAT.getEquality());
	
	at.setSubstring(uidAT.getSubstring());
	at.setSchemaName(uidAT.getSchemaName());
	at.setSpecification(uidAT.getSpecification());
	at.setUsage(uidAT.getUsage());
	
	LOG.warn("Creating dynamic schema entry : '{}' {}", at.getName(), at.getOid());
	
	schemaManager.add(at);
	return at;
}
 
Example 4
Source File: ApacheDSUtil.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
public static AttributeType addBinaryAttributeToSchema(Attribute attribute,SchemaManager schemaManager) throws LdapException {
	String newOID = generateRandomOID(schemaManager);
	MutableAttributeType at = new MutableAttributeType(newOID);
	
	// base new attributes on javaSerializedData
	AttributeType uidAT = schemaManager.getAttributeType("1.3.6.1.4.1.42.2.27.4.1.8");
	at.setNames(attribute.getId());
	at.setSyntax(uidAT.getSyntax());
	at.setSingleValued(false);
	
	at.setSchemaName(uidAT.getSchemaName());
	at.setSpecification(uidAT.getSpecification());
	at.setUsage(uidAT.getUsage());
	
	LOG.warn("Creating dynamic schema entry : '{}' {}", at.getName(), at.getOid());
	
	schemaManager.add(at);
	return at;
}
 
Example 5
Source File: Rdn.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * A constructor that constructs a schema aware Rdn from a type and a value.
 * <p>
 * The string attribute values are not interpreted as RFC 414 formatted Rdn
 * strings. That is, the values are used literally (not parsed) and assumed
 * to be un-escaped.
  *
 * @param schemaManager the schema manager
 * @param upType the user provided type of the Rdn
 * @param upValue the user provided value of the Rdn
 * @throws LdapInvalidDnException if the Rdn is invalid
 * @throws LdapInvalidAttributeValueException  If the given AttributeType or value are invalid
 */
public Rdn( SchemaManager schemaManager, String upType, String upValue ) throws LdapInvalidDnException, LdapInvalidAttributeValueException
{
    if ( schemaManager != null )
    {
        AttributeType attributeType = schemaManager.getAttributeType( upType );
        addAVA( schemaManager, upType, new Value( attributeType, upValue ) );
    }
    else
    {
        addAVA( schemaManager, upType, new Value( upValue ) );
    }

    StringBuilder sb = new StringBuilder();
    sb.append( upType ).append( '=' ).append( upValue );
    upName = sb.toString();
    
    sb.setLength( 0 );
    sb.append( ava.getNormType() ).append( '=' );
    
    Value value = ava.getValue();
    
    if ( value != null )
    {
        sb.append( value.getNormalized() );
    }
    
    normName = sb.toString();
    normalized = true;

    hashCode();
}
 
Example 6
Source File: SchemaManagerLoadTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * test loading the "nis" schema, which depends on "system", "core" and "cosine",
 * but is disabled
 */
@Test
public void testLoadNis() throws Exception
{
    LdifSchemaLoader loader = new LdifSchemaLoader( schemaRepository );
    SchemaManager schemaManager = new DefaultSchemaManager( loader );

    assertTrue( schemaManager.load( "system" ) );
    assertTrue( schemaManager.load( "core" ) );
    assertTrue( schemaManager.load( "cosine" ) );
    assertFalse( schemaManager.load( "nis" ) );

    AttributeType at = schemaManager.getAttributeType( "uidNumber" );
    // if nis schema was loaded then the at will not be null
    assertNull( at );

    assertTrue( schemaManager.getErrors().isEmpty() );
    assertEquals( 133, schemaManager.getAttributeTypeRegistry().size() );
    assertEquals( 36, schemaManager.getComparatorRegistry().size() );
    assertEquals( 42, schemaManager.getMatchingRuleRegistry().size() );
    assertEquals( 35, schemaManager.getNormalizerRegistry().size() );
    assertEquals( 49, schemaManager.getObjectClassRegistry().size() );
    assertEquals( 59, schemaManager.getSyntaxCheckerRegistry().size() );
    assertEquals( 66, schemaManager.getLdapSyntaxRegistry().size() );
    assertEquals( 290, schemaManager.getGlobalOidRegistry().size() );

    assertEquals( 3, schemaManager.getRegistries().getLoadedSchemas().size() );
    assertNotNull( schemaManager.getRegistries().getLoadedSchema( "system" ) );
    assertNotNull( schemaManager.getRegistries().getLoadedSchema( "core" ) );
    assertNotNull( schemaManager.getRegistries().getLoadedSchema( "cosine" ) );
    assertNull( schemaManager.getRegistries().getLoadedSchema( "nis" ) );
}
 
Example 7
Source File: SchemaManagerLoadTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadDisabled() throws Exception
{
    SchemaManager schemaManager = new DefaultSchemaManager();

    assertTrue( schemaManager.loadDisabled( "nis" ) );

    assertTrue( schemaManager.getErrors().isEmpty() );

    AttributeType at = schemaManager.getAttributeType( "uidNumber" );
    // if nis schema was loaded then the at will not be null
    assertNotNull( at );
}
 
Example 8
Source File: ApacheDSUtil.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
public static String generateRandomOID(SchemaManager schemaManager) {
	String base ="1.2.840.113556.1.4.";
	int num = (int) (Math.random() * 5000);
	
	StringBuffer b = new StringBuffer(base);
	b.append(num);
	
	if (schemaManager.getAttributeType(b.toString()) == null ) {
		return b.toString();
	} else {
		return generateRandomOID(schemaManager);
	}
}
 
Example 9
Source File: ApacheDSUtil.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
public static String generateRandomOID(SchemaManager schemaManager) {
	String base ="1.2.840.113556.1.4.";
	int num = (int) (Math.random() * 5000);
	
	StringBuffer b = new StringBuffer(base);
	b.append(num);
	
	if (schemaManager.getAttributeType(b.toString()) == null ) {
		return b.toString();
	} else {
		return generateRandomOID(schemaManager);
	}
}
 
Example 10
Source File: LdifAttributesReader.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Parse an AttributeType/AttributeValue
 *
 * @param schemaManager The SchemaManager
 * @param entry The entry where to store the value
 * @param line The line to parse
 * @param lowerLine The same line, lowercased
 * @throws LdapLdifException If anything goes wrong
 */
private void parseEntryAttribute( SchemaManager schemaManager, Entry entry, String line, String lowerLine )
    throws LdapLdifException
{
    int colonIndex = line.indexOf( ':' );

    String attributeName = lowerLine.substring( 0, colonIndex );
    AttributeType attributeType = null;

    // We should *not* have a Dn twice
    if ( "dn".equals( attributeName ) )
    {
        LOG.error( I18n.err( I18n.ERR_13400_ENTRY_WITH_TWO_DNS ) );
        throw new LdapLdifException( I18n.err( I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS ) );
    }

    if ( schemaManager != null )
    {
        attributeType = schemaManager.getAttributeType( attributeName );

        if ( attributeType == null )
        {
            String msg = I18n.err( I18n.ERR_13475_UNKNOWN_ATTRIBUTETYPE,  attributeName );
            LOG.error( msg );
            throw new LdapLdifException( msg );
        }
    }

    Object attributeValue = parseValue( attributeName, line, colonIndex );

    // Update the entry
    Attribute attribute;

    if ( schemaManager == null )
    {
        attribute = entry.get( attributeName );
    }
    else
    {
        attribute = entry.get( attributeType );
    }

    if ( attribute == null )
    {
        if ( schemaManager == null )
        {
            if ( attributeValue instanceof String )
            {
                entry.put( attributeName, ( String ) attributeValue );
            }
            else
            {
                entry.put( attributeName, ( byte[] ) attributeValue );
            }
        }
        else
        {
            try
            {
                if ( attributeValue instanceof String )
                {
                    entry.put( attributeName, attributeType, ( String ) attributeValue );
                }
                else
                {
                    entry.put( attributeName, attributeType, ( byte[] ) attributeValue );
                }
            }
            catch ( LdapException le )
            {
                throw new LdapLdifException( I18n.err( I18n.ERR_13460_BAD_ATTRIBUTE ), le );
            }
        }
    }
    else
    {
        try
        {
            if ( attributeValue instanceof String )
            {
                attribute.add( ( String ) attributeValue );
            }
            else
            {
                attribute.add( ( byte[] ) attributeValue );
            }
        }
        catch ( LdapInvalidAttributeValueException liave )
        {
            throw new LdapLdifException( liave.getMessage(), liave );
        }
    }
}