@Override public RequestType getRequestType(final Peer peer) throws IOException { if (!handshakeCompleted) { throw new IllegalStateException("Handshake has not been completed"); } if (shutdown) { throw new IllegalStateException("Protocol is shutdown"); } logger.debug("{} Reading Request Type from {} using {}", new Object[]{this, peer, peer.getCommunicationsSession()}); final RequestType requestType = RequestType.readRequestType(new DataInputStream(peer.getCommunicationsSession().getInput().getInputStream())); logger.debug("{} Got Request Type {} from {}", new Object[]{this, requestType, peer}); return requestType; }
@Override public void shutdown(final Peer peer) throws IOException { readyForFileTransfer = false; final CommunicationsSession commsSession = peer.getCommunicationsSession(); final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream()); logger.debug("{} Shutting down with {}", this, peer); // Indicate that we would like to have some data RequestType.SHUTDOWN.writeRequestType(dos); dos.flush(); }
SocketClientTransaction(final int protocolVersion, final String destinationId, final Peer peer, final FlowFileCodec codec, final TransferDirection direction, final boolean useCompression, final int penaltyMillis, final EventReporter eventReporter) throws IOException { super(peer, direction, useCompression, codec, eventReporter, protocolVersion, penaltyMillis, destinationId); this.dis = new DataInputStream(peer.getCommunicationsSession().getInput().getInputStream()); this.dos = new DataOutputStream(peer.getCommunicationsSession().getOutput().getOutputStream()); initialize(); }
private int holdTransaction(Peer peer, FlowFileTransaction transaction) { // We don't commit the session here yet, // to avoid losing sent flow files in case some issue happens at client side while it is processing, // hold the transaction until we confirm additional request from client. HttpServerCommunicationsSession commSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession(); String transactionId = commSession.getTransactionId(); logger.debug("{} Holding transaction. transactionId={}", this, transactionId); transactionManager.holdTransaction(transactionId, transaction, handshakeProperties); return transaction.getFlowFilesSent().size(); }
@Override public void close() throws IOException { taskExecutor.shutdown(); peerSelector.clear(); for (final HttpClientTransaction transaction : activeTransactions) { transaction.getCommunicant().getCommunicationsSession().interrupt(); } } }
@Override public void shutdown() { super.shutdown(); requestLock.lock(); try { this.shutdown = true; for (final FlowFileRequest request : activeRequests) { final CommunicationsSession commsSession = request.getPeer().getCommunicationsSession(); if (commsSession != null) { commsSession.interrupt(); } } } finally { requestLock.unlock(); } }
public void shutdown() { shutdown = true; taskExecutor.shutdown(); peerSelector.clear(); for (final EndpointConnection conn : activeConnections) { conn.getPeer().getCommunicationsSession().interrupt(); } for (final BlockingQueue<EndpointConnection> connectionQueue : connectionQueueMap.values()) { EndpointConnection state; while ((state = connectionQueue.poll()) != null) { terminate(state); } } }
@Override public void write(OutputStream outputStream) throws IOException, WebApplicationException { HttpOutput output = (HttpOutput) peer.getCommunicationsSession().getOutput(); output.setOutputStream(outputStream); try { int numOfFlowFiles = serverProtocol.getPort().transferFlowFiles(peer, serverProtocol); logger.debug("finished transferring flow files, numOfFlowFiles={}", numOfFlowFiles); if (numOfFlowFiles < 1) { // There was no flow file to transfer. Throw this exception to stop responding with SEE OTHER. throw new WebApplicationException(Response.Status.OK); } } catch (NotAuthorizedException | BadRequestException | RequestExpiredException e) { // Handshake is done outside of write() method, so these exception wouldn't be thrown. throw new IOException("Failed to process the request.", e); } }
@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); }
@Override public Set<PeerStatus> getPeerStatuses(final Peer peer) throws IOException { if (!handshakeComplete) { throw new IllegalStateException("Handshake has not been performed"); } logger.debug("{} Get Peer Statuses from {}", this, peer); final CommunicationsSession commsSession = peer.getCommunicationsSession(); final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream()); final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream()); final boolean queryPeersForOtherPeers = getVersionNegotiator().getVersion() >= 6; RequestType.REQUEST_PEER_LIST.writeRequestType(dos); dos.flush(); final int numPeers = dis.readInt(); final Set<PeerStatus> peers = new HashSet<>(numPeers); for (int i = 0; i < numPeers; i++) { final String hostname = dis.readUTF(); final int port = dis.readInt(); final boolean secure = dis.readBoolean(); final int flowFileCount = dis.readInt(); peers.add(new PeerStatus(new PeerDescription(hostname, port, secure), flowFileCount, queryPeersForOtherPeers)); } logger.debug("{} Received {} Peer Statuses from {}", this, peers.size(), peer); return peers; }
@Override public FlowFileCodec negotiateCodec(final Peer peer) throws IOException, ProtocolException { if (!handshakeCompleted) { throw new IllegalStateException("Handshake has not been completed"); } if (shutdown) { throw new IllegalStateException("Protocol is shutdown"); } logger.debug("{} Negotiating Codec with {} using {}", new Object[]{this, peer, peer.getCommunicationsSession()}); final CommunicationsSession commsSession = peer.getCommunicationsSession(); final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream()); final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream()); if (port == null) { RemoteResourceFactory.rejectCodecNegotiation(dis, dos, "Cannot transfer FlowFiles because no port was specified"); } // Negotiate the FlowFileCodec to use. try { negotiatedFlowFileCodec = RemoteResourceFactory.receiveCodecNegotiation(dis, dos); logger.debug("{} Negotiated Codec {} with {}", new Object[]{this, negotiatedFlowFileCodec, peer}); return negotiatedFlowFileCodec; } catch (final HandshakeException e) { throw new ProtocolException(e.toString()); } }
@Override public int commitTransferTransaction(Peer peer, String clientChecksum) throws IOException, IllegalStateException { logger.debug("{} Committing the transfer transaction. peer={} clientChecksum={}", this, peer, clientChecksum); HttpServerCommunicationsSession commSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession(); String transactionId = commSession.getTransactionId(); FlowFileTransaction transaction = transactionManager.finalizeTransaction(transactionId); commSession.setChecksum(clientChecksum); commSession.setStatus(Transaction.TransactionState.DATA_EXCHANGED); return super.commitTransferTransaction(peer, transaction); }
@Override public FlowFileCodec negotiateCodec(final Peer peer) throws IOException, ProtocolException { if (!handshakeComplete) { throw new IllegalStateException("Handshake has not been performed"); } logger.debug("{} Negotiating Codec with {}", this, peer); final CommunicationsSession commsSession = peer.getCommunicationsSession(); final DataInputStream dis = new DataInputStream(commsSession.getInput().getInputStream()); final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream()); RequestType.NEGOTIATE_FLOWFILE_CODEC.writeRequestType(dos); FlowFileCodec codec = new StandardFlowFileCodec(); try { codec = (FlowFileCodec) RemoteResourceInitiator.initiateResourceNegotiation(codec, dis, dos); } catch (HandshakeException e) { throw new ProtocolException(e.toString()); } logger.debug("{} negotiated FlowFileCodec {} with {}", new Object[]{this, codec, commsSession}); return codec; }
private HttpFlowFileServerProtocol initiateServerProtocol(final HttpServletRequest req, final Peer peer, final Integer transportProtocolVersion) throws IOException { // Switch transaction protocol version based on transport protocol version. TransportProtocolVersionNegotiator negotiatedTransportProtocolVersion = new TransportProtocolVersionNegotiator(transportProtocolVersion); VersionNegotiator versionNegotiator = new StandardVersionNegotiator(negotiatedTransportProtocolVersion.getTransactionProtocolVersion()); final String dataTransferUrl = req.getRequestURL().toString(); ((HttpCommunicationsSession)peer.getCommunicationsSession()).setDataTransferUrl(dataTransferUrl); HttpFlowFileServerProtocol serverProtocol = getHttpFlowFileServerProtocol(versionNegotiator); HttpRemoteSiteListener.getInstance(nifiProperties).setupServerProtocol(serverProtocol); serverProtocol.handshake(peer); return serverProtocol; }
final CommunicationsSession commsSession = peer.getCommunicationsSession(); final DataOutputStream dos = new DataOutputStream(commsSession.getOutput().getOutputStream());
final OutputStream os = peer.getCommunicationsSession().getOutput().getOutputStream(); final OutputStream dataOut = compress ? new CompressionOutputStream(os) : os; final OutputStream out = new CheckedOutputStream(dataOut, crc);
@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; }
protected int commitReceiveTransaction(Peer peer, FlowFileTransaction transaction) throws IOException { CommunicationsSession commsSession = peer.getCommunicationsSession(); ProcessSession session = transaction.getSession(); final Response confirmTransactionResponse = readTransactionResponse(false, commsSession);
final PortAuthorizationResult portAuthResult = this.port.checkUserAuthorization(peer.getCommunicationsSession().getUserDn()); if (!portAuthResult.isAuthorized()) { logger.debug("Responding with ResponseCode UNAUTHORIZED: ", portAuthResult.getExplanation());
@Override protected Response readTransactionResponse() throws IOException { HttpCommunicationsSession commSession = (HttpCommunicationsSession) peer.getCommunicationsSession(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos);