Search in sources :

Example 11 with LdapEntryManager

use of org.gluu.persist.ldap.impl.LdapEntryManager in project oxCore by GluuFederation.

the class LdapSampleBatchJob method main.

public static void main(String[] args) {
    // Prepare sample connection details
    LdapSampleEntryManager ldapSampleEntryManager = new LdapSampleEntryManager();
    // Create LDAP entry manager
    final LdapEntryManager ldapEntryManager = ldapSampleEntryManager.createLdapEntryManager();
    BatchOperation<SimpleTokenLdap> tokenLdapBatchOperation = new ProcessBatchOperation<SimpleTokenLdap>() {

        private int processedCount = 0;

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

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleSession> objects) {
            for (SimpleSession simpleSession : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute(ldapEntryManager, "oxLastAccessTime", simpleSession.getAttribute("oxLastAccessTime"));
                    simpleSession.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    ldapEntryManager.merge(simpleSession);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    LOG.error("Failed to update entry", ex);
                }
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter2 = Filter.createPresenceFilter("oxLastAccessTime");
    ldapEntryManager.findEntries("o=gluu", SimpleSession.class, filter2, SearchScope.SUB, new String[] { "oxLastAccessTime" }, 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: " + processedCount);
        }
    };
    final Filter filter3 = Filter.createPresenceFilter("oxAuthClientSecretExpiresAt");
    List<SimpleClient> result3 = ldapEntryManager.findEntries("o=gluu", SimpleClient.class, filter3, SearchScope.SUB, new String[] { "oxAuthClientSecretExpiresAt" }, 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: " + processedCount);
        }
    };
    final Filter filter4 = Filter.createPresenceFilter("oxAuthClientSecretExpiresAt");
    List<SimpleClient> result4 = ldapEntryManager.findEntries("o=gluu", SimpleClient.class, filter4, SearchScope.SUB, new String[] { "oxAuthClientSecretExpiresAt" }, clientBatchOperation2, 0, 0, 1000);
    LOG.info("Result count (with collecting results): " + result4.size());
}
Also used : SimpleTokenLdap(org.gluu.ldap.model.SimpleTokenLdap) CustomAttribute(org.gluu.persist.model.base.CustomAttribute) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) DefaultBatchOperation(org.gluu.persist.model.DefaultBatchOperation) LdapEntryManager(org.gluu.persist.ldap.impl.LdapEntryManager) Filter(org.gluu.search.filter.Filter) ProcessBatchOperation(org.gluu.persist.model.ProcessBatchOperation) List(java.util.List) SimpleClient(org.gluu.ldap.model.SimpleClient) SimpleSession(org.gluu.ldap.model.SimpleSession)

Example 12 with LdapEntryManager

use of org.gluu.persist.ldap.impl.LdapEntryManager in project oxCore by GluuFederation.

the class LdapSampleSimpleSessionSample method main.

