/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered.org <http://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.repoindexer;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FTPUtils {
    public static boolean uploadFTP(String user, String pass, String server, File local, String remoteLocation) {
        FTPClient ftpClient = new FTPClient();
        boolean done = false;
        try {

            ftpClient.connect(server);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();

            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            InputStream inputStream = new FileInputStream(local);
            done = ftpClient.storeFile(remoteLocation, inputStream);
            inputStream.close();
        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
            return false;
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return done;
        }
    }
    public static boolean uploadSFTP(String user, String pass, String server, File local, String remoteLocation) {
        JSch jsch = new JSch();
        Session session = null;
        Channel channel = null;
        ChannelSftp sftpChannel = null;
        boolean done = false;
        try {
            session = jsch.getSession(user, server, 22);
            session.setPassword(pass);

            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            channel = session.openChannel("sftp");
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);
            channel.connect();
            sftpChannel = (ChannelSftp) channel;

            byte[] bufr = new byte[(int) local.length()];
            FileInputStream fis = new FileInputStream(local);
            fis.read(bufr);
            ByteArrayInputStream fileStream = new ByteArrayInputStream(bufr);
            sftpChannel.put(fileStream, remoteLocation);
            fileStream.close();
            done = true;
        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
            return false;
        } finally {
            if (sftpChannel != null) {
                sftpChannel.exit();
            }
            if (channel != null) {
                channel.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
            return done;
        }
    }


}


@@ -1409,24 +1409,62 @@
 ch.Session;%0A
+import org.apache.commons.io.IOUtils;%0A
 import org.a
@@ -2849,32 +2849,81 @@
 moteLocation) %7B%0A
+        System.out.println(%22uploading to SFTP%22);%0A
         JSch jsc
@@ -3503,17 +3503,16 @@
 hannel;%0A
-%0A
         
@@ -3519,52 +3519,68 @@
     
-byte%5B%5D bufr = new byte%5B(int) local.length()%5D
+System.out.println(%22prepped to upload to %22 + remoteLocation)
 ;%0A  
@@ -3655,99 +3655,38 @@
     
-fis.read(bufr);%0A            ByteArrayInputStream fileStream = new ByteArrayInputStream(bufr
+System.out.println(%22uploading%22
 );%0A 
@@ -3714,24 +3714,17 @@
 l.put(fi
-leStream
+s
 , remote
@@ -3750,51 +3750,91 @@
     
-fileStream.close();%0A            done = true
+done = true;%0A            System.out.println(%22uploaded %22 + local.getCanonicalPath())
 ;%0A  

add extra debugging to sftp deploys, clean sftp code