Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAK-11100: remove use of Guava transform/filter #1723

Draft
wants to merge 21 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
cef0d98
OAK-11100: remove use of Guava transform/filter - oak-auth-external
reschke Sep 14, 2024
4a0c445
OAK-11100: remove use of Guava transform/filter - oak-auth-ldap
reschke Sep 14, 2024
340990f
OAK-11100: remove use of Guava transform/filter - oak-authorization-cug
reschke Sep 14, 2024
38aaec2
OAK-11100: remove use of Guava transform/filter - oak-authorization-p…
reschke Sep 14, 2024
e6a9e62
OAK-11100: remove use of Guava transform/filter - oak-blob-cloud
reschke Sep 15, 2024
f0dfd55
OAK-11100: remove use of Guava transform/filter - oak-blob-plugins
reschke Sep 16, 2024
c78df7d
OAK-11100: remove use of Guava transform/filter - oak-commons
reschke Sep 16, 2024
b512c55
OAK-11100: remove use of Guava transform/filter - oak-core-spi
reschke Sep 16, 2024
5853b9c
OAK-11100: remove use of Guava transform/filter - oak-core
reschke Sep 16, 2024
804e154
OAK-11100: remove use of Guava transform/filter - oak-exercise
reschke Sep 16, 2024
76cc28b
OAK-11100: remove use of Guava transform/filter - oak-it
reschke Sep 16, 2024
ad6ed68
OAK-11100: remove use of Guava transform/filter - oak-jcr
reschke Sep 16, 2024
c984958
OAK-11100: remove use of Guava transform/filter - oak-lucene
reschke Sep 17, 2024
f07a446
OAK-11100: remove use of Guava transform/filter - oak-run-commons
reschke Sep 17, 2024
582caf4
OAK-11100: remove use of Guava transform/filter - oak-run
reschke Sep 17, 2024
f141294
OAK-11100: remove use of Guava transform/filter - oak-search
reschke Sep 20, 2024
ca85045
Merge branch 'trunk' into OAK-11100
reschke Sep 20, 2024
68000c1
OAK-11100: remove use of Guava transform/filter - oak-segment-tar
reschke Sep 20, 2024
3edf33b
OAK-11100: remove use of Guava transform/filter - oak-store-composite
reschke Sep 21, 2024
e74d23e
OAK-11100: remove use of Guava transform/filter - oak-store-document …
reschke Sep 24, 2024
275faec
Merge branch 'trunk' into OAK-11100
reschke Sep 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalGroup;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentity;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityException;
Expand Down Expand Up @@ -234,7 +235,8 @@ private Map<ExternalIdentityRef, SyncEntry> collectSyncEntries(@NotNull Iterable
*/
private void collectSyncEntries(@NotNull Iterable<ExternalIdentityRef> declaredGroupRefs, long depth, @NotNull Map<ExternalIdentityRef, SyncEntry> map) throws ExternalIdentityException, RepositoryException {
boolean shortcut = shortcut(depth);
for (ExternalIdentityRef ref : Iterables.filter(declaredGroupRefs, this::isSameIDP)) {
Iterable<ExternalIdentityRef> filtered = () -> CollectionUtils.toStream(declaredGroupRefs).filter(this::isSameIDP).iterator();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about having a util for this transformation as well?

@NotNull
public static Iterable toIterable(@NotNull Iterable iterable, @NotNull Predicate<? super T> predicate) {
Objects.requireNonNull(iterable);
Objects.requireNonNull(predicate);
return () -> StreamSupport.stream(iterable.spliterator(), false).filter(predicate).iterator();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option could be to use commons-collections IterableUtils.filteredIterable and same goes for Iterator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the first proposal wouldn't work well when mutiple stream manipulations are changed.

for (ExternalIdentityRef ref : filtered) {
String principalName = null;
Authorizable a = null;
ExternalGroup externalGroup = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.jackrabbit.oak.api.ContentRepository;
import org.apache.jackrabbit.oak.api.ContentSession;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
import org.apache.jackrabbit.oak.plugins.value.jcr.ValueFactoryImpl;
import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
Expand Down Expand Up @@ -54,6 +55,7 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -354,25 +356,27 @@ private List<SyncResult> commit(@NotNull ResultMessages messages, @NotNull List<

@NotNull
private Iterator<String> internalListOrphanedIdentities() {
try {
Iterator<SyncedIdentity> it = handler.listIdentities(userMgr);
return Iterators.filter(Iterators.transform(it, syncedIdentity -> {
if (syncedIdentity != null && isMyIDP(syncedIdentity)) {
try {
// nonNull-ExternalIdRef has already been asserted by 'isMyIDP'
ExternalIdentity extId = idp.getIdentity(requireNonNull(syncedIdentity.getExternalIdRef()));
if (extId == null) {
return syncedIdentity.getId();
}
} catch (ExternalIdentityException e) {
log.error("Error while fetching external identity {}", syncedIdentity, e);
Iterable<SyncedIdentity> it = () -> {
try {
return handler.listIdentities(userMgr);
} catch (RepositoryException e) {
log.error("Error while listing orphaned users", e);
return Collections.emptyIterator();
}
};
return CollectionUtils.toStream(it).map(syncedIdentity -> {
if (syncedIdentity != null && isMyIDP(syncedIdentity)) {
try {
// nonNull-ExternalIdRef has already been asserted by 'isMyIDP'
ExternalIdentity extId = idp.getIdentity(requireNonNull(syncedIdentity.getExternalIdRef()));
if (extId == null) {
return syncedIdentity.getId();
}
} catch (ExternalIdentityException e) {
log.error("Error while fetching external identity {}", syncedIdentity, e);
}
return null;
}), x -> x != null);
} catch (RepositoryException e) {
log.error("Error while listing orphaned users", e);
return Collections.emptyIterator();
}
}
return null;
}).filter(Objects::nonNull).iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.jackrabbit.guava.common.collect.ImmutableSet;
import org.apache.jackrabbit.guava.common.collect.Iterables;
import org.apache.jackrabbit.guava.common.collect.Iterators;
import org.apache.jackrabbit.guava.common.collect.Sets;
import org.apache.jackrabbit.api.security.principal.GroupPrincipal;
import org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal;
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
Expand All @@ -35,6 +34,7 @@
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.api.Tree;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.namepath.NamePathMapper;
import org.apache.jackrabbit.oak.plugins.memory.PropertyValues;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef;
Expand Down Expand Up @@ -213,7 +213,7 @@ public Iterator<? extends Principal> findPrincipals(@Nullable String nameHint, i
// search for external group principals that have not been synchronzied into the repository
Result result = findPrincipals(Objects.toString(nameHint, ""), false);
if (result != null) {
return Iterators.filter(new GroupPrincipalIterator(nameHint, result), Objects::nonNull);
return CollectionUtils.toStream(new GroupPrincipalIterator(nameHint, result)).filter(Objects::nonNull).iterator();
} else {
return Collections.emptyIterator();
}
Expand Down Expand Up @@ -305,15 +305,15 @@ public boolean isMember(@NotNull Group group, @NotNull Authorizable authorizable
}

Set<Value> valueSet = ImmutableSet.copyOf(vs);
Iterator<Group> declared = Iterators.filter(Iterators.transform(valueSet.iterator(), value -> {
Iterator<Group> declared = valueSet.stream().map(value -> {
try {
String groupPrincipalName = value.getString();
Authorizable gr = userManager.getAuthorizable(new PrincipalImpl(groupPrincipalName));
return isValidGroup(gr, authorizable) ? (Group) gr : null;
} catch (RepositoryException e) {
return null;
}
}), Objects::nonNull);
}).filter(Objects::nonNull).iterator();
if (includeInherited) {
// retrieve groups inherited from dynamic groups through cross-IDP membership
return new InheritedMembershipIterator(declared);
Expand Down Expand Up @@ -640,7 +640,8 @@ private GroupPrincipalIterator(@Nullable String queryString, @NotNull Result que
if (!propValues.hasNext()) {
if (rows.hasNext()) {
ResultRow row = rows.next();
propValues = Iterators.filter(row.getValue(REP_EXTERNAL_PRINCIPAL_NAMES).getValue(Type.STRINGS).iterator(), Objects::nonNull);
propValues = CollectionUtils.toStream(row.getValue(REP_EXTERNAL_PRINCIPAL_NAMES).getValue(Type.STRINGS))
.filter(Objects::nonNull).iterator();
idpName = getIdpName(row);
} else {
propValues = Collections.emptyIterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.jackrabbit.oak.spi.security.authentication.external.impl.principal;

import org.apache.jackrabbit.guava.common.collect.Iterables;
import org.apache.jackrabbit.oak.commons.PropertiesUtil;
import org.apache.jackrabbit.oak.spi.security.authentication.external.SyncHandler;
import org.apache.jackrabbit.oak.spi.security.authentication.external.impl.SyncHandlerMapping;
Expand All @@ -29,6 +28,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static org.apache.jackrabbit.oak.spi.security.authentication.external.impl.SyncHandlerMapping.PARAM_IDP_NAME;
import static org.apache.jackrabbit.oak.spi.security.authentication.external.impl.SyncHandlerMapping.PARAM_SYNC_HANDLER_NAME;
Expand Down Expand Up @@ -77,14 +77,13 @@ private void addMapping(ServiceReference reference) {
}

Iterable<String> getIdpNames(@NotNull final String syncHandlerName) {
return Iterables.filter(Iterables.transform(referenceMap.values(), mapping -> {
return () -> referenceMap.values().stream().map(mapping -> {
if (syncHandlerName.equals(mapping.syncHandlerName)) {
return mapping.idpName;
} else {
// different synchandler name
return null;
}
}), x -> x != null);
}}).filter(Objects::nonNull).iterator();
}

private static final class Mapping {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.jackrabbit.oak.spi.security.authentication.external;

import org.apache.jackrabbit.guava.common.collect.ImmutableMap;
import org.apache.jackrabbit.guava.common.collect.Iterators;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
Expand Down Expand Up @@ -136,7 +135,7 @@ protected static void assertException(@NotNull CommitFailedException e, @NotNull
@NotNull
private static Iterator<String> getAllAuthorizableIds(@NotNull UserManager userManager) throws Exception {
Iterator<Authorizable> iter = userManager.findAuthorizables("jcr:primaryType", null);
return Iterators.filter(Iterators.transform(iter, input -> {
return CollectionUtils.toStream(iter).map(input -> {
try {
if (input != null) {
return input.getID();
Expand All @@ -145,7 +144,7 @@ private static Iterator<String> getAllAuthorizableIds(@NotNull UserManager userM
// failed to retrieve ID
}
return null;
}), Objects::nonNull);
}).filter(Objects::nonNull).iterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
*/
package org.apache.jackrabbit.oak.spi.security.authentication.external.impl;

import org.apache.jackrabbit.guava.common.collect.ImmutableList;
import org.apache.jackrabbit.guava.common.collect.Iterables;
import org.apache.jackrabbit.guava.common.collect.Iterators;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.spi.security.authentication.external.AbstractExternalAuthTest;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentity;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser;
Expand All @@ -34,8 +33,10 @@
import javax.jcr.RepositoryException;
import javax.jcr.ValueFactory;
import java.security.Principal;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertSame;

Expand Down Expand Up @@ -116,17 +117,19 @@ protected void sync(@NotNull ExternalIdentity externalIdentity, @NotNull SyncRes

@NotNull
static List<String> getIds(@NotNull Iterator<? extends Authorizable> authorizables) {
return ImmutableList.copyOf(Iterators.transform(authorizables, authorizable -> {
return Collections.unmodifiableList(
CollectionUtils.toStream(authorizables).map(authorizable -> {
try {
return authorizable.getID();
} catch (RepositoryException repositoryException) {
throw new RuntimeException();
}
}));
}).collect(Collectors.toList()));
}

@NotNull
static List<String> getPrincipalNames(@NotNull Iterator<Principal> groupPrincipals) {
return ImmutableList.copyOf(Iterators.transform(groupPrincipals, Principal::getName));
return Collections
.unmodifiableList(CollectionUtils.toStream(groupPrincipals).map(Principal::getName).collect(Collectors.toList()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.oak.api.QueryEngine;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef;
import org.apache.jackrabbit.oak.spi.security.authentication.external.basic.DefaultSyncConfig;
import org.apache.jackrabbit.oak.spi.security.principal.EveryonePrincipal;
Expand All @@ -38,9 +39,11 @@
import javax.jcr.RepositoryException;
import java.text.ParseException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.apache.jackrabbit.oak.spi.security.authentication.external.impl.ExternalIdentityConstants.REP_EXTERNAL_ID;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -108,13 +111,13 @@ private AutoMembershipProvider createAutoMembershipProvider(@NotNull Root root,
}

private static void assertMatchingEntries(@NotNull Iterator<Authorizable> it, @NotNull String... expectedIds) {
Set<String> ids = ImmutableSet.copyOf(Iterators.transform(it, authorizable -> {
Set<String> ids = Collections.unmodifiableSet(CollectionUtils.toStream(it).map(authorizable -> {
try {
return authorizable.getID();
} catch (RepositoryException repositoryException) {
return "";
}
}));
}).collect(Collectors.toSet()));
assertEquals(ImmutableSet.copyOf(expectedIds), ids);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.apache.jackrabbit.oak.spi.security.authentication.external.impl.principal;

import org.apache.jackrabbit.guava.common.collect.ImmutableList;
import org.apache.jackrabbit.guava.common.collect.ImmutableMap;
import org.apache.jackrabbit.guava.common.collect.Iterables;
import org.apache.jackrabbit.api.security.principal.GroupPrincipal;
import org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal;
import org.apache.jackrabbit.api.security.user.Authorizable;
Expand All @@ -27,6 +25,8 @@
import org.apache.jackrabbit.oak.api.QueryEngine;
import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalGroup;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentity;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser;
Expand All @@ -40,6 +40,7 @@
import java.text.ParseException;
import java.util.Enumeration;
import java.util.Map;
import java.util.stream.Stream;

import static org.apache.jackrabbit.oak.spi.security.authentication.external.TestIdentityProvider.ID_SECOND_USER;
import static org.apache.jackrabbit.oak.spi.security.authentication.external.impl.ExternalIdentityConstants.REP_EXTERNAL_PRINCIPAL_NAMES;
Expand Down Expand Up @@ -81,7 +82,9 @@ public void testNotIsMember() throws Exception {
public void testIsMemberExternalGroup() throws Exception {
GroupPrincipal principal = getGroupPrincipal();

Iterable<String> exGroupPrincNames = Iterables.transform(ImmutableList.copyOf(idp.listGroups()), ExternalIdentity::getPrincipalName);
Stream<ExternalGroup> s = CollectionUtils.toStream(idp.listGroups());
Iterable<String> exGroupPrincNames = () -> s.map(ExternalIdentity::getPrincipalName).iterator();

for (String principalName : exGroupPrincNames) {
assertFalse(principal.isMember(new PrincipalImpl(principalName)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.jackrabbit.oak.security.authentication.ldap.impl;

import org.apache.jackrabbit.guava.common.collect.ImmutableSet;
import org.apache.jackrabbit.guava.common.collect.Iterables;
import org.apache.jackrabbit.guava.common.collect.Iterators;
import org.apache.directory.api.util.Strings;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalGroup;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentity;
import org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityException;
Expand Down Expand Up @@ -200,7 +199,8 @@ public void testGetDeclaredMembersByRef() throws Exception {
public void testGetDeclaredMembers() throws Exception {
ExternalGroup gr = idp.getGroup(TEST_GROUP1_NAME);
Iterable<ExternalIdentityRef> memberrefs = gr.getDeclaredMembers();
Iterable<String> memberIds = Iterables.transform(memberrefs, externalIdentityRef -> externalIdentityRef.getId());
Iterable<String> memberIds = () -> CollectionUtils.toStream(memberrefs)
.map(externalIdentityRef -> externalIdentityRef.getId()).iterator();

Set<String> expected = ImmutableSet.copyOf(TEST_GROUP1_MEMBERS);
assertEquals(expected, ImmutableSet.copyOf(memberIds));
Expand All @@ -212,7 +212,7 @@ public void testGetDeclaredMembersInvalidMemberAttribute() throws Exception {

ExternalGroup gr = idp.getGroup(TEST_GROUP1_NAME);
Iterable<ExternalIdentityRef> memberrefs = gr.getDeclaredMembers();
assertTrue(Iterables.isEmpty(memberrefs));
assertTrue(!memberrefs.iterator().hasNext());
}

@Test
Expand All @@ -237,7 +237,8 @@ public void testGetDeclaredGroupMissingIdAttribute() throws Exception {

ExternalUser user = idp.getUser(TEST_USER1_UID);
Iterable<ExternalIdentityRef> groupRefs = user.getDeclaredGroups();
Iterable<String> groupIds = Iterables.transform(groupRefs, externalIdentityRef -> externalIdentityRef.getId());
Iterable<String> groupIds = () -> CollectionUtils.toStream(groupRefs)
.map(externalIdentityRef -> externalIdentityRef.getId()).iterator();
assertEquals(ImmutableSet.copyOf(TEST_USER1_GROUPS), ImmutableSet.copyOf(groupIds));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public AccessControlPolicy[] getEffectivePolicies(@NotNull Set<Principal> princi
return new AccessControlPolicy[0];
}
Root r = getLatestRoot();
Set<String> candidates = collectEffectiveCandidates(r, Iterables.transform(principals, Principal::getName));
Set<String> candidates = collectEffectiveCandidates(r, () -> principals.stream().map(Principal::getName).iterator());
if (candidates.isEmpty()) {
return new AccessControlPolicy[0];
} else {
Expand Down
Loading
Loading