Search in sources :

Example 6 with Transformer

use of org.apache.commons.collections.Transformer in project sling by apache.

the class FileResourceMapper method getChildren.

@SuppressWarnings("unchecked")
@Override
public Iterator<Resource> getChildren(final ResourceResolver resolver, final Resource parent) {
    final String parentPath = parent.getPath();
    File parentFile = parent.adaptTo(File.class);
    // not a FsResource, try to create one from the resource
    if (parentFile == null) {
        // if the parent path is at or below the provider root, get
        // the respective file
        parentFile = getFile(parentPath);
        // a repository item with the same path actually exists
        if (parentFile == null) {
            if (providerFile.exists() && !StringUtils.startsWith(parentPath, providerRoot)) {
                String parentPathPrefix = parentPath.concat("/");
                if (providerRoot.startsWith(parentPathPrefix)) {
                    String relPath = providerRoot.substring(parentPathPrefix.length());
                    if (relPath.indexOf('/') < 0) {
                        Resource res = new FileResource(resolver, providerRoot, providerFile, contentFileExtensions, contentFileCache);
                        return IteratorUtils.singletonIterator(res);
                    }
                }
            }
            // no children here
            return null;
        }
    }
    // ensure parent is a directory
    if (!parentFile.isDirectory()) {
        return null;
    }
    Iterator<File> children = IteratorUtils.filteredIterator(IteratorUtils.arrayIterator(parentFile.listFiles()), new Predicate() {

        @Override
        public boolean evaluate(Object object) {
            File file = (File) object;
            return !contentFileExtensions.matchesSuffix(file);
        }
    });
    if (!children.hasNext()) {
        return null;
    }
    return IteratorUtils.transformedIterator(children, new Transformer() {

        @Override
        public Object transform(Object input) {
            File file = (File) input;
            String path = parentPath + "/" + file.getName();
            return new FileResource(resolver, path, file, contentFileExtensions, contentFileCache);
        }
    });
}
Also used : Transformer(org.apache.commons.collections.Transformer) Resource(org.apache.sling.api.resource.Resource) File(java.io.File) Predicate(org.apache.commons.collections.Predicate)

Example 7 with Transformer

use of org.apache.commons.collections.Transformer in project ddf by codice.

the class SolrMetacardClientImpl method deleteByIds.

@Override
public void deleteByIds(String fieldName, List<? extends Serializable> identifiers, boolean forceCommit) throws IOException, SolrServerException {
    if (identifiers == null || identifiers.size() == 0) {
        return;
    }
    if (Metacard.ID.equals(fieldName)) {
        CollectionUtils.transform(identifiers, new Transformer() {

            @Override
            public Object transform(Object o) {
                return o.toString();
            }
        });
        client.deleteById((List<String>) identifiers);
    } else {
        if (identifiers.size() < SolrCatalogProvider.MAX_BOOLEAN_CLAUSES) {
            client.deleteByQuery(getIdentifierQuery(fieldName, identifiers));
        } else {
            int i = 0;
            for (i = SolrCatalogProvider.MAX_BOOLEAN_CLAUSES; i < identifiers.size(); i += SolrCatalogProvider.MAX_BOOLEAN_CLAUSES) {
                client.deleteByQuery(getIdentifierQuery(fieldName, identifiers.subList(i - SolrCatalogProvider.MAX_BOOLEAN_CLAUSES, i)));
            }
            client.deleteByQuery(getIdentifierQuery(fieldName, identifiers.subList(i - SolrCatalogProvider.MAX_BOOLEAN_CLAUSES, identifiers.size())));
        }
    }
    if (forceCommit) {
        client.commit();
    }
}
Also used : Transformer(org.apache.commons.collections.Transformer)

Example 8 with Transformer

use of org.apache.commons.collections.Transformer in project pentaho-platform by pentaho.

the class DefaultLdapUserRoleListServiceTest method testGetAllUserNames1Sorted.

/**
 * Same as above except sorted.
 */
