Java Code Examples for org.apache.directory.api.ldap.model.message.ResultCodeEnum#INVALID_DN_SYNTAX

The following examples show how to use org.apache.directory.api.ldap.model.message.ResultCodeEnum#INVALID_DN_SYNTAX . 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: ComplexDnParser.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Parses an Dn.
 * 
 * @param schemaManager The SchemaManager
 * @param name the string representation of the distinguished name
 * @param rdns the (empty) list where parsed RDNs are put to
 * @return The normalized Dn
 * 
 * @throws LdapInvalidDnException the invalid name exception
 */
/* No protection*/String parseDn( SchemaManager schemaManager, String name, List<Rdn> rdns ) throws LdapInvalidDnException
{
    AntlrDnParser dnParser = new AntlrDnParser( new AntlrDnLexer( new StringReader( name ) ) );

    try
    {
        return dnParser.relativeDistinguishedNames( schemaManager, rdns );
    }
    catch ( Exception e )
    {
        throw new LdapInvalidDnException( ResultCodeEnum.INVALID_DN_SYNTAX, e.getMessage(), e );
    }
}
 
Example 2
Source File: ComplexDnParser.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Parses an Rdn.
 * 
 * @param schemaManager The SchemaManager
 * @param name the string representation of the relative distinguished name
 * @param rdn the (empty) Rdn where parsed ATAVs are put into
 * 
 * @throws LdapInvalidDnException the invalid name exception
 */
/* No protection*/void parseRdn( SchemaManager schemaManager, String name, Rdn rdn ) throws LdapInvalidDnException
{
    AntlrDnParser dnParser = new AntlrDnParser( new AntlrDnLexer( new StringReader( name ) ) );

    try
    {
        dnParser.relativeDistinguishedName( schemaManager, rdn );
    }
    catch ( Exception e )
    {
        throw new LdapInvalidDnException( ResultCodeEnum.INVALID_DN_SYNTAX, e.getMessage(), e );
    }
}
 
Example 3
Source File: Ava.java    From directory-ldap-api with Apache License 2.0 4 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 attributeType The AttributeType for this value
 * @param upType The User Provided type
 * @param normType The normalized type
 * @param value The value
 * @param upName The User Provided name (may be escaped)
 * 
 * @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( AttributeType attributeType, String upType, String normType, Value value, String upName )
    throws LdapInvalidDnException
{
    this.attributeType = attributeType;
    String upTypeTrimmed = Strings.trim( upType );
    String normTypeTrimmed = Strings.trim( normType );

    if ( Strings.isEmpty( upTypeTrimmed ) )
    {
        if ( Strings.isEmpty( normTypeTrimmed ) )
        {
            String message = I18n.err( I18n.ERR_13600_TYPE_IS_NULL_OR_EMPTY );
            LOG.error( message );
            throw new LdapInvalidDnException( ResultCodeEnum.INVALID_DN_SYNTAX, message );
        }
        else
        {
            // In this case, we will use the normType instead
            this.normType = Strings.lowerCaseAscii( normTypeTrimmed );
            this.upType = normType;
        }
    }
    else if ( Strings.isEmpty( normTypeTrimmed ) )
    {
        // In this case, we will use the upType instead
        this.normType = Strings.lowerCaseAscii( upTypeTrimmed );
        this.upType = upType;
    }
    else
    {
        this.normType = Strings.lowerCaseAscii( normTypeTrimmed );
        this.upType = upType;
    }

    this.value = value;
    this.upName = upName;
    hashCode();
}
 
Example 4
Source File: Ava.java    From directory-ldap-api with Apache License 2.0 4 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 upType The User Provided type
 * @param upValue The User Provided value
 * 
 * @throws LdapInvalidDnException If the given type or value are invalid
 */
