Search in sources :

Example 1 with HttpRemoteSiteListener

use of org.apache.nifi.remote.HttpRemoteSiteListener 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 2 with HttpRemoteSiteListener

use of org.apache.nifi.remote.HttpRemoteSiteListener in project nifi by apache.

the class TestDataTransferResource method testTransferFlowFiles.

@Test
public void testTransferFlowFiles() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final DataTransferResource resource = getDataTransferResource();
    final ServletContext context = null;
    final HttpServletResponse res = null;
    final InputStream inputStream = null;
    final HttpRemoteSiteListener transactionManager = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
    final String transactionId = transactionManager.createTransaction();
    final Response response = resource.transferFlowFiles("port-id", transactionId, req, res, context, inputStream);
    transactionManager.cancelTransaction(transactionId);
    final Object entity = response.getEntity();
    assertEquals(202, response.getStatus());
    assertTrue(entity instanceof StreamingOutput);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) InputStream(java.io.InputStream) HttpRemoteSiteListener(org.apache.nifi.remote.HttpRemoteSiteListener) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) StreamingOutput(javax.ws.rs.core.StreamingOutput) Test(org.junit.Test)

Example 3 with HttpRemoteSiteListener

use of org.apache.nifi.remote.HttpRemoteSiteListener in project nifi by apache.

the class TestDataTransferResource method testCommitOutputPortTransaction.

@Test
public void testCommitOutputPortTransaction() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final DataTransferResource resource = getDataTransferResource();
    final ServletContext context = null;
    final InputStream inputStream = null;
    final HttpRemoteSiteListener transactionManager = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
    final String transactionId = transactionManager.createTransaction();
    final Response response = resource.commitOutputPortTransaction(ResponseCode.CONFIRM_TRANSACTION.getCode(), "client-checksum", "port-id", transactionId, req, context, inputStream);
    transactionManager.cancelTransaction(transactionId);
    TransactionResultEntity resultEntity = (TransactionResultEntity) response.getEntity();
    assertEquals(200, response.getStatus());
    assertEquals(ResponseCode.CONFIRM_TRANSACTION.getCode(), resultEntity.getResponseCode());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) InputStream(java.io.InputStream) HttpRemoteSiteListener(org.apache.nifi.remote.HttpRemoteSiteListener) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 4 with HttpRemoteSiteListener

use of org.apache.nifi.remote.HttpRemoteSiteListener in project nifi by apache.

the class TestHttpFlowFileServerProtocol method transferFlowFiles.

private Peer transferFlowFiles(final HttpFlowFileServerProtocol serverProtocol, final String transactionId, final Peer peer, final Function<MockProcessSession, Collection<MockFlowFile>> flowFileGenerator) throws IOException {
    setupMockProcessSession();
    // Enqueue flow files to be transferred.
    final Collection<MockFlowFile> flowFiles = flowFileGenerator.apply(processSession);
    for (final MockFlowFile flowFile : flowFiles) {
        sessionState.getFlowFileQueue().offer(flowFile);
    }
    final HttpRemoteSiteListener remoteSiteListener = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
    serverProtocol.handshake(peer);
    assertTrue(serverProtocol.isHandshakeSuccessful());
    final FlowFileCodec negotiatedCoded = serverProtocol.negotiateCodec(peer);
    // Execute test using mock
    final int flowFileSent = serverProtocol.transferFlowFiles(peer, processContext, processSession, negotiatedCoded);
    assertEquals(flowFiles.size(), flowFileSent);
    assertTrue(remoteSiteListener.isTransactionActive(transactionId));
    return peer;
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HttpRemoteSiteListener(org.apache.nifi.remote.HttpRemoteSiteListener) FlowFileCodec(org.apache.nifi.remote.codec.FlowFileCodec) StandardFlowFileCodec(org.apache.nifi.remote.codec.StandardFlowFileCodec)

Example 5 with HttpRemoteSiteListener

use of org.apache.nifi.remote.HttpRemoteSiteListener in project nifi by apache.

the class TestDataTransferResource method testCommitOutputPortTransactionBadChecksum.

@Test
public void testCommitOutputPortTransactionBadChecksum() throws Exception {
    final HttpServletRequest req = createCommonHttpServletRequest();
    final DataTransferResource resource = getDataTransferResource();
    final HttpFlowFileServerProtocol serverProtocol = resource.getHttpFlowFileServerProtocol(null);
    doThrow(new HandshakeException(ResponseCode.BAD_CHECKSUM, "Bad checksum.")).when(serverProtocol).commitTransferTransaction(any(), any());
    final ServletContext context = null;
    final InputStream inputStream = null;
    final HttpRemoteSiteListener transactionManager = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
    final String transactionId = transactionManager.createTransaction();
    final Response response = resource.commitOutputPortTransaction(ResponseCode.CONFIRM_TRANSACTION.getCode(), "client-checksum", "port-id", transactionId, req, context, inputStream);
    transactionManager.cancelTransaction(transactionId);
    TransactionResultEntity resultEntity = (TransactionResultEntity) response.getEntity();
    assertEquals(400, response.getStatus());
    assertEquals(ResponseCode.BAD_CHECKSUM.getCode(), resultEntity.getResponseCode());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) InputStream(java.io.InputStream) HttpRemoteSiteListener(org.apache.nifi.remote.HttpRemoteSiteListener) HttpFlowFileServerProtocol(org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol) ServletContext(javax.servlet.ServletContext) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) Test(org.junit.Test)

Aggregations

HttpRemoteSiteListener (org.apache.nifi.remote.HttpRemoteSiteListener)9 InputStream (java.io.InputStream)8 ServletContext (javax.servlet.ServletContext)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Response (javax.ws.rs.core.Response)7 Test (org.junit.Test)7 TransactionResultEntity (org.apache.nifi.web.api.entity.TransactionResultEntity)4 HttpFlowFileServerProtocol (org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol)3 Peer (org.apache.nifi.remote.Peer)2 RootGroupPort (org.apache.nifi.remote.RootGroupPort)2 FlowFileCodec (org.apache.nifi.remote.codec.FlowFileCodec)2 StandardFlowFileCodec (org.apache.nifi.remote.codec.StandardFlowFileCodec)2 HttpServerCommunicationsSession (org.apache.nifi.remote.io.http.HttpServerCommunicationsSession)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 UriInfo (javax.ws.rs.core.UriInfo)1 HandshakeException (org.apache.nifi.remote.exception.HandshakeException)1 HttpInput (org.apache.nifi.remote.io.http.HttpInput)1