@Test
public void testGetAllUserNames1Sorted() throws Exception {
    SearchControls con1 = new SearchControls();
    // $NON-NLS-1$
    con1.setReturningAttributes(new String[] { "uniqueMember" });
    LdapSearchParamsFactoryImpl paramFactory = // $NON-NLS-1$//$NON-NLS-2$
    new LdapSearchParamsFactoryImpl("ou=groups", "(objectClass=groupOfUniqueNames)", con1);
    paramFactory.afterPropertiesSet();
    // $NON-NLS-1$ //$NON-NLS-2$
    Transformer transformer1 = new SearchResultToAttrValueList("uniqueMember", "uid");
    GenericLdapSearch allUsernamesSearch = new GenericLdapSearch(getContextSource(), paramFactory, transformer1);
    allUsernamesSearch.afterPropertiesSet();
    DefaultLdapUserRoleListService userRoleListService = getDefaultLdapUserRoleListService();
    userRoleListService.setAllUsernamesSearch(allUsernamesSearch);
    userRoleListService.setUsernameComparator(new DefaultUsernameComparator());
    List res = userRoleListService.getAllUsers();
    assertTrue(res.indexOf("pat") < res.indexOf("tiffany"));
    if (logger.isDebugEnabled()) {
        // $NON-NLS-1$
        logger.debug("results of getAllUserNames1Sorted(): " + res);
    }
}
Also used : DefaultUsernameComparator(org.pentaho.platform.engine.security.DefaultUsernameComparator) DefaultLdapUserRoleListService(org.pentaho.platform.plugin.services.security.userrole.ldap.DefaultLdapUserRoleListService) ChainedTransformer(org.apache.commons.collections.functors.ChainedTransformer) Transformer(org.apache.commons.collections.Transformer) SearchControls(javax.naming.directory.SearchControls) SearchResultToAttrValueList(org.pentaho.platform.plugin.services.security.userrole.ldap.transform.SearchResultToAttrValueList) GenericLdapSearch(org.pentaho.platform.plugin.services.security.userrole.ldap.search.GenericLdapSearch) ArrayList(java.util.ArrayList) SearchResultToAttrValueList(org.pentaho.platform.plugin.services.security.userrole.ldap.transform.SearchResultToAttrValueList) List(java.util.List) LdapSearchParamsFactoryImpl(org.pentaho.platform.plugin.services.security.userrole.ldap.search.LdapSearchParamsFactoryImpl) Test(org.junit.Test)

Example 9 with Transformer

use of org.apache.commons.collections.Transformer in project pentaho-platform by pentaho.

the class DefaultLdapUserRoleListServiceTest method testGetAllAuthorities1.

/**
 * Search for all roles (aka authorities) starting at <code>ou=roles</code>, looking for objects with
 * <code>objectClass=organizationalRole</code>, and returning the <code>cn</code> attribute.
 */
@Test
public void testGetAllAuthorities1() {
    SearchControls con1 = new SearchControls();
    // $NON-NLS-1$
    con1.setReturningAttributes(new String[] { "cn" });
    LdapSearchParamsFactory paramsFactory = // $NON-NLS-1$//$NON-NLS-2$
    new LdapSearchParamsFactoryImpl("ou=roles", "(objectClass=organizationalRole)", con1);
    // $NON-NLS-1$
    Transformer one = new SearchResultToAttrValueList("cn");
    Transformer two = new StringToGrantedAuthority();
    Transformer[] transformers = { one, two };
    Transformer transformer = new ChainedTransformer(transformers);
    LdapSearch rolesSearch = new GenericLdapSearch(getContextSource(), paramsFactory, transformer);
    DefaultLdapUserRoleListService userRoleListService = getDefaultLdapUserRoleListService();
    userRoleListService.setAllAuthoritiesSearch(rolesSearch);
    List res = userRoleListService.getAllRoles();
    // $NON-NLS-1$
    assertTrue(res.contains("ROLE_CTO"));
    // $NON-NLS-1$
    assertTrue(res.contains("ROLE_CEO"));
    if (logger.isDebugEnabled()) {
        // $NON-NLS-1$
        logger.debug("results of getAllAuthorities1(): " + res);
    }
}
Also used : DefaultLdapUserRoleListService(org.pentaho.platform.plugin.services.security.userrole.ldap.DefaultLdapUserRoleListService) LdapSearchParamsFactory(org.pentaho.platform.plugin.services.security.userrole.ldap.search.LdapSearchParamsFactory) ChainedTransformer(org.apache.commons.collections.functors.ChainedTransformer) Transformer(org.apache.commons.collections.Transformer) StringToGrantedAuthority(org.pentaho.platform.plugin.services.security.userrole.ldap.transform.StringToGrantedAuthority) ChainedTransformer(org.apache.commons.collections.functors.ChainedTransformer) SearchControls(javax.naming.directory.SearchControls) SearchResultToAttrValueList(org.pentaho.platform.plugin.services.security.userrole.ldap.transform.SearchResultToAttrValueList) GenericLdapSearch(org.pentaho.platform.plugin.services.security.userrole.ldap.search.GenericLdapSearch) ArrayList(java.util.ArrayList) SearchResultToAttrValueList(org.pentaho.platform.plugin.services.security.userrole.ldap.transform.SearchResultToAttrValueList) List(java.util.List) UnionizingLdapSearch(org.pentaho.platform.plugin.services.security.userrole.ldap.search.UnionizingLdapSearch) LdapSearch(org.pentaho.platform.plugin.services.security.userrole.ldap.search.LdapSearch) GenericLdapSearch(org.pentaho.platform.plugin.services.security.userrole.ldap.search.GenericLdapSearch) LdapSearchParamsFactoryImpl(org.pentaho.platform.plugin.services.security.userrole.ldap.search.LdapSearchParamsFactoryImpl) Test(org.junit.Test)

Example 10 with Transformer

