Search in sources :

Example 31 with LdapException

use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.

the class LdapNetworkConnection method bindAsync.

/**
 * Do an asynchronous bind, based on a GssApiRequest.
 *
 * @param request The GssApiRequest POJO containing all the needed parameters
 * @return The bind operation's future
 * @throws LdapException if some error occurred
 */
public BindFuture bindAsync(SaslGssApiRequest request) throws LdapException {
    // Krb5.conf file
    if (request.getKrb5ConfFilePath() != null) {
        // Using the krb5.conf file provided by the user
        System.setProperty(KRB5_CONF, request.getKrb5ConfFilePath());
    } else if ((request.getRealmName() != null) && (request.getKdcHost() != null) && (request.getKdcPort() != 0)) {
        try {
            // Using a custom krb5.conf we create from the settings provided by the user
            String krb5ConfPath = createKrb5ConfFile(request.getRealmName(), request.getKdcHost(), request.getKdcPort());
            System.setProperty(KRB5_CONF, krb5ConfPath);
        } catch (IOException ioe) {
            throw new LdapException(ioe);
        }
    } else {
        // Using the system Kerberos configuration
        System.clearProperty(KRB5_CONF);
    }
    // Login Module configuration
    if (request.getLoginModuleConfiguration() != null) {
        // Using the configuration provided by the user
        Configuration.setConfiguration(request.getLoginModuleConfiguration());
    } else {
        // Using the default configuration
        Configuration.setConfiguration(new Krb5LoginConfiguration());
    }
    try {
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "true");
        LoginContext loginContext = new LoginContext(request.getLoginContextName(), new SaslCallbackHandler(request));
        loginContext.login();
        final SaslGssApiRequest requetFinal = request;
        return (BindFuture) Subject.doAs(loginContext.getSubject(), new PrivilegedExceptionAction<Object>() {

            @Override
            public Object run() throws Exception {
                return bindSasl(requetFinal);
            }
        });
    } catch (Exception e) {
        throw new LdapException(e);
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) SaslCallbackHandler(org.apache.directory.ldap.client.api.callback.SaslCallbackHandler) IOException(java.io.IOException) BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 32 with LdapException

use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.

the class LdapNetworkConnection method add.

/**
 * {@inheritDoc}
 */
@Override
public AddResponse add(AddRequest addRequest) throws LdapException {
    if (addRequest == null) {
        String msg = "Cannot process a null addRequest";
        LOG.debug(msg);
        throw new IllegalArgumentException(msg);
    }
    if (addRequest.getEntry() == null) {
        String msg = "Cannot add a null entry";
        LOG.debug(msg);
        throw new IllegalArgumentException(msg);
    }
    AddFuture addFuture = addAsync(addRequest);
    // Get the result from the future
    try {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        AddResponse addResponse = addFuture.get(timeout, TimeUnit.MILLISECONDS);
        if (addResponse == null) {
            // We didn't received anything : this is an error
            if (LOG.isErrorEnabled()) {
                LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Add"));
            }
            throw new LdapException(TIME_OUT_ERROR);
        }
        if (addResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
            // Everything is fine, return the response
            if (LOG.isDebugEnabled()) {
                LOG.debug(I18n.msg(I18n.MSG_03209_ADD_SUCCESSFUL, addResponse));
            }
        } else {
            // We have had an error
            if (LOG.isDebugEnabled()) {
                LOG.debug(I18n.msg(I18n.MSG_03208_ADD_FAILED, addResponse));
            }
        }
        return addResponse;
    } catch (Exception ie) {
        // Catch all other exceptions
        LOG.error(NO_RESPONSE_ERROR, ie);
        // Send an abandon request
        if (!addFuture.isCancelled()) {
            abandon(addRequest.getMessageId());
        }
        throw new LdapException(NO_RESPONSE_ERROR, ie);
    }
}
Also used : AddFuture(org.apache.directory.ldap.client.api.future.AddFuture) AddResponse(org.apache.directory.api.ldap.model.message.AddResponse) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 33 with LdapException

use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.

the class LdapNetworkConnection method bind.

/**
 * Bind to the server using a SaslRequest object.
 *
 * @param request The SaslRequest POJO containing all the needed parameters
 * @return A LdapResponse containing the result
 * @throws LdapException if some error occurred
 */
public BindResponse bind(SaslRequest request) throws LdapException {
    if (request == null) {
        String msg = I18n.msg(I18n.MSG_03204_NULL_REQUEST);
        LOG.debug(msg);
        throw new IllegalArgumentException(msg);
    }
    BindFuture bindFuture = bindAsync(request);
    // Get the result from the future
    try {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        BindResponse bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
        if (bindResponse == null) {
            // We didn't received anything : this is an error
            if (LOG.isErrorEnabled()) {
                LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
            }
            throw new LdapException(TIME_OUT_ERROR);
        }
        if (bindResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
            authenticated.set(true);
            // Everything is fine, return the response
            if (LOG.isDebugEnabled()) {
                LOG.debug(I18n.msg(I18n.MSG_03202_BIND_SUCCESSFUL, bindResponse));
            }
        } else {
            // We have had an error
            if (LOG.isDebugEnabled()) {
                LOG.debug(I18n.msg(I18n.MSG_03201_BIND_FAIL, bindResponse));
            }
        }
        return bindResponse;
    } catch (Exception ie) {
        // Catch all other exceptions
        LOG.error(NO_RESPONSE_ERROR, ie);
        throw new LdapException(NO_RESPONSE_ERROR, ie);
    }
}
Also used : BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 34 with LdapException

