Search in sources :

Example 26 with StreamCallback

use of org.apache.nifi.processor.io.StreamCallback in project nifi by apache.

the class OpenPGPPasswordBasedEncryptorTest method testShouldDecryptExternalFile.

@Test
public void testShouldDecryptExternalFile() throws Exception {
    // Arrange
    byte[] plainBytes = Files.readAllBytes(Paths.get(plainFile.getPath()));
    final String PLAINTEXT = new String(plainBytes, "UTF-8");
    InputStream cipherStream = new FileInputStream(encryptedFile);
    OutputStream recoveredStream = new ByteArrayOutputStream();
    // No file, just streams
    String filename = encryptedFile.getName();
    OpenPGPPasswordBasedEncryptor encryptor = new OpenPGPPasswordBasedEncryptor(EncryptionMethod.PGP.getAlgorithm(), EncryptionMethod.PGP.getProvider(), LEGACY_PASSWORD.toCharArray(), filename);
    StreamCallback decryptionCallback = encryptor.getDecryptionCallback();
    // Act
    decryptionCallback.process(cipherStream, recoveredStream);
    // Assert
    byte[] recoveredBytes = ((ByteArrayOutputStream) recoveredStream).toByteArray();
    String recovered = new String(recoveredBytes, "UTF-8");
    logger.info("Recovered: {}", recovered);
    Assert.assertEquals("Recovered text", PLAINTEXT, recovered);
}
Also used : FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) StreamCallback(org.apache.nifi.processor.io.StreamCallback) Test(org.junit.Test)

Example 27 with StreamCallback

use of org.apache.nifi.processor.io.StreamCallback in project nifi by apache.

the class OpenPGPPasswordBasedEncryptorTest method testShouldEncryptAndDecrypt.

@Test
public void testShouldEncryptAndDecrypt() throws Exception {
    // Arrange
    final String PLAINTEXT = "This is a plaintext message.";
    logger.info("Plaintext: {}", PLAINTEXT);
    InputStream plainStream = new java.io.ByteArrayInputStream(PLAINTEXT.getBytes("UTF-8"));
    OutputStream cipherStream = new ByteArrayOutputStream();
    OutputStream recoveredStream = new ByteArrayOutputStream();
    // No file, just streams
    String filename = "tempFile.txt";
    OpenPGPPasswordBasedEncryptor encryptor = new OpenPGPPasswordBasedEncryptor(EncryptionMethod.PGP.getAlgorithm(), EncryptionMethod.PGP.getProvider(), PASSWORD.toCharArray(), filename);
    StreamCallback encryptionCallback = encryptor.getEncryptionCallback();
    StreamCallback decryptionCallback = encryptor.getDecryptionCallback();
    // Act
    encryptionCallback.process(plainStream, cipherStream);
    final byte[] cipherBytes = ((ByteArrayOutputStream) cipherStream).toByteArray();
    logger.info("Encrypted: {}", Hex.encodeHexString(cipherBytes));
    InputStream cipherInputStream = new ByteArrayInputStream(cipherBytes);
    decryptionCallback.process(cipherInputStream, recoveredStream);
    // Assert
    byte[] recoveredBytes = ((ByteArrayOutputStream) recoveredStream).toByteArray();
    String recovered = new String(recoveredBytes, "UTF-8");
    logger.info("Recovered: {}", recovered);
    assert PLAINTEXT.equals(recovered);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamCallback(org.apache.nifi.processor.io.StreamCallback) Test(org.junit.Test)

Aggregations

StreamCallback (org.apache.nifi.processor.io.StreamCallback)27 InputStream (java.io.InputStream)25 OutputStream (java.io.OutputStream)25 FlowFile (org.apache.nifi.flowfile.FlowFile)23 IOException (java.io.IOException)16 ProcessException (org.apache.nifi.processor.exception.ProcessException)15 StopWatch (org.apache.nifi.util.StopWatch)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileInputStream (java.io.FileInputStream)9 Test (org.junit.Test)8 ComponentLog (org.apache.nifi.logging.ComponentLog)7 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)6 OutputStreamCallback (org.apache.nifi.processor.io.OutputStreamCallback)6 FileOutputStream (java.io.FileOutputStream)5 FilterOutputStream (java.io.FilterOutputStream)5 MockFlowFile (org.apache.nifi.util.MockFlowFile)4 BufferedInputStream (java.io.BufferedInputStream)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Schema (org.apache.avro.Schema)3