Skip to content

Commit

Permalink
Reduced code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakky54 committed Jul 21, 2023
1 parent d7f91eb commit fe8494b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
24 changes: 9 additions & 15 deletions sslcontext-kickstart/src/main/java/nl/altindag/ssl/SSLFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static nl.altindag.ssl.util.internal.CollectorsUtils.toStringArray;
import static nl.altindag.ssl.util.internal.ValidationUtils.requireNotBlank;
import static nl.altindag.ssl.util.internal.ValidationUtils.requireNotEmpty;

Expand Down Expand Up @@ -904,27 +905,20 @@ private SSLParameters createSslParameters(SSLContext sslContext) {
List<String> defaultCiphers = Arrays.asList(defaultSSLParameters.getCipherSuites());
List<String> defaultProtocols = Arrays.asList(defaultSSLParameters.getProtocols());

List<String> preferredCiphers = ciphers.stream()
String[] preferredCiphers = ciphers.stream()
.distinct()
.filter(StringUtils::isNotBlank)
.filter(defaultCiphers::contains)
.collect(Collectors.toList());

if (preferredCiphers.isEmpty()) {
sslParameters.setCipherSuites(defaultCiphers.stream().toArray(String[]::new));
} else {
sslParameters.setCipherSuites(preferredCiphers.stream().toArray(String[]::new));
}
.collect(toStringArray());

List<String> preferredProtocols = protocols.stream()
String[] preferredProtocols = protocols.stream()
.distinct()
.filter(StringUtils::isNotBlank)
.filter(defaultProtocols::contains)
.collect(Collectors.toList());
.collect(toStringArray());

if (preferredProtocols.isEmpty()) {
sslParameters.setProtocols(defaultProtocols.stream().toArray(String[]::new));
} else {
sslParameters.setProtocols(preferredProtocols.stream().toArray(String[]::new));
}
sslParameters.setCipherSuites(preferredCiphers);
sslParameters.setProtocols(preferredProtocols);

return SSLParametersUtils.merge(sslParameters, defaultSSLParameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public static SSLParameters merge(SSLParameters baseSslParameters, SSLParameters
SSLParameters target = new SSLParameters();

String[] ciphers = Optional.ofNullable(baseSslParameters.getCipherSuites())
.filter(array -> array.length != 0)
.orElseGet(alternativeSslParameters::getCipherSuites);
String[] protocols = Optional.ofNullable(baseSslParameters.getProtocols())
.filter(array -> array.length != 0)
.orElseGet(alternativeSslParameters::getProtocols);

target.setCipherSuites(ciphers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collector;
import java.util.stream.Collectors;

Expand All @@ -45,4 +46,12 @@ private CollectorsUtils() {
return Collectors.collectingAndThen(Collectors.toList(), finisher);
}

public static <T> Collector<T, ?, T[]> toArray(IntFunction<T[]> generator) {
return Collectors.collectingAndThen(Collectors.toList(), list -> list.toArray(generator));
}

public static Collector<String, ?, String[]> toStringArray() {
return toArray(String[]::new);
}

}

0 comments on commit fe8494b

Please sign in to comment.