Search in sources :

Example 1 with SimpleSession

use of io.jans.orm.sql.model.SimpleSession in project jans by JanssenProject.

the class SqlBatchJobSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    final SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    BatchOperation<SimpleToken> tokenSQLBatchOperation = new ProcessBatchOperation<SimpleToken>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleToken> objects) {
            for (SimpleToken simpleTokenSQL : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute(sqlEntryManager, simpleTokenSQL.getDn(), "exp", simpleTokenSQL.getAttribute("exp"));
                    simpleTokenSQL.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    sqlEntryManager.merge(simpleTokenSQL);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    LOG.error("Failed to update entry", ex);
                }
            }
            LOG.info("Total processed tokens: " + processedCount);
        }
    };
    final Filter filter1 = Filter.createPresenceFilter("exp");
    sqlEntryManager.findEntries("o=jans", SimpleToken.class, filter1, SearchScope.SUB, new String[] { "exp" }, tokenSQLBatchOperation, 0, 0, 100);
    BatchOperation<SimpleSession> sessionBatchOperation = new ProcessBatchOperation<SimpleSession>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleSession> objects) {
            int currentProcessedCount = 0;
            for (SimpleSession simpleSession : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute(sqlEntryManager, simpleSession.getDn(), "jansLastAccessTime", simpleSession.getAttribute("jansLastAccessTime"));
                    simpleSession.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    sqlEntryManager.merge(simpleSession);
                    processedCount++;
                    currentProcessedCount++;
                } catch (EntryPersistenceException ex) {
                    LOG.error("Failed to update entry", ex);
                }
            }
            LOG.info("Currnet batch count processed sessions: " + currentProcessedCount);
            LOG.info("Total processed sessions: " + processedCount);
        }
    };
    final Filter filter2 = Filter.createPresenceFilter("jansLastAccessTime");
    sqlEntryManager.findEntries("o=jans", SimpleSession.class, filter2, SearchScope.SUB, new String[] { "jansLastAccessTime" }, sessionBatchOperation, 0, 0, 100);
    BatchOperation<SimpleClient> clientBatchOperation = new ProcessBatchOperation<SimpleClient>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleClient> objects) {
            for (SimpleClient simpleClient : objects) {
                processedCount++;
            }
            LOG.info("Total processed clients: " + processedCount);
        }
    };
    final Filter filter3 = Filter.createPresenceFilter("exp");
    List<SimpleClient> result3 = sqlEntryManager.findEntries("o=jans", SimpleClient.class, filter3, SearchScope.SUB, new String[] { "exp" }, clientBatchOperation, 0, 0, 1000);
    LOG.info("Result count (without collecting results): " + result3.size());
    BatchOperation<SimpleClient> clientBatchOperation2 = new DefaultBatchOperation<SimpleClient>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleClient> objects) {
            for (SimpleClient simpleClient : objects) {
                processedCount++;
            }
            LOG.info("Total processed clients: " + processedCount);
        }
    };
    final Filter filter4 = Filter.createPresenceFilter("exp");
    List<SimpleClient> result4 = sqlEntryManager.findEntries("o=jans", SimpleClient.class, filter4, SearchScope.SUB, new String[] { "exp" }, clientBatchOperation2, 0, 0, 1000);
    LOG.info("Result count (with collecting results): " + result4.size());
}
Also used : CustomAttribute(io.jans.orm.model.base.CustomAttribute) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) DefaultBatchOperation(io.jans.orm.model.DefaultBatchOperation) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager) Filter(io.jans.orm.search.filter.Filter) ProcessBatchOperation(io.jans.orm.model.ProcessBatchOperation) List(java.util.List) SimpleClient(io.jans.orm.sql.model.SimpleClient) SimpleSession(io.jans.orm.sql.model.SimpleSession) SimpleToken(io.jans.orm.sql.model.SimpleToken)

Example 2 with SimpleSession

use of io.jans.orm.sql.model.SimpleSession in project jans by JanssenProject.

the class SqlSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    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"));
    sqlEntryManager.persist(newUser);
    SimpleUser dummyUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
    LOG.info("Dummy User '{}'", dummyUser);
    // 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);
    }
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) SimpleUser(io.jans.orm.sql.model.SimpleUser) SimpleAttribute(io.jans.orm.sql.model.SimpleAttribute) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager) SimpleGrant(io.jans.orm.sql.model.SimpleGrant) Filter(io.jans.orm.search.filter.Filter) SimpleSession(io.jans.orm.sql.model.SimpleSession)

Aggregations

Filter (io.jans.orm.search.filter.Filter)2 SqlEntryManager (io.jans.orm.sql.impl.SqlEntryManager)2 SimpleSession (io.jans.orm.sql.model.SimpleSession)2 SqlEntryManagerSample (io.jans.orm.sql.persistence.SqlEntryManagerSample)2 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)1 DefaultBatchOperation (io.jans.orm.model.DefaultBatchOperation)1 ProcessBatchOperation (io.jans.orm.model.ProcessBatchOperation)1 CustomAttribute (io.jans.orm.model.base.CustomAttribute)1 CustomObjectAttribute (io.jans.orm.model.base.CustomObjectAttribute)1 SimpleAttribute (io.jans.orm.sql.model.SimpleAttribute)1 SimpleClient (io.jans.orm.sql.model.SimpleClient)1 SimpleGrant (io.jans.orm.sql.model.SimpleGrant)1 SimpleToken (io.jans.orm.sql.model.SimpleToken)1 SimpleUser (io.jans.orm.sql.model.SimpleUser)1 List (java.util.List)1