Search in sources :

Example 11 with HttpServerCommunicationsSession

use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testReceiveZeroFile.

@Test
public void testReceiveZeroFile() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final Peer peer = getDefaultPeer("testReceiveZeroFile");
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    commsSession.setUserDn("unit-test");
    serverProtocol.handshake(peer);
    assertTrue(serverProtocol.isHandshakeSuccessful());
    final FlowFileCodec negotiatedCoded = serverProtocol.negotiateCodec(peer);
    final ProcessContext context = null;
    final ProcessSession processSession = mock(ProcessSession.class);
    final InputStream httpInputStream = new ByteArrayInputStream(new byte[] {});
    ((HttpInput) commsSession.getInput()).setInputStream(httpInputStream);
    // Execute test using mock
    final int flowFileReceived = serverProtocol.receiveFlowFiles(peer, context, processSession, negotiatedCoded);
    assertEquals(0, flowFileReceived);
}
Also used : MockProcessSession(org.apache.nifi.util.MockProcessSession) ProcessSession(org.apache.nifi.processor.ProcessSession) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) HttpInput(org.apache.nifi.remote.io.http.HttpInput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Peer(org.apache.nifi.remote.Peer) FlowFileCodec(org.apache.nifi.remote.codec.FlowFileCodec) StandardFlowFileCodec(org.apache.nifi.remote.codec.StandardFlowFileCodec) MockProcessContext(org.apache.nifi.util.MockProcessContext) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 12 with HttpServerCommunicationsSession

use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testTransferOneFileBadChecksum.

@Test
public void testTransferOneFileBadChecksum() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final String transactionId = "testTransferOneFileBadChecksum";
    final Peer peer = getDefaultPeer(transactionId);
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    final String endpointUri = "https://remote-host:8443/nifi-api/output-ports/port-id/transactions/" + transactionId + "/flow-files";
    commsSession.putHandshakeParam(HandshakeProperty.BATCH_COUNT, "1");
    commsSession.setUserDn("unit-test");
    commsSession.setDataTransferUrl(endpointUri);
    transferFlowFiles(serverProtocol, transactionId, peer, processSession -> {
        final MockFlowFile flowFile = processSession.createFlowFile("Server content".getBytes());
        final HashMap<String, String> attributes = new HashMap<>();
        attributes.put("uuid", "server-uuid");
        attributes.put("filename", "server-filename");
        attributes.put("server-attr-1", "server-attr-1-value");
        attributes.put("server-attr-2", "server-attr-2-value");
        flowFile.putAttributes(attributes);
        return Arrays.asList(flowFile);
    });
    // Commit transaction
    try {
        serverProtocol.commitTransferTransaction(peer, "client-sent-wrong-checksum");
        fail();
    } catch (final IOException e) {
        assertTrue(e.getMessage().contains("CRC32 Checksum"));
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) HashMap(java.util.HashMap) Peer(org.apache.nifi.remote.Peer) IOException(java.io.IOException) Test(org.junit.Test)

Example 13 with HttpServerCommunicationsSession

use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.

the class StandardHttpFlowFileServerProtocol method commitTransferTransaction.

@Override
public int commitTransferTransaction(Peer peer, String clientChecksum) throws IOException, IllegalStateException {
    logger.debug("{} Committing the transfer transaction. peer={} clientChecksum={}", this, peer, clientChecksum);
    HttpServerCommunicationsSession commSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    String transactionId = commSession.getTransactionId();
    FlowFileTransaction transaction = transactionManager.finalizeTransaction(transactionId);
    commSession.setChecksum(clientChecksum);
    commSession.setStatus(Transaction.TransactionState.DATA_EXCHANGED);
    return super.commitTransferTransaction(peer, transaction);
}
Also used : FlowFileTransaction(org.apache.nifi.remote.protocol.FlowFileTransaction) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession)

Example 14 with HttpServerCommunicationsSession

use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.

the class StandardHttpFlowFileServerProtocol method holdTransaction.

private int holdTransaction(Peer peer, FlowFileTransaction transaction) {
    // We don't commit the session here yet,
    // to avoid losing sent flow files in case some issue happens at client side while it is processing,
    // hold the transaction until we confirm additional request from client.
    HttpServerCommunicationsSession commSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    String transactionId = commSession.getTransactionId();
    logger.debug("{} Holding transaction. transactionId={}", this, transactionId);
    transactionManager.holdTransaction(transactionId, transaction, handshakeProperties);
    return transaction.getFlowFilesSent().size();
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession)

Example 15 with HttpServerCommunicationsSession

use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testPortDestinationFull.

@Test
public void testPortDestinationFull() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final Peer peer = getDefaultPeer();
    ((HttpServerCommunicationsSession) peer.getCommunicationsSession()).putHandshakeParam(HandshakeProperty.PORT_IDENTIFIER, "port-identifier");
    final ProcessGroup processGroup = mock(ProcessGroup.class);
    final RootGroupPort port = mock(RootGroupPort.class);
    final PortAuthorizationResult authResult = mock(PortAuthorizationResult.class);
    doReturn(true).when(processGroup).isRootGroup();
    doReturn(port).when(processGroup).getOutputPort("port-identifier");
    doReturn(authResult).when(port).checkUserAuthorization(any(String.class));
    doReturn(true).when(authResult).isAuthorized();
    doReturn(true).when(port).isValid();
    doReturn(true).when(port).isRunning();
    final Set<Connection> connections = new HashSet<>();
    final Connection connection = mock(Connection.class);
    connections.add(connection);
    doReturn(connections).when(port).getConnections();
    final FlowFileQueue flowFileQueue = mock(FlowFileQueue.class);
    doReturn(flowFileQueue).when(connection).getFlowFileQueue();
    doReturn(true).when(flowFileQueue).isFull();
    serverProtocol.setRootProcessGroup(processGroup);
    try {
        serverProtocol.handshake(peer);
        fail();
    } catch (final HandshakeException e) {
        assertEquals(ResponseCode.PORTS_DESTINATION_FULL, e.getResponseCode());
    }
    assertFalse(serverProtocol.isHandshakeSuccessful());
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) RootGroupPort(org.apache.nifi.remote.RootGroupPort) Peer(org.apache.nifi.remote.Peer) ProcessGroup(org.apache.nifi.groups.ProcessGroup) Connection(org.apache.nifi.connectable.Connection) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) PortAuthorizationResult(org.apache.nifi.remote.PortAuthorizationResult) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

HttpServerCommunicationsSession (org.apache.nifi.remote.io.http.HttpServerCommunicationsSession)24 Peer (org.apache.nifi.remote.Peer)17 Test (org.junit.Test)12 HandshakeException (org.apache.nifi.remote.exception.HandshakeException)7 IOException (java.io.IOException)5 MockFlowFile (org.apache.nifi.util.MockFlowFile)5 InputStream (java.io.InputStream)4 UnknownHostException (java.net.UnknownHostException)4 ProcessGroup (org.apache.nifi.groups.ProcessGroup)4 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)4 RootGroupPort (org.apache.nifi.remote.RootGroupPort)4 HttpFlowFileServerProtocol (org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol)4 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3