Search in sources :

Example 11 with TransactionResultEntity

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

the class TestSiteToSiteResource method testPeersVersionWasNotSpecified.

@Test
public void testPeersVersionWasNotSpecified() throws Exception {
    final HttpServletRequest req = mock(HttpServletRequest.class);
    final NiFiServiceFacade serviceFacade = mock(NiFiServiceFacade.class);
    final SiteToSiteResource resource = getSiteToSiteResource(serviceFacade);
    final Response response = resource.getPeers(req);
    TransactionResultEntity resultEntity = (TransactionResultEntity) response.getEntity();
    assertEquals(400, response.getStatus());
    assertEquals(ResponseCode.ABORT.getCode(), resultEntity.getResponseCode());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) Test(org.junit.Test)

Example 12 with TransactionResultEntity

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

the class HttpClientTransaction method readTransactionResponse.

@Override
protected Response readTransactionResponse() throws IOException {
    HttpCommunicationsSession commSession = (HttpCommunicationsSession) peer.getCommunicationsSession();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    if (TransferDirection.RECEIVE.equals(direction)) {
        switch(state) {
            case TRANSACTION_STARTED:
            case DATA_EXCHANGED:
                logger.debug("{} {} readTransactionResponse. checksum={}", this, peer, commSession.getChecksum());
                if (StringUtils.isEmpty(commSession.getChecksum())) {
                    // We don't know if there's more data to receive, so just continue it.
                    ResponseCode.CONTINUE_TRANSACTION.writeResponse(dos);
                } else {
                    // We got a checksum to send to server.
                    if (TransactionState.TRANSACTION_STARTED.equals(state)) {
                        logger.debug("{} {} There's no transaction to confirm.", this, peer);
                        ResponseCode.CONFIRM_TRANSACTION.writeResponse(dos, "");
                    } else {
                        TransactionResultEntity transactionResult = apiClient.commitReceivingFlowFiles(transactionUrl, ResponseCode.CONFIRM_TRANSACTION, commSession.getChecksum());
                        ResponseCode responseCode = ResponseCode.fromCode(transactionResult.getResponseCode());
                        if (responseCode.containsMessage()) {
                            String message = transactionResult.getMessage();
                            responseCode.writeResponse(dos, message == null ? "" : message);
                        } else {
                            responseCode.writeResponse(dos);
                        }
                    }
                }
                break;
        }
    } else {
        switch(state) {
            case DATA_EXCHANGED:
                // Some flow files have been sent via stream, finish transferring.
                apiClient.finishTransferFlowFiles(commSession);
                ResponseCode.CONFIRM_TRANSACTION.writeResponse(dos, commSession.getChecksum());
                break;
            case TRANSACTION_CONFIRMED:
                TransactionResultEntity resultEntity = apiClient.commitTransferFlowFiles(transactionUrl, ResponseCode.CONFIRM_TRANSACTION);
                ResponseCode responseCode = ResponseCode.fromCode(resultEntity.getResponseCode());
                if (responseCode.containsMessage()) {
                    responseCode.writeResponse(dos, resultEntity.getMessage());
                } else {
                    responseCode.writeResponse(dos);
                }
                break;
        }
    }
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    return Response.read(new DataInputStream(bis));
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) ResponseCode(org.apache.nifi.remote.protocol.ResponseCode) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Example 13 with TransactionResultEntity

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

the class SiteToSiteRestApiClient method readResponse.

private TransactionResultEntity readResponse(final InputStream inputStream) throws IOException {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamUtils.copy(inputStream, bos);
    String responseMessage = null;
    try {
        responseMessage = new String(bos.toByteArray(), "UTF-8");
        logger.debug("readResponse responseMessage={}", responseMessage);
        final ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(responseMessage, TransactionResultEntity.class);
    } catch (JsonParseException | JsonMappingException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to parse JSON.", e);
        }
        final TransactionResultEntity entity = new TransactionResultEntity();
        entity.setResponseCode(ResponseCode.ABORT.getCode());
        entity.setMessage(responseMessage);
        return entity;
    }
}
Also used : TransactionResultEntity(org.apache.nifi.web.api.entity.TransactionResultEntity) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 14 with TransactionResultEntity

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

the class TestHttpClientTransaction method testReceiveOneFlowFile.

@Test
public void testReceiveOneFlowFile() 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("3680976076"));
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    codec.encode(createDataPacket("contents on server 1"), serverResponseBos);
    ByteArrayInputStream serverResponse = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream clientRequest = new ByteArrayOutputStream();
    HttpClientTransaction transaction = getClientTransaction(serverResponse, clientRequest, apiClient, TransferDirection.RECEIVE, transactionUrl);
    execReceiveOneFlowFile(transaction);
    assertEquals("Client sends nothing as payload to receive flow files.", 0, clientRequest.toByteArray().length);
    verify(apiClient).commitReceivingFlowFiles(transactionUrl, CONFIRM_TRANSACTION, "3680976076");
}
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 15 with TransactionResultEntity

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

the class TestHttpClientTransaction method testReceiveWithInvalidChecksum.

@Test
public void testReceiveWithInvalidChecksum() 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));
    // The checksum is correct, but here we simulate as if it's wrong, BAD_CHECKSUM.
    TransactionResultEntity resultEntity = new TransactionResultEntity();
    resultEntity.setResponseCode(ResponseCode.BAD_CHECKSUM.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);
    execReceiveWithInvalidChecksum(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