1+ /*
2+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License").
5+ * You may not use this file except in compliance with the License.
6+ * A copy of the License is located at
7+ *
8+ * http://aws.amazon.com/apache2.0
9+ *
10+ * or in the "license" file accompanying this file. This file is distributed
11+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+ * express or implied. See the License for the specific language governing
13+ * permissions and limitations under the License.
14+ */
15+
16+ package software .amazon .awssdk .codegen .poet .auth .scheme ;
17+
18+ import com .squareup .javapoet .ClassName ;
19+ import com .squareup .javapoet .MethodSpec ;
20+ import com .squareup .javapoet .ParameterizedTypeName ;
21+ import com .squareup .javapoet .TypeSpec ;
22+ import java .util .ArrayList ;
23+ import java .util .Collections ;
24+ import java .util .List ;
25+ import javax .lang .model .element .Modifier ;
26+ import software .amazon .awssdk .annotations .SdkInternalApi ;
27+ import software .amazon .awssdk .codegen .model .intermediate .IntermediateModel ;
28+ import software .amazon .awssdk .codegen .poet .ClassSpec ;
29+ import software .amazon .awssdk .codegen .poet .PoetUtils ;
30+ import software .amazon .awssdk .utils .CollectionUtils ;
31+
32+ public class PreferredAuthSchemeProviderSpec implements ClassSpec {
33+ private final AuthSchemeSpecUtils authSchemeSpecUtils ;
34+
35+ public PreferredAuthSchemeProviderSpec (IntermediateModel intermediateModel ) {
36+ this .authSchemeSpecUtils = new AuthSchemeSpecUtils (intermediateModel );
37+ }
38+
39+ @ Override
40+ public ClassName className () {
41+ return authSchemeSpecUtils .preferredAuthSchemeProviderName ();
42+ }
43+
44+ @ Override
45+ public TypeSpec poetSpec () {
46+ return PoetUtils .createClassBuilder (className ())
47+ .addModifiers (Modifier .PUBLIC , Modifier .FINAL )
48+ .addAnnotation (SdkInternalApi .class )
49+ .addField (
50+ authSchemeSpecUtils .providerInterfaceName (), "delegate" ,
51+ Modifier .PRIVATE , Modifier .FINAL )
52+ .addField (
53+ ParameterizedTypeName .get (List .class , String .class ), "authSchemePreference" ,
54+ Modifier .PRIVATE , Modifier .FINAL )
55+ .addSuperinterface (authSchemeSpecUtils .providerInterfaceName ())
56+ .addMethod (constructor ())
57+ .addMethod (resolveAuthSchemeMethod ())
58+ .build ();
59+ }
60+
61+ private MethodSpec constructor () {
62+ return MethodSpec
63+ .constructorBuilder ()
64+ .addModifiers (Modifier .PUBLIC )
65+ .addParameter (authSchemeSpecUtils .providerInterfaceName (), "delegate" )
66+ .addParameter (ParameterizedTypeName .get (List .class , String .class ), "authSchemePreference" )
67+ .addStatement ("this.delegate = delegate" )
68+ .addStatement ("this.authSchemePreference = authSchemePreference != null ? authSchemePreference : $T.emptyList()" ,
69+ Collections .class )
70+ .build ();
71+ }
72+
73+ private MethodSpec resolveAuthSchemeMethod () {
74+ MethodSpec .Builder b = MethodSpec .methodBuilder ("resolveAuthScheme" )
75+ .addModifiers (Modifier .PUBLIC )
76+ .addAnnotation (Override .class )
77+ .returns (authSchemeSpecUtils .resolverReturnType ())
78+ .addParameter (authSchemeSpecUtils .parametersInterfaceName (), "params" );
79+ b .addJavadoc ("Resolve the auth schemes based on the given set of parameters." );
80+ b .addStatement ("$T candidateAuthSchemes = delegate.resolveAuthScheme(params)" ,
81+ authSchemeSpecUtils .resolverReturnType ());
82+ b .beginControlFlow ("if ($T.isNullOrEmpty(authSchemePreference))" , CollectionUtils .class )
83+ .addStatement ("return candidateAuthSchemes" )
84+ .endControlFlow ();
85+
86+ b .addStatement ("$T authSchemes = new $T<>()" , authSchemeSpecUtils .resolverReturnType (), ArrayList .class );
87+ b .beginControlFlow ("authSchemePreference.forEach( preferredSchemeId -> " )
88+ .addStatement ("candidateAuthSchemes.stream().filter(a -> a.schemeId().equals(preferredSchemeId)).findFirst()"
89+ + ".ifPresent(a -> authSchemes.add(a))" )
90+ .endControlFlow (")" );
91+
92+ b .beginControlFlow ("candidateAuthSchemes.forEach(candidate -> " )
93+ .beginControlFlow ("if (!authSchemes.contains(candidate))" )
94+ .addStatement ("authSchemes.add(candidate)" )
95+ .endControlFlow ()
96+ .endControlFlow (")" );
97+
98+ b .addStatement ("return authSchemes" );
99+ return b .build ();
100+ }
101+ }
0 commit comments