Search in sources :

Example 1 with CertReader

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();
    }
}
Also used : ExtensionFilter(javafx.stage.FileChooser.ExtensionFilter) FileChooser(javafx.stage.FileChooser) ArrayList(java.util.ArrayList) CertReader(de.carne.certmgr.certs.spi.CertReader) File(java.io.File) FXML(javafx.fxml.FXML)

Example 2 with CertReader

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);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) CertReader(de.carne.certmgr.certs.spi.CertReader) URL(java.net.URL) Test(org.junit.Test)

Example 3 with CertReader

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;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) IOException(java.io.IOException) CertObjectStore(de.carne.certmgr.certs.CertObjectStore) CertReader(de.carne.certmgr.certs.spi.CertReader) ArrayDeque(java.util.ArrayDeque) InvalidPathException(java.nio.file.InvalidPathException) Nullable(de.carne.check.Nullable)

Example 4 with CertReader

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;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) CertObjectStore(de.carne.certmgr.certs.CertObjectStore) CertReader(de.carne.certmgr.certs.spi.CertReader) ArrayDeque(java.util.ArrayDeque) Nullable(de.carne.check.Nullable)

Aggregations

CertReader (de.carne.certmgr.certs.spi.CertReader)4 Path (java.nio.file.Path)3 CertObjectStore (de.carne.certmgr.certs.CertObjectStore)2 Nullable (de.carne.check.Nullable)2 InputStream (java.io.InputStream)2 ArrayDeque (java.util.ArrayDeque)2 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 InvalidPathException (java.nio.file.InvalidPathException)1 ArrayList (java.util.ArrayList)1 FXML (javafx.fxml.FXML)1 FileChooser (javafx.stage.FileChooser)1 ExtensionFilter (javafx.stage.FileChooser.ExtensionFilter)1 Test (org.junit.Test)1