Search in sources :

Example 1 with CertWriter

use of de.carne.certmgr.certs.spi.CertWriter in project certmgr by hdecarne.

the class CertExportController method onCmdChooseFileDestination.

@SuppressWarnings("unused")
@FXML
void onCmdChooseFileDestination(ActionEvent evt) {
    FileChooser chooser = new FileChooser();
    List<ExtensionFilter> extensionFilters = new ArrayList<>();
    CertWriter writer = this.ctlFormatOption.getValue();
    if (writer != null) {
        extensionFilters.add(new ExtensionFilter(writer.fileType(), writer.fileExtensionPatterns()));
    }
    extensionFilters.add(FileChooserHelper.filterFromString(CertExportI18N.formatSTR_FILTER_ALLFILES()));
    chooser.getExtensionFilters().addAll(extensionFilters);
    chooser.setSelectedExtensionFilter(extensionFilters.get(0));
    chooser.setInitialDirectory(this.preferenceInitalDirectory.getValueAsFile());
    File fileSource = chooser.showSaveDialog(getUI());
    if (fileSource != null) {
        this.ctlFileDestinationInput.setText(fileSource.getAbsolutePath());
        this.preferenceInitalDirectory.putValueFromFile(fileSource.getParentFile());
        syncPreferences();
    }
}
Also used : ExtensionFilter(javafx.stage.FileChooser.ExtensionFilter) FileChooser(javafx.stage.FileChooser) ArrayList(java.util.ArrayList) CertWriter(de.carne.certmgr.certs.spi.CertWriter) File(java.io.File) FXML(javafx.fxml.FXML)

Example 2 with CertWriter

use of de.carne.certmgr.certs.spi.CertWriter in project certmgr by hdecarne.

the class CertExportController method exportToClipboard.

void exportToClipboard(CertWriter format, CertObjectStore exportObjects, boolean encryptExport) throws IOException {
    StringWriter text = new StringWriter();
    try (IOResource<Writer> out = new IOResource<>(text, CertExportI18N.formatSTR_TEXT_CLIPBOARD())) {
        if (encryptExport) {
            format.writeEncryptedString(out, exportObjects, PasswordDialog.enterNewPassword(this));
        } else {
            format.writeString(out, exportObjects);
        }
    }
    String textData = text.toString();
    if (Platform.IS_WINDOWS) {
        // JavaFX on Windows doubles Windows "\r\n" line breaks
        // We replace them Unix line breaks "\n" as a workaround.
        textData = textData.replace("\r\n", "\n");
    }
    String clipboardData = textData;
    PlatformHelper.runLater(() -> {
        Clipboard clipboard = Clipboard.getSystemClipboard();
        ClipboardContent content = new ClipboardContent();
        content.putString(clipboardData);
        clipboard.setContent(content);
    });
}
Also used : IOResource(de.carne.certmgr.certs.io.IOResource) StringWriter(java.io.StringWriter) ClipboardContent(javafx.scene.input.ClipboardContent) Clipboard(javafx.scene.input.Clipboard) Writer(java.io.Writer) CertWriter(de.carne.certmgr.certs.spi.CertWriter) StringWriter(java.io.StringWriter)

Example 3 with CertWriter

use of de.carne.certmgr.certs.spi.CertWriter in project certmgr by hdecarne.

the class CertReadersWritersTest method testReaderAndWriter.

