All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.security.ldap.context.AttributesConvertibleValues Maven / Gradle / Ivy

There is a newer version: 4.11.1
Show newest version
/*
 * Copyright 2017-2023 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.security.ldap.context;

import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.value.ConvertibleValues;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;

/**
 * A {@link ConvertibleValues} implementation that uses {@link Attributes} as
 * the backing data source.
 *
 * @author James Kleeh
 * @since 1.0
 */
public class AttributesConvertibleValues implements ConvertibleValues {

    private final Attributes attributes;
    private final ConversionService conversionService = ConversionService.SHARED;

    /**
     * @param attributes The attributes
     */
    public AttributesConvertibleValues(Attributes attributes) {
        this.attributes = attributes;
    }

    @Override
    public Set names() {
        Set names = new HashSet<>();
        NamingEnumeration ids = attributes.getIDs();
        try {
            while (ids.hasMore()) {
                names.add(ids.next());
            }
        } catch (NamingException e) {
            //swallow
        }
        return names;
    }

    @Override
    public Collection values() {
        List values = new ArrayList<>(attributes.size());
        NamingEnumeration all = attributes.getAll();
        try {
            while (all.hasMore()) {
                values.add(all.next().get());
            }
        } catch (NamingException e) {
            //swallow
        }
        return values;
    }

    @Override
    public  Optional get(CharSequence name, ArgumentConversionContext conversionContext) {
        try {
            Attribute attr = attributes.get(name.toString());
            if (attr == null) {
                return Optional.empty();
            }
            NamingEnumeration enumeration = attr.getAll();
            List results = new ArrayList<>();
            while (enumeration.hasMore()) {
                results.add(enumeration.next());
            }
            if (!results.isEmpty()) {
                Object value;
                if (results.size() > 1) {
                    value = results;
                } else {
                    value = results.get(0);
                }
                //noinspection unchecked
                return conversionService.convert(value, conversionContext);
            } else {
                return Optional.empty();
            }
        } catch (NamingException e) {
            return Optional.empty();
        }
    }
}