diff --git a/src/main/java/org/jabref/gui/JabRefMain.java b/src/main/java/org/jabref/gui/JabRefMain.java index ce9ea5b4546..665f38d18b4 100644 --- a/src/main/java/org/jabref/gui/JabRefMain.java +++ b/src/main/java/org/jabref/gui/JabRefMain.java @@ -22,6 +22,7 @@ import org.jabref.logic.net.ProxyPreferences; import org.jabref.logic.net.ProxyRegisterer; import org.jabref.logic.net.ssl.SSLPreferences; +import org.jabref.logic.net.ssl.TrustStoreManager; import org.jabref.logic.protectedterms.ProtectedTermsLoader; import org.jabref.logic.remote.RemotePreferences; import org.jabref.logic.remote.client.RemoteClient; @@ -151,6 +152,7 @@ private static void configureProxy(ProxyPreferences proxyPreferences) { } private static void configureSSL(SSLPreferences sslPreferences) { + TrustStoreManager.createTruststoreFileIfNotExist(Path.of(sslPreferences.getTruststorePath())); System.setProperty("javax.net.ssl.trustStore", sslPreferences.getTruststorePath()); System.setProperty("javax.net.ssl.trustStorePassword", "changeit"); } diff --git a/src/main/java/org/jabref/logic/net/ssl/TrustStoreManager.java b/src/main/java/org/jabref/logic/net/ssl/TrustStoreManager.java index 9aa5c8a881e..01e71b2d30e 100644 --- a/src/main/java/org/jabref/logic/net/ssl/TrustStoreManager.java +++ b/src/main/java/org/jabref/logic/net/ssl/TrustStoreManager.java @@ -31,19 +31,7 @@ public class TrustStoreManager { public TrustStoreManager(Path storePath) { this.storePath = storePath; - try { - LOGGER.info("Trust store path: {}", storePath.toAbsolutePath()); - Path storeResourcePath = Path.of(TrustStoreManager.class.getResource("/ssl/truststore.jks").toURI()); - Files.createDirectories(storePath.getParent()); - if (Files.notExists(storePath)) { - Files.copy(storeResourcePath, storePath); - } - } catch (IOException e) { - LOGGER.warn("Bad truststore path", e); - } catch (URISyntaxException e) { - LOGGER.warn("Bad resource path", e); - } - + createTruststoreFileIfNotExist(storePath); try { store = KeyStore.getInstance(KeyStore.getDefaultType()); store.load(new FileInputStream(storePath.toFile()), STORE_PASSWORD.toCharArray()); @@ -141,4 +129,25 @@ public X509Certificate getCertificate(String alias) { } return null; } + + /** + * This method checks to see if the truststore is present in {@code storePath}, + * and if it isn't, it copies the default JDK truststore to the specified location. + * + * @param storePath path of the truststore + */ + public static void createTruststoreFileIfNotExist(Path storePath) { + try { + LOGGER.info("Trust store path: {}", storePath.toAbsolutePath()); + Path storeResourcePath = Path.of(TrustStoreManager.class.getResource("/ssl/truststore.jks").toURI()); + Files.createDirectories(storePath.getParent()); + if (Files.notExists(storePath)) { + Files.copy(storeResourcePath, storePath); + } + } catch (IOException e) { + LOGGER.warn("Bad truststore path", e); + } catch (URISyntaxException e) { + LOGGER.warn("Bad resource path", e); + } + } }