Search in sources :

Example 1 with FileTransferData

use of com.fo0.robot.model.FileTransferData in project Robot by fo0.

the class ChainActionItem method scp.

public EChainResponse scp(List<KeyValue> list) throws Exception {
    List<KeyValue> scpList = list;
    KeyValue scpHost = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.HOST)).findFirst().orElse(null);
    KeyValue scpPort = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PORT)).findFirst().orElse(KeyValue.builder().key(CONSTANTS_PATTERN.PORT).value("22").build());
    KeyValue scpUser = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.USER)).findFirst().orElse(null);
    KeyValue scpPassword = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PASSWORD)).findFirst().orElse(KeyValue.builder().key(CONSTANTS_PATTERN.PASSWORD).value("").build());
    KeyValue scpSrc = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    KeyValue scpDst = scpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(null);
    ctx.addToLog(type, "HOST: " + scpHost.getValue());
    ctx.addToLog(type, "PORT: " + scpPort.getValue());
    ctx.addToLog(type, "User: " + scpUser.getValue());
    ctx.addToLog(type, "Password: " + StringUtils.join(IntStream.range(0, scpPassword.getValue().length()).mapToObj(e -> "*").toArray(String[]::new)));
    ctx.addToLog(type, "SRC: " + scpSrc.getValue());
    ctx.addToLog(type, "DST: " + scpDst.getValue());
    SCPClient scpClient = new SCPClient(Host.builder().address(scpHost.getValue()).port(Integer.parseInt(scpPort.getValue())).username(scpUser.getValue()).password(scpPassword.getValue()).build());
    try {
        scpClient.connect();
    } catch (Exception e2) {
        ctx.addToLog(type, "failed to connect to Host " + e2);
        return EChainResponse.Failed;
    }
    FileTransferData data = null;
    // establish transfer
    try {
        if (type == EActionType.SCP_Download) {
            data = scpClient.download(scpDst.getValue(), scpSrc.getValue());
        } else {
            data = scpClient.upload(scpDst.getValue(), scpSrc.getValue());
        }
    } catch (Exception e2) {
        ctx.addToLog(type, "failed to transfer data " + e2);
        data = FileTransferData.builder().build();
    }
    ctx.addToLog(type, "Transfer: " + data.info());
    return EChainResponse.Continue;
}
Also used : SCPClient(com.fo0.robot.connector.SCPClient) KeyValue(com.fo0.robot.model.KeyValue) FileTransferData(com.fo0.robot.model.FileTransferData)

Example 2 with FileTransferData

use of com.fo0.robot.model.FileTransferData in project Robot by fo0.

the class FTPClient method download.

public FileTransferData download(String localPath, String remotePath) {
    try {
        FileTransferData ftd = FileTransferData.builder().build();
        ftd.setLocalPath(localPath);
        ftd.setRemotePath(remotePath);
        File localFile = new File(localPath);
        ftd.setStarted(System.currentTimeMillis());
        InputStream inputStream = ftpClient.retrieveFileStream(remotePath);
        FileUtils.copyInputStreamToFile(inputStream, localFile);
        ftd.setFinished(System.currentTimeMillis());
        boolean success = ftpClient.completePendingCommand();
        if (!success) {
            return ftd;
        }
        ftd.setSuccess(true);
        ftd.setSize(FileUtils.sizeOf(new File(localPath)));
        return ftd;
    } catch (Exception e) {
        Logger.error("failed to download ftp file: " + remotePath + "," + e);
    }
    return null;
}
Also used : FileTransferData(com.fo0.robot.model.FileTransferData) InputStream(java.io.InputStream) File(java.io.File)

Example 3 with FileTransferData

use of com.fo0.robot.model.FileTransferData in project Robot by fo0.

the class FTPClientTest method downloadFTPFile.

@Test
public void downloadFTPFile() {
    FTPClient client = new FTPClient(Host.builder().address(FTP_ADDRESS).port(21).username("anonymous").password("").build());
    boolean connected = client.connect();
    System.out.println("connected: " + connected);
    Assert.assertEquals(true, connected);
    FileTransferData f = client.download("test/1MB.zip", FTP_FILE_NAME);
    Assert.assertNotNull(f);
    Assert.assertEquals(FTP_FILE_NAME, new File(f.getLocalPath()).getName());
    Assert.assertEquals(1048576, f.getSize());
    new File(f.getLocalPath()).deleteOnExit();
}
Also used : FileTransferData(com.fo0.robot.model.FileTransferData) File(java.io.File) FTPClient(com.fo0.robot.connector.FTPClient) Test(org.junit.Test)

Example 4 with FileTransferData

use of com.fo0.robot.model.FileTransferData in project Robot by fo0.

the class ChainActionItem method ftp.

public EChainResponse ftp(List<KeyValue> list) throws Exception {
    List<KeyValue> ftpList = list;
    KeyValue ftpHost = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.HOST)).findFirst().orElse(null);
    KeyValue ftpPort = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PORT)).findFirst().orElse(KeyValue.builder().key("PORT").value("21").build());
    KeyValue ftpUser = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.USER)).findFirst().orElse(KeyValue.builder().key("USER").value("anonymous").build());
    KeyValue ftpPassword = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.PASSWORD)).findFirst().orElse(KeyValue.builder().key("PASSWORD").value("").build());
    KeyValue ftpSrc = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.SOURCE)).findFirst().orElse(null);
    KeyValue ftpDst = ftpList.stream().filter(e -> e.getKey().equals(CONSTANTS_PATTERN.DESTINATION)).findFirst().orElse(null);
    ctx.addToLog(type, "HOST: " + ftpHost.getValue());
    ctx.addToLog(type, "PORT: " + ftpPort.getValue());
    ctx.addToLog(type, "User: " + ftpUser.getValue());
    ctx.addToLog(type, "Password: " + StringUtils.join(IntStream.range(0, ftpPassword.getValue().length()).mapToObj(e -> "*").toArray(String[]::new)));
    ctx.addToLog(type, "SRC: " + ftpSrc.getValue());
    ctx.addToLog(type, "DST: " + ftpDst.getValue());
    FTPClient ftpClient = new FTPClient(Host.builder().address(ftpHost.getValue()).port(Integer.parseInt(ftpPort.getValue())).username(ftpUser.getValue()).password(ftpPassword.getValue()).build());
    if (!ftpClient.connect()) {
        ctx.addToLog(type, "failed to connect to Host");
        return EChainResponse.Failed;
    }
    FileTransferData ftpData = ftpClient.download(ftpDst.getValue(), ftpSrc.getValue());
    try {
        ctx.addToLog(type, "Transfer: " + ftpData.info());
    } catch (Exception e2) {
        e2.printStackTrace();
    }
    return EChainResponse.Continue;
}
Also used : KeyValue(com.fo0.robot.model.KeyValue) FileTransferData(com.fo0.robot.model.FileTransferData) FTPClient(com.fo0.robot.connector.FTPClient)

Example 5 with FileTransferData

use of com.fo0.robot.model.FileTransferData in project Robot by fo0.

the class SCPClient method upload.

public FileTransferData upload(String remotePath, String localPath) {
    if (client == null) {
        return null;
    }
    FileTransferData data = FileTransferData.builder().build();
    data.setLocalPath(localPath);
    data.setRemotePath(remotePath);
    data.setStarted(System.currentTimeMillis());
    SCPFileTransfer dlClient = client.newSCPFileTransfer();
    try {
        dlClient.upload(localPath, remotePath);
        data.setSuccess(true);
        data.setSize(FileUtils.sizeOf(new File(localPath)));
    } catch (Exception e) {
        Logger.error("failed to upload file " + e);
    } finally {
        data.setFinished(System.currentTimeMillis());
    }
    return data;
}
Also used : FileTransferData(com.fo0.robot.model.FileTransferData) SCPFileTransfer(net.schmizz.sshj.xfer.scp.SCPFileTransfer) File(java.io.File)

Aggregations

FileTransferData (com.fo0.robot.model.FileTransferData)6 File (java.io.File)4 FTPClient (com.fo0.robot.connector.FTPClient)2 KeyValue (com.fo0.robot.model.KeyValue)2 SCPFileTransfer (net.schmizz.sshj.xfer.scp.SCPFileTransfer)2 SCPClient (com.fo0.robot.connector.SCPClient)1 InputStream (java.io.InputStream)1 Test (org.junit.Test)1