<commit_before>package org.jasig.cas.persondir.support;

import net.shibboleth.idp.attribute.IdPAttributeValue;
import net.shibboleth.idp.attribute.resolver.AttributeResolver;
import net.shibboleth.idp.attribute.resolver.ResolutionException;
import net.shibboleth.idp.attribute.resolver.context.AttributeResolutionContext;
import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
import net.shibboleth.utilities.java.support.component.InitializableComponent;
import org.jasig.services.persondir.IPersonAttributeDao;
import org.jasig.services.persondir.IPersonAttributes;
import org.jasig.services.persondir.support.NamedPersonImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * A {@link IPersonAttributeDao} implementation that can use a Shibboleth
 * {@link net.shibboleth.idp.attribute.resolver.AttributeResolver} to resolve attributes.
 *
 * @author Jj
 * @since 4.3.0
 */
@Component("shibbolethPersonAttributeDao")
public class ShibbolethPersonAttributeDao implements IPersonAttributeDao {
    @Autowired
    private AttributeResolver attributeResolver;

    /**
     * Initializes the component. Right now, all it does is makes sure that the attribute resolver is initialized
     *
     * @throws ComponentInitializationException thrown if there is a problem initializing
     */
    @PostConstruct
    public void init() throws ComponentInitializationException {
        if (attributeResolver instanceof InitializableComponent && !((InitializableComponent) attributeResolver).isInitialized()) {
            ((InitializableComponent) attributeResolver).initialize();
        }
    }

    @Override
    public IPersonAttributes getPerson(final String uid) {
        final AttributeResolutionContext attributeResolutionContext = new AttributeResolutionContext();
        attributeResolutionContext.setPrincipal(uid);

        try {
            attributeResolver.resolveAttributes(attributeResolutionContext);

            final Map<String, List<Object>> attributes = attributeResolutionContext.getResolvedIdPAttributes()
                    .entrySet()
                    .stream()
                    .collect(Collectors.toMap(
                            Map.Entry::getKey,
                            p -> p.getValue().getValues().stream().map(IdPAttributeValue::getValue).collect(Collectors.toList()))
                    );

            return new NamedPersonImpl(uid, attributes);
        } catch (final ResolutionException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Does nothing. Look elsewhere.<commit_msg>wrap checked exception rather than throw<commit_after>package org.jasig.cas.persondir.support;

import net.shibboleth.idp.attribute.IdPAttributeValue;
import net.shibboleth.idp.attribute.resolver.AttributeResolver;
import net.shibboleth.idp.attribute.resolver.ResolutionException;
import net.shibboleth.idp.attribute.resolver.context.AttributeResolutionContext;
import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
import net.shibboleth.utilities.java.support.component.InitializableComponent;
import org.jasig.services.persondir.IPersonAttributeDao;
import org.jasig.services.persondir.IPersonAttributes;
import org.jasig.services.persondir.support.NamedPersonImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * A {@link IPersonAttributeDao} implementation that can use a Shibboleth
 * {@link net.shibboleth.idp.attribute.resolver.AttributeResolver} to resolve attributes.
 *
 * @author Jj
 * @since 4.3.0
 */
@Component("shibbolethPersonAttributeDao")
public class ShibbolethPersonAttributeDao implements IPersonAttributeDao {
    @Autowired
    private AttributeResolver attributeResolver;

    /**
     * Initializes the component. Right now, all it does is makes sure that the attribute resolver is initialized
     *
     * @throws ComponentInitializationException thrown if there is a problem initializing
     */
    @PostConstruct
    public void init() {
        if (attributeResolver instanceof InitializableComponent && !((InitializableComponent) attributeResolver).isInitialized()) {
            try {
                ((InitializableComponent) attributeResolver).initialize();
            } catch (final ComponentInitializationException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public IPersonAttributes getPerson(final String uid) {
        final AttributeResolutionContext attributeResolutionContext = new AttributeResolutionContext();
        attributeResolutionContext.setPrincipal(uid);

        try {
            attributeResolver.resolveAttributes(attributeResolutionContext);

            final Map<String, List<Object>> attributes = attributeResolutionContext.getResolvedIdPAttributes()
                    .entrySet()
                    .stream()
                    .collect(Collectors.toMap(
                            Map.Entry::getKey,
                            p -> p.getValue().getValues().stream().map(IdPAttributeValue::getValue).collect(Collectors.toList()))
                    );

            return new NamedPersonImpl(uid, attributes);
        } catch (final ResolutionException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Does nothing. Look elsewhere.
{'fraction_non_alphanumeric': 0.08651000870322019, 'fraction_numerical': 0.0010443864229765013, 'mean_word_length': 3.4890625, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5569348', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1474, 'n_words': 294}