Skip to content

Commit a196c87

Browse files
authored
Remove unnecessary public modifier (#290)
- Remove unnecessary public modifier - private fields - Remove some whitespace
1 parent db82ca4 commit a196c87

File tree

7 files changed

+19
-49
lines changed

7 files changed

+19
-49
lines changed

avaje-jex-ssl/src/main/java/io/avaje/jex/ssl/DSslPlugin.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,16 @@
88

99
final class DSslPlugin implements SslPlugin {
1010

11-
final HttpsConfigurator sslConfigurator;
11+
private final HttpsConfigurator sslConfigurator;
1212

1313
DSslPlugin(Consumer<SslConfig> consumer) {
14-
1514
final var config = new DSslConfig();
16-
1715
consumer.accept(config);
18-
sslConfigurator = SSLConfigurator.create(config);
16+
this.sslConfigurator = SSLConfigurator.create(config);
1917
}
2018

2119
@Override
2220
public void apply(Jex jex) {
23-
2421
jex.config().httpsConfig(sslConfigurator);
2522
}
2623
}

avaje-jex-ssl/src/main/java/io/avaje/jex/ssl/DTrustConfig.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public TrustConfig certificateFromClasspath(String certificateFile) {
2626

2727
@Override
2828
public TrustConfig certificateFromInputStream(InputStream certificateInputStream) {
29-
3029
certificates.addAll(KeyStoreUtil.parseCertificates(certificateInputStream));
31-
3230
return this;
3331
}
3432

@@ -42,11 +40,11 @@ public TrustConfig certificateFromPath(String certificatePath) {
4240
return this;
4341
}
4442

45-
public List<Certificate> certificates() {
43+
List<Certificate> certificates() {
4644
return certificates;
4745
}
4846

49-
public List<KeyStore> keyStores() {
47+
List<KeyStore> keyStores() {
5048
return keyStores;
5149
}
5250

@@ -62,12 +60,8 @@ public TrustConfig trustStoreFromClasspath(String trustStoreFile, String trustSt
6260
}
6361

6462
@Override
65-
public TrustConfig trustStoreFromInputStream(
66-
InputStream trustStoreInputStream, String trustStorePassword) {
67-
68-
keyStores.add(
69-
KeyStoreUtil.loadKeyStore(trustStoreInputStream, trustStorePassword.toCharArray()));
70-
63+
public TrustConfig trustStoreFromInputStream(InputStream trustStoreInputStream, String trustStorePassword) {
64+
keyStores.add(KeyStoreUtil.loadKeyStore(trustStoreInputStream, trustStorePassword.toCharArray()));
7165
return this;
7266
}
7367

avaje-jex-ssl/src/main/java/io/avaje/jex/ssl/KeyStoreUtil.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ final class KeyStoreUtil {
3535
Pattern.DOTALL);
3636

3737
static KeyStore loadKeyStore(InputStream inputStream, char[] password) {
38-
3938
// Read all bytes first so we can try different formats
4039
byte[] data;
4140
try {
@@ -60,14 +59,12 @@ static KeyStore loadKeyStore(InputStream inputStream, char[] password) {
6059
return keyStore;
6160
}
6261

63-
throw new SslConfigException(
64-
"Unable to load KeyStore - format not recognized or invalid password");
62+
throw new SslConfigException("Unable to load KeyStore - format not recognized or invalid password");
6563
}
6664

6765
private static KeyStore tryLoadKeyStore(byte[] data, String type, char[] password) {
6866
try (var bis = new ByteArrayInputStream(data)) {
6967
var keyStore = KeyStore.getInstance(type);
70-
7168
keyStore.load(bis, password);
7269
return keyStore;
7370
} catch (Exception e) {
@@ -80,16 +77,13 @@ static X509ExtendedKeyManager loadIdentityFromPem(
8077
InputStream certificateInputStream, String privateKeyContent, char[] password) {
8178
try {
8279
var certificates = parseCertificates(certificateInputStream);
83-
8480
var privateKey = parsePrivateKey(privateKeyContent, password);
85-
8681
var keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
8782
keyStore.load(null, null);
8883

8984
var certChain = certificates.toArray(new Certificate[0]);
9085
var alias = "identity";
9186
var keyPassword = password != null ? password : new char[0];
92-
9387
keyStore.setKeyEntry(alias, privateKey, keyPassword, certChain);
9488

9589
var kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
@@ -113,9 +107,8 @@ static X509ExtendedKeyManager loadIdentityFromPem(
113107
}
114108

115109
static List<Certificate> parsePemCertificates(String content) {
116-
117110
List<Certificate> certs = new ArrayList<>();
118-
CertificateFactory factory = null;
111+
CertificateFactory factory;
119112
try {
120113
factory = CertificateFactory.getInstance("X.509");
121114

@@ -132,11 +125,9 @@ static List<Certificate> parsePemCertificates(String content) {
132125
} catch (Exception e) {
133126
throw new SslConfigException("Failed to parse PEM certificate", e);
134127
}
135-
136128
if (certs.isEmpty()) {
137129
throw new SslConfigException("No valid certificate found in PEM content");
138130
}
139-
140131
return certs;
141132
}
142133

@@ -182,7 +173,6 @@ static List<Certificate> parseCertificates(InputStream inputStream) {
182173

183174
// Try to parse as PEM first (check if it contains PEM markers)
184175
var content = new String(data, StandardCharsets.UTF_8);
185-
186176
if (content.contains("-----BEGIN CERTIFICATE-----")) {
187177
certs.addAll(parsePemCertificates(content));
188178
} else {
@@ -195,7 +185,6 @@ static List<Certificate> parseCertificates(InputStream inputStream) {
195185
throw new SslConfigException("Unable to load KeyStore", e);
196186
}
197187
}
198-
199188
return certs;
200189
}
201190

avaje-jex-ssl/src/main/java/io/avaje/jex/ssl/SSLConfigurator.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class SSLConfigurator extends HttpsConfigurator {
2525

2626
private final boolean clientAuth;
2727

28-
public SSLConfigurator(SSLContext context, boolean clientAuth) {
28+
SSLConfigurator(SSLContext context, boolean clientAuth) {
2929
super(context);
3030
this.clientAuth = clientAuth;
3131
}
@@ -40,7 +40,6 @@ public void configure(HttpsParameters params) {
4040
static SSLConfigurator create(DSslConfig sslConfig) throws SslConfigException {
4141
try {
4242
var sslContext = createContext(sslConfig);
43-
4443
var keyManagers = createKeyManagers(sslConfig);
4544
var trustManagers = createTrustManagers(sslConfig);
4645

@@ -60,7 +59,6 @@ private static SSLContext createContext(DSslConfig sslConfig) throws NoSuchAlgor
6059
}
6160

6261
private static KeyManager[] createKeyManagers(DSslConfig sslConfig) throws SslConfigException {
63-
6462
try {
6563
return switch (sslConfig.loadedIdentity()) {
6664
case KEY_MANAGER -> new KeyManager[] {sslConfig.keyManager()};
@@ -81,32 +79,27 @@ private static KeyManager[] createKeyManagersFromKeyStore(DSslConfig sslConfig)
8179
return keyManagerFactory.getKeyManagers();
8280
}
8381

84-
private static KeyManagerFactory createKeyManagerFactory(DSslConfig sslConfig)
85-
throws NoSuchAlgorithmException {
82+
private static KeyManagerFactory createKeyManagerFactory(DSslConfig sslConfig) throws NoSuchAlgorithmException {
8683
if (sslConfig.securityProvider() != null) {
8784
return KeyManagerFactory.getInstance(KEY_MANAGER_ALGORITHM, sslConfig.securityProvider());
8885
}
8986
return KeyManagerFactory.getInstance(KEY_MANAGER_ALGORITHM);
9087
}
9188

92-
private static TrustManager[] createTrustManagers(DSslConfig sslConfig)
93-
throws SslConfigException {
89+
private static TrustManager[] createTrustManagers(DSslConfig sslConfig) throws SslConfigException {
9490
var trustConfig = sslConfig.trustConfig();
95-
9691
if (trustConfig == null) {
9792
return null; // Use system default trust managers
9893
}
9994

10095
try {
10196
var trustStores = trustConfig.keyStores();
10297
var certificates = trustConfig.certificates();
103-
10498
if (trustStores.isEmpty() && certificates.isEmpty()) {
10599
return null; // No custom trust configuration
106100
}
107101

108102
var trustStore = createCombinedTrustStore(trustStores, certificates);
109-
110103
var trustManagerFactory = createTrustManagerFactory(sslConfig);
111104
trustManagerFactory.init(trustStore);
112105

@@ -116,19 +109,15 @@ private static TrustManager[] createTrustManagers(DSslConfig sslConfig)
116109
}
117110
}
118111

119-
private static TrustManagerFactory createTrustManagerFactory(DSslConfig sslConfig)
120-
throws NoSuchAlgorithmException {
112+
private static TrustManagerFactory createTrustManagerFactory(DSslConfig sslConfig) throws NoSuchAlgorithmException {
121113
if (sslConfig.securityProvider() != null) {
122114
return TrustManagerFactory.getInstance(TRUST_MANAGER_ALGORITHM, sslConfig.securityProvider());
123115
}
124116
return TrustManagerFactory.getInstance(TRUST_MANAGER_ALGORITHM);
125117
}
126118

127-
private static KeyStore createCombinedTrustStore(
128-
List<KeyStore> trustStores, List<Certificate> certificates) throws Exception {
129-
119+
private static KeyStore createCombinedTrustStore(List<KeyStore> trustStores, List<Certificate> certificates) throws Exception {
130120
var combinedTrustStore = KeyStore.getInstance(KeyStore.getDefaultType());
131-
132121
combinedTrustStore.load(null, null);
133122

134123
// Add certificates from existing trust stores

avaje-jex-ssl/src/main/java/io/avaje/jex/ssl/SslConfig.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ default void pemFromClasspath(String certificateFile, String privateKeyFile) {
9696
* @param certificateInputStream input stream to the certificate chain PEM file.
9797
* @param privateKeyInputStream input stream to the private key PEM file.
9898
*/
99-
default void pemFromInputStream(
100-
InputStream certificateInputStream, InputStream privateKeyInputStream) {
99+
default void pemFromInputStream(InputStream certificateInputStream, InputStream privateKeyInputStream) {
101100
pemFromInputStream(certificateInputStream, privateKeyInputStream, null);
102101
}
103102

@@ -162,7 +161,7 @@ default void pemFromString(String certificateString, String privateKeyString) {
162161
/**
163162
* Configure the Provider for the SSLContext.
164163
*
165-
* @param trustConfigConsumer consumer to configure the trust configuration.
164+
* @param securityProvider the security provider to use.
166165
*/
167166
void securityProvider(Provider securityProvider);
168167

avaje-jex-ssl/src/main/java/io/avaje/jex/ssl/SslConfigException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.avaje.jex.ssl;
22

3+
/**
4+
* SSL Configuration exception.
5+
*/
36
public class SslConfigException extends RuntimeException {
47

58
public SslConfigException(String message, Throwable t) {

avaje-jex-ssl/src/main/java/io/avaje/jex/ssl/TrustConfig.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public interface TrustConfig {
6060
* @param trustStorePassword password for the trust store.
6161
* @return The updated TrustConfig instance for method chaining.
6262
*/
63-
TrustConfig trustStoreFromInputStream(
64-
InputStream trustStoreInputStream, String trustStorePassword);
63+
TrustConfig trustStoreFromInputStream(InputStream trustStoreInputStream, String trustStorePassword);
6564

6665
/**
6766
* Load a trust store from a given path in the system. The trust store can be in JKS or PKCS12

0 commit comments

Comments
 (0)