Search in sources :

Example 16 with SiteToSiteRestApiClient

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

the class TestHttpClientTransaction method testSendButDestinationFull.

@Test
public void testSendButDestinationFull() 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("portId"), 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("3359812065");
            return null;
        }
    }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class));
    TransactionResultEntity resultEntity = new TransactionResultEntity();
    resultEntity.setResponseCode(ResponseCode.TRANSACTION_FINISHED_BUT_DESTINATION_FULL.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);
    execSendButDestinationFull(transaction);
    InputStream sentByClient = new ByteArrayInputStream(clientRequest.toByteArray());
    DataPacket packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 1", readContents(packetByClient));
    packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 2", 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 17 with SiteToSiteRestApiClient

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

the class SiteInfoProvider method createSiteToSiteRestApiClient.

protected SiteToSiteRestApiClient createSiteToSiteRestApiClient(final SSLContext sslContext, final HttpProxy proxy) {
    final SiteToSiteRestApiClient apiClient = new SiteToSiteRestApiClient(sslContext, proxy, EventReporter.NO_OP);
    apiClient.setConnectTimeoutMillis(connectTimeoutMillis);
    apiClient.setReadTimeoutMillis(readTimeoutMillis);
    apiClient.setLocalAddress(localAddress);
    apiClient.setCacheExpirationMillis(cachedContentExpirationMillis);
    return apiClient;
}
Also used : SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient)

Example 18 with SiteToSiteRestApiClient

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

the class SiteInfoProvider method refreshRemoteInfo.

private ControllerDTO refreshRemoteInfo() throws IOException {
    final ControllerDTO controller;
    final URI connectedClusterUrl;
    try (final SiteToSiteRestApiClient apiClient = createSiteToSiteRestApiClient(sslContext, proxy)) {
        controller = apiClient.getController(clusterUrls);
        try {
            connectedClusterUrl = new URI(apiClient.getBaseUrl());
        } catch (URISyntaxException e) {
            // This should not happen since apiClient has successfully communicated with this URL.
            throw new RuntimeException("Failed to parse connected cluster URL due to " + e);
        }
    }
    remoteInfoWriteLock.lock();
    try {
        this.siteToSitePort = controller.getRemoteSiteListeningPort();
        this.siteToSiteHttpPort = controller.getRemoteSiteHttpListeningPort();
        this.siteToSiteSecure = controller.isSiteToSiteSecure();
        this.activeClusterUrl = connectedClusterUrl;
        inputPortMap.clear();
        for (final PortDTO inputPort : controller.getInputPorts()) {
            inputPortMap.put(inputPort.getName(), inputPort.getId());
        }
        outputPortMap.clear();
        for (final PortDTO outputPort : controller.getOutputPorts()) {
            outputPortMap.put(outputPort.getName(), outputPort.getId());
        }
        this.remoteRefreshTime = System.currentTimeMillis();
    } finally {
        remoteInfoWriteLock.unlock();
    }
    return controller;
}
Also used : PortDTO(org.apache.nifi.web.api.dto.PortDTO) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) ControllerDTO(org.apache.nifi.web.api.dto.ControllerDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

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