Session session = null;
try {
session = jsch.getSession(serverInfo.getUserName(), serverInfo.getHost(), serverInfo.getPort());
} catch (JSchException e) {
throw new SSHApiException("An exception occurred while creating SSH session." +
"Connecting server - " + serverInfo.getHost() + ":" + serverInfo.getPort() +
" connecting user name - "
+ serverInfo.getUserName(), e);
}
java.util.Properties config = this.configReader.getProperties();
session.setConfig(config);
// Not a good way, but we dont have any choice
if (session instanceof ExtendedSession) {
((ExtendedSession) session).setAuthenticationInfo(authenticationInfo);
}
try {
session.connect();
} catch (JSchException e) {
throw new SSHApiException("An exception occurred while connecting to server." +
"Connecting server - " + serverInfo.getHost() + ":" + serverInfo.getPort() +
" connecting user name - "
+ serverInfo.getUserName(), e);
}
boolean ptimestamp = true;
// exec 'scp -t rfile' remotely
String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + rFile;
Channel channel = session.openChannel("exec");
StandardOutReader stdOutReader = new StandardOutReader();
((ChannelExec) channel).setErrStream(stdOutReader.getStandardError());
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
if (checkAck(in) != 0) {
String error = "Error Reading input Stream";
log.error(error);
throw new SSHApiException(error);
}
File _lfile = new File(lFile);
if (ptimestamp) {
command = "T " + (_lfile.lastModified() / 1000) + " 0";
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command += (" " + (_lfile.lastModified() / 1000) + " 0\n");
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
String error = "Error Reading input Stream";
log.error(error);
throw new SSHApiException(error);
}
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize = _lfile.length();
command = "C0644 " + filesize + " ";
if (lFile.lastIndexOf('/') > 0) {
command += lFile.substring(lFile.lastIndexOf('/') + 1);
} else {
command += lFile;
}
command += "\n";
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
String error = "Error Reading input Stream";
log.error(error);
throw new SSHApiException(error);
}
// send a content of lFile
fis = new FileInputStream(lFile);
byte[] buf = new byte[1024];
while (true) {
int len = fis.read(buf, 0, buf.length);
if (len <= 0) break;
out.write(buf, 0, len); //out.flush();
}
fis.close();
fis = null;
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
if (checkAck(in) != 0) {
String error = "Error Reading input Stream";
log.error(error);
throw new SSHApiException(error);
}
out.close();
stdOutReader.onOutput(channel);
if (!stdOutReader.getStdErrorString().equals("")) {
throw new SSHApiException(stdOutReader.getStdErrorString());
}
channel.disconnect();
session.disconnect();
}