Search in sources :

Example 1 with SearchOperation

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

the class LdapUtils method executeSearchOperation.

/**
 * Execute search operation.
 *
 * @param connectionFactory the connection factory
 * @param baseDn            the base dn
 * @param filter            the filter
 * @param binaryAttributes  the binary attributes
 * @param returnAttributes  the return attributes
 * @return the response
 * @throws LdapException the ldap exception
 */
public static Response<SearchResult> executeSearchOperation(final ConnectionFactory connectionFactory, final String baseDn, final SearchFilter filter, final String[] binaryAttributes, final String[] returnAttributes) throws LdapException {
    try (Connection connection = createConnection(connectionFactory)) {
        final SearchOperation searchOperation = new SearchOperation(connection);
        final SearchRequest request = LdapUtils.newLdaptiveSearchRequest(baseDn, filter, binaryAttributes, returnAttributes);
        request.setReferralHandler(new SearchReferralHandler());
        return searchOperation.execute(request);
    }
}
Also used : SearchRequest(org.ldaptive.SearchRequest) SearchOperation(org.ldaptive.SearchOperation) Connection(org.ldaptive.Connection) SearchReferralHandler(org.ldaptive.referral.SearchReferralHandler)

Example 2 with SearchOperation

use of org.ldaptive.SearchOperation 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
 */
@SneakyThrows
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 IllegalArgumentException("Failed to establish a connection ldap. " + searchResult.getMessage());
    } 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) SneakyThrows(lombok.SneakyThrows)

Aggregations

Connection (org.ldaptive.Connection)2 SearchOperation (org.ldaptive.SearchOperation)2 SneakyThrows (lombok.SneakyThrows)1 Operation (org.ldaptive.Operation)1 SearchRequest (org.ldaptive.SearchRequest)1 SearchResult (org.ldaptive.SearchResult)1 SearchReferralHandler (org.ldaptive.referral.SearchReferralHandler)1