public static void main(String[] args) throws InterruptedException {
    // Prepare sample connection details
    LdapSampleEntryManager ldapSampleEntryManager = new LdapSampleEntryManager();
    final LdapEntryManager ldapEntryManager = ldapSampleEntryManager.createLdapEntryManager();
    try {
        // Create LDAP entry manager
        String sessionId = "xyzcyzxy-a41a-45ad-8a83-61485dbad561";
        final String sessionDn = "uniqueIdentifier=" + sessionId + ",ou=session,o=@!E8F2.853B.1E7B.ACE2!0001!39A4.C163,o=gluu";
        final String userDn = "inum=@!E8F2.853B.1E7B.ACE2!0001!39A4.C163!0000!A8F2.DE1E.D7FB,ou=people,o=@!E8F2.853B.1E7B.ACE2!0001!39A4.C163,o=gluu";
        final SimpleSessionState simpleSessionState = new SimpleSessionState();
        simpleSessionState.setDn(sessionDn);
        simpleSessionState.setId(sessionId);
        simpleSessionState.setLastUsedAt(new Date());
        ldapEntryManager.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 simpleSessionStateFromLdap = ldapEntryManager.find(SimpleSessionState.class, sessionDn);
                    String beforeUserDn = simpleSessionStateFromLdap.getUserDn();
                    String randomUserDn = count % 2 == 0 ? userDn : "";
                    try {
                        simpleSessionStateFromLdap.setUserDn(randomUserDn);
                        simpleSessionStateFromLdap.setLastUsedAt(new Date());
                        ldapEntryManager.merge(simpleSessionStateFromLdap);
                        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 {
        ldapEntryManager.getOperationService().getConnectionPool().close();
    }
}
Also used : SimpleSessionState(org.gluu.ldap.model.SimpleSessionState) LdapEntryManager(org.gluu.persist.ldap.impl.LdapEntryManager) ExecutorService(java.util.concurrent.ExecutorService) Date(java.util.Date)

Example 13 with LdapEntryManager

use of org.gluu.persist.ldap.impl.LdapEntryManager in project oxTrust by GluuFederation.

the class ConfigurationFactory method loadConfigurationFromLdap.

public LdapOxTrustConfiguration loadConfigurationFromLdap(String... returnAttributes) {
    final LdapEntryManager ldapEntryManager = ldapEntryManagerInstance.get();
    final String configurationDn = getConfigurationDn();
    try {
        final LdapOxTrustConfiguration conf = ldapEntryManager.find(LdapOxTrustConfiguration.class, configurationDn, returnAttributes);
        return conf;
    } catch (BaseMappingException ex) {
        log.error("Failed to load configuration from LDAP", ex);
    }
    return null;
}
Also used : BaseMappingException(org.gluu.persist.exception.mapping.BaseMappingException) LdapOxTrustConfiguration(org.xdi.config.oxtrust.LdapOxTrustConfiguration) LdapEntryManager(org.gluu.persist.ldap.impl.LdapEntryManager)

Example 14 with LdapEntryManager

use of org.gluu.persist.ldap.impl.LdapEntryManager in project oxTrust by GluuFederation.

the class CacheRefreshTimer method updateTargetEntriesViaVDS.

private List<String> updateTargetEntriesViaVDS(CacheRefreshConfiguration cacheRefreshConfiguration, LdapServerConnection targetServerConnection, Set<String> changedInums) {
    List<String> result = new ArrayList<String>();
    LdapEntryManager targetLdapEntryManager = targetServerConnection.getLdapEntryManager();
    Filter filter = cacheRefreshService.createObjectClassPresenceFilter();
    for (String changedInum : changedInums) {
        String baseDn = "action=synchronizecache," + personService.getDnForPerson(changedInum);
        try {
            targetLdapEntryManager.findEntries(baseDn, GluuDummyEntry.class, filter, SearchScope.SUB, null, 0, cacheRefreshConfiguration.getLdapSearchSizeLimit());
            result.add(changedInum);
            log.debug("Updated entry with inum {}", changedInum);
        } catch (BaseMappingException ex) {
            log.error("Failed to update entry with inum '{}' using baseDN {}", changedInum, baseDn, ex);
        }
    }
    return result;
}
Also used : BaseMappingException(org.gluu.persist.exception.mapping.BaseMappingException) LdapEntryManager(org.gluu.persist.ldap.impl.LdapEntryManager) Filter(org.gluu.search.filter.Filter) ArrayList(java.util.ArrayList)

Example 15 with LdapEntryManager

use of org.gluu.persist.ldap.impl.LdapEntryManager in project oxTrust by GluuFederation.

the class CacheRefreshTimer method loadSourceServerEntriesWithoutLimits.

private List<GluuSimplePerson> loadSourceServerEntriesWithoutLimits(CacheRefreshConfiguration cacheRefreshConfiguration, LdapServerConnection[] sourceServerConnections) {
    Filter customFilter = cacheRefreshService.createFilter(cacheRefreshConfiguration.getCustomLdapFilter());
    String[] keyAttributes = getCompoundKeyAttributes(cacheRefreshConfiguration);
    String[] keyAttributesWithoutValues = getCompoundKeyAttributesWithoutValues(cacheRefreshConfiguration);
    String[] keyObjectClasses = getCompoundKeyObjectClasses(cacheRefreshConfiguration);
    String[] sourceAttributes = getSourceAttributes(cacheRefreshConfiguration);
    String[] returnAttributes = ArrayHelper.arrayMerge(keyAttributesWithoutValues, sourceAttributes);
    Set<String> addedDns = new HashSet<String>();
    List<GluuSimplePerson> sourcePersons = new ArrayList<GluuSimplePerson>();
    for (LdapServerConnection sourceServerConnection : sourceServerConnections) {
        String sourceServerName = sourceServerConnection.getSourceServerName();
        LdapEntryManager sourceLdapEntryManager = sourceServerConnection.getLdapEntryManager();
        String[] baseDns = sourceServerConnection.getBaseDns();
        Filter filter = cacheRefreshService.createFilter(keyAttributes, keyObjectClasses, "", customFilter);
        if (log.isTraceEnabled()) {
            log.trace("Using next filter to load entris from source server: {}", filter);
        }
        for (String baseDn : baseDns) {
            List<GluuSimplePerson> currentSourcePersons = sourceLdapEntryManager.findEntries(baseDn, GluuSimplePerson.class, filter, SearchScope.SUB, returnAttributes, 0, cacheRefreshConfiguration.getLdapSearchSizeLimit());
            // Add to result and ignore root entry if needed
            for (GluuSimplePerson currentSourcePerson : currentSourcePersons) {
                currentSourcePerson.setSourceServerName(sourceServerName);
                // if (!StringHelper.equalsIgnoreCase(baseDn,
                // currentSourcePerson.getDn())) {
                String currentSourcePersonDn = currentSourcePerson.getDn().toLowerCase();
                if (!addedDns.contains(currentSourcePersonDn)) {
                    sourcePersons.add(currentSourcePerson);
                    addedDns.add(currentSourcePersonDn);
                }
            // }
            }
        }
    }
    return sourcePersons;
}
Also used : GluuSimplePerson(org.gluu.oxtrust.ldap.cache.model.GluuSimplePerson) LdapEntryManager(org.gluu.persist.ldap.impl.LdapEntryManager) Filter(org.gluu.search.filter.Filter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

LdapEntryManager (org.gluu.persist.ldap.impl.LdapEntryManager)20 Filter (org.gluu.search.filter.Filter)6 Properties (java.util.Properties)5 GluuSimplePerson (org.gluu.oxtrust.ldap.cache.model.GluuSimplePerson)5 ArrayList (java.util.ArrayList)4 GluuInumMap (org.gluu.oxtrust.ldap.cache.model.GluuInumMap)3 BaseMappingException (org.gluu.persist.exception.mapping.BaseMappingException)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Produces (javax.enterprise.inject.Produces)2 Named (javax.inject.Named)2 SimpleSession (org.gluu.ldap.model.SimpleSession)2 CacheCompoundKey (org.gluu.oxtrust.ldap.cache.model.CacheCompoundKey)2 LdapEntryManagerFactory (org.gluu.persist.ldap.impl.LdapEntryManagerFactory)2 CustomAttribute (org.gluu.persist.model.base.CustomAttribute)2 FileConfiguration (org.xdi.util.properties.FileConfiguration)2 List (java.util.List)1