/** * Detects the type of KeyStore / TrustStore file from the file extension. * If the file name ends with ".jks", returns <code>StoreFileType.JKS</code>. * If the file name ends with ".pem", returns <code>StoreFileType.PEM</code>. * Otherwise, throws an IllegalArgumentException. * @param filename the filename of the key store or trust store file. * @return a KeyStoreFileType. * @throws IllegalArgumentException if the filename does not end with * ".jks" or ".pem". */ public static KeyStoreFileType fromFilename(String filename) { int i = filename.lastIndexOf('.'); if (i >= 0) { String extension = filename.substring(i); for (KeyStoreFileType storeFileType : KeyStoreFileType.values()) { if (storeFileType.getDefaultFileExtension().equals(extension)) { return storeFileType; } } } throw new IllegalArgumentException( "Unable to auto-detect store file type from file name: " + filename); }
/** * Returns the path to the trust store file in the given format (JKS or PEM). Note that the file is created lazily, * the first time this method is called. The trust store file is temporary and will be deleted on exit. * @param storeFileType the store file type (JKS or PEM). * @return the path to the trust store file. * @throws IOException if there is an error creating the trust store file. */ public File getTrustStoreFile(KeyStoreFileType storeFileType) throws IOException { switch (storeFileType) { case JKS: return getTrustStoreJksFile(); case PEM: return getTrustStorePemFile(); default: throw new IllegalArgumentException("Invalid trust store type: " + storeFileType + ", must be one of: " + Arrays.toString(KeyStoreFileType.values())); } }
/** * Returns the path to the key store file in the given format (JKS or PEM). Note that the file is created lazily, * the first time this method is called. The key store file is temporary and will be deleted on exit. * @param storeFileType the store file type (JKS or PEM). * @return the path to the key store file. * @throws IOException if there is an error creating the key store file. */ public File getKeyStoreFile(KeyStoreFileType storeFileType) throws IOException { switch (storeFileType) { case JKS: return getKeyStoreJksFile(); case PEM: return getKeyStorePemFile(); default: throw new IllegalArgumentException("Invalid key store type: " + storeFileType + ", must be one of: " + Arrays.toString(KeyStoreFileType.values())); } }