Search in sources :

Example 46 with ByteArrayOutputStream

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

the class HttpClientTransaction method readTransactionResponse.

@Override
protected Response readTransactionResponse() throws IOException {
    HttpCommunicationsSession commSession = (HttpCommunicationsSession) peer.getCommunicationsSession();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    if (TransferDirection.RECEIVE.equals(direction)) {
        switch(state) {
            case TRANSACTION_STARTED:
            case DATA_EXCHANGED:
                logger.debug("{} {} readTransactionResponse. checksum={}", this, peer, commSession.getChecksum());
                if (StringUtils.isEmpty(commSession.getChecksum())) {
                    // We don't know if there's more data to receive, so just continue it.
                    ResponseCode.CONTINUE_TRANSACTION.writeResponse(dos);
                } else {
                    // We got a checksum to send to server.
                    if (TransactionState.TRANSACTION_STARTED.equals(state)) {
                        logger.debug("{} {} There's no transaction to confirm.", this, peer);
                        ResponseCode.CONFIRM_TRANSACTION.writeResponse(dos, "");
                    } else {
                        TransactionResultEntity transactionResult = apiClient.commitReceivingFlowFiles(transactionUrl, ResponseCode.CONFIRM_TRANSACTION, commSession.getChecksum());
                        ResponseCode responseCode = ResponseCode.fromCode(transactionResult.getResponseCode());
                        if (responseCode.containsMessage()) {
                            String message = transactionResult.getMessage();
                            responseCode.writeResponse(dos, message == null ? "" : message);
                        } else {
                            responseCode.writeResponse(dos);
                        }
                    }
                }
                break;
        }
    } else {
        switch(state) {
            case DATA_EXCHANGED:
                // Some flow files have been sent via stream, finish transferring.
                apiClient.finishTransferFlowFiles(commSession);
                ResponseCode.CONFIRM_TRANSACTION.writeResponse(dos, commSession.getChecksum());
                break;
            case TRANSACTION_CONFIRMED:
                TransactionResultEntity resultEntity = apiClient.commitTransferFlowFiles(transactionUrl, ResponseCode.CONFIRM_TRANSACTION);
                ResponseCode responseCode = ResponseCode.fromCode(resultEntity.getResponseCode());
                if (responseCode.containsMessage()) {
                    responseCode.writeResponse(dos, resultEntity.getMessage());
                } else {
                    responseCode.writeResponse(dos);
                }
                break;
        }
    }
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    return Response.read(new DataInputStream(bis));
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) ResponseCode(org.apache.nifi.remote.protocol.ResponseCode) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Example 47 with ByteArrayOutputStream

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

the class TestHttpClient method consumeDataPacket.

private static void consumeDataPacket(DataPacket packet) throws IOException {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamUtils.copy(packet.getData(), bos);
    String contents = new String(bos.toByteArray());
    logger.info("received: {}, {}", contents, packet.getAttributes());
}
Also used : ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream)

Example 48 with ByteArrayOutputStream

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

the class TestSiteToSiteClient method testSerialization.

@Test
public void testSerialization() {
    final SiteToSiteClientConfig clientConfig = new SiteToSiteClient.Builder().url("http://localhost:8080/nifi").portName("input").buildConfig();
    final Kryo kryo = new Kryo();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Output output = new Output(out);
    try {
        kryo.writeObject(output, clientConfig);
    } finally {
        output.close();
    }
    final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    final Input input = new Input(in);
    try {
        SiteToSiteClientConfig clientConfig2 = kryo.readObject(input, SiteToSiteClient.StandardSiteToSiteClientConfig.class);
        Assert.assertEquals(clientConfig.getUrls(), clientConfig2.getUrls());
    } finally {
        input.close();
    }
}
Also used : SiteToSiteClient(org.apache.nifi.remote.client.SiteToSiteClient) SiteToSiteClientConfig(org.apache.nifi.remote.client.SiteToSiteClientConfig) Input(com.esotericsoftware.kryo.io.Input) ByteArrayInputStream(java.io.ByteArrayInputStream) Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) Kryo(com.esotericsoftware.kryo.Kryo) Test(org.junit.Test)

Example 49 with ByteArrayOutputStream

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

the class TestHttpClientTransaction method testReceiveOneFlowFile.

@Test
public void testReceiveOneFlowFile() throws IOException {
    SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
    final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
    doReturn(true).when(apiClient).openConnectionForReceive(eq(transactionUrl), any(Peer.class));
    TransactionResultEntity resultEntity = new TransactionResultEntity();
    resultEntity.setResponseCode(CONFIRM_TRANSACTION.getCode());
    doReturn(resultEntity).when(apiClient).commitReceivingFlowFiles(eq(transactionUrl), eq(CONFIRM_TRANSACTION), eq("3680976076"));
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    codec.encode(createDataPacket("contents on server 1"), serverResponseBos);
    ByteArrayInputStream serverResponse = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
    HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.RECEIVE, transactionUrl);
    execReceiveOneFlowFile(transaction);
    assertEquals("Client sends nothing as payload to receive flow files.", 0, clientRequest.toByteArray().length);
    verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "3680976076");
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) Peer(org.apache.nifi.remote.Peer) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 50 with ByteArrayOutputStream

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

the class TestHttpClientTransaction method testReceiveWithInvalidChecksum.

@Test
public void testReceiveWithInvalidChecksum() throws IOException {
    SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
    final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
    doReturn(true).when(apiClient).openConnectionForReceive(eq(transactionUrl), any(Peer.class));
    // The checksum is correct, but here we simulate as if it's wrong, BAD_CHECKSUM.
    TransactionResultEntity resultEntity = new TransactionResultEntity();
    resultEntity.setResponseCode(ResponseCode.BAD_CHECKSUM.getCode());
    doReturn(resultEntity).when(apiClient).commitReceivingFlowFiles(eq(transactionUrl), eq(CONFIRM_TRANSACTION), eq("2969091230"));
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    codec.encode(createDataPacket("contents on server 1"), serverResponseBos);
    codec.encode(createDataPacket("contents on server 2"), serverResponseBos);
    ByteArrayInputStream serverResponse = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
    HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.RECEIVE, transactionUrl);
    execReceiveWithInvalidChecksum(transaction);
    assertEquals("Client sends nothing as payload to receive flow files.", 0, clientRequest.toByteArray().length);
    verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "2969091230");
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) Peer(org.apache.nifi.remote.Peer) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) Test(org.junit.Test)

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