Search in sources :

Example 1 with TransactionResultEntity

use of org.apache.nifi.web.api.entity.TransactionResultEntity in project nifi by apache.

the class HttpClientTransaction method writeTransactionResponse.

@Override
protected void writeTransactionResponse(ResponseCode response, String explanation) throws IOException {
    HttpCommunicationsSession commSession = (HttpCommunicationsSession) peer.getCommunicationsSession();
    if (TransferDirection.RECEIVE.equals(direction)) {
        switch(response) {
            case CONFIRM_TRANSACTION:
                logger.debug("{} Confirming transaction. checksum={}", this, explanation);
                commSession.setChecksum(explanation);
                break;
            case TRANSACTION_FINISHED:
                logger.debug("{} Finishing transaction.", this);
                break;
            case CANCEL_TRANSACTION:
                logger.debug("{} Canceling transaction. explanation={}", this, explanation);
                TransactionResultEntity resultEntity = apiClient.commitReceivingFlowFiles(transactionUrl, ResponseCode.CANCEL_TRANSACTION, null);
                ResponseCode cancelResponse = ResponseCode.fromCode(resultEntity.getResponseCode());
                switch(cancelResponse) {
                    case CANCEL_TRANSACTION:
                        logger.debug("{} CANCEL_TRANSACTION, The transaction is canceled on server properly.", this);
                        break;
                    default:
                        logger.warn("{} CANCEL_TRANSACTION, Expected the transaction is canceled on server, but received {}.", this, cancelResponse);
                        break;
                }
                break;
        }
    } else {
        switch(response) {
            case FINISH_TRANSACTION:
                // The actual HTTP request will be sent in readTransactionResponse.
                logger.debug("{} Finished sending flow files.", this);
                break;
            case BAD_CHECKSUM:
                {
                    TransactionResultEntity resultEntity = apiClient.commitTransferFlowFiles(transactionUrl, ResponseCode.BAD_CHECKSUM);
                    ResponseCode badChecksumCancelResponse = ResponseCode.fromCode(resultEntity.getResponseCode());
                    switch(badChecksumCancelResponse) {
                        case CANCEL_TRANSACTION:
                            logger.debug("{} BAD_CHECKSUM, The transaction is canceled on server properly.", this);
                            break;
                        default:
                            logger.warn("{} BAD_CHECKSUM, Expected the transaction is canceled on server, but received {}.", this, badChecksumCancelResponse);
                            break;
                    }
                }
                break;
            case CONFIRM_TRANSACTION:
                // The actual HTTP request will be sent in readTransactionResponse.
                logger.debug("{} Transaction is confirmed.", this);
                break;
            case CANCEL_TRANSACTION:
                {
                    logger.debug("{} Canceling transaction.", this);
                    TransactionResultEntity resultEntity = apiClient.commitTransferFlowFiles(transactionUrl, ResponseCode.CANCEL_TRANSACTION);
                    ResponseCode cancelResponse = ResponseCode.fromCode(resultEntity.getResponseCode());
                    switch(cancelResponse) {
                        case CANCEL_TRANSACTION:
                            logger.debug("{} CANCEL_TRANSACTION, The transaction is canceled on server properly.", this);
                            break;
                        default:
                            logger.warn("{} CANCEL_TRANSACTION, Expected the transaction is canceled on server, but received {}.", this, cancelResponse);
                            break;
                    }
                }
                break;
        }
    }
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) ResponseCode(org.apache.nifi.remote.protocol.ResponseCode) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession)

Example 2 with TransactionResultEntity

use of org.apache.nifi.web.api.entity.TransactionResultEntity in project nifi by apache.

the class SiteToSiteRestApiClient method handleErrResponse.

private IOException handleErrResponse(final int responseCode, final InputStream in) throws IOException {
    if (in == null) {
        return new IOException("Unexpected response code: " + responseCode);
    }
    final TransactionResultEntity errEntity = readResponse(in);
    final ResponseCode errCode = ResponseCode.fromCode(errEntity.getResponseCode());
    switch(errCode) {
        case UNKNOWN_PORT:
            return new UnknownPortException(errEntity.getMessage());
        case PORT_NOT_IN_VALID_STATE:
            return new PortNotRunningException(errEntity.getMessage());
        default:
            switch(responseCode) {
                case RESPONSE_CODE_FORBIDDEN:
                    return new HandshakeException(errEntity.getMessage());
                default:
                    return new IOException("Unexpected response code: " + responseCode + " errCode:" + errCode + " errMessage:" + errEntity.getMessage());
            }
    }
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) ResponseCode(org.apache.nifi.remote.protocol.ResponseCode) UnknownPortException(org.apache.nifi.remote.exception.UnknownPortException) PortNotRunningException(org.apache.nifi.remote.exception.PortNotRunningException) IOException(java.io.IOException) HandshakeException(org.apache.nifi.remote.exception.HandshakeException)

Example 3 with TransactionResultEntity

use of org.apache.nifi.web.api.entity.TransactionResultEntity in project nifi by apache.

the class TestHttpClientTransaction method testSendTwoFlowFiles.

@Test
public void testSendTwoFlowFiles() 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.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);
    execSendTwoFlowFiles(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 4 with TransactionResultEntity

use of org.apache.nifi.web.api.entity.TransactionResultEntity in project nifi by apache.

the class TestHttpClientTransaction method testSendWithInvalidChecksum.

@Test
public void testSendWithInvalidChecksum() 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 incorrect checksum.
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpCommunicationsSession commSession = (HttpCommunicationsSession) invocation.getArguments()[0];
            commSession.setChecksum("Different checksum");
            return null;
        }
    }).when(apiClient).finishTransferFlowFiles(any(CommunicationsSession.class));
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            TransactionResultEntity serverResult = new TransactionResultEntity();
            serverResult.setResponseCode(ResponseCode.CANCEL_TRANSACTION.getCode());
            return serverResult;
        }
    }).when(apiClient).commitTransferFlowFiles(eq(transactionUrl), eq(ResponseCode.BAD_CHECKSUM));
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    ByteArrayInputStream serverResponse = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
    HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.SEND, transactionUrl);
    execSendWithInvalidChecksum(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, ResponseCode.BAD_CHECKSUM);
}
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 5 with TransactionResultEntity

use of org.apache.nifi.web.api.entity.TransactionResultEntity 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)

Aggregations

TransactionResultEntity (org.apache.nifi.web.api.entity.TransactionResultEntity)24 Test (org.junit.Test)16 InputStream (java.io.InputStream)12 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)12 Peer (org.apache.nifi.remote.Peer)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 Response (javax.ws.rs.core.Response)9 HandshakeException (org.apache.nifi.remote.exception.HandshakeException)9 ServletContext (javax.servlet.ServletContext)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)8 SiteToSiteRestApiClient (org.apache.nifi.remote.util.SiteToSiteRestApiClient)7 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)6 HttpFlowFileServerProtocol (org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol)6 IOException (java.io.IOException)5 UriInfo (javax.ws.rs.core.UriInfo)5 UnknownHostException (java.net.UnknownHostException)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 AccessDeniedException (org.apache.nifi.authorization.AccessDeniedException)4 HttpRemoteSiteListener (org.apache.nifi.remote.HttpRemoteSiteListener)4