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

The following examples show how to use org.apache.directory.api.ldap.model.entry.Value#isHumanReadable() . 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: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean compare( Dn dn, String attributeName, Value value ) throws LdapException
{
    CompareRequest compareRequest = new CompareRequestImpl();
    compareRequest.setName( dn );
    compareRequest.setAttributeId( attributeName );

    if ( value.isHumanReadable() )
    {
        compareRequest.setAssertionValue( value.getString() );
    }
    else
    {
        compareRequest.setAssertionValue( value.getBytes() );
    }

    CompareResponse compareResponse = compare( compareRequest );

    return processResponse( compareResponse );
}
 
Example 2
Source File: SearchResultEntryFactory.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Encode the values recursively
 *
 * <pre>
 * 0x04 LL attributeValue
 * ...
 * 0x04 LL attributeValue
 * </pre>
 *
 * @param buffer The buffer where to put the PDU
 * @param values The iterator on the values
 */
private void encodeValues( Asn1Buffer buffer, Iterator<Value> values )
{
    if ( values.hasNext() )
    {
        Value value = values.next();

        encodeValues( buffer, values );

        // The value
        if ( value.isHumanReadable() )
        {
            BerValue.encodeOctetString( buffer, value.getString() );
        }
        else
        {
            BerValue.encodeOctetString( buffer, value.getBytes() );
        }
    }
}
 
Example 3
Source File: ModifyRequestFactory.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Encode the values, recursively
 * <pre>
 * 0x04 LL attributeValue
 * ...
 * 0x04 LL attributeValue
 * </pre>
 *
 * @param buffer The buffer where to put the PDU
 * @param values the values to encode
 */
private void encodeValues( Asn1Buffer buffer, Iterator<Value> values )
{
    if ( values.hasNext() )
    {
        Value value = values.next();

        // Recurse on the values
        encodeValues( buffer, values );

        // The value
        if ( value.isHumanReadable() )
        {
            BerValue.encodeOctetString( buffer, value.getString() );
        }
        else
        {
            BerValue.encodeOctetString( buffer, value.getBytes() );
        }
    }
}
 
Example 4
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();
}
 
Example 5
Source File: AttributeClassLoader.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Class<?> findClass( String name ) throws ClassNotFoundException
{
    byte[] classBytes;

    Value value = attribute.get();

    if ( value.isHumanReadable() )
    {
        throw new ClassNotFoundException( I18n.err( I18n.ERR_16008_AT_ACCESS_FAILURE ) );
    }

    classBytes = value.getBytes();

    // May be we are dealing with a JAR ?
    try 
    {
        Map<String, Class<?>> classes = loadClasses( classBytes );
        
        if ( classes == null )
        {
            // May be a simple class ?
            return defineClass( name, classBytes, 0, classBytes.length );
        }
        
        for ( Map.Entry<String, Class<?>> entry : classes.entrySet() )
        {
            if ( entry.getKey().contains( name ) )
            {
                return entry.getValue();
            }
        }
    }
    catch ( IOException ioe )
    {
        // Ok, may be a pure class
        return defineClass( name, classBytes, 0, classBytes.length );
    }
    
    return null;
}
 
Example 6
Source File: MyVDInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Handles the escaping of special characters in LDAP search filter assertion values using the
 * &lt;valueencoding&gt; rule as described in
 * <a href="http://www.ietf.org/rfc/rfc4515.txt">RFC 4515</a>. Needed so that
 * {@link ExprNode#printToBuffer(StringBuffer)} results in a valid filter string that can be parsed
 * again (as a way of cloning filters).
 *
 * @param value Right hand side of "attrId=value" assertion occurring in an LDAP search filter.
 * @return Escaped version of <code>value</code>
 */
protected Value<?> escapeFilterValue( Value<?> value )
{
    if ( value.isNull() )
    {
        return value;
    }

    StringBuilder sb = null;
    String val;

    if ( !value.isHumanReadable() )
    {
        sb = new StringBuilder( ( ( BinaryValue ) value ).getReference().length * 3 );

        for ( byte b : ( ( BinaryValue ) value ).getReference() )
        {
            if ( ( b < 0x7F ) && ( b >= 0 ) )
            {
                switch ( b )
                {
                    case '*':
                        sb.append( "\\2A" );
                        break;

                    case '(':
                        sb.append( "\\28" );
                        break;

                    case ')':
                        sb.append( "\\29" );
                        break;

                    case '\\':
                        sb.append( "\\5C" );
                        break;

                    case '\0':
                        sb.append( "\\00" );
                        break;

                    default:
                        sb.append( ( char ) b );
                }
            }
            else
            {
                sb.append( '\\' );
                String digit = Integer.toHexString( b & 0x00FF );

                if ( digit.length() == 1 )
                {
                    sb.append( '0' );
                }

                sb.append( digit.toUpperCase() );
            }
        }

        return new StringValue( sb.toString() );
    }

    val = ( ( StringValue ) value ).getString();
    String encodedVal = FilterEncoder.encodeFilterValue( val );
    if ( val.equals( encodedVal ) )
    {
        return value;
    }
    else
    {
        return new StringValue( encodedVal );
    }
}
 
Example 7
Source File: MyVDInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Handles the escaping of special characters in LDAP search filter assertion values using the
 * &lt;valueencoding&gt; rule as described in
 * <a href="http://www.ietf.org/rfc/rfc4515.txt">RFC 4515</a>. Needed so that
 * {@link ExprNode#printToBuffer(StringBuffer)} results in a valid filter string that can be parsed
 * again (as a way of cloning filters).
 *
 * @param value Right hand side of "attrId=value" assertion occurring in an LDAP search filter.
 * @return Escaped version of <code>value</code>
 */
protected Value<?> escapeFilterValue( Value<?> value )
{
    if ( value.isNull() )
    {
        return value;
    }

    StringBuilder sb = null;
    String val;

    if ( !value.isHumanReadable() )
    {
        sb = new StringBuilder( ( ( BinaryValue ) value ).getReference().length * 3 );

        for ( byte b : ( ( BinaryValue ) value ).getReference() )
        {
            if ( ( b < 0x7F ) && ( b >= 0 ) )
            {
                switch ( b )
                {
                    case '*':
                        sb.append( "\\2A" );
                        break;

                    case '(':
                        sb.append( "\\28" );
                        break;

                    case ')':
                        sb.append( "\\29" );
                        break;

                    case '\\':
                        sb.append( "\\5C" );
                        break;

                    case '\0':
                        sb.append( "\\00" );
                        break;

                    default:
                        sb.append( ( char ) b );
                }
            }
            else
            {
                sb.append( '\\' );
                String digit = Integer.toHexString( b & 0x00FF );

                if ( digit.length() == 1 )
                {
                    sb.append( '0' );
                }

                sb.append( digit.toUpperCase() );
            }
        }

        return new StringValue( sb.toString() );
    }

    val = ( ( StringValue ) value ).getString();
    String encodedVal = FilterEncoder.encodeFilterValue( val );
    if ( val.equals( encodedVal ) )
    {
        return value;
    }
    else
    {
        return new StringValue( encodedVal );
    }
}