@Test public void testProcessCommand() throws Exception { final ExecCommand command = new ExecCommand("frag1", this.getClass().getClassLoader().getResource("jsch-scp.txt").getPath(), "frag3"); final RemoteSystemConnection mockRemoteSystemConnection = mock(RemoteSystemConnection.class); when(mockRemoteExecCommand.getCommand()).thenReturn(command); when(mockRemoteExecCommand.getRemoteSystemConnection()).thenReturn(mockRemoteSystemConnection); when(mockRemoteSystemConnection.getEncryptedPassword()).thenReturn("#$@%aaa==".toCharArray()); final Session mockSession = mock(Session.class); final ChannelExec mockChannelExec = mock(ChannelExec.class); when(mockChannelExec.getOutputStream()).thenReturn(mock(OutputStream.class)); when(mockChannelExec.getInputStream()).thenReturn(new AckIn()); when(mockSession.openChannel(eq("exec"))).thenReturn(mockChannelExec); when(mockJsch.getSession(anyString(), anyString(), anyInt())).thenReturn(mockSession); jschScpCommandProcessor.processCommand(); }
@Override public void processCommand() throws RemoteCommandFailureException { checkAckOk = false; final List<String> commandFragments = remoteCommand.getCommand().getCommandFragments(); Session session = null; Channel channel = null; try { final RemoteSystemConnection remoteSystemConnection = remoteCommand.getRemoteSystemConnection(); LOGGER.debug("scp remote command {} source:{} destination:{}", remoteSystemConnection, commandFragments.get(1), commandFragments.get(2)); session = prepareSession(remoteSystemConnection); session.connect(); final String target = commandFragments.get(2).replace("\\", "/"); final String command = "scp -t " + target; channel = session.openChannel("exec"); final ChannelExec channelExec = (ChannelExec) channel; channelExec.setCommand(command); localInput = channelExec.getOutputStream(); remoteOutput = channelExec.getInputStream(); channelExec.connect(); LOGGER.debug(">>>>>> Channel connected..."); if (checkAck(remoteOutput) != 0) { throw new RemoteCommandFailureException(remoteCommand, new Throwable("Failed to connect to the remote host during secure copy")); } final String filePath = commandFragments.get(1); sendFileInfo(filePath); sendFileContent(filePath); } catch (Exception e) { LOGGER.error("Failed to copy file with error: {}", e.getMessage(), e); throw new RemoteCommandFailureException(remoteCommand, e); } finally { closeLocalInput(); if (channel != null && channel.isConnected()) { channel.disconnect(); } if (session != null && session.isConnected()) { session.disconnect(); } } }
JschScpCommandProcessorImpl implements CommandProcessor { @Override public void processCommand() throws RemoteCommandFailureException { checkAckOk = false; final List<String> commandFragments = remoteCommand.getCommand().getCommandFragments(); Session session = null; Channel channel = null; try { final RemoteSystemConnection remoteSystemConnection = remoteCommand.getRemoteSystemConnection(); LOGGER.debug("scp remote command {} source:{} destination:{}", remoteSystemConnection, commandFragments.get(1), commandFragments.get(2)); session = prepareSession(remoteSystemConnection); session.connect(); final String target = commandFragments.get(2).replace("\\", "/"); final String command = "scp -t " + target; channel = session.openChannel("exec"); final ChannelExec channelExec = (ChannelExec) channel; channelExec.setCommand(command); localInput = channelExec.getOutputStream(); remoteOutput = channelExec.getInputStream(); channelExec.connect(); LOGGER.debug(">>>>>> Channel connected..."); if (checkAck(remoteOutput) != 0) { throw new RemoteCommandFailureException(remoteCommand, new Throwable("Failed to connect to the remote host during secure copy")); } final String filePath = commandFragments.get(1); sendFileInfo(filePath); sendFileContent(filePath); } catch (Exception e) { LOGGER.error("Failed to copy file with error: {}", e.getMessage(), e); throw new RemoteCommandFailureException(remoteCommand, e); } finally { closeLocalInput(); if (channel != null && channel.isConnected()) { channel.disconnect(); } if (session != null && session.isConnected()) { session.disconnect(); } } } }
JschScpCommandProcessorImpl implements CommandProcessor { @Override public void processCommand() throws RemoteCommandFailureException { checkAckOk = false; final List<String> commandFragments = remoteCommand.getCommand().getCommandFragments(); Session session = null; Channel channel = null; try { final RemoteSystemConnection remoteSystemConnection = remoteCommand.getRemoteSystemConnection(); LOGGER.debug("scp remote command {} source:{} destination:{}", remoteSystemConnection, commandFragments.get(1), commandFragments.get(2)); session = prepareSession(remoteSystemConnection); session.connect(); final String target = commandFragments.get(2).replace("\\", "/"); final String command = "scp -t " + target; channel = session.openChannel("exec"); final ChannelExec channelExec = (ChannelExec) channel; channelExec.setCommand(command); localInput = channelExec.getOutputStream(); remoteOutput = channelExec.getInputStream(); channelExec.connect(); LOGGER.debug(">>>>>> Channel connected..."); if (checkAck(remoteOutput) != 0) { throw new RemoteCommandFailureException(remoteCommand, new Throwable("Failed to connect to the remote host during secure copy")); } final String filePath = commandFragments.get(1); sendFileInfo(filePath); sendFileContent(filePath); } catch (Exception e) { LOGGER.error("Failed to copy file with error: {}", e.getMessage(), e); throw new RemoteCommandFailureException(remoteCommand, e); } finally { closeLocalInput(); if (channel != null && channel.isConnected()) { channel.disconnect(); } if (session != null && session.isConnected()) { session.disconnect(); } } } JschScpCommandProcessorImpl(JSch jsch, RemoteExecCommand remoteCommand); }
JschScpCommandProcessorImpl implements CommandProcessor { @Override public void processCommand() throws RemoteCommandFailureException { checkAckOk = false; final List<String> commandFragments = remoteCommand.getCommand().getCommandFragments(); Session session = null; Channel channel = null; try { final RemoteSystemConnection remoteSystemConnection = remoteCommand.getRemoteSystemConnection(); LOGGER.debug("scp remote command {} source:{} destination:{}", remoteSystemConnection, commandFragments.get(1), commandFragments.get(2)); session = prepareSession(remoteSystemConnection); session.connect(); final String target = commandFragments.get(2).replace("\\", "/"); final String command = "scp -t " + target; channel = session.openChannel("exec"); final ChannelExec channelExec = (ChannelExec) channel; channelExec.setCommand(command); localInput = channelExec.getOutputStream(); remoteOutput = channelExec.getInputStream(); channelExec.connect(); LOGGER.debug(">>>>>> Channel connected..."); if (checkAck(remoteOutput) != 0) { throw new RemoteCommandFailureException(remoteCommand, new Throwable("Failed to connect to the remote host during secure copy")); } final String filePath = commandFragments.get(1); sendFileInfo(filePath); sendFileContent(filePath); } catch (Exception e) { LOGGER.error("Failed to copy file with error: {}", e.getMessage(), e); throw new RemoteCommandFailureException(remoteCommand, e); } finally { closeLocalInput(); if (channel != null && channel.isConnected()) { channel.disconnect(); } if (session != null && session.isConnected()) { session.disconnect(); } } } JschScpCommandProcessorImpl(JSch jsch, RemoteExecCommand remoteCommand); @Override void processCommand(); @Override String getCommandOutputStr(); @Override String getErrorOutputStr(); @Override ExecReturnCode getExecutionReturnCode(); @Override void close(); }
JschScpCommandProcessorImpl implements CommandProcessor { @Override public void processCommand() throws RemoteCommandFailureException { checkAckOk = false; final List<String> commandFragments = remoteCommand.getCommand().getCommandFragments(); Session session = null; Channel channel = null; try { final RemoteSystemConnection remoteSystemConnection = remoteCommand.getRemoteSystemConnection(); LOGGER.debug("scp remote command {} source:{} destination:{}", remoteSystemConnection, commandFragments.get(1), commandFragments.get(2)); session = prepareSession(remoteSystemConnection); session.connect(); final String target = commandFragments.get(2).replace("\\", "/"); final String command = "scp -t " + target; channel = session.openChannel("exec"); final ChannelExec channelExec = (ChannelExec) channel; channelExec.setCommand(command); localInput = channelExec.getOutputStream(); remoteOutput = channelExec.getInputStream(); channelExec.connect(); LOGGER.debug(">>>>>> Channel connected..."); if (checkAck(remoteOutput) != 0) { throw new RemoteCommandFailureException(remoteCommand, new Throwable("Failed to connect to the remote host during secure copy")); } final String filePath = commandFragments.get(1); sendFileInfo(filePath); sendFileContent(filePath); } catch (Exception e) { LOGGER.error("Failed to copy file with error: {}", e.getMessage(), e); throw new RemoteCommandFailureException(remoteCommand, e); } finally { closeLocalInput(); if (channel != null && channel.isConnected()) { channel.disconnect(); } if (session != null && session.isConnected()) { session.disconnect(); } } } JschScpCommandProcessorImpl(JSch jsch, RemoteExecCommand remoteCommand); @Override void processCommand(); @Override String getCommandOutputStr(); @Override String getErrorOutputStr(); @Override ExecReturnCode getExecutionReturnCode(); @Override void close(); static final String JSCH_READ_ACK_SLEEP_DURATION; static final String READ_ACK_SLEEP_DEFAULT_VALUE; }