use of de.carne.certmgr.certs.io.IOResource 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.io.IOResource 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();
}
Aggregations