private void createAva( String upType, Value upValue ) throws LdapInvalidDnException
{
    String upTypeTrimmed = Strings.trim( upType );
    String normTypeTrimmed = Strings.trim( normType );

    if ( Strings.isEmpty( upTypeTrimmed ) )
    {
        if ( Strings.isEmpty( normTypeTrimmed ) )
        {
            String message = I18n.err( I18n.ERR_13600_TYPE_IS_NULL_OR_EMPTY );
            // Do NOT log the message here. The error may be handled and therefore the log message may polute the log files.
            // Let the caller log the exception if needed.
            throw new LdapInvalidDnException( ResultCodeEnum.INVALID_DN_SYNTAX, message );
        }
        else
        {
            // In this case, we will use the normType instead
            this.normType = Strings.lowerCaseAscii( normTypeTrimmed );
            this.upType = normType;
        }
    }
    else if ( Strings.isEmpty( normTypeTrimmed ) )
    {
        // In this case, we will use the upType instead
        this.normType = Strings.lowerCaseAscii( upTypeTrimmed );
        this.upType = upType;
    }
    else
    {
        this.normType = Strings.lowerCaseAscii( normTypeTrimmed );
        this.upType = upType;

    }

    value = upValue;

    upName = getEscaped();
    
    hashCode();
}
 
Example 5
Source File: Dn.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new instance of schema aware Dn, using varargs to declare the RDNs. Each
 * String is either a full Rdn, or a couple of AttributeType DI and a value.
 * If the String contains a '=' symbol, the the constructor will assume that
 * the String arg contains afull Rdn, otherwise, it will consider that the
 * following arg is the value.<br>
 * The created Dn is Schema aware.
 * <br><br>
 * An example of usage would be :
 * <pre>
 * String exampleName = "example";
 * String baseDn = "dc=apache,dc=org";
 *
 * Dn dn = new Dn( DefaultSchemaManager.INSTANCE,
 *     "cn=Test",
 *     "ou", exampleName,
 *     baseDn);
 * </pre>
 *
 * @param schemaManager the schema manager
 * @param upRdns The list of String composing the Dn
 * @throws LdapInvalidDnException If the resulting Dn is invalid
 */
public Dn( SchemaManager schemaManager, String... upRdns ) throws LdapInvalidDnException
{
    StringBuilder sbUpName = new StringBuilder();
    boolean valueExpected = false;
    boolean isFirst = true;
    this.schemaManager = schemaManager;

    for ( String upRdn : upRdns )
    {
        if ( Strings.isEmpty( upRdn ) )
        {
            continue;
        }

        if ( isFirst )
        {
            isFirst = false;
        }
        else if ( !valueExpected )
        {
            sbUpName.append( ',' );
        }

        if ( !valueExpected )
        {
            sbUpName.append( upRdn );

            if ( upRdn.indexOf( '=' ) == -1 )
            {
                valueExpected = true;
            }
        }
        else
        {
            sbUpName.append( "=" ).append( upRdn );

            valueExpected = false;
        }
    }

    if ( !isFirst && valueExpected )
    {
        throw new LdapInvalidDnException( ResultCodeEnum.INVALID_DN_SYNTAX, I18n.err( I18n.ERR_13611_VALUE_MISSING_ON_RDN ) );
    }

    // Stores the representations of a Dn : internal (as a string and as a
    // byte[]) and external.
    upName = sbUpName.toString();

    try
    {
        normName = parseInternal( schemaManager, upName, rdns );
    }
    catch ( LdapInvalidDnException e )
    {
        if ( schemaManager == null || !schemaManager.isRelaxed() )
        {
            throw e;
        }
        // Ignore invalid DN formats in relaxed mode.
        // This is needed to support unbelievably insane
        // DN formats such as <GUI=abcd...> format used by
        // Active Directory
    }
}
 
Example 6
Source File: Dn.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Get the descendant of a given DN, using the ancestr DN. Assuming that
 * a DN has two parts :<br>
 * DN = [descendant DN][ancestor DN]<br>
 * To get back the descendant from the full DN, you just pass the ancestor DN
 * as a parameter. Here is a working example :
 * <pre>
 * Dn dn = new Dn( "cn=test, dc=server, dc=directory, dc=apache, dc=org" );
 *
 * Dn descendant = dn.getDescendantOf( "dc=apache, dc=org" );
 *
 * // At this point, the descendant contains cn=test, dc=server, dc=directory"
 * </pre>
 *
 * @param ancestor The parent DN
 * @return The part of the DN that is the descendant
 * @throws LdapInvalidDnException If the Dn is invalid
 */
