Java Code Examples for org.apache.directory.api.util.Strings#utf8ToString()

The following examples show how to use org.apache.directory.api.util.Strings#utf8ToString() . 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: StoreSearchRequestAttributeDesc.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void action( LdapMessageContainer<SearchRequest> container )
{
    SearchRequest searchRequest = container.getMessage();
    TLV tlv = container.getCurrentTLV();
    String attributeDescription = null;

    if ( tlv.getLength() != 0 )
    {
        attributeDescription = Strings.utf8ToString( tlv.getValue().getData() );

        // If the attributeDescription is empty, we won't add it
        if ( !Strings.isEmpty( attributeDescription.trim() ) )
        {
            searchRequest.addAttributes( attributeDescription );
        }
    }

    // We can have an END transition
    container.setGrammarEndAllowed( true );

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05159_DECODED_ATT_DESC, attributeDescription ) );
    }
}
 
Example 2
Source File: BitStringSyntaxChecker.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isValidSyntax( Object value )
{
    String strValue;

    if ( value == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
        }
        
        return false;
    }

    if ( value instanceof String )
    {
        strValue = ( String ) value;
    }
    else if ( value instanceof byte[] )
    {
        strValue = Strings.utf8ToString( ( byte[] ) value );
    }
    else
    {
        strValue = value.toString();
    }

    return isValid( strValue );
}
 
Example 3
Source File: DnParserTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringParser() throws LdapException
{
    String dn = Strings.utf8ToString( new byte[]
        { 'C', 'N', ' ', '=', ' ', 'E', 'm', 'm', 'a', 'n', 'u', 'e', 'l', ' ', ' ', 'L', ( byte ) 0xc3,
            ( byte ) 0xa9, 'c', 'h', 'a', 'r', 'n', 'y' } );

    Dn name = new Dn( dn );

    assertEquals( "CN = Emmanuel  L\u00e9charny", name.getName() );
    assertEquals( "CN=Emmanuel  L\u00e9charny", name.getEscaped() );
}
 
Example 4
Source File: DerefAliasSyntaxChecker.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isValidSyntax( Object value )
{
    String strValue;

    if ( value == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
        }
        
        return false;
    }

    if ( value instanceof String )
    {
        strValue = ( String ) value;
    }
    else if ( value instanceof byte[] )
    {
        strValue = Strings.utf8ToString( ( byte[] ) value );
    }
    else
    {
        strValue = value.toString();
    }

    strValue = Strings.trim( Strings.toLowerCaseAscii( strValue ) );

    return "never".equals( strValue ) || "finding".equals( strValue ) || "searching".equals( strValue ) || "always"
        .equals( strValue );
}
 
Example 5
Source File: Csn.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of SimpleCSN from the serialized data
 *
 * @param value The byte array which contains the serialized CSN
 */
Csn( byte[] value )
{
    csnStr = Strings.utf8ToString( value );
    Csn csn = new Csn( csnStr );
    timestamp = csn.timestamp;
    changeCount = csn.changeCount;
    replicaId = csn.replicaId;
    operationNumber = csn.operationNumber;
    bytes = Strings.getBytesUtf8( csnStr );
}
 
Example 6
Source File: SearchScopeSyntaxChecker.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isValidSyntax( Object value )
{
    String strValue;

    if ( value == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
        }
        
        return false;
    }

    if ( value instanceof String )
    {
        strValue = ( String ) value;
    }
    else if ( value instanceof byte[] )
    {
        strValue = Strings.utf8ToString( ( byte[] ) value );
    }
    else
    {
        strValue = value.toString();
    }

    strValue = Strings.trim( Strings.toLowerCaseAscii( strValue ) );

    return "base".equals( strValue ) || "one".equals( strValue ) || "sub".equals( strValue );
}
 
Example 7
Source File: SchemaAwareRdnTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * test a simple Rdn with pair char attribute value : l = \,\=\+\<\>\#\;\\\"\C3\A9"
 * 
 * @throws LdapException
 */
@Test
public void testRdnPairCharAttributeValue() throws LdapException
{
    String rdn = Strings.utf8ToString( new byte[]
        {
            'l',
            '=',
            '\\',
            ',',
            '\\',
            '=',
            '\\',
            '+',
            '\\',
            '<',
            '\\',
            '>',
            '#',
            '\\',
            ';',
            '\\',
            '\\',
            '\\',
            '"',
            '\\',
            'C',
            '3',
            '\\',
            'A',
            '9' } );
    assertEquals( "l=\\,=\\+\\<\\>#\\;\\\\\\\"\u00e9", new Rdn( schemaManager, rdn ).getEscaped() );
}
 
