use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.
the class SCPClientImpl method put.
public void put(String local, String remote, String mode) throws SCPException {
if (connection == null) {
throw new SCPException("Connection is not established!");
}
if (scpClient == null) {
throw new SCPException("It is not authenticated!");
}
File localFile = null;
try {
localFile = new File(local);
if (homeDir != null && !localFile.isAbsolute()) {
localFile = new File(homeDir, local);
}
if (!localFile.exists()) {
throw new SCPException("File not exists! path=" + local);
}
File remoteFile = new File(remote);
if (localFile.getName().equals(remoteFile.getName())) {
if (mode == null) {
scpClient.put(localFile.getPath(), remoteFile.getParentFile() == null ? "." : remoteFile.getParentFile().getPath());
} else {
scpClient.put(localFile.getPath(), remoteFile.getParentFile() == null ? "." : remoteFile.getParentFile().getPath(), mode);
}
} else {
FileInputStream fis = null;
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
fis = new FileInputStream(localFile);
int length = 0;
byte[] buf = new byte[1024];
while ((length = fis.read(buf)) != -1) {
baos.write(buf, 0, length);
}
} catch (IOException e) {
throw new SCPException("It failed to read path=" + localFile, e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
}
}
}
if (mode == null) {
scpClient.put(baos.toByteArray(), remoteFile.getName(), remoteFile.getParentFile() == null ? "." : remoteFile.getParentFile().getPath());
} else {
scpClient.put(baos.toByteArray(), remoteFile.getName(), remoteFile.getParentFile() == null ? "." : remoteFile.getParentFile().getPath(), mode);
}
}
} catch (IOException e) {
throw new SCPException("It failed to put! file=" + localFile, e);
}
}
use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.
the class SCPClientImpl method mput.
public void mput(String local, String remoteDir, String mode) throws SCPException {
if (connection == null) {
throw new SCPException("Connection is not established!");
}
if (scpClient == null) {
throw new SCPException("It is not authenticated!");
}
RecurciveSearchFile rsf = new RecurciveSearchFile(".");
File[] localFiles = rsf.listAllTreeFiles(local);
try {
for (int i = 0; i < localFiles.length; i++) {
if (mode == null) {
scpClient.put(localFiles[i].getPath(), remoteDir);
} else {
scpClient.put(localFiles[i].getPath(), remoteDir, mode);
}
}
} catch (IOException e) {
throw new SCPException("It failed to mput! local=" + local, e);
}
}
use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.
the class SCPClientImpl method get.
public File get(String remote) throws SCPException {
if (connection == null) {
throw new SCPException("Connection is not established!");
}
if (scpClient == null) {
throw new SCPException("It is not authenticated!");
}
File localFile = null;
try {
File file = new File(remote);
String name = file.getName();
localFile = homeDir == null ? new File(name) : new File(homeDir, name);
scpClient.get(remote, homeDir == null ? "." : homeDir.getPath());
} catch (IOException e) {
throw new SCPException("It failed to get! file=" + remote, e);
}
return localFile;
}
use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.
the class SCPClientImpl method mget.
public File[] mget(String remote, String localDir) throws SCPException {
if (remote == null || remote.length() == 0) {
throw new SCPException("Path is null.");
}
if (localDir == null) {
localDir = ".";
}
File localDirFile = new File(localDir);
if (homeDir != null && !localDirFile.isAbsolute()) {
localDirFile = new File(homeDir, localDir);
}
List localFiles = new ArrayList();
Session session = null;
final String cmd = "scp -f " + remote;
File localFile = null;
try {
session = connection.openSession();
session.execCommand(cmd);
byte[] buf = new byte[1024];
OutputStream os = new BufferedOutputStream(session.getStdin(), 512);
InputStream is = new BufferedInputStream(session.getStdout(), 40000);
os.write(0);
os.flush();
while (true) {
int c = checkAck(is);
if (c == -1) {
if (localFiles.size() == 0) {
throw new IOException("Remote SCP terminated unexpectedly.");
}
break;
}
if (c != 'C') {
break;
}
is.read(buf, 0, 5);
long fileSize = 0L;
while (true) {
if (is.read(buf, 0, 1) < 0) {
throw new SCPException("Unexpected EOF.");
}
if (buf[0] == ' ') {
break;
}
fileSize = fileSize * 10L + (long) (buf[0] - '0');
}
String fileName = null;
for (int i = 0; ; i++) {
is.read(buf, i, 1);
if (buf[i] == (byte) 0x0a) {
fileName = new String(buf, 0, i);
break;
}
}
buf[0] = 0;
os.write(buf, 0, 1);
os.flush();
localFile = new File(localDirFile, fileName);
localFiles.add(localFile);
FileOutputStream fos = new FileOutputStream(localFile);
try {
int readLen = 0;
while (true) {
if (buf.length < fileSize) {
readLen = buf.length;
} else {
readLen = (int) fileSize;
}
readLen = is.read(buf, 0, readLen);
if (readLen < 0) {
throw new SCPException("Unexpected EOF.");
}
fos.write(buf, 0, readLen);
fileSize -= readLen;
if (fileSize == 0L) {
break;
}
}
} finally {
fos.close();
fos = null;
}
checkAck(is);
localFile = null;
buf[0] = 0;
os.write(buf, 0, 1);
os.flush();
}
} catch (IOException e) {
throw new SCPException("It failed to mget! from=" + remote + ", to=" + (localFile == null ? localDirFile : localFile), e);
} finally {
if (session != null) {
session.close();
}
}
return (File[]) localFiles.toArray(new File[localFiles.size()]);
}
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, String password) throws SCPException {
if (connection != null) {
throw new SCPException("It is already connected!");
}
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.authenticateWithPassword(user, password)) {
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 connect!", e);
}
}
Aggregations