Search in sources :

Example 21 with ByteArrayOutputStream

use of org.apache.nifi.stream.io.ByteArrayOutputStream in project nifi by apache.

the class TestExtractEmailHeaders method testStrictParsingFailsForInvalidAddresses.

/**
 * NIFI-4326 adds a new feature to disable strict address parsing for
 * mailbox list header fields. This is a test case that asserts that
 * strict address parsing fails (when set to "strict=true") for malformed
 * addresses.
 */
@Test
public void testStrictParsingFailsForInvalidAddresses() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailHeaders());
    runner.setProperty(ExtractEmailHeaders.STRICT_PARSING, "true");
    MimeMessage simpleEmailMimeMessage = attachmentGenerator.SimpleEmailMimeMessage();
    simpleEmailMimeMessage.setHeader("From", "<bad_email>");
    simpleEmailMimeMessage.setHeader("To", "<>, Joe, <invalid>");
    ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
    try {
        simpleEmailMimeMessage.writeTo(messageBytes);
    } catch (IOException | MessagingException e) {
        e.printStackTrace();
    }
    runner.enqueue(messageBytes.toByteArray());
    runner.run();
    runner.assertTransferCount(ExtractEmailHeaders.REL_SUCCESS, 0);
    runner.assertTransferCount(ExtractEmailHeaders.REL_FAILURE, 1);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) TestRunner(org.apache.nifi.util.TestRunner) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 22 with ByteArrayOutputStream

use of org.apache.nifi.stream.io.ByteArrayOutputStream in project nifi by apache.

the class TestExtractEmailHeaders method testValidEmailWithNoRecipients.

/**
 * Test case added for NIFI-4326 for a potential NPE bug
 * if the email message contains no recipient header fields, ie,
 * TO, CC, BCC.
 */
@Test
public void testValidEmailWithNoRecipients() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailHeaders());
    runner.setProperty(ExtractEmailHeaders.CAPTURED_HEADERS, "MIME-Version");
    MimeMessage simpleEmailMimeMessage = attachmentGenerator.SimpleEmailMimeMessage();
    simpleEmailMimeMessage.removeHeader("To");
    simpleEmailMimeMessage.removeHeader("Cc");
    simpleEmailMimeMessage.removeHeader("Bcc");
    ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
    try {
        simpleEmailMimeMessage.writeTo(messageBytes);
    } catch (IOException | MessagingException e) {
        e.printStackTrace();
    }
    runner.enqueue(messageBytes.toByteArray());
    runner.run();
    runner.assertTransferCount(ExtractEmailHeaders.REL_SUCCESS, 1);
    runner.assertTransferCount(ExtractEmailHeaders.REL_FAILURE, 0);
    runner.assertQueueEmpty();
    final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(ExtractEmailHeaders.REL_SUCCESS);
    splits.get(0).assertAttributeEquals("email.headers.from.0", from);
    splits.get(0).assertAttributeExists("email.headers.mime-version");
    splits.get(0).assertAttributeNotExists("email.headers.to");
    splits.get(0).assertAttributeNotExists("email.headers.cc");
    splits.get(0).assertAttributeNotExists("email.headers.bcc");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) TestRunner(org.apache.nifi.util.TestRunner) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 23 with ByteArrayOutputStream

use of org.apache.nifi.stream.io.ByteArrayOutputStream in project nifi by apache.

the class TestExtractEmailHeaders method testNonStrictParsingPassesForInvalidAddresses.

/**
 * NIFI-4326 adds a new feature to disable strict address parsing for
 * mailbox list header fields. This is a test case that asserts that
 * lax address parsing passes (when set to "strict=false") for malformed
 * addresses.
 */
@Test
public void testNonStrictParsingPassesForInvalidAddresses() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailHeaders());
    runner.setProperty(ExtractEmailHeaders.STRICT_PARSING, "false");
    MimeMessage simpleEmailMimeMessage = attachmentGenerator.SimpleEmailMimeMessage();
    simpleEmailMimeMessage.setHeader("From", "<bad_email>");
    simpleEmailMimeMessage.setHeader("To", "<>, Joe, \"\" <>");
    ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
    try {
        simpleEmailMimeMessage.writeTo(messageBytes);
    } catch (IOException | MessagingException e) {
        e.printStackTrace();
    }
    runner.enqueue(messageBytes.toByteArray());
    runner.run();
    runner.assertTransferCount(ExtractEmailHeaders.REL_SUCCESS, 1);
    runner.assertTransferCount(ExtractEmailHeaders.REL_FAILURE, 0);
    runner.assertQueueEmpty();
    final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(ExtractEmailHeaders.REL_SUCCESS);
    splits.get(0).assertAttributeEquals("email.headers.from.0", "bad_email");
    splits.get(0).assertAttributeEquals("email.headers.to.0", "");
    splits.get(0).assertAttributeEquals("email.headers.to.1", "Joe");
    splits.get(0).assertAttributeEquals("email.headers.to.2", "");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) TestRunner(org.apache.nifi.util.TestRunner) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 24 with ByteArrayOutputStream

use of org.apache.nifi.stream.io.ByteArrayOutputStream in project nifi by apache.

the class CaptureServlet method doPost.

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Capture all the headers for reference.  Intentionally choosing to not special handling for headers with multiple values for clarity
    final Enumeration<String> headerNames = request.getHeaderNames();
    lastPostHeaders = new HashMap<>();
    while (headerNames.hasMoreElements()) {
        final String nextHeader = headerNames.nextElement();
        lastPostHeaders.put(nextHeader, request.getHeader(nextHeader));
    }
    try {
        StreamUtils.copy(request.getInputStream(), baos);
        this.lastPost = baos.toByteArray();
    } finally {
        FileUtils.closeQuietly(baos);
    }
    response.setStatus(Status.OK.getStatusCode());
}
Also used : ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream)

Example 25 with ByteArrayOutputStream

use of org.apache.nifi.stream.io.ByteArrayOutputStream in project nifi by apache.

the class JmsFactory method getMessageBytes.

private static byte[] getMessageBytes(ObjectMessage message) throws JMSException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // will fail if Object is not Serializable
        try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
            // will fail if Object is not Serializable
            oos.writeObject(message.getObject());
            oos.flush();
        }
        return baos.toByteArray();
    } catch (IOException e) {
        return new byte[0];
    }
}
Also used : ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)71 Test (org.junit.Test)51 TestRunner (org.apache.nifi.util.TestRunner)27 MockFlowFile (org.apache.nifi.util.MockFlowFile)25 File (java.io.File)22 ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)22 Schema (org.apache.avro.Schema)21 IOException (java.io.IOException)15 Peer (org.apache.nifi.remote.Peer)15 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)14 GenericRecord (org.apache.avro.generic.GenericRecord)13 TransactionResultEntity (org.apache.nifi.web.api.entity.TransactionResultEntity)12 DataInputStream (java.io.DataInputStream)11 DataOutputStream (java.io.DataOutputStream)11 SiteToSiteRestApiClient (org.apache.nifi.remote.util.SiteToSiteRestApiClient)9 DataPacket (org.apache.nifi.remote.protocol.DataPacket)8 SiteToSiteTestUtils.createDataPacket (org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket)8 Response (org.apache.nifi.remote.protocol.Response)7 UnknownHostException (java.net.UnknownHostException)6 ApiOperation (io.swagger.annotations.ApiOperation)5