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

The following examples show how to use org.apache.directory.api.ldap.model.entry.Value#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: DefaultSchemaLoader.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
private void loadComparators( Attribute comparators ) throws LdapException
{
    if ( comparators == null )
    {
        return;
    }

    for ( Value value : comparators )
    {
        String desc = value.getString();

        try
        {
            LdapComparatorDescription comparator = C_DESCR_SCHEMA_PARSER.parse( desc );

            updateSchemas( comparator );
        }
        catch ( ParseException pe )
        {
            throw new LdapException( pe );
        }
    }
}
 
Example 2
Source File: DefaultSchemaLoader.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
private void loadDitStructureRules( Attribute ditStructureRules ) throws LdapException
{
    if ( ditStructureRules == null )
    {
        return;
    }

    for ( Value value : ditStructureRules )
    {
        String desc = value.getString();

        try
        {
            DitStructureRule ditStructureRule = DSR_DESCR_SCHEMA_PARSER.parse( desc );

            updateSchemas( ditStructureRule );
        }
        catch ( ParseException pe )
        {
            throw new LdapException( pe );
        }
    }
}
 
Example 3
Source File: DefaultSchemaLoader.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
private void loadLdapSyntaxes( Attribute ldapSyntaxes ) throws LdapException
{
    if ( ldapSyntaxes == null )
    {
        return;
    }

    for ( Value value : ldapSyntaxes )
    {
        String desc = value.getString();

        try
        {
            LdapSyntax ldapSyntax = LS_DESCR_SCHEMA_PARSER.parse( desc );

            updateSchemas( ldapSyntax );
        }
        catch ( ParseException pe )
        {
            throw new LdapException( pe );
        }
    }
}
 
Example 4
Source File: DefaultSchemaLoader.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
private void loadObjectClasses( Attribute objectClasses ) throws LdapException
{
    if ( objectClasses == null )
    {
        return;
    }

    for ( Value value : objectClasses )
    {
        String desc = value.getString();

        try
        {
            ObjectClass objectClass = OC_DESCR_SCHEMA_PARSER.parse( desc );

            updateSchemas( objectClass );
        }
        catch ( ParseException pe )
        {
            throw new LdapException( pe );
        }
    }
}
 
Example 5
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
private Set<String> getAllMust( Attribute objectClasses ) throws LdapException
{
    Set<String> must = new HashSet<String>();

    // Loop on all objectclasses
    for ( Value<?> value : objectClasses )
    {
        String ocName = value.getString();
        ObjectClass oc = schemaManager.lookupObjectClassRegistry( ocName );

        List<AttributeType> types = oc.getMustAttributeTypes();

        // For each objectClass, loop on all MUST attributeTypes, if any
        if ( ( types != null ) && ( types.size() > 0 ) )
        {
            for ( AttributeType type : types )
            {
                must.add( type.getOid() );
            }
        }
    }

    return must;
}
 
Example 6
Source File: DefaultSchemaLoader.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
private void loadNameForms( Attribute nameForms ) throws LdapException
{
    if ( nameForms == null )
    {
        return;
    }

    for ( Value value : nameForms )
    {
        String desc = value.getString();

        try
        {
            NameForm nameForm = NF_DESCR_SCHEMA_PARSER.parse( desc );

            updateSchemas( nameForm );
        }
        catch ( ParseException pe )
        {
            throw new LdapException( pe );
        }
    }
}
 
Example 7
Source File: Ava.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Construct an Ava. The type and value are normalized :
 * <ul>
 *   <li> the type is trimmed and lowercased </li>
 *   <li> the value is trimmed </li>
 * </ul>
 * <p>
 * Note that the upValue should <b>not</b> be null or empty, or resolved
 * to an empty string after having trimmed it.
 *
 * @param schemaManager The SchemaManager
 * @param upType The User Provided type
 * @param normType The normalized type
 * @param value The value
 * 
 * @throws LdapInvalidDnException If the given type or value are invalid
 */
