use of io.jans.orm.cloud.spanner.impl.SpannerEntryManager in project jans by JanssenProject.
the class SpannerSample method main.
public static void main(String[] args) {
// Prepare sample connection details
SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
// Create SQL entry manager
SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
SimpleUser newUser = new SimpleUser();
newUser.setDn(String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis()));
newUser.setUserId("sample_user_" + System.currentTimeMillis());
newUser.setUserPassword("pwd");
newUser.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
newUser.getCustomAttributes().add(new CustomObjectAttribute("transientId", "transientId"));
newUser.getCustomAttributes().add(new CustomObjectAttribute("memberOf", Arrays.asList("1", "2", "3", "3")));
sqlEntryManager.persist(newUser);
SimpleUser dummyUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Dummy User '{}'", dummyUser);
String[] values = new String[] { "Somewhere: " + System.currentTimeMillis(), "Somewhere2: " + System.currentTimeMillis() };
dummyUser.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList(values)));
dummyUser.getCustomAttributes().add(new CustomObjectAttribute("transientId", "new_transientId"));
dummyUser.getCustomAttributes().add(new CustomObjectAttribute("jansGuid", "test_guid"));
dummyUser.getCustomAttributes().add(new CustomObjectAttribute("memberOf", Arrays.asList("1", "9")));
dummyUser.setUserId("user1");
dummyUser.setUserPassword("test_pwd");
sqlEntryManager.merge(dummyUser);
/*
Filter filterX1 = Filter.createEqualityFilter("transientId", "new_transientId");
List<SimpleUser> usersss = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filterX1);
System.out.println(usersss.size());
*/
Filter filterX = Filter.createEqualityFilter("memberOf", "1");
List<SimpleUser> userss = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filterX, new String[] { "memberOf", "uid" }, 10);
System.out.println(userss.size());
Filter filterR = Filter.createEqualityFilter("jansGuid", "test_guid");
int removed = sqlEntryManager.remove("ou=people,o=jans", SimpleUser.class, filterR, 10);
System.out.println(removed);
// sqlEntryManager.remove(dummyUser);
sqlEntryManager.destroy();
System.exit(0);
// Find all users which have specified object classes defined in SimpleUser
List<SimpleUser> users = sqlEntryManager.findEntries("ou=people,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 "address" to first user
SimpleUser user = users.get(0);
LOG.info("Updating: " + user.getUserId());
String[] values = new String[] { "Somewhere: " + System.currentTimeMillis(), "Somewhere2: " + System.currentTimeMillis() };
user.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList(values)));
user.getCustomAttributes().add(new CustomObjectAttribute("transientId", "new_transientId"));
user.getCustomAttributes().add(new CustomObjectAttribute("jansGuid", "test_guid"));
user.setUserId("user1");
user.setUserPassword("test_pwd");
sqlEntryManager.merge(user);
}
*/
for (SimpleUser user : users) {
boolean result1 = sqlEntryManager.authenticate(user.getDn(), SimpleUser.class, "test_pwd");
boolean result2 = sqlEntryManager.authenticate("ou=people,o=jans", SimpleUser.class, user.getUserId(), "test");
System.out.println("authetication result: " + result1 + ", " + result2);
}
Filter filter = Filter.createEqualityFilter("jansStatus", "active");
List<SimpleAttribute> attributes = sqlEntryManager.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));
}
Filter filter2 = Filter.createEqualityFilter("jansState", "authenticated");
List<SimpleSession> sessions = sqlEntryManager.findEntries("o=jans", SimpleSession.class, filter2, SearchScope.SUB, null, null, 10, 0, 0);
LOG.info("Found sessions: " + sessions.size());
List<SimpleGrant> grants = sqlEntryManager.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 = sqlEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "jansStatus" }, "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 = sqlEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "jansStatus" }, "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.cloud.spanner.impl.SpannerEntryManager in project jans by JanssenProject.
the class SpannerUpateCacheEntrySample 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 key = UUID.randomUUID().toString();
final String cacheDn = String.format("uuid=%s,%s", key, "ou=cache,o=gluu");
int expirationInSeconds = 60;
Calendar expirationDate = Calendar.getInstance();
expirationDate.setTime(new Date());
expirationDate.add(Calendar.SECOND, expirationInSeconds);
SimpleCacheEntry entity = new SimpleCacheEntry();
entity.setTtl(expirationInSeconds);
entity.setData("sample_data");
entity.setId(key);
entity.setDn(cacheDn);
entity.setCreationDate(new Date());
entity.setExpirationDate(expirationDate.getTime());
entity.setDeletable(true);
sqlEntryManager.persist(entity);
// Try to update
sqlEntryManager.merge(entity);
// Try to update with removed value
entity.setData(null);
sqlEntryManager.merge(entity);
}
use of io.jans.orm.cloud.spanner.impl.SpannerEntryManager in project jans by JanssenProject.
the class SpannerConcurentSessionUpdateSample method main.
public static void main(String[] args) throws InterruptedException {
// Prepare sample connection details
SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
// Create SQL entry manager
final SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
try {
String sessionId = UUID.randomUUID().toString();
final String sessionDn = "uniqueIdentifier=" + sessionId + ",ou=session,o=jans";
final String userDn = "inum=@!E8F2.853B.1E7B.ACE2!0001!39A4.C163!0000!A8F2.DE1E.D7FB,ou=people,o=jans";
final SimpleSessionState simpleSessionState = new SimpleSessionState();
simpleSessionState.setDn(sessionDn);
simpleSessionState.setId(sessionId);
simpleSessionState.setLastUsedAt(new Date());
sqlEntryManager.persist(simpleSessionState);
System.out.println("Persisted");
int threadCount = 500;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount, daemonThreadFactory());
for (int i = 0; i < threadCount; i++) {
final int count = i;
executorService.execute(new Runnable() {
@Override
public void run() {
final SimpleSessionState simpleSessionStateFromSQL = sqlEntryManager.find(SimpleSessionState.class, sessionDn);
String beforeUserDn = simpleSessionStateFromSQL.getUserDn();
String randomUserDn = count % 2 == 0 ? userDn : "";
try {
simpleSessionStateFromSQL.setUserDn(randomUserDn);
simpleSessionStateFromSQL.setLastUsedAt(new Date());
sqlEntryManager.merge(simpleSessionStateFromSQL);
System.out.println("Merged thread: " + count + ", userDn: " + randomUserDn + ", before userDn: " + beforeUserDn);
} catch (Throwable e) {
System.out.println("ERROR !!!, thread: " + count + ", userDn: " + randomUserDn + ", before userDn: " + beforeUserDn + ", error:" + e.getMessage());
// e.printStackTrace();
}
}
});
}
Thread.sleep(5000L);
} finally {
sqlEntryManager.destroy();
}
}
use of io.jans.orm.cloud.spanner.impl.SpannerEntryManager 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.cloud.spanner.impl.SpannerEntryManager 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);
}
Aggregations