use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.
the class DataTransferResource method constructPeer.
private Peer constructPeer(final HttpServletRequest req, final InputStream inputStream, final OutputStream outputStream, final String portId, final String transactionId) {
String clientHostName = req.getRemoteHost();
try {
// req.getRemoteHost returns IP address, try to resolve hostname to be consistent with RAW protocol.
final InetAddress clientAddress = InetAddress.getByName(clientHostName);
clientHostName = clientAddress.getHostName();
} catch (UnknownHostException e) {
logger.info("Failed to resolve client hostname {}, due to {}", clientHostName, e.getMessage());
}
final int clientPort = req.getRemotePort();
final PeerDescription peerDescription = new PeerDescription(clientHostName, clientPort, req.isSecure());
final NiFiUser user = NiFiUserUtils.getNiFiUser();
final String userDn = user == null ? null : user.getIdentity();
final HttpServerCommunicationsSession commSession = new HttpServerCommunicationsSession(inputStream, outputStream, transactionId, userDn);
boolean useCompression = false;
final String useCompressionStr = req.getHeader(HANDSHAKE_PROPERTY_USE_COMPRESSION);
if (!isEmpty(useCompressionStr) && Boolean.valueOf(useCompressionStr)) {
useCompression = true;
}
final String requestExpiration = req.getHeader(HANDSHAKE_PROPERTY_REQUEST_EXPIRATION);
final String batchCount = req.getHeader(HANDSHAKE_PROPERTY_BATCH_COUNT);
final String batchSize = req.getHeader(HANDSHAKE_PROPERTY_BATCH_SIZE);
final String batchDuration = req.getHeader(HANDSHAKE_PROPERTY_BATCH_DURATION);
commSession.putHandshakeParam(HandshakeProperty.PORT_IDENTIFIER, portId);
commSession.putHandshakeParam(HandshakeProperty.GZIP, String.valueOf(useCompression));
if (!isEmpty(requestExpiration)) {
commSession.putHandshakeParam(REQUEST_EXPIRATION_MILLIS, requestExpiration);
}
if (!isEmpty(batchCount)) {
commSession.putHandshakeParam(BATCH_COUNT, batchCount);
}
if (!isEmpty(batchSize)) {
commSession.putHandshakeParam(BATCH_SIZE, batchSize);
}
if (!isEmpty(batchDuration)) {
commSession.putHandshakeParam(BATCH_DURATION, batchDuration);
}
if (peerDescription.isSecure()) {
final NiFiUser nifiUser = NiFiUserUtils.getNiFiUser();
logger.debug("initiating peer, nifiUser={}", nifiUser);
commSession.setUserDn(nifiUser.getIdentity());
}
// TODO: Followed how SocketRemoteSiteListener define peerUrl and clusterUrl, but it can be more meaningful values, especially for clusterUrl.
final String peerUrl = "nifi://" + clientHostName + ":" + clientPort;
final String clusterUrl = "nifi://localhost:" + req.getLocalPort();
return new Peer(peerDescription, commSession, peerUrl, clusterUrl);
}
use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession 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.io.http.HttpServerCommunicationsSession in project nifi by apache.
the class StandardHttpFlowFileServerProtocol method doHandshake.
@Override
protected HandshakeProperties doHandshake(Peer peer) throws IOException, HandshakeException {
HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
final String transactionId = commsSession.getTransactionId();
HandshakeProperties confirmed = null;
if (!StringUtils.isEmpty(transactionId)) {
// If handshake is already done, use it.
confirmed = transactionManager.getHandshakenProperties(transactionId);
}
if (confirmed == null) {
// If it's not, then do handshake.
confirmed = new HandshakeProperties();
confirmed.setCommsIdentifier(transactionId);
validateHandshakeRequest(confirmed, peer, commsSession.getHandshakeParams());
}
logger.debug("{} Done handshake, confirmed={}", this, confirmed);
return confirmed;
}
use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.
the class StandardHttpFlowFileServerProtocol method writeTransactionResponse.
@Override
protected void writeTransactionResponse(boolean isTransfer, ResponseCode response, CommunicationsSession commsSession, String explanation) throws IOException {
HttpServerCommunicationsSession commSession = (HttpServerCommunicationsSession) commsSession;
commSession.setResponseCode(response);
if (isTransfer) {
switch(response) {
case NO_MORE_DATA:
logger.debug("{} There's no data to send.", this);
break;
case CONTINUE_TRANSACTION:
logger.debug("{} Continue transaction... expecting more flow files.", this);
commSession.setStatus(Transaction.TransactionState.DATA_EXCHANGED);
break;
case BAD_CHECKSUM:
logger.debug("{} Received BAD_CHECKSUM.", this);
commSession.setStatus(Transaction.TransactionState.ERROR);
break;
case CONFIRM_TRANSACTION:
logger.debug("{} Transaction is confirmed.", this);
commSession.setStatus(Transaction.TransactionState.TRANSACTION_CONFIRMED);
break;
case FINISH_TRANSACTION:
logger.debug("{} transaction is completed.", this);
commSession.setStatus(Transaction.TransactionState.TRANSACTION_COMPLETED);
break;
}
} else {
switch(response) {
case CONFIRM_TRANSACTION:
logger.debug("{} Confirming transaction. checksum={}", this, explanation);
commSession.setChecksum(explanation);
commSession.setStatus(Transaction.TransactionState.DATA_EXCHANGED);
break;
case TRANSACTION_FINISHED:
case TRANSACTION_FINISHED_BUT_DESTINATION_FULL:
logger.debug("{} Transaction is completed. responseCode={}", this, response);
commSession.setStatus(Transaction.TransactionState.TRANSACTION_COMPLETED);
break;
}
}
}
use of org.apache.nifi.remote.io.http.HttpServerCommunicationsSession in project nifi by apache.
the class StandardHttpFlowFileServerProtocol method commitReceiveTransaction.
@Override
public int commitReceiveTransaction(Peer peer) throws IOException, IllegalStateException {
logger.debug("{} Committing the receive transaction. peer={}", this, peer);
HttpServerCommunicationsSession commSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
String transactionId = commSession.getTransactionId();
FlowFileTransaction transaction = transactionManager.finalizeTransaction(transactionId);
commSession.setStatus(Transaction.TransactionState.TRANSACTION_CONFIRMED);
return super.commitReceiveTransaction(peer, transaction);
}
Aggregations