Search in sources :

Example 6 with SiteToSiteRestApiClient

use of org.apache.nifi.remote.util.SiteToSiteRestApiClient in project nifi by apache.

the class TestHttpClientTransaction method testReceiveTwoFlowFiles.

@Test
public void testReceiveTwoFlowFiles() 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("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);
    execReceiveTwoFlowFiles(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)

Example 7 with SiteToSiteRestApiClient

use of org.apache.nifi.remote.util.SiteToSiteRestApiClient in project nifi by apache.

the class TestHttpClientTransaction method testSendOneFlowFile.

@Test
public void testSendOneFlowFile() throws IOException {
    SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
    final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
    doNothing().when(apiClient).openConnectionForSend(eq(transactionUrl), any(Peer.class));
    // Emulate that server returns correct checksum.
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpCommunicationsSession commSession = (HttpCommunicationsSession) invocation.getArguments()[0];
            commSession.setChecksum("2946083981");
            return null;
        }
    }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class));
    TransactionResultEntity resultEntity = new TransactionResultEntity();
    resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED.getCode());
    doReturn(resultEntity).when(apiClient).commitTransferFlowFiles(eq(transactionUrl), eq(CONFIRM_TRANSACTION));
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    ByteArrayInputStream serverResponse = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
    HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.SEND, transactionUrl);
    execSendOneFlowFile(transaction);
    InputStream sentByClient = new ByteArrayInputStream(clientRequest.toByteArray());
    DataPacket packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 1", readContents(packetByClient));
    assertEquals(-1, sentByClient.read());
    verify(apiClient).commitTransferFlowFiles(transactionUrl, CONFIRM_TRANSACTION);
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) InputStream(java.io.InputStream) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) Peer(org.apache.nifi.remote.Peer) CommunicationsSession(org.apache.nifi.remote.protocol.CommunicationsSession) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataPacket(org.apache.nifi.remote.protocol.DataPacket) SiteToSiteTestUtils.createDataPacket(org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 8 with SiteToSiteRestApiClient

use of org.apache.nifi.remote.util.SiteToSiteRestApiClient in project nifi by apache.

the class TestHttpClientTransaction method testReceiveZeroFlowFile.

@Test
public void testReceiveZeroFlowFile() throws IOException {
    SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
    final String transactionUrl = "http://www.example.com/data-transfer/input-ports/portId/transactions/transactionId";
    doReturn(false).when(apiClient).openConnectionForReceive(eq(transactionUrl), any(Peer.class));
    ByteArrayInputStream serverResponse = new ByteArrayInputStream(new byte[0]);
    ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
    HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.RECEIVE, transactionUrl);
    execReceiveZeroFlowFile(transaction);
    assertEquals("Client sends nothing as payload to receive flow files.", 0, clientRequest.toByteArray().length);
}
Also used : 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 9 with SiteToSiteRestApiClient

use of org.apache.nifi.remote.util.SiteToSiteRestApiClient in project nifi by apache.

the class StandardRemoteProcessGroup method refreshFlowContents.

@Override
public void refreshFlowContents() throws CommunicationsException {
    if (!initialized) {
        return;
    }
    try {
        // perform the request
        final ControllerDTO dto;
        try (final SiteToSiteRestApiClient apiClient = getSiteToSiteRestApiClient()) {
            dto = apiClient.getController(targetUris);
        } catch (IOException e) {
            throw new CommunicationsException("Unable to communicate with Remote NiFi at URI " + targetUris + " due to: " + e.getMessage());
        }
        writeLock.lock();
        try {
            if (dto.getInputPorts() != null) {
                setInputPorts(convertRemotePort(dto.getInputPorts()), true);
            }
            if (dto.getOutputPorts() != null) {
                setOutputPorts(convertRemotePort(dto.getOutputPorts()), true);
            }
            // set the controller details
            setTargetId(dto.getId());
            setName(dto.getName());
            setComments(dto.getComments());
            // get the component counts
            int inputPortCount = 0;
            if (dto.getInputPortCount() != null) {
                inputPortCount = dto.getInputPortCount();
            }
            int outputPortCount = 0;
            if (dto.getOutputPortCount() != null) {
                outputPortCount = dto.getOutputPortCount();
            }
            this.listeningPort = dto.getRemoteSiteListeningPort();
            this.listeningHttpPort = dto.getRemoteSiteHttpListeningPort();
            this.destinationSecure = dto.isSiteToSiteSecure();
            final RemoteProcessGroupCounts newCounts = new RemoteProcessGroupCounts(inputPortCount, outputPortCount);
            setCounts(newCounts);
            this.refreshContentsTimestamp = System.currentTimeMillis();
        } finally {
            writeLock.unlock();
        }
    } catch (final IOException e) {
        throw new CommunicationsException(e);
    }
}
Also used : RemoteProcessGroupCounts(org.apache.nifi.groups.RemoteProcessGroupCounts) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) ControllerDTO(org.apache.nifi.web.api.dto.ControllerDTO) IOException(java.io.IOException) CommunicationsException(org.apache.nifi.controller.exception.CommunicationsException)

Example 10 with SiteToSiteRestApiClient

use of org.apache.nifi.remote.util.SiteToSiteRestApiClient in project nifi by apache.

the class StandardRemoteProcessGroup method getSiteToSiteRestApiClient.

private SiteToSiteRestApiClient getSiteToSiteRestApiClient() {
    SiteToSiteRestApiClient apiClient = new SiteToSiteRestApiClient(sslContext, new HttpProxy(proxyHost, proxyPort, proxyUser, proxyPassword), getEventReporter());
    apiClient.setConnectTimeoutMillis(getCommunicationsTimeout(TimeUnit.MILLISECONDS));
    apiClient.setReadTimeoutMillis(getCommunicationsTimeout(TimeUnit.MILLISECONDS));
    apiClient.setLocalAddress(getLocalAddress());
    apiClient.setCacheExpirationMillis(remoteContentsCacheExpiration);
    return apiClient;
}
Also used : HttpProxy(org.apache.nifi.remote.protocol.http.HttpProxy) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient)

Aggregations

SiteToSiteRestApiClient (org.apache.nifi.remote.util.SiteToSiteRestApiClient)18 Test (org.junit.Test)12 Peer (org.apache.nifi.remote.Peer)11 ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)9 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)9 TransactionResultEntity (org.apache.nifi.web.api.entity.TransactionResultEntity)7 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)6 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)6 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 DataPacket (org.apache.nifi.remote.protocol.DataPacket)4 SiteToSiteTestUtils.createDataPacket (org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket)4 ControllerDTO (org.apache.nifi.web.api.dto.ControllerDTO)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Answer (org.mockito.stubbing.Answer)4 LinkedHashSet (java.util.LinkedHashSet)3 URI (java.net.URI)2 HashSet (java.util.HashSet)2 PeerStatus (org.apache.nifi.remote.PeerStatus)2