Example 8
Source File: InitDelRequest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<DeleteRequest> container ) throws DecoderException
{
    // Create the DeleteRequest LdapMessage instance and store it in the container
    DeleteRequest delRequest = new DeleteRequestImpl();
    delRequest.setMessageId( container.getMessageId() );
    container.setMessage( delRequest );

    // And store the Dn into it
    // Get the Value and store it in the DelRequest
    TLV tlv = container.getCurrentTLV();

    // We have to handle the special case of a 0 length matchedDN
    if ( tlv.getLength() == 0 )
    {
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException( I18n.err( I18n.ERR_05119_NULL_ENTRY ) );
    }
    else
    {
        byte[] dnBytes = tlv.getValue().getData();
        String dnStr = Strings.utf8ToString( dnBytes );

        try
        {
            Dn entry = new Dn( dnStr );
            delRequest.setName( entry );
        }
        catch ( LdapInvalidDnException ine )
        {
            String msg = I18n.err( I18n.ERR_05120_INVALID_DELETE_DN, dnStr, 
                Strings.dumpBytes( dnBytes ), ine.getLocalizedMessage() );
            LOG.error( msg );

            DeleteResponseImpl response = new DeleteResponseImpl( delRequest.getMessageId() );
            throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
                Dn.EMPTY_DN, ine );
        }
    }

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05124_DELETING_DN, delRequest.getName() ) );
    }

    // We can have an END transition
    container.setGrammarEndAllowed( true );
}
 
Example 9
Source File: StoreCompareRequestEntryName.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<CompareRequest> container ) throws DecoderException
{
    CompareRequest compareRequest = container.getMessage();

    // Get the Value and store it in the CompareRequest
    TLV tlv = container.getCurrentTLV();

    // We have to handle the special case of a 0 length matched Dn
    if ( tlv.getLength() == 0 )
    {
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException( I18n.err( I18n.ERR_05119_NULL_ENTRY ) );
    }
    else
    {
        byte[] dnBytes = tlv.getValue().getData();
        String dnStr = Strings.utf8ToString( dnBytes );

        try
        {
            Dn entry = new Dn( dnStr );
            compareRequest.setName( entry );
        }
        catch ( LdapInvalidDnException ine )
        {
            String msg = I18n.err( I18n.ERR_05113_INVALID_DN, dnStr, Strings.dumpBytes( dnBytes ) );
            LOG.error( I18n.err( I18n.ERR_05114_ERROR_MESSAGE, msg, ine.getMessage() ) );

            CompareResponseImpl response = new CompareResponseImpl( compareRequest.getMessageId() );
            throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
                Dn.EMPTY_DN, ine );
        }
    }

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05123_COMPARING_DN, compareRequest.getName() ) );
    }
}
 
Example 10
Source File: DitStructureRuleDescriptionSyntaxChecker.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isValidSyntax( Object value )
{
    String strValue;

    if ( value == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
        }
        
        return false;
    }

    if ( value instanceof String )
    {
        strValue = ( String ) value;
    }
    else if ( value instanceof byte[] )
    {
        strValue = Strings.utf8ToString( ( byte[] ) value );
    }
    else
    {
        strValue = value.toString();
    }

    try
    {
        schemaParser.parse( strValue );

        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
        }
        
        return true;
    }
    catch ( ParseException pe )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
        }
        
        return false;
    }
}
 
Example 11
Source File: StoreModifyDnRequestEntryName.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<ModifyDnRequest> container ) throws DecoderException
{
    ModifyDnRequest modifyDnRequest = container.getMessage();

    // Get the Value and store it in the modifyDNRequest
    TLV tlv = container.getCurrentTLV();

    // We have to handle the special case of a 0 length matched DN
    if ( tlv.getLength() == 0 )
    {
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException( I18n.err( I18n.ERR_05119_NULL_ENTRY ) );
    }
    else
    {
        byte[] dnBytes = tlv.getValue().getData();
        String dnStr = Strings.utf8ToString( dnBytes );

        try
        {
            Dn entry = new Dn( dnStr );
            modifyDnRequest.setName( entry );
        }
        catch ( LdapInvalidDnException ine )
        {
            String msg = I18n.err( I18n.ERR_05113_INVALID_DN, dnStr, Strings.dumpBytes( dnBytes ) );
            LOG.error( I18n.err( I18n.ERR_05114_ERROR_MESSAGE, msg, ine.getMessage() ) );

            ModifyDnResponseImpl response = new ModifyDnResponseImpl( modifyDnRequest.getMessageId() );
            throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
                Dn.EMPTY_DN, ine );
        }
    }

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05137_MODIFYING_DN, modifyDnRequest.getName() ) );
    }
}
 
Example 12
Source File: Value.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a schema aware binary Value with an initial value.
 *
 * @param attributeType the schema type associated with this Value
 * @param upValue the value to wrap
 * @throws LdapInvalidAttributeValueException If the added value is invalid accordingly
 * to the schema
 */
