com.github.wnameless.linereader.SftpLineReader.java Source code

Java tutorial

Introduction

Here is the source code for com.github.wnameless.linereader.SftpLineReader.java

Source

/**
 *
 * @author Wei-Ming Wu
 *
 *
 * Copyright 2014 Wei-Ming Wu
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */
package com.github.wnameless.linereader;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.compress.archivers.ArchiveException;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SftpLineReader extends ForwardingLineReader implements AutoCloseable {

    public static Helper forSiteUser(String username, String ftpServer) {
        return new Helper(username, ftpServer);
    }

    public static class Helper {

        private final String username;
        private final String ftpServer;

        private int port = 22;
        private String password;
        private String remoteFile;
        private String archiveEntry;
        private String encoding;

        private Helper(String username, String ftpServer) {
            this.username = username;
            this.ftpServer = ftpServer;
        }

        public Helper withPassword(String password) {
            this.password = password;

            return this;
        }

        public Helper onPort(int port) {
            this.port = port;

            return this;
        }

        public Helper setEncoding(String encoding) {
            this.encoding = encoding;

            return this;
        }

        public SftpLineReader openFile(String remoteFile)
                throws JSchException, SftpException, IOException, ArchiveException {
            this.remoteFile = remoteFile;

            return new SftpLineReader(this, false);
        }

        public SftpLineReader openArchiveFile(String remoteFile)
                throws JSchException, SftpException, IOException, ArchiveException {
            this.remoteFile = remoteFile;

            return new SftpLineReader(this, true);
        }

        public SftpLineReader openArchiveFileWithEntry(String remoteFile, String archiveEntry)
                throws JSchException, SftpException, IOException, ArchiveException {
            this.remoteFile = remoteFile;
            this.archiveEntry = archiveEntry;

            return new SftpLineReader(this, true);
        }

    }

    private final JSch jsch = new JSch();
    private final Channel channel;
    private final Session session;
    private final LineReader lineReader;

    private SftpLineReader(Helper builder, boolean isArchive)
            throws JSchException, SftpException, IOException, ArchiveException {
        session = jsch.getSession(builder.username, builder.ftpServer, builder.port);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(builder.password);
        session.connect();

        channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;

        InputStream is = sftpChannel.get(builder.remoteFile);
        if (!isArchive)
            lineReader = new InputStreamLineReader(is, builder.encoding);
        else
            lineReader = builder.archiveEntry == null ? //
                    new ArchiveLineReader(is, null, builder.encoding) : //
                    new ArchiveLineReader(is, builder.archiveEntry, builder.encoding);
    }

    @Override
    protected LineReader delegate() {
        return lineReader;
    }

    @Override
    public void close() throws Exception {
        channel.disconnect();
        session.disconnect();
    }

}