// WARNING : The protection level is left unspecified intentionally.
// We need this method to be visible from the DnParser class, but not
// from outside this package.
/* Unspecified protection */Ava( SchemaManager schemaManager, String upType, String normType, Value value )
    throws LdapInvalidDnException
{
    StringBuilder sb = new StringBuilder();

    this.upType = upType;
    this.normType = normType;
    this.value = value;
    
    sb.append( upType );
    sb.append( '=' );
    
    if ( ( value != null ) && ( value.getString() != null ) )
    {
        sb.append( value.getString() );
    }
    
    upName = sb.toString();

    if ( schemaManager != null )
    {
        apply( schemaManager );
    }

    hashCode();
}
 
Example 8
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a addModification applied to an entry with the same attribute
 * but with another value 
 */
@Test
public void testApplyAddModificationToEntryWithValues() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "apache" );
    assertEquals( 1, entry.size() );

    Attribute attr = new DefaultAttribute( "cn", "test" );
    Modification modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );

    Attribute attribute = entry.get( "cn" );

    assertTrue( attribute.size() != 0 );

    Set<String> expectedValues = new HashSet<String>();
    expectedValues.add( "apache" );
    expectedValues.add( "test" );

    for ( Value value : attribute )
    {
        String valueStr = value.getString();

        assertTrue( expectedValues.contains( valueStr ) );

        expectedValues.remove( valueStr );
    }

    assertEquals( 0, expectedValues.size() );
}
 
Example 9
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a addModification applied to an entry with the same attribute
 * and the same value 
 */
@Test
public void testApplyAddModificationToEntryWithSameValue() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "test", "apache" );
    assertEquals( 1, entry.size() );

    Attribute attr = new DefaultAttribute( "cn", "test" );
    Modification modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );

    Attribute cnAttr = entry.get( "cn" );

    assertTrue( cnAttr.size() != 0 );

    Set<String> expectedValues = new HashSet<String>();
    expectedValues.add( "apache" );
    expectedValues.add( "test" );

    for ( Value value : cnAttr )
    {
        String valueStr = value.getString();

        assertTrue( expectedValues.contains( valueStr ) );

        expectedValues.remove( valueStr );
    }

    assertEquals( 0, expectedValues.size() );
}
 
Example 10
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the replacement by modification of an attribute in an empty entry.
 * 
 * As we are replacing a non existing attribute, it should not change the entry.
 *
 * @throws LdapException
 */
@Test
public void testApplyModifyAttributeModification() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "test" );
    entry.put( "ou", "apache", "acme corp" );

    Attribute newOu = new DefaultAttribute( "ou", "Big Company", "directory" );

    Modification modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, newOu );

    AttributeUtils.applyModification( entry, modification );

    assertEquals( 2, entry.size() );

    assertNotNull( entry.get( "cn" ) );
    assertNotNull( entry.get( "ou" ) );

    Attribute modifiedAttr = entry.get( "ou" );

    assertTrue( modifiedAttr.size() != 0 );

    Set<String> expectedValues = new HashSet<String>();
    expectedValues.add( "Big Company" );
    expectedValues.add( "directory" );

    for ( Value value : modifiedAttr )
    {
        String valueStr = value.getString();

        assertTrue( expectedValues.contains( valueStr ) );

        expectedValues.remove( valueStr );
    }

    assertEquals( 0, expectedValues.size() );
}
 
Example 11
Source File: DefaultOperationManager.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
private LdapReferralException buildReferralException( Entry parentEntry, Dn childDn ) throws LdapException
{
    // Get the Ref attributeType
    Attribute refs = parentEntry.get( SchemaConstants.REF_AT );

    List<String> urls = new ArrayList<String>();

    try
    {
        // manage each Referral, building the correct URL for each of them
        for ( Value<?> url : refs )
        {
            // we have to replace the parent by the referral
            LdapUrl ldapUrl = new LdapUrl( url.getString() );

            // We have a problem with the Dn : we can't use the UpName,
            // as we may have some spaces around the ',' and '+'.
            // So we have to take the Rdn one by one, and create a
            // new Dn with the type and value UP form

            Dn urlDn = ldapUrl.getDn().add( childDn );

            ldapUrl.setDn( urlDn );
            urls.add( ldapUrl.toString() );
        }
    }
    catch ( LdapURLEncodingException luee )
    {
        throw new LdapOperationErrorException( luee.getMessage(), luee );
    }

    // Return with an exception
    LdapReferralException lre = new LdapReferralException( urls );
    lre.setRemainingDn( childDn );
    lre.setResolvedDn( parentEntry.getDn() );
    lre.setResolvedObject( parentEntry );

    return lre;
}
 
