Skip to content

Commit

Permalink
JAMES-3928 ldapHosts: List<String> -> List<URI>
Browse files Browse the repository at this point in the history
  • Loading branch information
quantranhong1999 authored and Arsnael committed Jul 21, 2023
1 parent 96ffd11 commit aeb4710
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

package org.apache.james.data;

import java.net.URI;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.james.GuiceModuleTestRule;
Expand All @@ -28,6 +30,7 @@
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import com.github.fge.lambdas.Throwing;
import com.google.inject.Module;

public class DockerLdapRule implements GuiceModuleTestRule {
Expand All @@ -44,9 +47,12 @@ public Statement apply(Statement statement, Description description) {
}

private LdapRepositoryConfiguration computeConfiguration(List<String> ldapIps) {
List<URI> uris = ldapIps.stream()
.map(Throwing.function(URI::new))
.collect(Collectors.toUnmodifiableList());
try {
return LdapRepositoryConfiguration.builder()
.ldapHosts(ldapIps)
.ldapHosts(uris)
.principal("cn=admin,dc=james,dc=org")
.credentials("mysecretpassword")
.userBase("ou=People,dc=james,dc=org")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.james.user.ldap;

import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
Expand All @@ -30,8 +31,10 @@
import org.apache.james.core.Domain;
import org.apache.james.core.Username;

import com.github.fge.lambdas.Throwing;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

public class LdapRepositoryConfiguration {
Expand All @@ -46,7 +49,7 @@ public class LdapRepositoryConfiguration {
private static final int DEFAULT_POOL_SIZE = 4;

public static class Builder {
private Optional<List<String>> ldapHosts;
private Optional<List<URI>> ldapHosts;
private Optional<String> principal;
private Optional<String> credentials;
private Optional<String> userBase;
Expand All @@ -68,7 +71,7 @@ public Builder() {
perDomainBaseDN = ImmutableMap.builder();
}

public Builder ldapHosts(List<String> ldapHosts) {
public Builder ldapHosts(List<URI> ldapHosts) {
this.ldapHosts = Optional.of(ldapHosts);
return this;
}
Expand Down Expand Up @@ -145,10 +148,14 @@ public static Builder builder() {
}

public static LdapRepositoryConfiguration from(HierarchicalConfiguration<ImmutableNode> configuration) throws ConfigurationException {
List<String> ldapHosts = Splitter.on(",")
List<URI> ldapHosts = Splitter.on(",")
.splitToList(Optional.ofNullable(configuration.getString("[@ldapHosts]", null))
.orElse(configuration.getString("[@ldapHost]", ""))
.trim());
.trim())
.stream()
.map(Throwing.function(URI::new))
.collect(ImmutableList.toImmutableList());

String principal = configuration.getString("[@principal]", "");
String credentials = configuration.getString("[@credentials]", "");
String userBase = configuration.getString("[@userBase]");
Expand Down Expand Up @@ -214,7 +221,7 @@ public static LdapRepositoryConfiguration from(HierarchicalConfiguration<Immutab
* attribute &quot;ldapHosts&quot; and fallback to the legacy attribute &quot;ldapHost&quot;.
* URLs are split by the comma &quot;,&quot; character.
*/
private final List<String> ldapHosts;
private final List<URI> ldapHosts;

/**
* The user with which to initially bind to the LDAP server. The value of
Expand Down Expand Up @@ -286,7 +293,7 @@ public static LdapRepositoryConfiguration from(HierarchicalConfiguration<Immutab

private final ImmutableMap<Domain, String> perDomainBaseDN;

private LdapRepositoryConfiguration(List<String> ldapHosts, String principal, String credentials, String userBase, String userIdAttribute,
private LdapRepositoryConfiguration(List<URI> ldapHosts, String principal, String credentials, String userBase, String userIdAttribute,
String userObjectClass, int connectionTimeout, int readTimeout,
boolean supportsVirtualHosting, int poolSize, ReadOnlyLDAPGroupRestriction restriction, String filter,
Optional<String> administratorId, boolean trustAllCerts,
Expand Down Expand Up @@ -322,7 +329,7 @@ private void checkState() throws ConfigurationException {
}
}

public List<String> getLdapHosts() {
public List<URI> getLdapHosts() {
return ldapHosts;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,8 @@ private SocketFactory supportLDAPS(URI uri) throws KeyManagementException, NoSuc
}
}

private ThrowingFunction<String, SingleServerSet> toSingleServerSet(LDAPConnectionOptions connectionOptions, BindRequest bindRequest) {
return Throwing.function(uriString -> {
URI uri = new URI(uriString);
return new SingleServerSet(uri.getHost(), uri.getPort(), supportLDAPS(uri), connectionOptions, bindRequest, null);
});
private ThrowingFunction<URI, SingleServerSet> toSingleServerSet(LDAPConnectionOptions connectionOptions, BindRequest bindRequest) {
return Throwing.function(uri -> new SingleServerSet(uri.getHost(), uri.getPort(), supportLDAPS(uri), connectionOptions, bindRequest, null));
}

@PreDestroy
Expand Down

0 comments on commit aeb4710

Please sign in to comment.