Search in sources :

Example 1 with HttpInput

use of org.apache.nifi.remote.io.http.HttpInput 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 HttpInput

use of org.apache.nifi.remote.io.http.HttpInput 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 3 with HttpInput

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

the class SiteToSiteRestApiClient method finishTransferFlowFiles.

public void finishTransferFlowFiles(final CommunicationsSession commSession) throws IOException {
    if (postResult == null) {
        new IllegalStateException("Data transfer has not started yet.");
    }
    // No more data can be sent.
    // Close PipedOutputStream so that dataPacketChannel doesn't blocked.
    // If we don't close this output stream, then PipedInputStream loops infinitely at read().
    commSession.getOutput().getOutputStream().close();
    logger.debug("{} FinishTransferFlowFiles no more data can be sent", this);
    try {
        if (!transferDataLatch.await(requestExpirationMillis, TimeUnit.MILLISECONDS)) {
            throw new IOException("Awaiting transferDataLatch has been timeout.");
        }
    } catch (final InterruptedException e) {
        throw new IOException("Awaiting transferDataLatch has been interrupted.", e);
    }
    stopExtendingTtl();
    final HttpResponse response;
    try {
        response = postResult.get(readTimeoutMillis, TimeUnit.MILLISECONDS);
    } catch (final ExecutionException e) {
        logger.debug("Something has happened at sending data thread. {}", e.getMessage());
        throw toIOException(e);
    } catch (TimeoutException | InterruptedException e) {
        throw new IOException(e);
    }
    final int responseCode = response.getStatusLine().getStatusCode();
    switch(responseCode) {
        case RESPONSE_CODE_ACCEPTED:
            final String receivedChecksum = EntityUtils.toString(response.getEntity());
            ((HttpInput) commSession.getInput()).setInputStream(new ByteArrayInputStream(receivedChecksum.getBytes()));
            ((HttpCommunicationsSession) commSession).setChecksum(receivedChecksum);
            logger.debug("receivedChecksum={}", receivedChecksum);
            break;
        default:
            try (InputStream content = response.getEntity().getContent()) {
                throw handleErrResponse(responseCode, content);
            }
    }
}
Also used : HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) PipedInputStream(java.io.PipedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) HttpInput(org.apache.nifi.remote.io.http.HttpInput) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with HttpInput

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

the class SiteToSiteRestApiClient method openConnectionForReceive.

public boolean openConnectionForReceive(final String transactionUrl, final Peer peer) throws IOException {
    final HttpGet get = createGet(transactionUrl + "/flow-files");
    // Set uri so that it'll be used as transit uri.
    ((HttpCommunicationsSession) peer.getCommunicationsSession()).setDataTransferUrl(get.getURI().toString());
    get.setHeader(HttpHeaders.PROTOCOL_VERSION, String.valueOf(transportProtocolVersionNegotiator.getVersion()));
    setHandshakeProperties(get);
    final CloseableHttpResponse response = getHttpClient().execute(get);
    final int responseCode = response.getStatusLine().getStatusCode();
    logger.debug("responseCode={}", responseCode);
    boolean keepItOpen = false;
    try {
        switch(responseCode) {
            case RESPONSE_CODE_OK:
                logger.debug("Server returned RESPONSE_CODE_OK, indicating there was no data.");
                EntityUtils.consume(response.getEntity());
                return false;
            case RESPONSE_CODE_ACCEPTED:
                final InputStream httpIn = response.getEntity().getContent();
                final InputStream streamCapture = new InputStream() {

                    boolean closed = false;

                    @Override
                    public int read() throws IOException {
                        if (closed) {
                            return -1;
                        }
                        final int r = httpIn.read();
                        if (r < 0) {
                            closed = true;
                            logger.debug("Reached to end of input stream. Closing resources...");
                            stopExtendingTtl();
                            closeSilently(httpIn);
                            closeSilently(response);
                        }
                        return r;
                    }
                };
                ((HttpInput) peer.getCommunicationsSession().getInput()).setInputStream(streamCapture);
                startExtendingTtl(transactionUrl, httpIn, response);
                keepItOpen = true;
                return true;
            default:
                try (InputStream content = response.getEntity().getContent()) {
                    throw handleErrResponse(responseCode, content);
                }
        }
    } finally {
        if (!keepItOpen) {
            response.close();
        }
    }
}
Also used : HttpInput(org.apache.nifi.remote.io.http.HttpInput) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) PipedInputStream(java.io.PipedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 5 with HttpInput

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

the class TestHttpClientTransaction method getClientTransaction.

private HttpClientTransaction getClientTransaction(InputStream is, OutputStream os, SiteToSiteRestApiClient apiClient, TransferDirection direction, String transactionUrl) throws IOException {
    PeerDescription description = null;
    String peerUrl = "";
    HttpCommunicationsSession commsSession = new HttpCommunicationsSession();
    ((HttpInput) commsSession.getInput()).setInputStream(is);
    ((HttpOutput) commsSession.getOutput()).setOutputStream(os);
    String clusterUrl = "";
    Peer peer = new Peer(description, commsSession, peerUrl, clusterUrl);
    String portId = "portId";
    boolean useCompression = false;
    int penaltyMillis = 1000;
    EventReporter eventReporter = new EventReporter() {

        @Override
        public void reportEvent(Severity severity, String category, String message) {
            logger.info("Reporting event... severity={}, category={}, message={}", severity, category, message);
        }
    };
    int protocolVersion = 5;
    HttpClientTransaction transaction = new HttpClientTransaction(protocolVersion, peer, direction, useCompression, portId, penaltyMillis, eventReporter);
    transaction.initialize(apiClient, transactionUrl);
    return transaction;
}
Also used : HttpInput(org.apache.nifi.remote.io.http.HttpInput) PeerDescription(org.apache.nifi.remote.PeerDescription) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) Peer(org.apache.nifi.remote.Peer) Severity(org.apache.nifi.reporting.Severity) HttpOutput(org.apache.nifi.remote.io.http.HttpOutput) EventReporter(org.apache.nifi.events.EventReporter)

Aggregations

HttpInput (org.apache.nifi.remote.io.http.HttpInput)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)3 PipedInputStream (java.io.PipedInputStream)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 Peer (org.apache.nifi.remote.Peer)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 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 HttpResponse (org.apache.http.HttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 EventReporter (org.apache.nifi.events.EventReporter)1 ProcessContext (org.apache.nifi.processor.ProcessContext)1 ProcessSession (org.apache.nifi.processor.ProcessSession)1 HttpRemoteSiteListener (org.apache.nifi.remote.HttpRemoteSiteListener)1