Example 12
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
private boolean getObjectClasses( Attribute objectClasses, List<ObjectClass> result ) throws LdapException
{
    Set<String> ocSeen = new HashSet<String>();

    // We must select all the ObjectClasses, except 'top',
    // but including all the inherited ObjectClasses
    boolean hasExtensibleObject = false;

    for ( Value<?> objectClass : objectClasses )
    {
        String objectClassName = objectClass.getString();

        if ( SchemaConstants.TOP_OC.equals( objectClassName ) )
        {
            continue;
        }

        if ( SchemaConstants.EXTENSIBLE_OBJECT_OC.equalsIgnoreCase( objectClassName ) )
        {
            hasExtensibleObject = true;
        }

        ObjectClass oc = schemaManager.lookupObjectClassRegistry( objectClassName );

        // Add all unseen objectClasses to the list, except 'top'
        if ( !ocSeen.contains( oc.getOid() ) )
        {
            ocSeen.add( oc.getOid() );
            result.add( oc );
        }

        // Find all current OC parents
        getSuperiors( oc, ocSeen, result );
    }

    return hasExtensibleObject;
}
 
Example 13
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
private Set<String> getAllAllowed( Attribute objectClasses, Set<String> must ) throws LdapException
{
    Set<String> allowed = new HashSet<String>( must );

    // Add the 'ObjectClass' attribute ID
    allowed.add( SchemaConstants.OBJECT_CLASS_AT_OID );

    // Loop on all objectclasses
    for ( Value<?> objectClass : objectClasses )
    {
        String ocName = objectClass.getString();
        ObjectClass oc = schemaManager.lookupObjectClassRegistry( ocName );

        List<AttributeType> types = oc.getMayAttributeTypes();

        // For each objectClass, loop on all MAY attributeTypes, if any
        if ( ( types != null ) && ( types.size() > 0 ) )
        {
            for ( AttributeType type : types )
            {
                String oid = type.getOid();

                allowed.add( oid );
            }
        }
    }

    return allowed;
}
 