public Dn getDescendantOf( Dn ancestor ) throws LdapInvalidDnException
{
    if ( ( ancestor == null ) || ( ancestor.size() == 0 ) )
    {
        return this;
    }

    if ( rdns.isEmpty() )
    {
        return EMPTY_DN;
    }

    int length = ancestor.size();

    if ( length > rdns.size() )
    {
        String message = I18n.err( I18n.ERR_13612_POSITION_NOT_IN_RANGE, length, rdns.size() );
        LOG.error( message );
        throw new ArrayIndexOutOfBoundsException( message );
    }

    Dn newDn = new Dn( schemaManager );
    List<Rdn> rdnsAncestor = ancestor.getRdns();

    for ( int i = 0; i < ancestor.size(); i++ )
    {
        Rdn rdn = rdns.get( size() - 1 - i );
        Rdn rdnDescendant = rdnsAncestor.get( ancestor.size() - 1 - i );

        if ( !rdn.equals( rdnDescendant ) )
        {
            throw new LdapInvalidDnException( ResultCodeEnum.INVALID_DN_SYNTAX );
        }
    }

    for ( int i = 0; i < rdns.size() - length; i++ )
    {
        newDn.rdns.add( rdns.get( i ) );
    }

    newDn.toUpName();

    return newDn;
}
 
Example 7
Source File: Dn.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Get the ancestor of a given DN, using the descendant DN. Assuming that
 * a DN has two parts :<br>
 * DN = [descendant DN][ancestor DN]<br>
 * To get back the ancestor from the full DN, you just pass the descendant DN
 * as a parameter. Here is a working example :
 * <pre>
 * Dn dn = new Dn( "cn=test, dc=server, dc=directory, dc=apache, dc=org" );
 *
 * Dn ancestor = dn.getAncestorOf( new Dn( "cn=test, dc=server, dc=directory" ) );
 *
 * // At this point, the ancestor contains "dc=apache, dc=org"
 * </pre>
 *
 * @param descendant The child DN
 * @return The part of the DN that is the ancestor
 * @throws LdapInvalidDnException If the Dn is invalid
 */
public Dn getAncestorOf( Dn descendant ) throws LdapInvalidDnException
{
    if ( ( descendant == null ) || ( descendant.size() == 0 ) )
    {
        return this;
    }

    if ( rdns.isEmpty() )
    {
        return EMPTY_DN;
    }

    int length = descendant.size();

    if ( length > rdns.size() )
    {
        String message = I18n.err( I18n.ERR_13612_POSITION_NOT_IN_RANGE, length, rdns.size() );
        LOG.error( message );
        throw new ArrayIndexOutOfBoundsException( message );
    }

    Dn newDn = new Dn( schemaManager );
    List<Rdn> rdnsDescendant = descendant.getRdns();

    for ( int i = 0; i < descendant.size(); i++ )
    {
        Rdn rdn = rdns.get( i );
        Rdn rdnDescendant = rdnsDescendant.get( i );

        if ( !rdn.equals( rdnDescendant ) )
        {
            throw new LdapInvalidDnException( ResultCodeEnum.INVALID_DN_SYNTAX );
        }
    }

    for ( int i = length; i < rdns.size(); i++ )
    {
        newDn.rdns.add( rdns.get( i ) );
    }

    newDn.toUpName();

    return newDn;
}
 
Example 8
Source File: StoreSearchRequestBaseObject.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void action( LdapMessageContainer<SearchRequest> container ) throws DecoderException
{
    SearchRequest searchRequest = container.getMessage();

    TLV tlv = container.getCurrentTLV();

    // We have to check that this is a correct Dn
    // We have to handle the special case of a 0 length base
    // object,
    // which means that the search is done from the default
    // root.
    if ( tlv.getLength() != 0 )
    {
        byte[] dnBytes = tlv.getValue().getData();
        String dnStr = Strings.utf8ToString( dnBytes );

        try
        {
            Dn baseObject = new Dn( dnStr );
            searchRequest.setBase( baseObject );
        }
        catch ( LdapInvalidDnException ine )
        {
            String msg = I18n.err( I18n.ERR_05132_INVALID_ROOT_DN, dnStr, Strings.dumpBytes( dnBytes ) );
            LOG.error( I18n.err( I18n.ERR_05114_ERROR_MESSAGE, msg, ine.getMessage() ) );

            SearchResultDoneImpl response = new SearchResultDoneImpl( searchRequest.getMessageId() );
            throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
                Dn.EMPTY_DN, ine );
        }
    }
    else
    {
        searchRequest.setBase( Dn.EMPTY_DN );
    }

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05160_SEARCHING_WITH_ROOT_DN, searchRequest.getBase() ) );
    }
}
 
