1
- package redis .clients .authentication ;
2
-
3
- import com .google .gson .FieldNamingPolicy ;
4
- import com .google .gson .Gson ;
5
- import com .google .gson .GsonBuilder ;
6
- import com .google .gson .reflect .TypeToken ;
7
-
8
- import redis .clients .jedis .DefaultJedisClientConfig ;
9
- import redis .clients .jedis .HostAndPort ;
10
- import redis .clients .jedis .util .JedisURIHelper ;
11
-
12
- import java .io .FileReader ;
13
- import java .net .URI ;
14
- import java .util .*;
15
-
16
- public class EndpointConfig {
17
-
18
- private final boolean tls ;
19
- private final String username ;
20
- private final String password ;
21
- private final int bdbId ;
22
- private final List <URI > endpoints ;
23
-
24
- public EndpointConfig (HostAndPort hnp , String username , String password , boolean tls ) {
25
- this .tls = tls ;
26
- this .username = username ;
27
- this .password = password ;
28
- this .bdbId = 0 ;
29
- this .endpoints = Collections .singletonList (
30
- URI .create (getURISchema (tls ) + hnp .getHost () + ":" + hnp .getPort ()));
31
- }
32
-
33
- public HostAndPort getHostAndPort () {
34
- return JedisURIHelper .getHostAndPort (endpoints .get (0 ));
35
- }
36
-
37
- public HostAndPort getHostAndPort (int index ) {
38
- return JedisURIHelper .getHostAndPort (endpoints .get (index ));
39
- }
40
-
41
- public String getPassword () {
42
- return password ;
43
- }
44
-
45
- public String getUsername () {
46
- return username == null ? "default" : username ;
47
- }
48
-
49
- public String getHost () {
50
- return getHostAndPort ().getHost ();
51
- }
52
-
53
- public int getPort () {
54
- return getHostAndPort ().getPort ();
55
- }
56
-
57
- public int getBdbId () { return bdbId ; }
58
-
59
- public URI getURI () {
60
- return endpoints .get (0 );
61
- }
62
-
63
- public class EndpointURIBuilder {
64
- private boolean tls ;
65
-
66
- private String username ;
67
-
68
- private String password ;
69
-
70
- private String path ;
71
-
72
- public EndpointURIBuilder () {
73
- this .username = "" ;
74
- this .password = "" ;
75
- this .path = "" ;
76
- this .tls = EndpointConfig .this .tls ;
77
- }
78
-
79
- public EndpointURIBuilder defaultCredentials () {
80
- this .username = EndpointConfig .this .username == null ? "" : getUsername ();
81
- this .password = EndpointConfig .this .getPassword ();
82
- return this ;
83
- }
84
-
85
- public EndpointURIBuilder tls (boolean v ) {
86
- this .tls = v ;
87
- return this ;
88
- }
89
-
90
- public EndpointURIBuilder path (String v ) {
91
- this .path = v ;
92
- return this ;
93
- }
94
-
95
- public EndpointURIBuilder credentials (String u , String p ) {
96
- this .username = u ;
97
- this .password = p ;
98
- return this ;
99
- }
100
-
101
- public URI build () {
102
- String userInfo = !(this .username .isEmpty () && this .password .isEmpty ()) ?
103
- this .username + ':' + this .password + '@' :
104
- "" ;
105
- return URI .create (
106
- getURISchema (this .tls ) + userInfo + getHost () + ":" + getPort () + this .path );
107
- }
108
- }
109
-
110
- public EndpointURIBuilder getURIBuilder () {
111
- return new EndpointURIBuilder ();
112
- }
113
-
114
- public DefaultJedisClientConfig .Builder getClientConfigBuilder () {
115
- DefaultJedisClientConfig .Builder builder = DefaultJedisClientConfig .builder ()
116
- .password (password ).ssl (tls );
117
-
118
- if (username != null ) {
119
- return builder .user (username );
120
- }
121
-
122
- return builder ;
123
- }
124
-
125
- protected String getURISchema (boolean tls ) {
126
- return (tls ? "rediss" : "redis" ) + "://" ;
127
- }
128
-
129
- public static HashMap <String , EndpointConfig > loadFromJSON (String filePath ) throws Exception {
130
- Gson gson = new GsonBuilder ().setFieldNamingPolicy (
131
- FieldNamingPolicy .LOWER_CASE_WITH_UNDERSCORES ).create ();
132
-
133
- HashMap <String , EndpointConfig > configs ;
134
- try (FileReader reader = new FileReader (filePath )) {
135
- configs = gson .fromJson (reader , new TypeToken <HashMap <String , EndpointConfig >>() {
136
- }.getType ());
137
- }
138
- return configs ;
139
- }
140
- }
1
+ //
0 commit comments