Example 14
Source File: LdifAnonymizerTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
@Test
public void testLdifAnonymizerChangeType() throws Exception
{
    String ldif =
        "dn: cn=test,ou=Groups,o=acme,dc=com\n" +
        "changetype: modify\n" +
        "replace: member\n" +
        "member::Y249YWNtZTEuY29tLG91PVNlcnZlcnMsbz1hY21lLGRjPWNvbQ==\n" +                  // cn=acme1.com,ou=Servers,o=acme,dc=com
        "member::dWlkPWpvaG4uZG9lQGFjbWUuY29tLG91PVBlb3BsZSxvPWFjbWUsZGM9Y29t\n" +          // [email protected],ou=People,o=acme,dc=com
        "member::dWlkPWphY2suZG9lQGFjbWUuY29tLG91PVBlb3BsZSxvPWFjbWUsZGM9Y29t\n" +          // [email protected],ou=People,o=acme,dc=com
        "member::dWlkPWppbS5nb256YWxlc0BhY21lLmNvbSxvdT1QZW9wbGUsbz1hY21lLGRjPWNvbQ==\n" +  // [email protected],ou=People,o=acme,dc=com
        "-";
    
    LdifAnonymizer anonymizer = new LdifAnonymizer( schemaManager );
    anonymizer.addNamingContext( "o=acme,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( "member", attribute.getUpId() );
    assertEquals( 4, attribute.size() );
    
    Set<String> values = new HashSet<String>();
    values.add( "cn=AAAAAAAAA,ou=AAAAAAA,o=acme,dc=com" );
    values.add( "uid=AAAAAAAAAAAAAAAAA,ou=AAAAAB,o=acme,dc=com" );
    values.add( "uid=AAAAAAAAAAAAAAAAB,ou=AAAAAB,o=acme,dc=com" );
    values.add( "uid=AAAAAAAAAAAAAAAAAAAAA,ou=AAAAAB,o=acme,dc=com" );
    
    for ( Value value : attribute )
    {
        String str = value.getString();
        
        // We can only test the length and teh fact teh values are not equal (as the vale has been anonymized)
        assertTrue( values.contains( str ) );
        assertTrue( str.endsWith( ",o=acme,dc=com" ) );
    }
}
 
Example 15
Source File: ModifyRequestDsml.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Element toDsml( Element root )
{
    Element element = super.toDsml( root );

    ModifyRequest request = getDecorated();

    // Dn
    if ( request.getName() != null )
    {
        element.addAttribute( "dn", request.getName().getName() );
    }

    // Modifications
    Collection<Modification> modifications = request.getModifications();

    for ( Modification modification : modifications )
    {
        Element modElement = element.addElement( "modification" );

        if ( modification.getAttribute() != null )
        {
            modElement.addAttribute( "name", modification.getAttribute().getId() );

            for ( Value value : modification.getAttribute() )
            {
                if ( value.getString() != null )
                {
                    if ( ParserUtils.needsBase64Encoding( value.getString() ) )
                    {
                        Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
                        Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
                        element.getDocument().getRootElement().add( xsdNamespace );
                        element.getDocument().getRootElement().add( xsiNamespace );

                        Element valueElement = modElement.addElement( "value" ).addText(
                            ParserUtils.base64Encode( value.getString() ) );
                        valueElement.addAttribute( new QName( "type", xsiNamespace ), "xsd:"
                            + ParserUtils.BASE64BINARY );
                    }
                    else
                    {
                        modElement.addElement( "value" ).setText( value.getString() );
                    }
                }
            }
        }

        ModificationOperation operation = modification.getOperation();

        if ( operation == ModificationOperation.ADD_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "add" );
        }
        else if ( operation == ModificationOperation.REPLACE_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "replace" );
        }
        else if ( operation == ModificationOperation.REMOVE_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "delete" );
        }
        else if ( operation == ModificationOperation.INCREMENT_ATTRIBUTE )
        {
            modElement.addAttribute( "operation", "increment" );
        }
    }

    return element;
}
 
Example 16
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Given the objectClasses for an entry, this method adds missing ancestors
 * in the hierarchy except for top which it removes.  This is used for this
 * solution to DIREVE-276.  More information about this solution can be found
 * <a href="http://docs.safehaus.org:8080/x/kBE">here</a>.
 *
 * @param objectClassAttr the objectClass attribute to modify
 * @throws Exception if there are problems
 */
private void alterObjectClasses( Attribute objectClassAttr ) throws LdapException
{
    Set<String> objectClasses = new HashSet<String>();
    Set<String> objectClassesUP = new HashSet<String>();

    // Init the objectClass list with 'top'
    objectClasses.add( SchemaConstants.TOP_OC );
    objectClassesUP.add( SchemaConstants.TOP_OC );

    // Construct the new list of ObjectClasses
    for ( Value<?> ocValue : objectClassAttr )
    {
        String ocName = ocValue.getString();

        if ( !ocName.equalsIgnoreCase( SchemaConstants.TOP_OC ) )
        {
            String ocLowerName = Strings.toLowerCase( ocName );

            ObjectClass objectClass = schemaManager.lookupObjectClassRegistry( ocLowerName );

            if ( !objectClasses.contains( ocLowerName ) )
            {
                objectClasses.add( ocLowerName );
                objectClassesUP.add( ocName );
            }

            List<ObjectClass> ocSuperiors = superiors.get( objectClass.getOid() );

            if ( ocSuperiors != null )
            {
                for ( ObjectClass oc : ocSuperiors )
                {
                    if ( !objectClasses.contains( Strings.toLowerCase( oc.getName() ) ) )
                    {
                        objectClasses.add( oc.getName() );
                        objectClassesUP.add( oc.getName() );
                    }
                }
            }
        }
    }

    // Now, reset the ObjectClass attribute and put the new list into it
    objectClassAttr.clear();

    for ( String attribute : objectClassesUP )
    {
        objectClassAttr.add( attribute );
    }
}
 