public Value( AttributeType attributeType, byte[] upValue ) throws LdapInvalidAttributeValueException
{
    init( attributeType );
    
    if ( upValue != null )
    {
        bytes = new byte[upValue.length];
        System.arraycopy( upValue, 0, bytes, 0, upValue.length );

        if ( isHR )
        {
            this.upValue = Strings.utf8ToString( upValue );
        }
    }
    else
    {
        bytes = null;
    }
    
    if ( ( attributeType != null ) && !attributeType.isRelaxed() )
    {
        // Check the value
        SyntaxChecker syntaxChecker = attributeType.getSyntax().getSyntaxChecker();

        if ( syntaxChecker != null )
        {
            if ( !syntaxChecker.isValidSyntax( bytes ) )
            {
                throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, 
                    I18n.err( I18n.ERR_13246_INVALID_VALUE_PER_SYNTAX ) );
            }
        }
        else
        {
            // We should always have a SyntaxChecker
            throw new IllegalArgumentException( I18n.err( I18n.ERR_13219_NULL_SYNTAX_CHECKER, normValue ) );
        }
    }

    hashCode();
}
 
Example 13
Source File: CountrySyntaxChecker.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isValidSyntax( Object value )
{
    String strValue;

    if ( value == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
        }
        
        return false;
    }

    if ( value instanceof String )
    {
        strValue = ( String ) value;
    }
    else if ( value instanceof byte[] )
    {
        strValue = Strings.utf8ToString( ( byte[] ) value );
    }
    else
    {
        strValue = value.toString();
    }

    if ( strValue.length() == 0 )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
        }
        
        return false;
    }

    boolean result = COUNTRIES.contains( Strings.toUpperCaseAscii( strValue ) );

    if ( LOG.isDebugEnabled() )
    {
        if ( result )
        {
            LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
        }
        else
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
        }
    }

    return result;
}
 
Example 14
Source File: StoredProcedureRequestImpl.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getProcedureSpecification()
{
    return Strings.utf8ToString( procedure );
}
 
Example 15
Source File: MailPreferenceSyntaxChecker.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isValidSyntax( Object value )
{
    String strValue;

    if ( value == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
        }
        
        return false;
    }

    if ( value instanceof String )
    {
        strValue = ( String ) value;
    }
    else if ( value instanceof byte[] )
    {
        strValue = Strings.utf8ToString( ( byte[] ) value );
    }
    else
    {
        strValue = value.toString();
    }

    if ( ( strValue.length() < 8 ) || ( strValue.length() > 18 ) )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
        }
        
        return false;
    }

    boolean result = ( "NO-LISTS".equals( strValue ) ) || ( "ANY-LIST".equals( strValue ) )
        || ( "PROFESSIONAL-LISTS".equals( strValue ) );

    if ( LOG.isDebugEnabled() )
    {
        if ( result )
        {
            LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
        }
        else
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
        }
    }

    return result;
}
 
Example 16
Source File: DirectoryStringSyntaxChecker.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isValidSyntax( Object value )
{
    String strValue;

    if ( value == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
        }
        
        return false;
    }

    if ( value instanceof String )
    {
        strValue = ( String ) value;
    }
    else if ( value instanceof byte[] )
    {
        strValue = Strings.utf8ToString( ( byte[] ) value );
    }
    else
    {
        strValue = value.toString();
    }

    // If the value was an invalid UTF8 string, then it's length
    // will be 0 as the StringTools.utf8ToString() call will
    // return an empty string
    if ( strValue.length() == 0 )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
        }
        
        return false;
    }

    // In any other case, we have to check that the
    // string does not contains the '0xFFFD' character
    for ( char c : strValue.toCharArray() )
    {
        if ( c == 0xFFFD )
        {
            if ( LOG.isDebugEnabled() )
            {
                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
            }
            
            return false;
        }
    }

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
    }
    
    return true;
}
 
Example 17
Source File: WhoAmIResponseImpl.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getAuthzIdString()
{
    return Strings.utf8ToString( authzId );
}
 
Example 18
Source File: BooleanSyntaxChecker.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isValidSyntax( Object value )
{
    String strValue;

    if ( value == null )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
        }
        
        return false;
    }

    if ( value instanceof String )
    {
        strValue = ( String ) value;
    }
    else if ( value instanceof byte[] )
    {
        strValue = Strings.utf8ToString( ( byte[] ) value );
    }
    else
    {
        strValue = value.toString();
    }

    if ( strValue.length() == 0 )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, strValue ) );
        }
        
        return false;
    }

    boolean valid = "TRUE".equalsIgnoreCase( strValue ) || "FALSE".equalsIgnoreCase( strValue );

    if ( LOG.isDebugEnabled() )
    {
        if ( valid )
        {
            LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, strValue ) );
        }
        else
        {
            LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, strValue ) );
        }
    }

    return valid;
}
 
Example 19
Source File: StoredProcedureParameter.java    From directory-ldap-api with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the type as a UTF8 String.
 *
 * @return The type as a UTF8 String.
 */
public String getTypeString()
{
    return Strings.utf8ToString( type );
}
 
Example 20
Source File: StoredProcedureParameter.java    From directory-ldap-api with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the value as a UTF8 String.
 *
 * @return The value as a UTF8 String.
 */
public String getValueString()
{
    return Strings.utf8ToString( value );
}