Example usage for org.apache.commons.collections15 ExtendedMapUtils mapCollectionMultiValues

List of usage examples for org.apache.commons.collections15 ExtendedMapUtils mapCollectionMultiValues

Introduction

In this page you can find the example usage for org.apache.commons.collections15 ExtendedMapUtils mapCollectionMultiValues.

Prototype

public static final <K, V, C extends Collection<V>> Map<K, C> mapCollectionMultiValues(
        Transformer<? super V, ? extends K> transformer, Factory<? extends C> factory,
        Collection<? extends V> values) 

Source Link

Usage

From source file:org.apache.sshd.server.PublickeyAuthenticatorUtils.java

/**
 * @param entries A {@link Collection} of {@link CryptoKeyEntry}-ies
 * @return A {@link PublickeyAuthenticator} that matches the received encoded
 * public key bytes to one of the authorized keys published by the user
 * @see CryptoKeyEntry#readAuthorizedKeys(File)
 *//*w ww.  ja  v a2  s  .c  o m*/
public static final PublickeyAuthenticator authorizedKeysAuthenticator(
        Collection<? extends CryptoKeyEntry> entries) {
    final Map<String, ? extends Collection<CryptoKeyEntry>> keysMap = ExtendedMapUtils.mapCollectionMultiValues(
            CryptoKeyEntry.USERNAME_EXTRACTOR, ExtendedCollectionUtils.<CryptoKeyEntry>linkedListFactory(),
            entries);
    return new PublickeyAuthenticator() {
        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            Collection<CryptoKeyEntry> keySet = keysMap.get(username);
            if (ExtendedCollectionUtils.isEmpty(keySet)) {
                return false;
            }

            final byte[] keyBytes = key.getEncoded();
            if (ArrayUtils.isEmpty(keyBytes)) {
                return false; // TODO consider throwing an exception
            }

            CryptoKeyEntry entry = CollectionUtils.find(keySet,
                    new AbstractExtendedPredicate<CryptoKeyEntry>(CryptoKeyEntry.class) {
                        @Override
                        public boolean evaluate(CryptoKeyEntry e) {
                            byte[] entryBytes = e.getKeyData();
                            if (Arrays.equals(keyBytes, entryBytes)) {
                                return true;
                            } else {
                                return false; // debug breakpoint;
                            }
                        }
                    });
            if (entry == null) {
                return false;
            } else {
                return true;
            }
        }
    };
}