Skip to content

Commit efd6ebf

Browse files
author
Vladimir Kotal
committed
add username and timeout to toString()
1 parent 1d477ae commit efd6ebf

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

plugins/src/main/java/opengrok/auth/plugin/ldap/LdapServer.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public LdapServer(String server) {
6767
this.url = server;
6868
}
6969

70+
public LdapServer(String server, String username, String password) {
71+
this(prepareEnv());
72+
this.url = server;
73+
this.username = username;
74+
this.password = password;
75+
}
76+
7077
public LdapServer(Hashtable<String, String> env) {
7178
this.env = env;
7279
}
@@ -134,7 +141,7 @@ public synchronized boolean isWorking() {
134141
* @return the new connection or null
135142
*/
136143
private synchronized LdapContext connect() {
137-
LOGGER.log(Level.INFO, "Server {0} connecting", this.url);
144+
LOGGER.log(Level.INFO, "Connecting to LDAP server {0} ", this.toString());
138145

139146
if (errorTimestamp > 0 && errorTimestamp + interval > System.currentTimeMillis()) {
140147
LOGGER.log(Level.INFO, "LDAP server {0} is down", this.url);
@@ -159,7 +166,7 @@ private synchronized LdapContext connect() {
159166
ctx = new InitialLdapContext(env, null);
160167
ctx.reconnect(null);
161168
ctx.setRequestControls(null);
162-
LOGGER.log(Level.INFO, "Connected to LDAP server {0}", env.get(Context.PROVIDER_URL));
169+
LOGGER.log(Level.INFO, "Connected to LDAP server {0}", this.toString());
163170
errorTimestamp = 0;
164171
} catch (NamingException ex) {
165172
LOGGER.log(Level.INFO, "LDAP server {0} is not responding", env.get(Context.PROVIDER_URL));
@@ -252,6 +259,20 @@ private static Hashtable<String, String> prepareEnv() {
252259

253260
@Override
254261
public String toString() {
255-
return getUrl();
262+
StringBuilder sb = new StringBuilder();
263+
264+
sb.append(getUrl());
265+
266+
if (getConnectTimeout() > 0) {
267+
sb.append(" timeout: ");
268+
sb.append(getConnectTimeout());
269+
}
270+
271+
if (getUsername() != null && !getUsername().isEmpty()) {
272+
sb.append(" username: ");
273+
sb.append(getUsername());
274+
}
275+
276+
return sb.toString();
256277
}
257278
}

plugins/src/test/java/opengrok/auth/plugin/LdapFacadeTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public void testConnectTimeoutInheritance() {
4343
public void testToString() {
4444
Configuration config = new Configuration();
4545
config.setServers(Arrays.asList(new LdapServer("http://foo.foo"),
46-
new LdapServer("http://bar.bar")));
46+
new LdapServer("http://bar.bar",
47+
"cn=FOOBAR,l=amer,dc=example,dc=com", "MySecretPassword")));
4748
config.setSearchBase("dc=foo,dc=com");
4849
int timeoutValue = 42;
4950
config.setConnectTimeout(timeoutValue);

0 commit comments

Comments
 (0)