use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.

the class LdapNetworkConnection method modify.

/**
 * {@inheritDoc}
 */
@Override
public ModifyResponse modify(ModifyRequest modRequest) throws LdapException {
    if (modRequest == null) {
        String msg = "Cannot process a null modifyRequest";
        LOG.debug(msg);
        throw new IllegalArgumentException(msg);
    }
    ModifyFuture modifyFuture = modifyAsync(modRequest);
    // Get the result from the future
    try {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        ModifyResponse modifyResponse = modifyFuture.get(timeout, TimeUnit.MILLISECONDS);
        if (modifyResponse == null) {
            // We didn't received anything : this is an error
            if (LOG.isErrorEnabled()) {
                LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Modify"));
            }
            throw new LdapException(TIME_OUT_ERROR);
        }
        if (modifyResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
            // Everything is fine, return the response
            if (LOG.isDebugEnabled()) {
                LOG.debug(I18n.msg(I18n.MSG_03224_MODIFY_SUCCESSFUL, modifyResponse));
            }
        } else {
            if (modifyResponse instanceof ModifyNoDResponse) {
                // A NoticeOfDisconnect : deserves a special treatment
                throw new LdapException(modifyResponse.getLdapResult().getDiagnosticMessage());
            }
            // We have had an error
            if (LOG.isDebugEnabled()) {
                LOG.debug(I18n.msg(I18n.MSG_03223_MODIFY_FAILED, modifyResponse));
            }
        }
        return modifyResponse;
    } catch (Exception ie) {
        // Catch all other exceptions
        LOG.error(NO_RESPONSE_ERROR, ie);
        // Send an abandon request
        if (!modifyFuture.isCancelled()) {
            abandon(modRequest.getMessageId());
        }
        throw new LdapException(ie.getMessage(), ie);
    }
}
Also used : ModifyFuture(org.apache.directory.ldap.client.api.future.ModifyFuture) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse) ModifyNoDResponse(org.apache.directory.api.ldap.model.message.extended.ModifyNoDResponse)

Example 35 with LdapException

use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.

the class LdapNetworkConnection method writeRequest.

/**
 * a reusable code block to be used in various bind methods
 */
private void writeRequest(Request request) throws LdapException {
    // Send the request to the server
    WriteFuture writeFuture = ldapSession.write(request);
    long localTimeout = timeout;
    while (localTimeout > 0) {
        // Wait only 100 ms
        boolean done = writeFuture.awaitUninterruptibly(100);
        if (done) {
            return;
        }
        // Wait for the message to be sent to the server
        if (!ldapSession.isConnected()) {
            // We didn't received anything : this is an error
            if (LOG.isErrorEnabled()) {
                LOG.error(I18n.err(I18n.ERR_03207_SOMETHING_WRONG_HAPPENED));
            }
            Exception exception = (Exception) ldapSession.removeAttribute(EXCEPTION_KEY);
            if (exception != null) {
                if (exception instanceof LdapException) {
                    throw (LdapException) exception;
                } else {
                    throw new InvalidConnectionException(exception.getMessage(), exception);
                }
            }
            throw new InvalidConnectionException("Error while sending some message : the session has been closed");
        }
        localTimeout -= 100;
    }
    if (LOG.isErrorEnabled()) {
        LOG.error(I18n.err(I18n.ERR_03208_TIMEOUT));
    }
    throw new LdapException(TIME_OUT_ERROR);
}
Also used : InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) WriteFuture(org.apache.mina.core.future.WriteFuture) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Aggregations

LdapException (org.apache.directory.api.ldap.model.exception.LdapException)329 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)159 ArrayList (java.util.ArrayList)93 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)91 FinderException (org.apache.directory.fortress.core.FinderException)73 Entry (org.apache.directory.api.ldap.model.entry.Entry)63 Modification (org.apache.directory.api.ldap.model.entry.Modification)63 IOException (java.io.IOException)54 SearchCursor (org.apache.directory.api.ldap.model.cursor.SearchCursor)51 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)50 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)49 UpdateException (org.apache.directory.fortress.core.UpdateException)41 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)35 Dn (org.apache.directory.api.ldap.model.name.Dn)34 LdapAuthenticationException (org.apache.directory.api.ldap.model.exception.LdapAuthenticationException)25 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)25 DecoderException (org.apache.directory.api.asn1.DecoderException)22 LdapNoPermissionException (org.apache.directory.api.ldap.model.exception.LdapNoPermissionException)22 LdapOtherException (org.apache.directory.api.ldap.model.exception.LdapOtherException)22 ConnectException (java.net.ConnectException)21