private void testReaderAndWriter(CertReader reader, URL testResourceURL, Path testPath) throws IOException {
    System.out.println("Testing I/O provider: " + reader.providerName());
    CertWriter writer = CertWriters.REGISTERED.get(reader.providerName());
    System.out.println(reader.fileType());
    System.out.println(Arrays.toString(reader.fileExtensionPatterns()));
    if (writer != null) {
        System.out.println("isCharWriter: " + writer.isCharWriter());
        System.out.println("isEncryptionRequired: " + writer.isEncryptionRequired());
    }
    CertObjectStore readCertObjects1 = CertReaders.readURL(testResourceURL, Tests.password());
    Assert.assertNotNull(readCertObjects1);
    for (CertObjectStore.Entry entry : readCertObjects1) {
        switch(entry.type()) {
            case CRT:
                reader.fileExtension(entry.getCRT().getClass());
                break;
            case KEY:
                reader.fileExtension(entry.getKey().getClass());
                break;
            case CSR:
                reader.fileExtension(entry.getCSR().getClass());
                break;
            case CRL:
                reader.fileExtension(entry.getCRL().getClass());
                break;
        }
        reader.fileExtension(getClass());
    }
    CertObjectStore readCertObjects2;
    try (IOResource<InputStream> in = new IOResource<>(testResourceURL.openStream(), reader.providerName())) {
        readCertObjects2 = reader.readBinary(in, Tests.password());
        Assert.assertNotNull(readCertObjects2);
        Assert.assertEquals(readCertObjects1.size(), readCertObjects2.size());
    }
    if (writer != null) {
        if (!writer.isEncryptionRequired()) {
            try (IOResource<OutputStream> out = IOResource.newOutputStream(writer.providerName(), testPath)) {
                writer.writeBinary(out, readCertObjects2);
            }
            verifyWriterOutput(readCertObjects2, reader, testPath);
            if (writer.isCharWriter()) {
                try (IOResource<Writer> out = new IOResource<>(Files.newBufferedWriter(testPath), writer.providerName())) {
                    writer.writeString(out, readCertObjects2);
                }
                verifyWriterOutput(readCertObjects2, reader, testPath);
            }
        }
        try (IOResource<OutputStream> out = IOResource.newOutputStream(writer.providerName(), testPath)) {
            writer.writeEncryptedBinary(out, readCertObjects2, Tests.password());
        }
        if (writer.isCharWriter()) {
            try (IOResource<Writer> out = new IOResource<>(Files.newBufferedWriter(testPath), writer.providerName())) {
                writer.writeEncryptedString(out, readCertObjects2, Tests.password());
            }
            verifyWriterOutput(readCertObjects2, reader, testPath);
        }
        verifyWriterOutput(readCertObjects2, reader, testPath);
    }
    System.out.println();
}
Also used : IOResource(de.carne.certmgr.certs.io.IOResource) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) CertWriter(de.carne.certmgr.certs.spi.CertWriter) CertObjectStore(de.carne.certmgr.certs.CertObjectStore) CertWriter(de.carne.certmgr.certs.spi.CertWriter) Writer(java.io.Writer)

Example 4 with CertWriter

use of de.carne.certmgr.certs.spi.CertWriter in project certmgr by hdecarne.

the class CertExportController method validateAndGetFormat.

private CertWriter validateAndGetFormat() throws ValidationException {
    CertWriter writer = InputValidator.notNull(this.ctlFormatOption.getValue(), CertExportI18N::formatSTR_MESSAGE_NO_FORMAT);
    InputValidator.isTrue(this.ctlEncryptOption.isSelected() || !writer.isEncryptionRequired(), (a) -> CertExportI18N.formatSTR_MESSAGE_ENCRYPTION_REQUIRED(writer.providerName()));
    InputValidator.isTrue(!this.ctlClipboardDestinationOption.isSelected() || writer.isCharWriter(), (a) -> CertExportI18N.formatSTR_MESSAGE_NO_CHARACTER_FORMAT(writer.providerName()));
    int exportObjectCount = 0;
    if (this.ctlExportCertOption.isSelected()) {
        exportObjectCount++;
    }
    if (this.ctlExportChainOption.isSelected()) {
        // This is not exact as the chain may be of length 0..n; however for
        // simplicity (regarding code and usability) we assume 1
        exportObjectCount++;
    }
    if (this.ctlExportKeyOption.isSelected()) {
        exportObjectCount++;
    }
    if (this.ctlExportCSROption.isSelected()) {
        exportObjectCount++;
    }
    if (this.ctlExportCRLOption.isSelected()) {
        exportObjectCount++;
    }
    InputValidator.isTrue(exportObjectCount > 0, (a) -> CertExportI18N.formatSTR_MESSAGE_NO_EXPORT(writer.providerName()));
    return writer;
}
Also used : CertWriter(de.carne.certmgr.certs.spi.CertWriter)

