44import java .net .URI ;
55import java .net .URISyntaxException ;
66import java .security .GeneralSecurityException ;
7+ import java .security .KeyManagementException ;
8+ import java .security .KeyStoreException ;
9+ import java .security .NoSuchAlgorithmException ;
710import java .time .Duration ;
811import java .util .HashMap ;
912import java .util .List ;
@@ -53,54 +56,59 @@ public class Admin implements AutoCloseable {
5356
5457 private static final Logger log = LoggerFactory .getLogger (Admin .class );
5558
56- public static final String CONTENT_TYPE_JSON = "application/json" ;
57- public static final String V1 = "/v1/" ;
58- public static final String V2 = "/v2/" ;
59+ public static final String DEFAULT_USER_NAME = "admin@redis.com" ;
60+ public static final String DEFAULT_PASSWORD = "redis123" ;
5961 public static final String DEFAULT_PROTOCOL = "https" ;
6062 public static final String DEFAULT_HOST = "localhost" ;
6163 public static final int DEFAULT_PORT = 9443 ;
62- public static final String BOOTSTRAP = "bootstrap" ;
63- public static final String ACTIONS = "actions" ;
64- public static final String MODULES = "modules" ;
65- public static final String BDBS = "bdbs" ;
66- public static final String COMMAND = "command" ;
64+
65+ private static final String BOOTSTRAP = "bootstrap" ;
66+ private static final String MODULES = "modules" ;
67+ private static final String BDBS = "bdbs" ;
68+ private static final String COMMAND = "command" ;
69+ private static final String CONTENT_TYPE_JSON = "application/json" ;
70+ private static final String V1 = "/v1/" ;
6771 private static final CharSequence PATH_SEPARATOR = "/" ;
6872
6973 private final ObjectMapper objectMapper = new ObjectMapper ()
7074 .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
71- private final UsernamePasswordCredentials credentials ;
72- private final CloseableHttpClient client ;
75+ private String userName = DEFAULT_USER_NAME ;
76+ private String password = DEFAULT_PASSWORD ;
77+ private CloseableHttpClient client ;
7378 private String protocol = DEFAULT_PROTOCOL ;
7479 private String host = DEFAULT_HOST ;
7580 private int port = DEFAULT_PORT ;
7681
77- public Admin (String userName , final char [] password ) throws GeneralSecurityException {
78- this .credentials = new UsernamePasswordCredentials (userName , password );
79- SSLContext sslcontext = SSLContexts .custom ().loadTrustMaterial (new TrustAllStrategy ()).build ();
80- SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder .create ()
81- .setSslContext (sslcontext ).setHostnameVerifier (NoopHostnameVerifier .INSTANCE ).build ();
82- HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder .create ()
83- .setSSLSocketFactory (sslSocketFactory ).build ();
84- HttpClientBuilder clientBuilder = HttpClients .custom ();
85- clientBuilder .setConnectionManager (cm );
86- this .client = clientBuilder .build ();
87- }
88-
89- @ Override
90- public void close () throws Exception {
91- client .close ();
82+ public void close () throws IOException {
83+ if (client != null ) {
84+ client .close ();
85+ client = null ;
86+ }
9287 }
9388
94- public void setHost (String host ) {
89+ public Admin withHost (String host ) {
9590 this .host = host ;
91+ return this ;
9692 }
9793
98- public void setProtocol (String protocol ) {
94+ public Admin withProtocol (String protocol ) {
9995 this .protocol = protocol ;
96+ return this ;
10097 }
10198
102- public void setPort (int port ) {
99+ public Admin withPort (int port ) {
103100 this .port = port ;
101+ return this ;
102+ }
103+
104+ public Admin withUserName (String userName ) {
105+ this .userName = userName ;
106+ return this ;
107+ }
108+
109+ public Admin withPassword (String password ) {
110+ this .password = password ;
111+ return this ;
104112 }
105113
106114 private static String v1 (String ... segments ) {
@@ -119,19 +127,19 @@ private URI uri(String path) {
119127 }
120128 }
121129
122- private <T > T get (String path , Class <T > type ) throws IOException {
130+ private <T > T get (String path , Class <T > type ) throws IOException , GeneralSecurityException {
123131 return get (path , SimpleType .constructUnsafe (type ));
124132 }
125133
126- private <T > T get (String path , JavaType type ) throws IOException {
134+ private <T > T get (String path , JavaType type ) throws IOException , GeneralSecurityException {
127135 return read (header (new HttpGet (uri (path ))), type , HttpStatus .SC_OK );
128136 }
129137
130- private <T > T delete (String path , Class <T > type ) throws IOException {
138+ private <T > T delete (String path , Class <T > type ) throws IOException , GeneralSecurityException {
131139 return delete (path , SimpleType .constructUnsafe (type ));
132140 }
133141
134- private <T > T delete (String path , JavaType type ) throws IOException {
142+ private <T > T delete (String path , JavaType type ) throws IOException , GeneralSecurityException {
135143 return read (header (new HttpDelete (uri (path ))), type , HttpStatus .SC_OK );
136144 }
137145
@@ -140,25 +148,28 @@ private ClassicHttpRequest header(ClassicHttpRequest request) {
140148 return request ;
141149 }
142150
143- private <T > T post (String path , Object request , Class <T > responseType ) throws IOException {
151+ private <T > T post (String path , Object request , Class <T > responseType )
152+ throws IOException , GeneralSecurityException {
144153 return post (path , request , SimpleType .constructUnsafe (responseType ));
145154 }
146155
147- private <T > T post (String path , Object request , JavaType responseType ) throws IOException {
156+ private <T > T post (String path , Object request , JavaType responseType )
157+ throws IOException , GeneralSecurityException {
148158 HttpPost post = new HttpPost (uri (path ));
149159 String json = objectMapper .writeValueAsString (request );
150160 log .debug ("POST {}" , json );
151161 post .setEntity (new StringEntity (json ));
152162 return read (header (post ), responseType , HttpStatus .SC_OK );
153163 }
154164
155- private <T > T read (ClassicHttpRequest request , JavaType type , int successCode ) throws IOException {
165+ private <T > T read (ClassicHttpRequest request , JavaType type , int successCode )
166+ throws IOException , GeneralSecurityException {
156167 HttpHost target = new HttpHost (protocol , host , port );
157168 HttpClientContext localContext = HttpClientContext .create ();
158169 BasicScheme basicAuth = new BasicScheme ();
159- basicAuth .initPreemptive (credentials );
170+ basicAuth .initPreemptive (new UsernamePasswordCredentials ( userName , password . toCharArray ()) );
160171 localContext .resetAuthExchange (target , basicAuth );
161- CloseableHttpResponse response = client .execute (request , localContext );
172+ CloseableHttpResponse response = client () .execute (request , localContext );
162173 String json ;
163174 try {
164175 json = EntityUtils .toString (response .getEntity ());
@@ -171,6 +182,20 @@ private <T> T read(ClassicHttpRequest request, JavaType type, int successCode) t
171182 throw new HttpResponseException (response .getCode (), response .getReasonPhrase () + " " + json );
172183 }
173184
185+ private CloseableHttpClient client () throws KeyManagementException , NoSuchAlgorithmException , KeyStoreException {
186+ if (client == null ) {
187+ SSLContext sslcontext = SSLContexts .custom ().loadTrustMaterial (new TrustAllStrategy ()).build ();
188+ SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder .create ()
189+ .setSslContext (sslcontext ).setHostnameVerifier (NoopHostnameVerifier .INSTANCE ).build ();
190+ HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder .create ()
191+ .setSSLSocketFactory (sslSocketFactory ).build ();
192+ HttpClientBuilder clientBuilder = HttpClients .custom ();
193+ clientBuilder .setConnectionManager (cm );
194+ client = clientBuilder .build ();
195+ }
196+ return client ;
197+ }
198+
174199 private static class HttpResponseParsingException extends IOException {
175200
176201 private static final long serialVersionUID = 1L ;
@@ -180,12 +205,12 @@ public HttpResponseParsingException(String message, Throwable cause) {
180205 }
181206 }
182207
183- public List <InstalledModule > getModules () throws IOException {
208+ public List <InstalledModule > getModules () throws IOException , GeneralSecurityException {
184209 return get (v1 (MODULES ),
185210 objectMapper .getTypeFactory ().constructCollectionType (List .class , InstalledModule .class ));
186211 }
187212
188- public Database createDatabase (Database database ) throws IOException {
213+ public Database createDatabase (Database database ) throws IOException , GeneralSecurityException {
189214 Map <String , InstalledModule > installedModules = new HashMap <>();
190215 for (InstalledModule module : getModules ()) {
191216 installedModules .put (module .getName (), module );
@@ -203,7 +228,7 @@ public Database createDatabase(Database database) throws IOException {
203228 return response ;
204229 }
205230
206- public List <Database > getDatabases () throws IOException {
231+ public List <Database > getDatabases () throws IOException , GeneralSecurityException {
207232 return get (v1 (BDBS ), objectMapper .getTypeFactory ().constructCollectionType (List .class , Database .class ));
208233 }
209234
@@ -228,12 +253,25 @@ public void waitForBoostrap() {
228253
229254 }
230255
231- private Bootstrap getBootstrap () throws IOException {
256+ private Bootstrap getBootstrap () throws IOException , GeneralSecurityException {
232257 return get (v1 (BOOTSTRAP ), Bootstrap .class );
233258 }
234259
235- public CommandResponse executeCommand (long bdb , Command command ) throws IOException {
260+ public CommandResponse executeCommand (long bdb , Command command ) throws IOException , GeneralSecurityException {
236261 return post (v1 (BDBS , String .valueOf (bdb ), COMMAND ), command , CommandResponse .class );
237262 }
238263
264+ public static Admin create (String host ) {
265+ Admin admin = new Admin ();
266+ admin .withHost (host );
267+ return admin ;
268+ }
269+
270+ public static Admin create (String host , int port ) {
271+ Admin admin = new Admin ();
272+ admin .withHost (host );
273+ admin .withPort (port );
274+ return admin ;
275+ }
276+
239277}
0 commit comments