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();
}
}
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);
});
}
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();
}
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;
}
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();
}
}
Aggregations