use of de.carne.certmgr.certs.spi.CertReader in project certmgr by hdecarne.
the class CertImportController method onCmdChooseFileSource.
@SuppressWarnings("unused")
@FXML
void onCmdChooseFileSource(ActionEvent evt) {
FileChooser chooser = new FileChooser();
List<ExtensionFilter> extensionFilters = new ArrayList<>();
extensionFilters.add(FileChooserHelper.filterFromString(CertImportI18N.formatSTR_FILTER_ALLFILES()));
for (CertReader reader : CertReaders.REGISTERED.providers()) {
extensionFilters.add(new ExtensionFilter(reader.fileType(), reader.fileExtensionPatterns()));
}
chooser.getExtensionFilters().addAll(extensionFilters);
chooser.setSelectedExtensionFilter(extensionFilters.get(0));
chooser.setInitialDirectory(this.preferenceInitalDirectory.getValueAsFile());
File fileSource = chooser.showOpenDialog(getUI());
if (fileSource != null) {
this.ctlFileSourceInput.setText(fileSource.getAbsolutePath());
this.preferenceInitalDirectory.putValueFromFile(fileSource.getParentFile());
syncPreferences();
validateAndReloadFileSource();
}
}
use of de.carne.certmgr.certs.spi.CertReader in project certmgr by hdecarne.
the class CertReadersWritersTest method testReadersAndWriters.
/**
* Test all Readers and Writers for the different object types.
*
* @throws IOException
*/
@Test
public void testReadersAndWriters() throws IOException {
for (CertReader reader : CertReaders.REGISTERED.providers()) {
for (int resourceIndex = 1; ; resourceIndex++) {
URL testResourceURL = getClass().getResource(reader.providerName() + "." + resourceIndex + ".dat");
if (testResourceURL == null) {
break;
}
Path testPath = Files.createTempFile(getClass().getSimpleName(), null);
try {
testReaderAndWriter(reader, testResourceURL, testPath);
} finally {
Files.delete(testPath);
}
}
}
}
use of de.carne.certmgr.certs.spi.CertReader in project certmgr by hdecarne.
the class CertReaders method readURL.
/**
* Read all available certificate objects from an {@link URL}.
* <p>
* All registered {@link CertReader}s are considered for reading certificate object until one recognizes the file
* data.
*
* @param url The URL to read from.
* @param password The callback to use for querying passwords (if needed).
* @return The read certificate objects, or {@code null} if no certificate data was recognized.
* @throws IOException if an I/O error occurs during reading/decoding.
*/
@Nullable
public static CertObjectStore readURL(URL url, PasswordCallback password) throws IOException {
Deque<CertReader> certReaders = new ArrayDeque<>();
Path file;
try {
String urlPath = url.getPath();
int fileNameIndex = urlPath.lastIndexOf('/');
String fileName = (fileNameIndex >= 0 ? urlPath.substring(fileNameIndex + 1) : urlPath);
file = Paths.get(fileName);
} catch (InvalidPathException e) {
throw new IOException(e.getLocalizedMessage(), e);
}
for (CertReader reader : REGISTERED.providers()) {
if (matchFileName(reader, file)) {
certReaders.addFirst(reader);
} else {
certReaders.addLast(reader);
}
}
CertObjectStore certObjects = null;
for (CertReader reader : certReaders) {
try (IOResource<InputStream> in = new IOResource<>(url.openStream(), file.toString())) {
certObjects = reader.readBinary(in, password);
} catch (IOException e) {
Exceptions.ignore(e);
}
if (certObjects != null) {
break;
}
}
return certObjects;
}
use of de.carne.certmgr.certs.spi.CertReader in project certmgr by hdecarne.
the class CertReaders method readFile.
/**
* Read all available certificate objects from a file.
* <p>
* All registered {@link CertReader}s are considered for reading certificate object until one recognizes the file
* data.
*
* @param file The file to read from.
* @param password The callback to use for querying passwords (if needed).
* @return The read certificate objects, or {@code null} if no certificate data was recognized.
* @throws IOException if an I/O error occurs during reading/decoding.
*/
@Nullable
public static CertObjectStore readFile(Path file, PasswordCallback password) throws IOException {
Deque<CertReader> certReaders = new ArrayDeque<>();
Path fileName = file.getFileName();
for (CertReader reader : REGISTERED.providers()) {
if (matchFileName(reader, fileName)) {
certReaders.addFirst(reader);
} else {
certReaders.addLast(reader);
}
}
CertObjectStore certObjects = null;
for (CertReader reader : certReaders) {
try (IOResource<InputStream> in = IOResource.newInputStream(file.toString(), file, StandardOpenOption.READ)) {
certObjects = reader.readBinary(in, password);
if (certObjects != null) {
break;
}
}
}
return certObjects;
}
Aggregations