use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.
the class LdapEntryManager method contains.
@Override
protected <T> boolean contains(String baseDN, String[] objectClasses, Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, Filter filter, String[] ldapReturnAttributes) {
if (StringHelper.isEmptyString(baseDN)) {
throw new MappingException("Base DN to check contain entries is null");
}
// Create filter
Filter searchFilter;
if (objectClasses.length > 0) {
searchFilter = addObjectClassFilter(filter, objectClasses);
} else {
searchFilter = filter;
}
SearchScope scope = SearchScope.SUB;
SearchResult searchResult = null;
try {
searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(scope), null, 0, 1, 1, null, ldapReturnAttributes);
if ((searchResult == null) || !ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: %s, filter: %s", baseDN, searchFilter));
}
} catch (SearchScopeException ex) {
throw new AuthenticationException(String.format("Failed to convert scope: %s", scope), ex);
} catch (SearchException ex) {
if (!(ResultCode.NO_SUCH_OBJECT_INT_VALUE == ex.getErrorCode())) {
throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
}
}
return (searchResult != null) && (searchResult.getEntryCount() > 0);
}
use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.
the class LdapEntryManager method findPagedEntries.
@Override
public <T> PagedResult<T> findPagedEntries(String baseDN, Class<T> entryClass, Filter filter, String[] ldapReturnAttributes, String sortBy, SortOrder sortOrder, int start, int count, int chunkSize) {
if (StringHelper.isEmptyString(baseDN)) {
throw new MappingException("Base DN to find entries is null");
}
// Check entry class
checkEntryClass(entryClass, false);
String[] objectClasses = getTypeObjectClasses(entryClass);
List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
String[] currentLdapReturnAttributes = ldapReturnAttributes;
if (ArrayHelper.isEmpty(currentLdapReturnAttributes)) {
currentLdapReturnAttributes = getAttributes(null, propertiesAnnotations, false);
}
// Find entries
Filter searchFilter;
if (objectClasses.length > 0) {
searchFilter = addObjectClassFilter(filter, objectClasses);
} else {
searchFilter = filter;
}
List<SearchResultEntry> searchResultEntries;
PagedResult<T> vlvResponse = new PagedResult<T>();
try {
searchResultEntries = getOperationService().searchSearchResultEntryList(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(SearchScope.SUB), start, count, chunkSize, sortBy, sortOrder, vlvResponse, currentLdapReturnAttributes);
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
}
List<T> entries = new ArrayList<T>(0);
if (searchResultEntries.size() > 0) {
entries = createEntitiesVirtualListView(entryClass, propertiesAnnotations, searchResultEntries.toArray(new SearchResultEntry[] {}));
}
vlvResponse.setEntries(entries);
return vlvResponse;
}
use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.
the class SpannerCustomStringAttributesSample method main.
public static void main(String[] args) {
// Prepare sample connection details
SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
// Create SQL entry manager
SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
String randomExternalUid = "" + System.currentTimeMillis();
// String randomExternalUid = "otp:" + System.currentTimeMillis();
// Add dummy user
SimpleCustomStringUser newUser = new SimpleCustomStringUser();
newUser.setDn(String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis()));
newUser.setUserId("sample_user_" + System.currentTimeMillis());
newUser.setUserPassword("test");
newUser.getCustomAttributes().add(new CustomAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
newUser.getCustomAttributes().add((new CustomAttribute("jansExtUid", randomExternalUid)).multiValued());
newUser.setUserRole(UserRole.ADMIN);
newUser.setMemberOf(Arrays.asList("group_1", "group_2", "group_3"));
sqlEntryManager.persist(newUser);
LOG.info("Added User '{}' with uid '{}' and key '{}'", newUser, newUser.getUserId(), newUser.getDn());
// Find added dummy user but use custom class with String values
SimpleCustomStringUser foundUser = sqlEntryManager.find(SimpleCustomStringUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
for (CustomAttribute customAttribute : foundUser.getCustomAttributes()) {
LOG.info("Found custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
}
// Find by jsExternalUid
Filter jsExternalUidFilter = Filter.createEqualityFilter("jansExtUid", randomExternalUid).multiValued();
List<SimpleCustomStringUser> foundUsers = sqlEntryManager.findEntries("ou=people,o=jans", SimpleCustomStringUser.class, jsExternalUidFilter);
for (SimpleCustomStringUser foundUser2 : foundUsers) {
LOG.info("Found User '{}' by jansExtUid with uid '{}' and key '{}'", foundUser2, foundUser2.getUserId(), foundUser2.getDn());
}
}
use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.
the class SpannerUmaResourceSample method main.
public static void main(String[] args) {
// Prepare sample connection details
SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
// Create SQL entry manager
SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
final Filter filter = Filter.createEqualityFilter("jansAssociatedClnt", "inum=AB77-1A2B,ou=clients,o=jans");
List<UmaResource> umaResource = sqlEntryManager.findEntries("ou=resources,ou=uma,o=jans", UmaResource.class, filter);
LOG.info("Found umaResources: " + umaResource);
}
use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.
the class LdapSample method main.
public static void main(String[] args) {
// Prepare sample connection details
LdapEntryManagerSample ldapEntryManagerSample = new LdapEntryManagerSample();
// Create LDAP entry manager
LdapEntryManager ldapEntryManager = ldapEntryManagerSample.createLdapEntryManager();
// Find all users which have specified object classes defined in SimpleUser
List<SimpleUser> users = ldapEntryManager.findEntries("o=jans", SimpleUser.class, null);
for (SimpleUser user : users) {
LOG.debug("User with uid: " + user.getUserId());
}
if (users.size() > 0) {
// Add attribute "streetAddress" to first user
SimpleUser user = users.get(0);
user.getCustomAttributes().add(new CustomAttribute("streetAddress", "Somewhere: " + System.currentTimeMillis()));
ldapEntryManager.merge(user);
}
Filter filter = Filter.createEqualityFilter("status", "active");
List<SimpleAttribute> attributes = ldapEntryManager.findEntries("o=jans", SimpleAttribute.class, filter, SearchScope.SUB, null, null, 10, 0, 0);
for (SimpleAttribute attribute : attributes) {
LOG.debug("Attribute with displayName: " + attribute.getCustomAttributes().get(1));
}
List<SimpleSession> sessions = ldapEntryManager.findEntries("o=jans", SimpleSession.class, filter, SearchScope.SUB, null, null, 10, 0, 0);
LOG.debug("Found sessions: " + sessions.size());
List<SimpleGrant> grants = ldapEntryManager.findEntries("o=jans", SimpleGrant.class, null, SearchScope.SUB, new String[] { "grtId" }, null, 10, 0, 0);
LOG.debug("Found grants: " + grants.size());
try {
PagedResult<SimpleUser> vlvResponse = ldapEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "status" }, "displayName", SortOrder.ASCENDING, 10, 100000, 1000);
LOG.debug("Found persons: " + vlvResponse.getTotalEntriesCount());
System.out.println(vlvResponse.getEntries().size());
} catch (Exception ex) {
LOG.error("Failed to search", ex);
}
}
Aggregations