Search in sources :

Example 6 with HttpServerCommunicationsSession

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

the class TestHttpFlowFileServerProtocol method receiveFlowFiles.

private void receiveFlowFiles(final HttpFlowFileServerProtocol serverProtocol, final String transactionId, final Peer peer, final DataPacket... dataPackets) throws IOException {
    final HttpRemoteSiteListener remoteSiteListener = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    serverProtocol.handshake(peer);
    assertTrue(serverProtocol.isHandshakeSuccessful());
    setupMockProcessSession();
    // Emulate dataPackets sent from a Site-to-Site client.
    final FlowFileCodec negotiatedCoded = serverProtocol.negotiateCodec(peer);
    final ByteArrayOutputStream testDataOs = new ByteArrayOutputStream();
    for (final DataPacket dataPacket : dataPackets) {
        negotiatedCoded.encode(dataPacket, testDataOs);
    }
    final InputStream httpInputStream = new ByteArrayInputStream(testDataOs.toByteArray());
    ((HttpInput) commsSession.getInput()).setInputStream(httpInputStream);
    // Execute test using mock
    final int flowFileReceived = serverProtocol.receiveFlowFiles(peer, processContext, processSession, negotiatedCoded);
    assertEquals(dataPackets.length, flowFileReceived);
    assertTrue(remoteSiteListener.isTransactionActive(transactionId));
}
Also used : 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) HttpRemoteSiteListener(org.apache.nifi.remote.HttpRemoteSiteListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataPacket(org.apache.nifi.remote.protocol.DataPacket) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) FlowFileCodec(org.apache.nifi.remote.codec.FlowFileCodec) StandardFlowFileCodec(org.apache.nifi.remote.codec.StandardFlowFileCodec)

Example 7 with HttpServerCommunicationsSession

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

the class TestHttpFlowFileServerProtocol method testTransferTwoFiles.

@Test
public void testTransferTwoFiles() throws Exception {
    final String transactionId = "testTransferTwoFiles";
    final Peer peer = getDefaultPeer(transactionId);
    final String endpointUri = "https://remote-host:8443/nifi-api/output-ports/port-id/transactions/" + transactionId + "/flow-files";
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    commsSession.putHandshakeParam(HandshakeProperty.BATCH_COUNT, "2");
    commsSession.setUserDn("unit-test");
    commsSession.setDataTransferUrl(endpointUri);
    transferFlowFiles(serverProtocol, transactionId, peer, processSession -> IntStream.of(1, 2).mapToObj(i -> {
        final MockFlowFile flowFile = processSession.createFlowFile(("Server content " + i).getBytes());
        final HashMap<String, String> attributes = new HashMap<>();
        attributes.put("uuid", "server-uuid-" + i);
        attributes.put("filename", "server-filename-" + i);
        attributes.put("server-attr-" + i + "-1", "server-attr-" + i + "-1-value");
        attributes.put("server-attr-" + i + "-2", "server-attr-" + i + "-2-value");
        flowFile.putAttributes(attributes);
        return flowFile;
    }).collect(Collectors.toList()));
    // Commit transaction
    final int flowFileSent = serverProtocol.commitTransferTransaction(peer, "3058746557");
    assertEquals(2, flowFileSent);
    // Assert provenance
    final List<ProvenanceEventRecord> provenanceEvents = sessionState.getProvenanceEvents();
    assertEquals(2, provenanceEvents.size());
    for (final ProvenanceEventRecord provenanceEvent : provenanceEvents) {
        assertEquals(ProvenanceEventType.SEND, provenanceEvent.getEventType());
        assertEquals(endpointUri, provenanceEvent.getTransitUri());
        assertEquals("Remote Host=peer-host, Remote DN=unit-test", provenanceEvent.getDetails());
    }
}
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) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Test(org.junit.Test)

Example 8 with HttpServerCommunicationsSession

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

the class TestHttpFlowFileServerProtocol method testPortNotInValidState.

@Test
public void testPortNotInValidState() 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();
    serverProtocol.setRootProcessGroup(processGroup);
    try {
        serverProtocol.handshake(peer);
        fail();
    } catch (final HandshakeException e) {
        assertEquals(ResponseCode.PORT_NOT_IN_VALID_STATE, 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) PortAuthorizationResult(org.apache.nifi.remote.PortAuthorizationResult) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) Test(org.junit.Test)

Example 9 with HttpServerCommunicationsSession

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

the class TestHttpFlowFileServerProtocol method testReceiveTwoFiles.

@Test
public void testReceiveTwoFiles() throws Exception {
    final String transactionId = "testReceiveTwoFile";
    final String endpointUri = "https://remote-host:8443/nifi-api/input-ports/port-id/transactions/" + transactionId + "/flow-files";
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final Peer peer = getDefaultPeer(transactionId);
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    commsSession.putHandshakeParam(HandshakeProperty.BATCH_COUNT, "2");
    commsSession.setUserDn("unit-test");
    commsSession.setDataTransferUrl(endpointUri);
    receiveFlowFiles(serverProtocol, transactionId, peer, createClientDataPacket(), createClientDataPacket());
    // Commit transaction
    commsSession.setResponseCode(ResponseCode.CONFIRM_TRANSACTION);
    final int flowFileReceived = serverProtocol.commitReceiveTransaction(peer);
    assertEquals(2, flowFileReceived);
    // Assert provenance.
    final List<ProvenanceEventRecord> provenanceEvents = sessionState.getProvenanceEvents();
    assertEquals(2, provenanceEvents.size());
    for (final ProvenanceEventRecord provenanceEvent : provenanceEvents) {
        assertEquals(ProvenanceEventType.RECEIVE, provenanceEvent.getEventType());
        assertEquals(endpointUri, provenanceEvent.getTransitUri());
        assertEquals("Remote Host=peer-host, Remote DN=unit-test", provenanceEvent.getDetails());
    }
    // Assert received flow files.
    processSession.assertAllFlowFilesTransferred(Relationship.ANONYMOUS);
    final List<MockFlowFile> flowFiles = processSession.getFlowFilesForRelationship(Relationship.ANONYMOUS);
    assertEquals(2, flowFiles.size());
    for (final MockFlowFile flowFile : flowFiles) {
        flowFile.assertAttributeEquals(SiteToSiteAttributes.S2S_HOST.key(), peer.getHost());
        flowFile.assertAttributeEquals(SiteToSiteAttributes.S2S_ADDRESS.key(), peer.getHost() + ":" + peer.getPort());
        flowFile.assertAttributeEquals("client-attr-1", "client-attr-1-value");
        flowFile.assertAttributeEquals("client-attr-2", "client-attr-2-value");
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) Peer(org.apache.nifi.remote.Peer) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Test(org.junit.Test)

Example 10 with HttpServerCommunicationsSession

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

the class TestHttpFlowFileServerProtocol method testUnauthorized.

@Test
public void testUnauthorized() 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));
    serverProtocol.setRootProcessGroup(processGroup);
    try {
        serverProtocol.handshake(peer);
        fail();
    } catch (final HandshakeException e) {
        assertEquals(ResponseCode.UNAUTHORIZED, 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) PortAuthorizationResult(org.apache.nifi.remote.PortAuthorizationResult) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) 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