1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springframework .security .oauth2 .client .registration ;
17
17
18
+ import java .util .Arrays ;
19
+ import java .util .Collections ;
18
20
import java .util .Iterator ;
19
21
import java .util .List ;
22
+ import java .util .Map ;
23
+ import java .util .concurrent .ConcurrentHashMap ;
20
24
21
25
import reactor .core .publisher .Mono ;
22
26
27
+ import org .springframework .util .Assert ;
28
+
23
29
/**
24
30
* A Reactive {@link ClientRegistrationRepository} that stores {@link ClientRegistration}(s) in-memory.
25
31
*
26
32
* @author Rob Winch
33
+ * @author Ebert Toribio
27
34
* @since 5.1
28
35
* @see ClientRegistrationRepository
29
36
* @see ClientRegistration
30
37
*/
31
38
public final class InMemoryReactiveClientRegistrationRepository
32
39
implements ReactiveClientRegistrationRepository , Iterable <ClientRegistration > {
33
40
34
- private final InMemoryClientRegistrationRepository delegate ;
41
+ private final Map < String , ClientRegistration > clientIdToClientRegistration ;
35
42
36
43
/**
37
44
* Constructs an {@code InMemoryReactiveClientRegistrationRepository} using the provided parameters.
38
45
*
39
46
* @param registrations the client registration(s)
40
47
*/
41
48
public InMemoryReactiveClientRegistrationRepository (ClientRegistration ... registrations ) {
42
- this .delegate = new InMemoryClientRegistrationRepository (registrations );
49
+ this (toList (registrations ));
50
+ }
51
+
52
+ private static List <ClientRegistration > toList (ClientRegistration ... registrations ) {
53
+ Assert .notEmpty (registrations , "registrations cannot be null or empty" );
54
+ return Arrays .asList (registrations );
43
55
}
44
56
45
57
/**
@@ -48,12 +60,12 @@ public InMemoryReactiveClientRegistrationRepository(ClientRegistration... regist
48
60
* @param registrations the client registration(s)
49
61
*/
50
62
public InMemoryReactiveClientRegistrationRepository (List <ClientRegistration > registrations ) {
51
- this .delegate = new InMemoryClientRegistrationRepository (registrations );
63
+ this .clientIdToClientRegistration = toUnmodifiableConcurrentMap (registrations );
52
64
}
53
65
54
66
@ Override
55
67
public Mono <ClientRegistration > findByRegistrationId (String registrationId ) {
56
- return Mono .justOrEmpty (this .delegate . findByRegistrationId (registrationId ));
68
+ return Mono .justOrEmpty (this .clientIdToClientRegistration . get (registrationId ));
57
69
}
58
70
59
71
/**
@@ -63,6 +75,20 @@ public Mono<ClientRegistration> findByRegistrationId(String registrationId) {
63
75
*/
64
76
@ Override
65
77
public Iterator <ClientRegistration > iterator () {
66
- return delegate .iterator ();
78
+ return this .clientIdToClientRegistration .values ().iterator ();
79
+ }
80
+
81
+ private static Map <String , ClientRegistration > toUnmodifiableConcurrentMap (List <ClientRegistration > registrations ) {
82
+ Assert .notEmpty (registrations , "registrations cannot be null or empty" );
83
+ ConcurrentHashMap <String , ClientRegistration > result = new ConcurrentHashMap <>();
84
+ for (ClientRegistration registration : registrations ) {
85
+ Assert .notNull (registration , "no registration can be null" );
86
+ if (result .containsKey (registration .getRegistrationId ())) {
87
+ throw new IllegalStateException (String .format ("Duplicate key %s" ,
88
+ registration .getRegistrationId ()));
89
+ }
90
+ result .put (registration .getRegistrationId (), registration );
91
+ }
92
+ return Collections .unmodifiableMap (result );
67
93
}
68
94
}
0 commit comments