Issue 79: created put method

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2390 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-12-10 06:32:31 +00:00
parent 572fe270c4
commit 60312b64f3
3 changed files with 46 additions and 0 deletions

View File

@ -40,6 +40,8 @@ public interface SshClient {
SshClient create(InetSocketAddress socket, String username, byte[] privateKey);
}
void put(String path, InputStream contents);
InputStream get(String path);
ExecResponse exec(String command);

View File

@ -37,6 +37,7 @@ import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.inject.Inject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ProxyInputStream;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.jclouds.logging.Logger;
@ -127,6 +128,30 @@ public class JschSshClient implements SshClient {
}
}
public void put(String path, InputStream contents) {
checkNotNull(path, "path");
checkNotNull(contents, "contents");
checkConnected();
logger.debug("%s@%s:%d: Opening sftp Channel.", username, host.getHostAddress(), port);
ChannelSftp sftp = null;
try {
sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
} catch (JSchException e) {
throw new SshException(String.format("%s@%s:%d: Error connecting to sftp.", username, host
.getHostAddress(), port), e);
}
try {
sftp.put(contents, path);
} catch (SftpException e) {
throw new SshException(String.format("%s@%s:%d: Error putting path: %s", username, host
.getHostAddress(), port, path), e);
} finally {
IOUtils.closeQuietly(contents);
}
}
private void checkConnected() {
checkState(session != null && session.isConnected(), String.format(
"%s@%s:%d: SFTP not connected!", username, host.getHostAddress(), port));

View File

@ -25,6 +25,7 @@ package org.jclouds.ssh.jsch;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -56,6 +57,7 @@ public class JschSshClientLiveTest {
protected static final String sshUser = System.getProperty("jclouds.test.ssh.username");
protected static final String sshPass = System.getProperty("jclouds.test.ssh.password");
protected static final String sshKeyFile = System.getProperty("jclouds.test.ssh.keyfile");
private File temp;
@BeforeGroups(groups = { "live" })
public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException {
@ -77,6 +79,8 @@ public class JschSshClientLiveTest {
public InputStream get(String path) {
if (path.equals("/etc/passwd")) {
return IOUtils.toInputStream("root");
} else if (path.equals(temp.getAbsolutePath())) {
return IOUtils.toInputStream("rabbit");
}
throw new RuntimeException("path " + path + " not stubbed");
}
@ -92,6 +96,11 @@ public class JschSshClientLiveTest {
throw new RuntimeException("command " + command + " not stubbed");
}
@Override
public void put(String path, InputStream contents) {
}
};
} else {
Injector i = Guice.createInjector(new JschSshClientModule());
@ -108,6 +117,16 @@ public class JschSshClientLiveTest {
}
}
public void testPutAndGet() throws IOException {
temp = File.createTempFile("foo", "bar");
temp.deleteOnExit();
SshClient client = setupClient();
client.put(temp.getAbsolutePath(), IOUtils.toInputStream("rabbit"));
InputStream input = setupClient().get(temp.getAbsolutePath());
String contents = Utils.toStringAndClose(input);
assertEquals(contents, "rabbit");
}
public void testGetEtcPassword() throws IOException {
InputStream input = setupClient().get("/etc/passwd");
String contents = Utils.toStringAndClose(input);