use of org.apache.commons.collections.Transformer in project pentaho-platform by pentaho.

the class DefaultLdapUserRoleListServiceTest method testGetUsernamesInRole1.

/**
 * Search for all users starting at <code>ou=users</code>, looking for objects with
 * <code>businessCategory=cn={0}*</code>, and returning the <code>uid</code> attribute. This search implies that the
 * schema is setup such that a user's roles come from one of the user's attributes.
 */
@Test
public void testGetUsernamesInRole1() {
    SearchControls con1 = new SearchControls();
    // $NON-NLS-1$
    con1.setReturningAttributes(new String[] { "uid" });
    LdapSearchParamsFactory paramFactory = // $NON-NLS-1$//$NON-NLS-2$
    new LdapSearchParamsFactoryImpl("ou=users", "(businessCategory=cn={0}*)", con1);
    // $NON-NLS-1$
    Transformer transformer1 = new SearchResultToAttrValueList("uid");
    GrantedAuthorityToString transformer2 = new GrantedAuthorityToString();
    LdapSearch usernamesInRoleSearch = new GenericLdapSearch(getContextSource(), paramFactory, transformer1, transformer2);
    DefaultLdapUserRoleListService userRoleListService = getDefaultLdapUserRoleListService();
    userRoleListService.setUsernamesInRoleSearch(usernamesInRoleSearch);
    // $NON-NLS-1$
    List<String> res = userRoleListService.getUsersInRole(null, "DEV");
    // $NON-NLS-1$
    assertTrue(res.contains("pat"));
    // $NON-NLS-1$
    assertTrue(res.contains("tiffany"));
    if (logger.isDebugEnabled()) {
        // $NON-NLS-1$
        logger.debug("results of getUsernamesInRole1(): " + res);
    }
}
Also used : DefaultLdapUserRoleListService(org.pentaho.platform.plugin.services.security.userrole.ldap.DefaultLdapUserRoleListService) LdapSearchParamsFactory(org.pentaho.platform.plugin.services.security.userrole.ldap.search.LdapSearchParamsFactory) ChainedTransformer(org.apache.commons.collections.functors.ChainedTransformer) Transformer(org.apache.commons.collections.Transformer) SearchControls(javax.naming.directory.SearchControls) SearchResultToAttrValueList(org.pentaho.platform.plugin.services.security.userrole.ldap.transform.SearchResultToAttrValueList) GenericLdapSearch(org.pentaho.platform.plugin.services.security.userrole.ldap.search.GenericLdapSearch) GrantedAuthorityToString(org.pentaho.platform.plugin.services.security.userrole.ldap.transform.GrantedAuthorityToString) UnionizingLdapSearch(org.pentaho.platform.plugin.services.security.userrole.ldap.search.UnionizingLdapSearch) LdapSearch(org.pentaho.platform.plugin.services.security.userrole.ldap.search.LdapSearch) GenericLdapSearch(org.pentaho.platform.plugin.services.security.userrole.ldap.search.GenericLdapSearch) GrantedAuthorityToString(org.pentaho.platform.plugin.services.security.userrole.ldap.transform.GrantedAuthorityToString) LdapSearchParamsFactoryImpl(org.pentaho.platform.plugin.services.security.userrole.ldap.search.LdapSearchParamsFactoryImpl) Test(org.junit.Test)

Aggregations

Transformer (org.apache.commons.collections.Transformer)38 ChainedTransformer (org.apache.commons.collections.functors.ChainedTransformer)20 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)16 SearchControls (javax.naming.directory.SearchControls)16 DefaultLdapUserRoleListService (org.pentaho.platform.plugin.services.security.userrole.ldap.DefaultLdapUserRoleListService)16 GenericLdapSearch (org.pentaho.platform.plugin.services.security.userrole.ldap.search.GenericLdapSearch)16 LdapSearchParamsFactoryImpl (org.pentaho.platform.plugin.services.security.userrole.ldap.search.LdapSearchParamsFactoryImpl)16 SearchResultToAttrValueList (org.pentaho.platform.plugin.services.security.userrole.ldap.transform.SearchResultToAttrValueList)16 LdapSearch (org.pentaho.platform.plugin.services.security.userrole.ldap.search.LdapSearch)13 LdapSearchParamsFactory (org.pentaho.platform.plugin.services.security.userrole.ldap.search.LdapSearchParamsFactory)13 UnionizingLdapSearch (org.pentaho.platform.plugin.services.security.userrole.ldap.search.UnionizingLdapSearch)13 List (java.util.List)11 Map (java.util.Map)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 ConstantTransformer (org.apache.commons.collections.functors.ConstantTransformer)5 GrantedAuthorityToString (org.pentaho.platform.plugin.services.security.userrole.ldap.transform.GrantedAuthorityToString)5 StringToGrantedAuthority (org.pentaho.platform.plugin.services.security.userrole.ldap.transform.StringToGrantedAuthority)5 Resource (org.apache.sling.api.resource.Resource)4