Search in sources :

Example 6 with Connection

use of org.ldaptive.Connection in project cas by apereo.

the class LdapSpnegoKnownClientSystemsFilterAction method executeSearchForSpnegoAttribute.

/**
     * Searches the ldap instance for the attribute value.
     *
     * @param remoteIp the remote ip
     * @return true/false
     */
protected boolean executeSearchForSpnegoAttribute(final String remoteIp) {
    Connection connection = null;
    final String remoteHostName = getRemoteHostName(remoteIp);
    LOGGER.debug("Resolved remote hostname [{}] based on ip [{}]", remoteHostName, remoteIp);
    try {
        connection = createConnection();
        final Operation searchOperation = new SearchOperation(connection);
        this.searchRequest.getSearchFilter().setParameter(0, remoteHostName);
        LOGGER.debug("Using search filter [{}] on baseDn [{}]", this.searchRequest.getSearchFilter().format(), this.searchRequest.getBaseDn());
        final Response<SearchResult> searchResult = searchOperation.execute(this.searchRequest);
        if (searchResult.getResultCode() == ResultCode.SUCCESS) {
            return processSpnegoAttribute(searchResult);
        }
        throw new RuntimeException("Failed to establish a connection ldap. " + searchResult.getMessage());
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
        throw Throwables.propagate(e);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : SearchOperation(org.ldaptive.SearchOperation) Connection(org.ldaptive.Connection) SearchResult(org.ldaptive.SearchResult) SearchOperation(org.ldaptive.SearchOperation) Operation(org.ldaptive.Operation) LdapException(org.ldaptive.LdapException)

Example 7 with Connection

use of org.ldaptive.Connection in project cas by apereo.

the class LdapSpnegoKnownClientSystemsFilterAction method createConnection.

/**
     * Create and open a connection to ldap
     * via the given config and provider.
     *
     * @return the connection
     * @throws LdapException the ldap exception
     */
protected Connection createConnection() throws LdapException {
    LOGGER.debug("Establishing a connection...");
    final Connection connection = this.connectionFactory.getConnection();
    connection.open();
    return connection;
}
Also used : Connection(org.ldaptive.Connection)

Example 8 with Connection

use of org.ldaptive.Connection in project cas by apereo.

the class LdapUtils method executeAddOperation.

/**
     * Execute add operation boolean.
     *
     * @param connectionFactory the connection factory
     * @param entry             the entry
     * @return true/false
     * @throws LdapException the ldap exception
     */
public static boolean executeAddOperation(final ConnectionFactory connectionFactory, final LdapEntry entry) throws LdapException {
    try (Connection connection = createConnection(connectionFactory)) {
        final AddOperation operation = new AddOperation(connection);
        operation.execute(new AddRequest(entry.getDn(), entry.getAttributes()));
        return true;
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return false;
}
Also used : AddRequest(org.ldaptive.AddRequest) Connection(org.ldaptive.Connection) AddOperation(org.ldaptive.AddOperation) LdapException(org.ldaptive.LdapException)

Aggregations

Connection (org.ldaptive.Connection)8 LdapException (org.ldaptive.LdapException)5 AttributeModification (org.ldaptive.AttributeModification)3 ModifyOperation (org.ldaptive.ModifyOperation)3 ModifyRequest (org.ldaptive.ModifyRequest)3 SearchOperation (org.ldaptive.SearchOperation)3 AddOperation (org.ldaptive.AddOperation)2 AddRequest (org.ldaptive.AddRequest)2 Credential (org.ldaptive.Credential)2 DeleteOperation (org.ldaptive.DeleteOperation)2 DeleteRequest (org.ldaptive.DeleteRequest)2 Response (org.ldaptive.Response)2 SearchRequest (org.ldaptive.SearchRequest)2 SearchResult (org.ldaptive.SearchResult)2 UnicodePwdAttribute (org.ldaptive.ad.UnicodePwdAttribute)2 PasswordModifyOperation (org.ldaptive.extended.PasswordModifyOperation)2 PasswordModifyRequest (org.ldaptive.extended.PasswordModifyRequest)2 DeleteReferralHandler (org.ldaptive.referral.DeleteReferralHandler)2 SearchReferralHandler (org.ldaptive.referral.SearchReferralHandler)2 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)1