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

software.amazon.awssdk.identity.spi.internal.DefaultIdentityProviders Maven / Gradle / Ivy

Go to download

The AWS SDK for Java - Identity SPI module contains the Identity interfaces that are used by other modules in the library.

There is a newer version: 2.29.16
Show newest version
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.identity.spi.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import software.amazon.awssdk.annotations.Immutable;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.identity.spi.Identity;
import software.amazon.awssdk.identity.spi.IdentityProvider;
import software.amazon.awssdk.identity.spi.IdentityProviders;
import software.amazon.awssdk.utils.Lazy;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;

/**
 * A default implementation of {@link IdentityProviders}. This implementation holds a map of {@link IdentityProvider}s and
 * retrieves from the collection based on identity type.
 */
@Immutable
@SdkInternalApi
public final class DefaultIdentityProviders implements IdentityProviders {
    /**
     * TODO(sra-identity-auth): Currently, some customers assume we won't interact with the identity providers when we create
     * the client. This isn't true - we need to call identityType. To TEMPORARILY work around those customer's tests failing,
     * this is marked lazy. Once we fully migrate over to the SRA as the default code path, we should remove this lazy and
     * ticket everyone in live who is making those bad assumptions.
     */
    private final Lazy, IdentityProvider>> identityProviders;
    private final List> identityProvidersList;

    private DefaultIdentityProviders(BuilderImpl builder) {
        this.identityProvidersList = new ArrayList<>(builder.identityProviders);
        this.identityProviders = new Lazy<>(() -> {
            Map, IdentityProvider> result = new HashMap<>();
            for (IdentityProvider identityProvider : identityProvidersList) {
                result.put(identityProvider.identityType(), identityProvider);
            }
            return result;
        });
    }

    public static Builder builder() {
        return new BuilderImpl();
    }

    @Override
    public  IdentityProvider identityProvider(Class identityType) {
        return (IdentityProvider) identityProviders.getValue().get(identityType);
    }

    @Override
    public Builder toBuilder() {
        return new BuilderImpl(this);
    }

    @Override
    public String toString() {
        return ToString.builder("IdentityProviders")
                       .add("identityProviders", identityProvidersList)
                       .build();
    }

    private static final class BuilderImpl implements Builder {
        private final List> identityProviders = new ArrayList<>();

        private BuilderImpl() {
        }

        private BuilderImpl(DefaultIdentityProviders identityProviders) {
            this.identityProviders.addAll(identityProviders.identityProvidersList);
        }

        @Override
        public  Builder putIdentityProvider(IdentityProvider identityProvider) {
            Validate.paramNotNull(identityProvider, "identityProvider");
            identityProviders.add(identityProvider);
            return this;
        }

        public IdentityProviders build() {
            return new DefaultIdentityProviders(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy