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;
}
}
}
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());
}
}
}
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);
}
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);
}
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");
}
Aggregations