Search in sources :

Example 1 with HttpServerCommunicationsSession

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);
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) UnknownHostException(java.net.UnknownHostException) PeerDescription(org.apache.nifi.remote.PeerDescription) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) Peer(org.apache.nifi.remote.Peer) InetAddress(java.net.InetAddress)

Example 2 with HttpServerCommunicationsSession

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));
}
Also used : ResponseCode(org.apache.nifi.remote.protocol.ResponseCode) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) FlowFileTransaction(org.apache.nifi.remote.protocol.FlowFileTransaction) Transaction(org.apache.nifi.remote.Transaction) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Example 3 with HttpServerCommunicationsSession

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;
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) HandshakeProperties(org.apache.nifi.remote.protocol.HandshakeProperties)

Example 4 with HttpServerCommunicationsSession

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;
        }
    }
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession)

Example 5 with HttpServerCommunicationsSession

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);
}
Also used : FlowFileTransaction(org.apache.nifi.remote.protocol.FlowFileTransaction) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession)

Aggregations

HttpServerCommunicationsSession (org.apache.nifi.remote.io.http.HttpServerCommunicationsSession)24 Peer (org.apache.nifi.remote.Peer)17 Test (org.junit.Test)12 HandshakeException (org.apache.nifi.remote.exception.HandshakeException)7 IOException (java.io.IOException)5 MockFlowFile (org.apache.nifi.util.MockFlowFile)5 InputStream (java.io.InputStream)4 UnknownHostException (java.net.UnknownHostException)4 ProcessGroup (org.apache.nifi.groups.ProcessGroup)4 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)4 RootGroupPort (org.apache.nifi.remote.RootGroupPort)4 HttpFlowFileServerProtocol (org.apache.nifi.remote.protocol.http.HttpFlowFileServerProtocol)4 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3