org.apache.directory.api.ldap.model.message.ResultResponse Java Examples

The following examples show how to use org.apache.directory.api.ldap.model.message.ResultResponse. 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: AbstractPasswordPolicyResponder.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public final PasswordWarning process( PasswordPolicyOperation operation )
    throws PasswordException
{
    try
    {
        ResultResponse response = operation.process();
        PasswordPolicyResponse passwordPolicyResponse = getPasswordPolicy( response );
        ResultCodeEnum resultCode = response.getLdapResult().getResultCode();
        
        if ( resultCode == ResultCodeEnum.SUCCESS )
        {
            return success( passwordPolicyResponse );
        }
        else
        {
            throw fail( response, passwordPolicyResponse, resultCode );
        }
    }
    catch ( LdapException e )
    {
        throw new PasswordException().setLdapException( e );
    }
}
 
Example #2
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
private PasswordWarning authenticateConnection( final LdapConnection connection,
    final Dn userDn, final char[] password ) throws PasswordException
{
    return passwordPolicyResponder.process(
        new PasswordPolicyOperation()
        {
            @Override
            public ResultResponse process() throws LdapException
            {
                MemoryClearingBuffer passwordBuffer = MemoryClearingBuffer.newInstance( password );
                try
                {
                    BindRequest bindRequest = new BindRequestImpl()
                        .setDn( userDn )
                        .setCredentials( passwordBuffer.getBytes() )
                        .addControl( passwordPolicyRequestControl );

                    return connection.bind( bindRequest );
                }
                finally
                {
                    passwordBuffer.clear();
                }
            }
        } );
}
 
Example #3
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
private void modifyPassword( final LdapConnection connection, final Dn userDn,
    final char[] newPassword ) throws PasswordException
{
    passwordPolicyResponder.process(
        new PasswordPolicyOperation()
        {
            @Override
            public ResultResponse process() throws PasswordException, LdapException
            {
                // Can't use Password Modify:
                // https://issues.apache.org/jira/browse/DIRSERVER-1935
                // So revert to regular Modify
                MemoryClearingBuffer newPasswordBuffer = MemoryClearingBuffer.newInstance( newPassword );
                try
                {
                    ModifyRequest modifyRequest = new ModifyRequestImpl()
                        .setName( userDn )
                        .replace( "userPassword", newPasswordBuffer.getComputedBytes() )
                        .addControl( passwordPolicyRequestControl );

                    return connection.modify( modifyRequest );
                }
                finally
                {
                    newPasswordBuffer.clear();
                }
            }
        } );

}
 
Example #4
Source File: ApacheLdapProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void processResponse( final ResultResponse response )
        throws ChaiOperationException
{
    final boolean success = response.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS;
    if ( !success )
    {
        final String msg = response.getLdapResult().getDiagnosticMessage();
        throw ChaiOperationException.forErrorMessage( msg );
    }
}
 
Example #5
Source File: InitReferrals.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<Message> container ) throws DecoderException
{
    TLV tlv = container.getCurrentTLV();

    // If we hae a Referrals sequence, then it should not be empty
    if ( tlv.getLength() == 0 )
    {
        String msg = I18n.err( I18n.ERR_05105_REFERRAL_MUST_NOT_BE_NULL );
        LOG.error( msg );

        // This will generate a PROTOCOL_ERROR
        throw new DecoderException( msg );
    }

    ResultResponse response = ( ResultResponse ) container.getMessage();
    LdapResult ldapResult = response.getLdapResult();

    Referral referral = new ReferralImpl();
    ldapResult.setReferral( referral );

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05105_INITIALISNG_REFERRAL_LIST ) );
    }
}
 
Example #6
Source File: ResponseCarryingException.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a DecoderException
 * 
 * @param message A message with meaning to a human
 * @param response The response to store
 * @param code the ResultCode
 * @param matchedDn The Matched DN
 * @param cause The Exception which caused the error
 */
public ResponseCarryingException( String message, ResultResponse response, ResultCodeEnum code,
    Dn matchedDn, Throwable cause )
{
    super( message, cause );

    response.getLdapResult().setDiagnosticMessage( message );
    response.getLdapResult().setResultCode( code );
    response.getLdapResult().setMatchedDn( matchedDn );

    this.response = response;
}
 
Example #7
Source File: LdapMessageContainer.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Copy the LdapResult element from a opaque response to a newly created 
 * extendedResponse
 *  
 * @param resultResponse The original response
 * @param extendedResponse The newly created ExtendedResponse
 */
public static void copyLdapResult( ResultResponse resultResponse, ExtendedResponse extendedResponse )
{
    extendedResponse.getLdapResult().setDiagnosticMessage( resultResponse.getLdapResult().getDiagnosticMessage() );
    extendedResponse.getLdapResult().setMatchedDn( resultResponse.getLdapResult().getMatchedDn() );
    extendedResponse.getLdapResult().setReferral( resultResponse.getLdapResult().getReferral() );
    extendedResponse.getLdapResult().setResultCode( resultResponse.getLdapResult().getResultCode() );
}
 
Example #8
Source File: AbstractPasswordPolicyResponder.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an exception to be thrown in the case of a non SUCCESS 
 * <code>resultCode</code>.
 * 
 * @param resultResponse The result response
 * @param passwordPolicyResponse The password policy in use
 * @param resultCode The result
 * @return The created PasswordException
 */
protected PasswordException fail( ResultResponse resultResponse, 
        PasswordPolicyResponse passwordPolicyResponse, ResultCodeEnum resultCode )
{
    PasswordException exception = new PasswordException();
    exception.setResultCode( resultCode );
    
    if ( passwordPolicyResponse != null
        && passwordPolicyResponse.getPasswordPolicyError() != null )
    {
        exception.setPasswordPolicyError( passwordPolicyResponse.getPasswordPolicyError() );
    }
    return exception;
}
 
Example #9
Source File: LdapConnectionTemplate.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T extends ResultResponse> T responseOrException( T response )
{
    if ( ResultCodeEnum.SUCCESS != response.getLdapResult().getResultCode() )
    {
        throw new LdapRequestUnsuccessfulException( response );
    }
    return response;
}
 
Example #10
Source File: Dsmlv2ResponseGrammar.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( Dsmlv2Container container ) throws XmlPullParserException
{
    DsmlDecorator<? extends Response> ldapResponse =
        container.getBatchResponse().getCurrentResponse();

    LdapResult ldapResult;

    // Search Response is a special case
    // ResultCode can only occur in a case of Search Result Done in a Search Response
    if ( ldapResponse.getDecorated() instanceof SearchResponse )
    {
        SearchResponse searchResponse = ( SearchResponse ) ldapResponse.getDecorated();
        ldapResult = searchResponse.getSearchResultDone().getLdapResult();
    }
    else
    {
        ldapResult = ( ( ResultResponse ) ldapResponse.getDecorated() ).getLdapResult();
    }

    XmlPullParser xpp = container.getParser();

    try
    {
        String nextText = xpp.nextText();

        if ( !Strings.isEmpty( nextText ) )
        {
            ldapResult.setDiagnosticMessage( nextText.trim() );
        }
    }
    catch ( IOException ioe )
    {
        throw new XmlPullParserException( I18n.err( I18n.ERR_03008_UNEXPECTED_ERROR, ioe.getMessage() ), xpp, ioe );
    }
}
 
Example #11
Source File: AbstractResultResponseRequestDsml.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ResultResponse getResultResponse()
{
    return getDecorated().getResultResponse();
}
 
Example #12
Source File: LdapRequestUnsuccessfulException.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * @return the associate LDAP Response
 */
public ResultResponse getResponse()
{
    return response;
}
 
Example #13
Source File: LdapRequestUnsuccessfulException.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new LdapRequestUnsuccessfulException instance
 * 
 * @param response The associated LDAP Response
 */
public LdapRequestUnsuccessfulException( ResultResponse response )
{
    super();
    this.response = response;
}
 
Example #14
Source File: Dsmlv2ResponseGrammar.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( Dsmlv2Container container ) throws XmlPullParserException
{
    DsmlDecorator<? extends Response> ldapResponse =
        container.getBatchResponse().getCurrentResponse();

    LdapResult ldapResult;

    // Search Response is a special case
    // ResultCode can only occur in a case of Search Result Done in a Search Response
    if ( ldapResponse.getDecorated() instanceof SearchResponse )
    {
        SearchResponse searchResponse = ( SearchResponse ) ldapResponse.getDecorated();
        ldapResult = searchResponse.getSearchResultDone().getLdapResult();
    }
    else
    {
        ldapResult = ( ( ResultResponse ) ldapResponse.getDecorated() ).getLdapResult();
    }

    // Initialization of the Referrals if needed
    if ( ldapResult.getReferral() == null )
    {
        ldapResult.setReferral( new ReferralImpl() );
    }

    XmlPullParser xpp = container.getParser();

    try
    {
        String nextText = xpp.nextText();

        if ( !Strings.isEmpty( nextText ) )
        {
            try
            {
                String urlStr = nextText.trim();
                LdapUrl ldapUrl = new LdapUrl( urlStr );
                ldapResult.getReferral().addLdapUrl( ldapUrl.toString() );
            }
            catch ( LdapURLEncodingException luee )
            {
                throw new XmlPullParserException( luee.getMessage(), xpp, luee );
            }
        }
    }
    catch ( IOException ioe )
    {
        throw new XmlPullParserException( I18n.err( I18n.ERR_03008_UNEXPECTED_ERROR, ioe.getMessage() ), xpp, ioe );
    }
}
 
Example #15
Source File: StoreResultCode.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<Message> container ) throws DecoderException
{
    // The current TLV should be a integer
    // We get it and store it in MessageId
    TLV tlv = container.getCurrentTLV();

    BerValue value = tlv.getValue();
    ResultCodeEnum resultCode = ResultCodeEnum.SUCCESS;

    try
    {
        resultCode = ResultCodeEnum.getResultCode( IntegerDecoder.parse( value, 0,
            ResultCodeEnum.E_SYNC_REFRESH_REQUIRED
                .getResultCode() ) );
    }
    catch ( IntegerDecoderException ide )
    {
        LOG.error( I18n.err( I18n.ERR_05108_INVALID_RESULT_CODE, Strings.dumpBytes( value.getData() ), ide.getMessage() ) );

        throw new DecoderException( ide.getMessage(), ide );
    }

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05109_RESULT_CODE_IS, resultCode ) );
    }

    ResultResponse response = ( ResultResponse ) container.getMessage();
    
    LdapResult ldapResult;
    
    if ( response == null ) 
    {
        ldapResult = new LdapResultImpl();
    }
    else
    {
        ldapResult = response.getLdapResult();
    }

    container.setLdapResult( ldapResult );
    ldapResult.setResultCode( resultCode );
}
 
Example #16
Source File: Dsmlv2ResponseGrammar.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( Dsmlv2Container container ) throws XmlPullParserException
{
    DsmlDecorator<? extends Response> ldapResponse =
        container.getBatchResponse().getCurrentResponse();

    LdapResult ldapResult;

    // Search Response is a special case
    // ResultCode can only occur in a case of Search Result Done in a Search Response
    if ( ldapResponse.getDecorated() instanceof SearchResponse )
    {
        SearchResponse searchResponse = ( SearchResponse ) ldapResponse.getDecorated();
        ldapResult = searchResponse.getSearchResultDone().getLdapResult();
    }
    else
    {
        ldapResult = ( ( ResultResponse ) ldapResponse.getDecorated() ).getLdapResult();
    }

    XmlPullParser xpp = container.getParser();

    // Checking and adding the request's attributes
    String attributeValue;
    // code
    attributeValue = xpp.getAttributeValue( "", "code" );

    if ( attributeValue != null )
    {
        try
        {
            ldapResult.setResultCode( ResultCodeEnum.getResultCode( Integer.parseInt( attributeValue ) ) );
        }
        catch ( NumberFormatException nfe )
        {
            throw new XmlPullParserException( I18n.err( I18n.ERR_03009_RESULT_CODE_NOT_INTEGER ), xpp, nfe );
        }
    }
    else
    {
        throw new XmlPullParserException( I18n.err( I18n.ERR_03010_CODE_ATTRIBUTE_REQUIRED ), xpp, null );
    }

    // descr
    attributeValue = xpp.getAttributeValue( "", "descr" );

    if ( ( attributeValue != null ) && !DSMLV2_DESCR_TAGS.contains( attributeValue ) )
    {
        throw new XmlPullParserException( I18n.err( I18n.ERR_03011_DESCR_DOESNT_MATCH_VALUES, attributeValue ), xpp, null );
    }
}
 
Example #17
Source File: ResponseFactory.java    From directory-ldap-api with Apache License 2.0 3 votes vote down vote up
/**
 * Encode the Response message to a PDU.
 * <br>
 * Response :
 * <pre>
 * 0x&lt;tag&gt; L1
 *  |
 *  +--&gt; LdapResult
 * </pre>
 *
 * @param codec The LdapApiService instance
 * @param buffer The buffer where to put the PDU
 * @param tag The message tag
 * @param message the Response to encode
 */
protected void encodeReverse( LdapApiService codec, Asn1Buffer buffer, byte tag, Message message )
{
    int start = buffer.getPos();

    // The LDAPResult part
    encodeLdapResultReverse( buffer, ( ( ResultResponse ) message ).getLdapResult() );

    // The BindResponse Tag
    BerValue.encodeSequence( buffer, tag, start );
}
 
Example #18
Source File: PasswordPolicyOperation.java    From directory-ldap-api with Apache License 2.0 2 votes vote down vote up
/**
 * Execute operations whose results imply somme sort of password policy
 * information.
 *
 * @return The final response of the operation
 * @throws PasswordException If there was a failure for a password policy
 * reason
 * @throws LdapException If there was an general ldap failure
 */
ResultResponse process() throws PasswordException, LdapException;
 
Example #19
Source File: LdapConnectionOperations.java    From directory-ldap-api with Apache License 2.0 2 votes vote down vote up
/**
 * Checks the supplied response for its result code, and if not 
 * ResultCodeEnum#SUCCESS, an exception is thrown. This method is 
 * intended to be used inline:
 * 
 * <pre>
 * template.responseOrException( template.delete( dn ) );
 * </pre>
 *
 * @param response The response to check for success
 * @param <T> The type of response
 * @return The supplied <code>response</code>
 */
<T extends ResultResponse> T responseOrException( T response );