use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.
the class SCPClientImpl method connect.
public void connect(String user, String host, int port, File pemFile, String passphrase) throws SCPException {
if (connection != null) {
throw new SCPException("It is already connected!");
}
if (!pemFile.exists()) {
throw new SCPException("The pemFile not exists! path=" + pemFile);
}
FileInputStream fis = null;
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
String pem = null;
try {
fis = new FileInputStream(pemFile);
int length = 0;
byte[] buf = new byte[1024];
while ((length = fis.read(buf)) != -1) {
baos.write(buf, 0, length);
}
pem = new String(baos.toByteArray());
} catch (IOException e) {
throw new SCPException("It failed to read pemFile! path=" + pemFile, e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
}
}
}
connection = new Connection(host, port);
try {
if (isTcpNoDelay != null) {
connection.setTCPNoDelay(isTcpNoDelay.booleanValue());
}
if (serverHostKeyAlgorithms != null) {
connection.setServerHostKeyAlgorithms(serverHostKeyAlgorithms);
}
connection.connect(null, connectionTimeout, keyExchangeTimeout);
if (!connection.authenticateWithPublicKey(user, pem.toCharArray(), passphrase)) {
throw new SCPException("It failed to authenticate!");
}
scpClient = connection.createSCPClient();
} catch (IOException e) {
scpClient = null;
connection.close();
connection = null;
throw new SCPException("It failed to authenticate!", e);
}
}
Aggregations