Example 9
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 10
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 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: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Handles processing with referrals without ManageDsaIT decorator.
 */
public void handleException( LdapSession session, ResultResponseRequest req, Exception e )
{
    LdapResult result = req.getResultResponse().getLdapResult();
    Exception cause = null;

    /*
     * Set the result code or guess the best option.
     */
    ResultCodeEnum code;

    if ( e instanceof CursorClosedException )
    {
        cause = ( Exception ) ( ( CursorClosedException ) e ).getCause();

        if ( cause == null )
        {
            cause = e;
        }
    }
    else
    {
        cause = e;
    }

    if ( cause instanceof LdapOperationException )
    {
        code = ( ( LdapOperationException ) cause ).getResultCode();
    }
    else
    {
        code = ResultCodeEnum.getBestEstimate( cause, req.getType() );
    }

    result.setResultCode( code );

    /*
     * Setup the error message to put into the request and put entire
     * exception into the message if we are in debug mode.  Note we
     * embed the result code name into the message.
     */
    String msg = code.toString() + ": failed for " + req + ": " + cause.getLocalizedMessage();

    if ( IS_DEBUG )
    {
        LOG.debug( msg, cause );
        msg += ":\n" + ExceptionUtils.getStackTrace( cause );
    }

    result.setDiagnosticMessage( msg );

    if ( cause instanceof LdapOperationException )
    {
        LdapOperationException ne = ( LdapOperationException ) cause;

        // Add the matchedDN if necessary
        boolean setMatchedDn = code == ResultCodeEnum.NO_SUCH_OBJECT || code == ResultCodeEnum.ALIAS_PROBLEM
            || code == ResultCodeEnum.INVALID_DN_SYNTAX || code == ResultCodeEnum.ALIAS_DEREFERENCING_PROBLEM;

        if ( ( ne.getResolvedDn() != null ) && setMatchedDn )
        {
            result.setMatchedDn( ne.getResolvedDn() );
        }
    }

    session.getIoSession().write( req.getResultResponse() );
}
 
Example 13
Source File: SearchRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Handles processing with referrals without ManageDsaIT decorator.
 */
public void handleException( LdapSession session, ResultResponseRequest req, Exception e )
{
    LdapResult result = req.getResultResponse().getLdapResult();
    Exception cause = null;

    /*
     * Set the result code or guess the best option.
     */
    ResultCodeEnum code;

    if ( e instanceof CursorClosedException )
    {
        cause = ( Exception ) ( ( CursorClosedException ) e ).getCause();

        if ( cause == null )
        {
            cause = e;
        }
    }
    else
    {
        cause = e;
    }

    if ( cause instanceof LdapOperationException )
    {
        code = ( ( LdapOperationException ) cause ).getResultCode();
    }
    else
    {
        code = ResultCodeEnum.getBestEstimate( cause, req.getType() );
    }

    result.setResultCode( code );

    /*
     * Setup the error message to put into the request and put entire
     * exception into the message if we are in debug mode.  Note we
     * embed the result code name into the message.
     */
    String msg = code.toString() + ": failed for " + req + ": " + cause.getLocalizedMessage();

    if ( IS_DEBUG )
    {
        LOG.debug( msg, cause );
        msg += ":\n" + ExceptionUtils.getStackTrace( cause );
    }

    result.setDiagnosticMessage( msg );

    if ( cause instanceof LdapOperationException )
    {
        LdapOperationException ne = ( LdapOperationException ) cause;

        // Add the matchedDN if necessary
        boolean setMatchedDn = code == ResultCodeEnum.NO_SUCH_OBJECT || code == ResultCodeEnum.ALIAS_PROBLEM
            || code == ResultCodeEnum.INVALID_DN_SYNTAX || code == ResultCodeEnum.ALIAS_DEREFERENCING_PROBLEM;

        if ( ( ne.getResolvedDn() != null ) && setMatchedDn )
        {
            result.setMatchedDn( ne.getResolvedDn() );
        }
    }

    session.getIoSession().write( req.getResultResponse() );
}