use of io.jans.orm.couchbase.model.SimpleUser in project jans by JanssenProject.
the class CouchbaseCustomMultiValuedTypesSample method main.
public static void main(String[] args) {
// Prepare sample connection details
CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
// Create Couchbase entry manager
CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
// Add dummy user
SimpleUser newUser = new SimpleUser();
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 CustomObjectAttribute("streetAddress", Arrays.asList("London", "Texas", "Kiev")));
newUser.getCustomAttributes().add(new CustomObjectAttribute("test", "test_value"));
newUser.getCustomAttributes().add(new CustomObjectAttribute("fuzzy", "test_value"));
newUser.setMemberOf(Arrays.asList("group_1", "group_2", "group_3"));
newUser.setAttributeValue("givenName", "john");
couchbaseEntryManager.persist(newUser);
LOG.info("Added User '{}' with uid '{}' and key '{}'", newUser, newUser.getUserId(), newUser.getDn());
LOG.info("Persisted custom attributes '{}'", newUser.getCustomAttributes());
// Find added dummy user
SimpleUser foundUser = couchbaseEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
// Update custom attributes
foundUser.setAttributeValues("streetAddress", Arrays.asList("London", "Texas", "Kiev", "Dublin"));
foundUser.setAttributeValues("test", Arrays.asList("test_value_1", "test_value_2", "test_value_3", "test_value_4"));
foundUser.setAttributeValues("fuzzy", Arrays.asList("fuzzy_value_1", "fuzzy_value_2"));
foundUser.setAttributeValue("simple", "simple");
CustomObjectAttribute multiValuedSingleValue = new CustomObjectAttribute("multivalued", "multivalued_single_valued");
multiValuedSingleValue.setMultiValued(true);
foundUser.getCustomAttributes().add(multiValuedSingleValue);
couchbaseEntryManager.merge(foundUser);
LOG.info("Updated custom attributes '{}'", foundUser.getCustomAttributes());
// Find updated dummy user
SimpleUser foundUpdatedUser = couchbaseEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUpdatedUser, foundUpdatedUser.getUserId(), foundUpdatedUser.getDn());
LOG.info("Cusom attributes '{}'", foundUpdatedUser.getCustomAttributes());
Filter filter = Filter.createEqualityFilter(Filter.createLowercaseFilter("givenName"), StringHelper.toLowerCase("jon"));
List<SimpleUser> foundUpdatedUsers = couchbaseEntryManager.findEntries("o=jans", SimpleUser.class, filter);
System.out.println(foundUpdatedUsers);
}
use of io.jans.orm.couchbase.model.SimpleUser in project jans by JanssenProject.
the class CouchbaseSample method main.
public static void main(String[] args) {
// Prepare sample connection details
CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
// Create Couchbase entry manager
CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
SimpleUser newUser = new SimpleUser();
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 CustomObjectAttribute("streetAddress", Arrays.asList("London", "Texas", "Kiev")));
newUser.getCustomAttributes().add(new CustomObjectAttribute("test", "test_value"));
couchbaseEntryManager.persist(newUser);
// SimpleUser dummyUser = couchbaseEntryManager.find(SimpleUser.class, "inum=test,o=test,o=jans");
// LOG.info("Dummy User '{}'", dummyUser);
// Find all users which have specified object classes defined in SimpleUser
List<SimpleUser> users = couchbaseEntryManager.findEntries("o=@!5304.5F36.0E64.E1AC!0001!179C.62D7,o=jans", SimpleUser.class, null);
for (SimpleUser user : users) {
LOG.info("User with uid: '{}' with DN: '{}'", user.getUserId(), user.getDn());
}
if (users.size() > 0) {
// Add attribute "streetAddress" to first user
SimpleUser user = users.get(3);
LOG.info("Updating: " + user.getUserId());
String[] values = new String[] { "Somewhere: " + System.currentTimeMillis(), "Somewhere2: " + System.currentTimeMillis() };
user.getCustomAttributes().add(new CustomObjectAttribute("streetAddress", Arrays.asList(values)));
user.getCustomAttributes().add(new CustomObjectAttribute("test", "test_value"));
user.getCustomAttributes().add(new CustomObjectAttribute("test2", "test_value2"));
user.getCustomAttributes().add(new CustomObjectAttribute("test3", "test_value3"));
user.setUserId("user1");
user.setUserPassword("test");
couchbaseEntryManager.merge(user);
}
for (SimpleUser user : users) {
boolean result1 = couchbaseEntryManager.authenticate(user.getDn(), "test");
boolean result2 = couchbaseEntryManager.authenticate("ou=people,o=jans", SimpleUser.class, user.getUserId(), "test");
System.out.println("authetication result: " + result1 + ", " + result2);
}
Filter filter = Filter.createEqualityFilter("status", "active");
List<SimpleAttribute> attributes = couchbaseEntryManager.findEntries("o=jans", SimpleAttribute.class, filter, SearchScope.SUB, null, null, 10, 0, 0);
for (SimpleAttribute attribute : attributes) {
LOG.info("Attribute with displayName: " + attribute.getCustomAttributes().get(1));
}
List<SimpleSession> sessions = couchbaseEntryManager.findEntries("o=jans", SimpleSession.class, filter, SearchScope.SUB, null, null, 10, 0, 0);
LOG.info("Found sessions: " + sessions.size());
List<SimpleGrant> grants = couchbaseEntryManager.findEntries("o=jans", SimpleGrant.class, null, SearchScope.SUB, new String[] { "grtId" }, null, 1, 0, 0);
LOG.info("Found grants: " + grants.size());
try {
PagedResult<SimpleUser> listViewResponse = couchbaseEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "status" }, "uid", SortOrder.ASCENDING, 0, 6, 4);
LOG.info("Found persons: " + listViewResponse.getEntriesCount() + ", total persons: " + listViewResponse.getTotalEntriesCount());
for (SimpleUser user : listViewResponse.getEntries()) {
System.out.println(user.getUserId());
}
} catch (Exception ex) {
LOG.info("Failed to search", ex);
}
try {
PagedResult<SimpleUser> listViewResponse = couchbaseEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "status" }, "uid", SortOrder.DESCENDING, 0, 6, 4);
LOG.info("Found persons: " + listViewResponse.getEntriesCount() + ", total persons: " + listViewResponse.getTotalEntriesCount());
for (SimpleUser user : listViewResponse.getEntries()) {
System.out.println(user.getUserId());
}
} catch (Exception ex) {
LOG.info("Failed to search", ex);
}
}
use of io.jans.orm.couchbase.model.SimpleUser in project jans by JanssenProject.
the class CouchbaseUpdateAttributeSample method main.
public static void main(String[] args) {
// Prepare sample connection details
CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
// Create Couchbase entry manager
CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
String uid = "sample_user_" + System.currentTimeMillis();
String dn = String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis());
SimpleUser newUser = new SimpleUser();
newUser.setDn(dn);
newUser.setUserId(uid);
newUser.setUserPassword("test");
couchbaseEntryManager.persist(newUser);
SimpleUser user = couchbaseEntryManager.find(SimpleUser.class, dn);
LOG.info("Found user '{}'", user);
CustomEntry customEntry = new CustomEntry();
customEntry.setDn(user.getDn());
customEntry.setCustomObjectClasses(new String[] { "jansPerson" });
Date now = new GregorianCalendar(TimeZone.getTimeZone("UTC")).getTime();
String nowDateString = couchbaseEntryManager.encodeTime(customEntry.getDn(), now);
CustomAttribute customAttribute = new CustomAttribute("jansLastLogonTime", nowDateString);
customEntry.getCustomAttributes().add(customAttribute);
couchbaseEntryManager.merge(customEntry);
SimpleUser userAfterUpdate = couchbaseEntryManager.find(SimpleUser.class, dn);
LOG.info("Found user after update '{}'", userAfterUpdate);
}
use of io.jans.orm.couchbase.model.SimpleUser in project jans by JanssenProject.
the class CouchbaseCustomObjectAttributesSample method main.
public static void main(String[] args) {
// Prepare sample connection details
CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
// Create SQL entry manager
CouchbaseEntryManager sqlEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
// Add dummy user
SimpleUser newUser = new SimpleUser();
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 CustomObjectAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
newUser.getCustomAttributes().add(new CustomObjectAttribute("jansGuid", "test_value"));
newUser.getCustomAttributes().add(new CustomObjectAttribute("birthdate", new Date()));
newUser.getCustomAttributes().add(new CustomObjectAttribute("jansActive", false));
// Require cusom attribute in table with age: INT type
newUser.getCustomAttributes().add(new CustomObjectAttribute("scimCustomThird", 18));
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
SimpleUser foundUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
for (CustomObjectAttribute customAttribute : foundUser.getCustomAttributes()) {
if (customAttribute.getValue() instanceof Date) {
LOG.info("Found date custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Integer) {
LOG.info("Found integer custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Boolean) {
LOG.info("Found boolean custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValues().size() > 1) {
LOG.info("Found list custom attribute '{}' with value '{}', multiValued: {}", customAttribute.getName(), customAttribute.getValues(), customAttribute.isMultiValued());
}
}
for (Iterator<CustomObjectAttribute> it = foundUser.getCustomAttributes().iterator(); it.hasNext(); ) {
CustomObjectAttribute attr = (CustomObjectAttribute) it.next();
if (StringHelper.equalsIgnoreCase(attr.getName(), "jansGuid")) {
attr.setValue("");
break;
}
}
sqlEntryManager.merge(foundUser);
// Find updated dummy user
SimpleUser foundUser2 = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser2, foundUser2.getUserId(), foundUser2.getDn());
LOG.info("Custom attributes after merge '{}'", foundUser2.getCustomAttributes());
for (CustomObjectAttribute customAttribute : foundUser2.getCustomAttributes()) {
if (customAttribute.getValue() instanceof Date) {
LOG.info("Found date custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Integer) {
LOG.info("Found integer custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Boolean) {
LOG.info("Found boolean custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValues().size() > 1) {
LOG.info("Found list custom attribute '{}' with value '{}', multiValued: {}", customAttribute.getName(), customAttribute.getValues(), customAttribute.isMultiValued());
}
}
// Find added dummy user by numeric attribute
Filter filter = Filter.createGreaterOrEqualFilter("scimCustomThird", 16);
List<SimpleUser> foundUsers = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filter);
if (foundUsers.size() > 0) {
foundUser = foundUsers.get(0);
LOG.info("Found User '{}' by filter '{}' with uid '{}' and key '{}'", foundUser, filter, foundUser, foundUser);
} else {
LOG.error("Can't find User by filter '{}'", filter);
}
}
use of io.jans.orm.couchbase.model.SimpleUser in project jans by JanssenProject.
the class CouchbaseUserSearchSample method main.
public static void main(String[] args) throws InterruptedException {
// Prepare sample connection details
CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
final CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
int countUsers = 1000000;
int threadCount = 200;
int threadIterationCount = 10;
long totalStart = System.currentTimeMillis();
try {
ExecutorService executorService = Executors.newFixedThreadPool(threadCount, daemonThreadFactory());
for (int i = 0; i < threadCount; i++) {
activeCount.incrementAndGet();
final int count = i;
executorService.execute(new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
for (int j = 0; j < threadIterationCount; j++) {
long userUid = Math.round(Math.random() * countUsers);
String uid = String.format("user%06d", userUid);
try {
Filter filter = Filter.createEqualityFilter(Filter.createLowercaseFilter("uid"), StringHelper.toLowerCase(uid));
// Filter filter = Filter.createEqualityFilter("uid", uid);
List<SimpleUser> foundUsers = couchbaseEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filter);
if (foundUsers.size() > 0) {
successResult.incrementAndGet();
} else {
LOG.warn("Failed to find user: " + uid);
failedResult.incrementAndGet();
}
} catch (Throwable e) {
errorResult.incrementAndGet();
System.out.println("ERROR !!!, thread: " + count + ", uid: " + uid + ", error:" + e.getMessage());
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
long duration = end - start;
LOG.info("Thread " + count + " execution time: " + duration);
totalTime.addAndGet(duration);
activeCount.decrementAndGet();
}
});
}
while (activeCount.get() != 0) {
Thread.sleep(1000L);
}
} finally {
couchbaseEntryManager.destroy();
}
long totalEnd = System.currentTimeMillis();
long duration = totalEnd - totalStart;
LOG.info("Total execution time: " + duration + " after execution: " + (threadCount * threadIterationCount));
System.out.println(String.format("successResult: '%d', failedResult: '%d', errorResult: '%d'", successResult.get(), failedResult.get(), errorResult.get()));
}
Aggregations