de.mklinger.qetcher.client.impl.QetcherClientBuilderImpl Maven / Gradle / Ivy
package de.mklinger.qetcher.client.impl;
import java.net.URI;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import de.mklinger.qetcher.client.QetcherClient;
/**
* @author Marc Klinger - mklinger[at]mklinger[dot]de
*/
public class QetcherClientBuilderImpl implements QetcherClient.Builder {
private List serviceUris;
private boolean serviceLookupDisabled;
private KeyStore trustStore;
private KeyStore keyStore;
private String keyPassword;
@Override
public QetcherClient.Builder serviceAddresses(final String... serviceAddresses) {
this.serviceUris = new ArrayList<>(Arrays.stream(serviceAddresses)
.map(s -> "https://" + s)
.map(URI::create)
.collect(Collectors.toList()));
return this;
}
@Override
public QetcherClient.Builder withServiceAddress(final String serviceAddress) {
if (serviceUris == null) {
serviceUris = new ArrayList<>();
}
serviceUris.add(URI.create("https://" + serviceAddress));
return this;
}
@Override
public QetcherClient.Builder withServiceAddress(final String host, final int port) {
return withServiceAddress(Objects.requireNonNull(host) + ":" + requireValidPort(port));
}
private static int requireValidPort(final int port) {
if (port <= 0) {
throw new IllegalArgumentException("Invalid port: " + port);
}
return port;
}
public List getServiceUris() {
return serviceUris;
}
@Override
public QetcherClient.Builder disableServiceLookup() {
this.serviceLookupDisabled = true;
return this;
}
public boolean isServiceLookupDisabled() {
return this.serviceLookupDisabled;
}
@Override
public QetcherClient.Builder trustStore(final KeyStore trustStore) {
this.trustStore = trustStore;
return this;
}
public KeyStore getTrustStore() {
return trustStore;
}
@Override
public QetcherClient.Builder keyStore(final KeyStore keyStore, final String keyPassword) {
this.keyStore = keyStore;
this.keyPassword = keyPassword;
return this;
}
public KeyStore getKeyStore() {
return keyStore;
}
public String getKeyPassword() {
return keyPassword;
}
@Override
public QetcherClient build() {
return InstanceFactory.newInstance(this);
}
}