View Javadoc

1   package org.rundeck.api;
2   
3   import org.rundeck.api.util.AssertUtil;
4   
5   
6   /**
7    * Builder to build a {@link RundeckClient}, you must specify a url, and at least one of (login, password), token, or
8    * sessionId.
9    */
10  public class RundeckClientBuilder {
11      private String url;
12      private String login;
13      private String password;
14      private String token = null;
15      private String id = null;
16      private int version = -1;
17      private boolean sslHostnameVerifyAllowAll = "true".equals(
18              System.getProperty(
19                      "rundeck.api.client.ssl.hostnameVerifyAllowAll",
20                      "false"
21              )
22      );
23      private boolean sslCertificateTrustAllowSelfSigned = "true".equals(
24              System.getProperty(
25                      "rundeck.api.client.ssl.sslCertificateTrustAllowSelfSigned",
26                      "false"
27              )
28      );
29      private boolean systemProxyEnabled = "true".equals(
30              System.getProperty(
31                      "rundeck.api.client.systemProxyEnabled",
32                      "false"
33              )
34      );
35  
36      RundeckClientBuilder(){
37  
38      }
39      /**
40       * Specify the URL
41       */
42      public RundeckClientBuilder url(String url) {
43          this.url = url;
44          return this;
45      }
46  
47      public RundeckClientBuilder login(String login) {
48          this.login = login;
49          return this;
50      }
51  
52      public RundeckClientBuilder login(String login, String password) {
53          this.login = login;
54          this.password = password;
55          return this;
56      }
57  
58      public RundeckClientBuilder password(String password) {
59          this.password = password;
60          return this;
61      }
62  
63      /**
64       * Specify a Rundeck API Token string for authentication
65       */
66      public RundeckClientBuilder token(String token) {
67          this.token = token;
68          return this;
69      }
70  
71      /**
72       * Specify a web session ID string for authentication
73       */
74      public RundeckClientBuilder sessionId(String id) {
75          this.id = id;
76          return this;
77      }
78  
79      /**
80       * Specify another version number to use
81       */
82      public RundeckClientBuilder version(final RundeckClient.Version version) {
83          this.version = version.getVersionNumber();
84          return this;
85      }
86  
87      /**
88       * Specify another version number to use
89       */
90      public RundeckClientBuilder version(final int version) {
91          this.version = version;
92          return this;
93      }
94      /**
95       * Specify whether to use system proxy settings, default false
96       */
97      public RundeckClientBuilder systemProxyEnabled(final boolean systemProxyEnabled) {
98          this.systemProxyEnabled = systemProxyEnabled;
99          return this;
100     }
101     /**
102      * Specify whether SSL hostname verifier allows all, otherwise it will use the default
103      */
104     public RundeckClientBuilder sslHostnameVerifyAllowAll(final boolean sslHostnameVerifyAllowAll) {
105         this.sslHostnameVerifyAllowAll = sslHostnameVerifyAllowAll;
106         return this;
107     }
108 
109     /**
110      * Specify whether SSL certificate trust allows self-signed certs, otherwise it will use the default
111      */
112     public RundeckClientBuilder sslCertificateTrustAllowSelfSigned(final boolean sslCertificateTrustAllowSelfSigned) {
113         this.sslCertificateTrustAllowSelfSigned = sslCertificateTrustAllowSelfSigned;
114         return this;
115     }
116 
117     /**
118      * Create the RundeckClient instance
119      */
120     public RundeckClient build() {
121         if (null == url) {
122             AssertUtil.notBlank(url, "The Rundeck URL is required");
123         }
124         final RundeckClient client = new RundeckClient(url);
125         if (null != login && null != password) {
126             AssertUtil.notBlank(login, "login cannot be blank");
127             AssertUtil.notBlank(password, "password cannot be blank");
128             client.setLogin(login);
129             client.setPassword(password);
130         } else if (null != token) {
131             AssertUtil.notBlank(token, "token cannot be blank");
132             client.setToken(token);
133         } else if (null != id) {
134             AssertUtil.notBlank(token, "sessionId cannot be blank");
135             client.setSessionID(id);
136         } else {
137             throw new IllegalStateException("login/password, token, or sessionID must be specified");
138         }
139         client.setSslCertificateTrustAllowSelfSigned(sslCertificateTrustAllowSelfSigned);
140         client.setSslHostnameVerifyAllowAll(sslHostnameVerifyAllowAll);
141         client.setSystemProxyEnabled(systemProxyEnabled);
142 
143         if (version > 0) {
144             client.setApiVersion(version);
145         }
146         return client;
147     }
148 }