Example 5 with CertWriter

use of de.carne.certmgr.certs.spi.CertWriter in project certmgr by hdecarne.

the class CertExportController method onCmdExport.

@SuppressWarnings("unused")
@FXML
void onCmdExport(ActionEvent evt) {
    try {
        CertWriter exportFormat = validateAndGetFormat();
        boolean encrypt = this.ctlEncryptOption.isSelected();
        boolean exportCert = this.ctlExportCertOption.isSelected();
        boolean exportChain = this.ctlExportChainOption.isSelected();
        boolean exportChainRoot = this.ctlExportChainRootOption.isSelected();
        boolean exportKey = this.ctlExportKeyOption.isSelected();
        boolean exportCSR = this.ctlExportCSROption.isSelected();
        boolean exportCRL = this.ctlExportCRLOption.isSelected();
        if (this.ctlFileDestinationOption.isSelected()) {
            Path exportFile = validateFileDestinationInput();
            getExecutorService().submit(new ExportTask<Path>(exportCert, exportChain, exportChainRoot, exportKey, exportCSR, exportCRL, exportFormat, exportFile, encrypt) {

                @Override
                protected void export(CertWriter format, @Nullable Path param, CertObjectStore exportObjects, boolean encryptExport) throws IOException {
                    exportToFile(format, Check.notNull(param), exportObjects, encryptExport);
                }
            });
        } else if (this.ctlDirectoryDestinationOption.isSelected()) {
            Path exportDirectory = validateDirectoryDestinationInput();
            getExecutorService().submit(new ExportTask<Path>(exportCert, exportChain, exportChainRoot, exportKey, exportCSR, exportCRL, exportFormat, exportDirectory, encrypt) {

                @Override
                protected void export(CertWriter format, @Nullable Path param, CertObjectStore exportObjects, boolean encryptExport) throws IOException {
                    exportToDirectory(format, Check.notNull(param), exportObjects, encryptExport);
                }
            });
        } else if (this.ctlClipboardDestinationOption.isSelected()) {
            getExecutorService().submit(new ExportTask<Void>(exportCert, exportChain, exportChainRoot, exportKey, exportCSR, exportCRL, exportFormat, null, encrypt) {

                @Override
                protected void export(CertWriter format, @Nullable Void param, CertObjectStore exportObjects, boolean encryptExport) throws IOException {
                    exportToClipboard(format, exportObjects, encryptExport);
                }
            });
        }
    } catch (ValidationException e) {
        ValidationAlerts.error(e).showAndWait();
    }
}
Also used : Path(java.nio.file.Path) ValidationException(de.carne.jfx.util.validation.ValidationException) CertWriter(de.carne.certmgr.certs.spi.CertWriter) CertObjectStore(de.carne.certmgr.certs.CertObjectStore) IOException(java.io.IOException) Nullable(de.carne.check.Nullable) FXML(javafx.fxml.FXML)

Aggregations

CertWriter (de.carne.certmgr.certs.spi.CertWriter)5 CertObjectStore (de.carne.certmgr.certs.CertObjectStore)2 IOResource (de.carne.certmgr.certs.io.IOResource)2 Writer (java.io.Writer)2 FXML (javafx.fxml.FXML)2 Nullable (de.carne.check.Nullable)1 ValidationException (de.carne.jfx.util.validation.ValidationException)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 StringWriter (java.io.StringWriter)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Clipboard (javafx.scene.input.Clipboard)1 ClipboardContent (javafx.scene.input.ClipboardContent)1 FileChooser (javafx.stage.FileChooser)1 ExtensionFilter (javafx.stage.FileChooser.ExtensionFilter)1