QueryBuilder.java revision b334b83b7ac2d9a8c60d935cad0365506f13333b
/**
* Copyright 2013 ForgeRock, AS.
*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*/
/**
* Fluent class responsible for constructing queries for the LDAP data store.
*
* This class will handle the details around preparing a query and executing the query, including processing
* the return results.
*
* Uses Token as its main means of expressing the data returned from LDAP and so is intended for use
* with the Core Token Service.
*
* @author robert.wapshott@forgerock.com
*/
public class QueryBuilder {
// Injected
private final DataLayerConnectionFactory connectionFactory;
private final TokenAttributeConversion attributeConversion;
private final CoreTokenConstants constants;
private final LDAPSearchHandler handler;
private int sizeLimit;
/**
* Default constructor ensures the Object Class is defined.
*
* @param connectionFactory The connectionFactory to query.
* @param attributeConversion Required for Token based conversions.
* @param constants Required for system wide constants.
*/
public QueryBuilder(DataLayerConnectionFactory connectionFactory, TokenAttributeConversion attributeConversion,
this(connectionFactory,
}
/**
* Constructor with all dependencies exposed.
*
* @param connectionFactory The connectionFactory to query.
* @param attributeConversion Required for Token based conversions.
* @param constants Required for system wide constants.
* @param debug The debugging instance to debug to.
*/
this.connectionFactory = connectionFactory;
sizeLimit = 0;
}
/**
* Limit the number of results returned from this query to the given amount.
*
* @param maxSize Positive number, zero indicates no limit.
* @return The QueryBuilder instance.
*/
return this;
}
/**
* Limit the search to return only the named attributes.
*
* @param fields Collection of CoreTokenField that are required in the search results.
* @return The QueryBuilder instance.
*/
throw new IllegalArgumentException("Must supply at least one field");
}
}
return this;
}
/**
* Assign a filter to the query. This can be a complex filter and is handled
* by the QueryFilter class.
*
* @see QueryFilter For more details on generating a filter.
*
* @param filter An OpenDJ SDK Filter to assign to the query.
* @return The QueryBuilder instance.
*/
return this;
}
private Filter getLDAPFilter() {
Filter objectClass = Filter.equality(CoreTokenConstants.OBJECT_CLASS, CoreTokenConstants.FR_CORE_TOKEN);
return objectClass;
} else {
}
}
/**
* Perform the query and return the results as unprocessed Entry instances.
*
* @return A non null but possibly empty collection.
*
* @throws com.sun.identity.sm.ldap.exceptions.QueryFailedException If there was an error during the query.
*/
// Prepare the search
// Perform the search
if (DEBUG.messageEnabled()) {
"Query: matched {0} results\n" +
"Search Request: {1}\n" +
"Filter: {2}",
ldapFilter.toString()));
}
return entries;
}
/**
* Perform the query and return the results as processed Token instances.
*
* @return A non null but possibly empty collection.
*
* @throws com.sun.identity.sm.ldap.exceptions.QueryFailedException If there was an error during the query.
*/
throw new IllegalStateException(
"Cannot convert results to Token if the query uses" +
"a reduced number of attributes in the return result");
}
}
if (DEBUG.messageEnabled()) {
// Ensure we don't fill up the logs
}
}
"Query: Matched {0}, some Tokens are shown below:\n" +
"{1}",
msg));
}
return tokens;
}
/**
* Presents the QueryBuilder in a human readable format.
*
* Note: This function is not performant and should only be used for debugging purposes.
*
* @return Non null string representing this QueryBuilder.
*/
return MessageFormat.format(
"Query:\n" +
" Filter: {0}\n" +
" Attributes: {1}",
}
}