Example 17
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Given the objectClasses for an entry, this method adds missing ancestors
 * in the hierarchy except for top which it removes.  This is used for this
 * solution to DIREVE-276.  More information about this solution can be found
 * <a href="http://docs.safehaus.org:8080/x/kBE">here</a>.
 *
 * @param objectClassAttr the objectClass attribute to modify
 * @throws Exception if there are problems
 */
private void alterObjectClasses( Attribute objectClassAttr ) throws LdapException
{
    Set<String> objectClasses = new HashSet<String>();
    Set<String> objectClassesUP = new HashSet<String>();

    // Init the objectClass list with 'top'
    objectClasses.add( SchemaConstants.TOP_OC );
    objectClassesUP.add( SchemaConstants.TOP_OC );

    // Construct the new list of ObjectClasses
    for ( Value<?> ocValue : objectClassAttr )
    {
        String ocName = ocValue.getString();

        if ( !ocName.equalsIgnoreCase( SchemaConstants.TOP_OC ) )
        {
            String ocLowerName = Strings.toLowerCase( ocName );

            ObjectClass objectClass = schemaManager.lookupObjectClassRegistry( ocLowerName );

            if ( !objectClasses.contains( ocLowerName ) )
            {
                objectClasses.add( ocLowerName );
                objectClassesUP.add( ocName );
            }

            List<ObjectClass> ocSuperiors = superiors.get( objectClass.getOid() );

            if ( ocSuperiors != null )
            {
                for ( ObjectClass oc : ocSuperiors )
                {
                    if ( !objectClasses.contains( Strings.toLowerCase( oc.getName() ) ) )
                    {
                        objectClasses.add( oc.getName() );
                        objectClassesUP.add( oc.getName() );
                    }
                }
            }
        }
    }

    // Now, reset the ObjectClass attribute and put the new list into it
    objectClassAttr.clear();

    for ( String attribute : objectClassesUP )
    {
        objectClassAttr.add( attribute );
    }
}
 
Example 18
Source File: DefaultOperationManager.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
private LdapReferralException buildReferralExceptionForSearch( Entry parentEntry, Dn childDn, SearchScope scope )
    throws LdapException
{
    // Get the Ref attributeType
    Attribute refs = parentEntry.get( SchemaConstants.REF_AT );

    List<String> urls = new ArrayList<String>();

    // manage each Referral, building the correct URL for each of them
    for ( Value<?> url : refs )
    {
        // we have to replace the parent by the referral
        try
        {
            LdapUrl ldapUrl = new LdapUrl( url.getString() );

            StringBuilder urlString = new StringBuilder();

            if ( ( ldapUrl.getDn() == null ) || ( ldapUrl.getDn() == Dn.ROOT_DSE ) )
            {
                ldapUrl.setDn( parentEntry.getDn() );
            }
            else
            {
                // We have a problem with the Dn : we can't use the UpName,
                // as we may have some spaces around the ',' and '+'.
                // So we have to take the Rdn one by one, and create a
                // new Dn with the type and value UP form

                Dn urlDn = ldapUrl.getDn().add( childDn );

                ldapUrl.setDn( urlDn );
            }

            urlString.append( ldapUrl.toString() ).append( "??" );

            switch ( scope )
            {
                case OBJECT:
                    urlString.append( "base" );
                    break;

                case SUBTREE:
                    urlString.append( "sub" );
                    break;

                case ONELEVEL:
                    urlString.append( "one" );
                    break;
            }

            urls.add( urlString.toString() );
        }
        catch ( LdapURLEncodingException luee )
        {
            // The URL is not correct, returns it as is
            urls.add( url.getString() );
        }
    }

    // Return with an exception
    LdapReferralException lre = new LdapReferralException( urls );
    lre.setRemainingDn( childDn );
    lre.setResolvedDn( parentEntry.getDn() );
    lre.setResolvedObject( parentEntry );

    return lre;
}
 
