use of org.apache.nifi.remote.protocol.ResponseCode in project nifi by apache.
the class StandardHttpFlowFileServerProtocol method readTransactionResponse.
@Override
protected Response readTransactionResponse(boolean isTransfer, CommunicationsSession commsSession) throws IOException {
// Returns Response based on current status.
HttpServerCommunicationsSession commSession = (HttpServerCommunicationsSession) commsSession;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Transaction.TransactionState currentStatus = commSession.getStatus();
if (isTransfer) {
switch(currentStatus) {
case DATA_EXCHANGED:
String clientChecksum = commSession.getChecksum();
logger.debug("readTransactionResponse. clientChecksum={}", clientChecksum);
ResponseCode.CONFIRM_TRANSACTION.writeResponse(new DataOutputStream(bos), clientChecksum);
break;
case TRANSACTION_CONFIRMED:
logger.debug("readTransactionResponse. finishing.");
ResponseCode.TRANSACTION_FINISHED.writeResponse(new DataOutputStream(bos));
break;
}
} else {
switch(currentStatus) {
case TRANSACTION_STARTED:
logger.debug("readTransactionResponse. returning CONTINUE_TRANSACTION.");
// We don't know if there's more data to receive, so just continue it.
ResponseCode.CONTINUE_TRANSACTION.writeResponse(new DataOutputStream(bos));
break;
case TRANSACTION_CONFIRMED:
// Checksum was successfully validated at client side, or BAD_CHECKSUM is returned.
ResponseCode responseCode = commSession.getResponseCode();
logger.debug("readTransactionResponse. responseCode={}", responseCode);
if (responseCode.containsMessage()) {
responseCode.writeResponse(new DataOutputStream(bos), "");
} else {
responseCode.writeResponse(new DataOutputStream(bos));
}
break;
}
}
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
return Response.read(new DataInputStream(bis));
}
use of org.apache.nifi.remote.protocol.ResponseCode 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.remote.protocol.ResponseCode 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.remote.protocol.ResponseCode in project nifi by apache.
the class SocketFlowFileServerProtocol method doHandshake.
@Override
protected HandshakeProperties doHandshake(Peer peer) throws IOException, HandshakeException {
HandshakeProperties confirmed = new HandshakeProperties();
final CommunicationsSession commsSession = peer.getCommunicationsSession();
final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream());
final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream());
confirmed.setCommsIdentifier(dis.readUTF());
if (versionNegotiator.getVersion() >= 3) {
String transitUriPrefix = dis.readUTF();
if (!transitUriPrefix.endsWith("/")) {
transitUriPrefix = transitUriPrefix + "/";
}
confirmed.setTransitUriPrefix(transitUriPrefix);
}
final Map<String, String> properties = new HashMap<>();
final int numProperties = dis.readInt();
for (int i = 0; i < numProperties; i++) {
final String propertyName = dis.readUTF();
final String propertyValue = dis.readUTF();
properties.put(propertyName, propertyValue);
}
// evaluate the properties received
boolean responseWritten = false;
try {
validateHandshakeRequest(confirmed, peer, properties);
} catch (HandshakeException e) {
ResponseCode handshakeResult = e.getResponseCode();
if (handshakeResult.containsMessage()) {
handshakeResult.writeResponse(dos, e.getMessage());
} else {
handshakeResult.writeResponse(dos);
}
switch(handshakeResult) {
case UNAUTHORIZED:
case PORT_NOT_IN_VALID_STATE:
case PORTS_DESTINATION_FULL:
responseWritten = true;
break;
default:
throw e;
}
}
// send "OK" response
if (!responseWritten) {
ResponseCode.PROPERTIES_OK.writeResponse(dos);
}
return confirmed;
}
use of org.apache.nifi.remote.protocol.ResponseCode 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));
}
Aggregations