Search in sources :

Example 31 with DataPacket

use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.

the class TestSocketClientTransaction method testSendWithInvalidChecksum.

@Test
public void testSendWithInvalidChecksum() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
    ResponseCode.CONFIRM_TRANSACTION.writeResponse(serverResponse, "Different checksum");
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.SEND);
    execSendWithInvalidChecksum(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.SEND_FLOWFILES, RequestType.readRequestType(sentByClient));
    DataPacket packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 1", readContents(packetByClient));
    Response continueDataResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.CONTINUE_TRANSACTION, continueDataResponse.getCode());
    packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 2", readContents(packetByClient));
    Response endOfDataResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.FINISH_TRANSACTION, endOfDataResponse.getCode());
    Response confirmResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.BAD_CHECKSUM, confirmResponse.getCode());
    assertEquals(-1, sentByClient.read());
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) DataPacket(org.apache.nifi.remote.protocol.DataPacket) SiteToSiteTestUtils.createDataPacket(org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket) Test(org.junit.Test)

Example 32 with DataPacket

use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.

the class AbstractTransaction method receive.

@Override
public final DataPacket receive() throws IOException {
    try {
        try {
            if (state != TransactionState.DATA_EXCHANGED && state != TransactionState.TRANSACTION_STARTED) {
                throw new IllegalStateException("Cannot receive data from " + peer + " because Transaction State is " + state);
            }
            if (direction == TransferDirection.SEND) {
                throw new IllegalStateException("Attempting to receive data from " + peer + " but started a SEND Transaction");
            }
            // if we already know there's no data, just return null
            if (!dataAvailable) {
                return null;
            }
            // if we have already received a packet, check if another is available.
            if (transfers > 0) {
                // Determine if Peer will send us data or has no data to send us
                final Response dataAvailableCode = readTransactionResponse();
                switch(dataAvailableCode.getCode()) {
                    case CONTINUE_TRANSACTION:
                        logger.debug("{} {} Indicates Transaction should continue", this, peer);
                        this.dataAvailable = true;
                        break;
                    case FINISH_TRANSACTION:
                        logger.debug("{} {} Indicates Transaction should finish", this, peer);
                        this.dataAvailable = false;
                        break;
                    default:
                        throw new ProtocolException("Got unexpected response from " + peer + " when asking for data: " + dataAvailableCode);
                }
            }
            // if no data available, return null
            if (!dataAvailable) {
                return null;
            }
            logger.debug("{} Receiving data from {}", this, peer);
            final InputStream is = peer.getCommunicationsSession().getInput().getInputStream();
            final InputStream dataIn = compress ? new CompressionInputStream(is) : is;
            final DataPacket packet = codec.decode(new CheckedInputStream(dataIn, crc));
            if (packet == null) {
                this.dataAvailable = false;
            } else {
                transfers++;
                contentBytes += packet.getSize();
            }
            this.state = TransactionState.DATA_EXCHANGED;
            return packet;
        } catch (final IOException ioe) {
            throw new IOException("Failed to receive data from " + peer + " due to " + ioe, ioe);
        }
    } catch (final Exception e) {
        error();
        throw e;
    }
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ProtocolException(org.apache.nifi.remote.exception.ProtocolException) CompressionInputStream(org.apache.nifi.remote.io.CompressionInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) CompressionInputStream(org.apache.nifi.remote.io.CompressionInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) DataPacket(org.apache.nifi.remote.protocol.DataPacket) CheckedInputStream(java.util.zip.CheckedInputStream) IOException(java.io.IOException) ProtocolException(org.apache.nifi.remote.exception.ProtocolException)

Example 33 with DataPacket

use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.

the class DataPacketDtoTest method testParserSingle.

@Test
public void testParserSingle() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    StringBuilder stringBuilder = new StringBuilder("[");
    DataPacketDto dataPacketDto = new DataPacketDto("test data".getBytes(StandardCharsets.UTF_8)).putAttribute("key", "value");
    stringBuilder.append(objectMapper.writeValueAsString(dataPacketDto));
    stringBuilder.append("]");
    List<DataPacket> dataPackets = DataPacketDto.getDataPacketStream(new ByteArrayInputStream(stringBuilder.toString().getBytes(StandardCharsets.UTF_8))).collect(Collectors.toList());
    assertEquals(1, dataPackets.size());
    assertEquals(dataPacketDto.toDataPacket(), dataPackets.get(0));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataPacket(org.apache.nifi.remote.protocol.DataPacket) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 34 with DataPacket

use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.

the class DataPacketDtoTest method testParserMultiple.

@Test
public void testParserMultiple() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    StringBuilder stringBuilder = new StringBuilder("[");
    DataPacketDto dataPacketDto = new DataPacketDto("test data".getBytes(StandardCharsets.UTF_8)).putAttribute("key", "value");
    stringBuilder.append(objectMapper.writeValueAsString(dataPacketDto));
    DataPacketDto dataPacketDto2 = new DataPacketDto("test data 2".getBytes(StandardCharsets.UTF_8)).putAttribute("key2", "value2");
    stringBuilder.append(",");
    stringBuilder.append(objectMapper.writeValueAsString(dataPacketDto2));
    stringBuilder.append("]");
    List<DataPacket> dataPackets = DataPacketDto.getDataPacketStream(new ByteArrayInputStream(stringBuilder.toString().getBytes(StandardCharsets.UTF_8))).collect(Collectors.toList());
    assertEquals(2, dataPackets.size());
    assertEquals(dataPacketDto.toDataPacket(), dataPackets.get(0));
    assertEquals(dataPacketDto2.toDataPacket(), dataPackets.get(1));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataPacket(org.apache.nifi.remote.protocol.DataPacket) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 35 with DataPacket

use of org.apache.nifi.remote.protocol.DataPacket in project apex-malhar by apache.

the class NiFiSinglePortOutputOperatorTest method verifyTransactions.

private void verifyTransactions(List<String> expectedContents, List<MockTransaction> transactions) throws IOException {
    // convert all the data packets in the transactions to strings
    final List<String> dataPacketContents = new ArrayList<>();
    for (MockTransaction mockTransaction : transactions) {
        List<DataPacket> dps = mockTransaction.getSentDataPackets();
        Assert.assertTrue(dps.size() > 0);
        for (DataPacket dp : dps) {
            final String dpContent = IOUtils.toString(dp.getData());
            dataPacketContents.add(dpContent);
        }
    }
    // verify each expected piece of content is found in the data packet contents
    for (String expectedContent : expectedContents) {
        boolean found = false;
        for (String dataPacket : dataPacketContents) {
            if (dataPacket.equals(expectedContent)) {
                found = true;
                break;
            }
        }
        Assert.assertTrue(found);
    }
}
Also used : ArrayList(java.util.ArrayList) MockTransaction(org.apache.apex.malhar.contrib.nifi.mock.MockTransaction) DataPacket(org.apache.nifi.remote.protocol.DataPacket)

Aggregations

DataPacket (org.apache.nifi.remote.protocol.DataPacket)36 Test (org.junit.Test)21 StandardDataPacket (org.apache.nifi.remote.util.StandardDataPacket)20 Transaction (org.apache.nifi.remote.Transaction)13 InputStream (java.io.InputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 SiteToSiteClient (org.apache.nifi.remote.client.SiteToSiteClient)8 SiteToSiteTestUtils.createDataPacket (org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket)8 ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)8 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)8 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)7 Peer (org.apache.nifi.remote.Peer)5 Response (org.apache.nifi.remote.protocol.Response)5 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)4 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)4