Example 19
Source File: FilterParser.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Parse a substring
 * 
 * @param schemaManager The {@link SchemaManager}
 * @param attribute The filter's attribute
 * @param initial The filter's initial part
 * @param filterBytes The filter bytes to parse
 * @param pos The position in the filter bytes
 * @return the created {@link ExprNode}
 * @throws ParseException If the Node can't be parsed
 * @throws LdapException If we met an error while parsing a filter element
 */
private static ExprNode parseSubstring( SchemaManager schemaManager, String attribute, Value initial,
    byte[] filterBytes, Position pos ) throws ParseException, LdapException
{
    SubstringNode node;

    if ( schemaManager != null )
    {
        AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( attribute );

        if ( attributeType != null )
        {
            node = new SubstringNode( schemaManager.lookupAttributeTypeRegistry( attribute ) );
        }
        else
        {
            return null;
        }
    }
    else
    {
        node = new SubstringNode( attribute );
    }

    if ( ( initial != null ) && !initial.isNull() )
    {
        // We have a substring starting with a value : val*...
        // Set the initial value. It must be a String
        String initialStr = initial.getString();
        node.setInitial( initialStr );
    }

    if ( Strings.isCharASCII( filterBytes, pos.start, ')' ) )
    {
        // No any or final, we are done
        return node;
    }

    //
    while ( true )
    {
        Value assertionValue = parseAssertionValue( schemaManager, attribute, filterBytes, pos );

        // Is there anything else but a ')' after the value ?
        if ( Strings.isCharASCII( filterBytes, pos.start, ')' ) )
        {
            // Nope : as we have had [initial] '*' (any '*' ) *,
            // this is the final
            if ( !assertionValue.isNull() )
            {
                String finalStr = assertionValue.getString();
                node.setFinal( finalStr );
            }

            return node;
        }
        else if ( Strings.isCharASCII( filterBytes, pos.start, '*' ) )
        {
            // We have a '*' : it's an any
            // If the value is empty, that means we have more than
            // one consecutive '*' : do nothing in this case.
            if ( !assertionValue.isNull() )
            {
                String anyStr = assertionValue.getString();
                node.addAny( anyStr );
            }

            pos.start++;

            // Skip any following '*'
            while ( Strings.isCharASCII( filterBytes, pos.start, '*' ) )
            {
                pos.start++;
            }

            // that may have been the closing '*'
            if ( Strings.isCharASCII( filterBytes, pos.start, ')' ) )
            {
                return node;
            }

        }
        else
        {
            // This is an error
            throw new ParseException( I18n.err( I18n.ERR_13309_BAD_SUBSTRING ), pos.start );
        }
    }
}
 
Example 20
Source File: LdifUtils.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Converts an EntryAttribute as LDIF
 * 
 * @param attr the EntryAttribute to convert
 * @param length the expected line length
 * @return the corresponding LDIF code as a String
 */
public static String convertToLdif( Attribute attr, int length )
{
    StringBuilder sb = new StringBuilder();
    
    if ( attr.size() == 0 )
    {
        // Special case : we don't have any value
        return "";
    }

    for ( Value value : attr )
    {
        StringBuilder lineBuffer = new StringBuilder();

        lineBuffer.append( attr.getUpId() );

        // First, deal with null value (which is valid)
        if ( value.isNull() )
        {
            lineBuffer.append( ':' );
        }
        else if ( value.isHumanReadable() )
        {
            // It's a String but, we have to check if encoding isn't required
            String str = value.getString();

            if ( !LdifUtils.isLDIFSafe( str ) )
            {
                lineBuffer.append( ":: " ).append( encodeBase64( str ) );
            }
            else
            {
                lineBuffer.append( ':' );

                if ( str != null )
                {
                    lineBuffer.append( ' ' ).append( str );
                }
            }
        }
        else
        {
            // It is binary, so we have to encode it using Base64 before adding it
            char[] encoded = Base64.encode( value.getBytes() );

            lineBuffer.append( ":: " + new String( encoded ) );
        }

        lineBuffer.append( '\n' );
        sb.append( stripLineToNChars( lineBuffer.toString(), length ) );
    }

    return sb.toString();
}