<commit_before>package net.atomique.ksar;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Logger;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;


/**
 * @author <NAME> <<EMAIL>>
 */
public class SshDataRetriever implements IDataRetriever {

	private static Pattern pattern = Pattern.compile("^([^@]+)+@([^:]+)(?:\\:(\\d{1,5}))?$");
	private static final int MAX_READY_TIMEOUT = 10;
	private static String passphrase;
	
    private String host;
    private String user;
    private int port = 22;
    private String command;
    private String password;
    private final boolean promptForData;
	private final IMessageCreator messageCreator;
    
    
    static {
    	JSch.setLogger(new DefaultLogger());    	
    }
	
	private SshDataRetriever(boolean promptForData, IMessageCreator messageCreator) {
		this.promptForData = promptForData;
		this.messageCreator = messageCreator;
	}
	
    /**
     * Creates an instance of SshDataRetriever
     * @param suggestedServer
     * @param suggestedCommand
     */
	public SshDataRetriever(String suggestedServer, String suggestedCommand, IMessageCreator messageCreator) {
		this(true, messageCreator);
		
        this.setServer(suggestedServer);
        this.command = suggestedCommand;
	}
	
	/**
	 * Creates an instance of SshDataRetriever
	 * @param redoCommand
	 */
	public SshDataRetriever(String redoCommand, IMessageCreator messageCreator) {
		this(false, messageCreator);

		this.parseRedoCommand(command);
	}

	
    private void parseRedoCommand(String redoCommand) {
        if (redoCommand == null) {
            return;
        }
        
        String[] splitted = redoCommand.split("/");
        
        if (splitted.length != 2) {
            return;
        }
        
        this.setServer(splitted[0]);
        this.command = splitted[1];
    }
    
    public String getServer() {
    	return this.getServer(true);
    }
    
    private String getServer(boolean includePassword) {
        return String.format("%s%s@%s%s", 
                             ((this.user != null) ? this.user : "user.name"),
                             ((includePassword && this.password != null) ? ":" + this.password : ""),
                             ((this.host != null) ? this.host : "localhost"),
                             ((this.port != 22) ? ":" + Integer.toString(this.port) : ""));
    }
    
    private void setServer(String server) {
        if (server == null) {
            return;
        }
        
        Matcher matcher = pattern.matcher(server);
        
        if (matcher.find()) {
            if (matcher.group(1).contains(":")) {
                String[] splittedLogin = matcher.group(1).split(":");
                
                this.user = splittedLogin[0];
                this.password = splittedLogin[1];
            }
            else
            {
                this.user = matcher.group(1);
            }
            
            this.host = matcher.group(2);
            
            if (matcher.group(3) != null) {
                this.port = Integer.parseInt(matcher.group(3));
            }
        }
    }
    
    public String getCommand() {
        return this.command;
    }

	/* (non-Javadoc)
	 * @see net.atomique.ksar.IDataRetriever#getData()
	 */
	@Override
	public Reader getData() throws DataRetrievingFailedException {
		Session session = connect();
		
		this.requestCommand();
        
		ChannelExec channel = null;
		
        try {
    		channel = (ChannelExec)session.openChannel("exec");
    		channel.setCommand("LC_ALL=C " + this.command + "\n");
            channel.setXForwarding(false);
            channel.setErrStream(System.err);
        	
			channel.connect();
			this.addCommandToLastUsed();
			
			return this.readToString(channel.getInputStream());
		} 
        catch (IOException ex) {
        	throw new DataRetrievingFailedException("Could not execute command.", ex);
		} 
        catch (JSchException ex) {
        	throw new DataRetrievingFailedException("Could not execute command.", ex);
		}
        finally {
        	if (channel != null) {
        		channel.disconnect();
        	}
        	
        	if (session != null) {
        		session.disconnect();
        	}
        }
	}
	
	private Reader readToString(InputStream inputStream) throws IOException, DataRetrievingFailedException {
		Reader reader = new InputStreamReader(inputStream);
		BufferedReader bufferedReader = new BufferedReader(reader);
	
		StringWriter writer = new StringWriter();
		BufferedWriter bufferedWriter = new BufferedWriter(writer);
	
		try {
			int secondsWaited = 0;<commit_msg>Second disconnect caused an error in JSch<commit_after>package net.atomique.ksar;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Logger;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;


/**
 * @author <NAME> <<EMAIL>>
 */
public class SshDataRetriever implements IDataRetriever {

	private static Pattern pattern = Pattern.compile("^([^@]+)+@([^:]+)(?:\\:(\\d{1,5}))?$");
	private static final int MAX_READY_TIMEOUT = 10;
	private static String passphrase;
	
    private String host;
    private String user;
    private int port = 22;
    private String command;
    private String password;
    private final boolean promptForData;
	private final IMessageCreator messageCreator;
    
    
    static {
    	JSch.setLogger(new DefaultLogger());    	
    }
	
	private SshDataRetriever(boolean promptForData, IMessageCreator messageCreator) {
		this.promptForData = promptForData;
		this.messageCreator = messageCreator;
	}
	
    /**
     * Creates an instance of SshDataRetriever
     * @param suggestedServer
     * @param suggestedCommand
     */
	public SshDataRetriever(String suggestedServer, String suggestedCommand, IMessageCreator messageCreator) {
		this(true, messageCreator);
		
        this.setServer(suggestedServer);
        this.command = suggestedCommand;
	}
	
	/**
	 * Creates an instance of SshDataRetriever
	 * @param redoCommand
	 */
	public SshDataRetriever(String redoCommand, IMessageCreator messageCreator) {
		this(false, messageCreator);

		this.parseRedoCommand(command);
	}

	
    private void parseRedoCommand(String redoCommand) {
        if (redoCommand == null) {
            return;
        }
        
        String[] splitted = redoCommand.split("/");
        
        if (splitted.length != 2) {
            return;
        }
        
        this.setServer(splitted[0]);
        this.command = splitted[1];
    }
    
    public String getServer() {
    	return this.getServer(true);
    }
    
    private String getServer(boolean includePassword) {
        return String.format("%s%s@%s%s", 
                             ((this.user != null) ? this.user : "user.name"),
                             ((includePassword && this.password != null) ? ":" + this.password : ""),
                             ((this.host != null) ? this.host : "localhost"),
                             ((this.port != 22) ? ":" + Integer.toString(this.port) : ""));
    }
    
    private void setServer(String server) {
        if (server == null) {
            return;
        }
        
        Matcher matcher = pattern.matcher(server);
        
        if (matcher.find()) {
            if (matcher.group(1).contains(":")) {
                String[] splittedLogin = matcher.group(1).split(":");
                
                this.user = splittedLogin[0];
                this.password = <PASSWORD>tedLogin[1];
            }
            else
            {
                this.user = matcher.group(1);
            }
            
            this.host = matcher.group(2);
            
            if (matcher.group(3) != null) {
                this.port = Integer.parseInt(matcher.group(3));
            }
        }
    }
    
    public String getCommand() {
        return this.command;
    }

	/* (non-Javadoc)
	 * @see net.atomique.ksar.IDataRetriever#getData()
	 */
	@Override
	public Reader getData() throws DataRetrievingFailedException {
		Session session = connect();
		
		this.requestCommand();
        
		ChannelExec channel = null;
		
        try {
    		channel = (ChannelExec)session.openChannel("exec");
    		channel.setCommand("LC_ALL=C " + this.command + "\n");
            channel.setXForwarding(false);
            channel.setErrStream(System.err);
        	
			channel.connect();
			this.addCommandToLastUsed();
			
			return this.readToString(channel.getInputStream());
		} 
        catch (IOException ex) {
        	throw new DataRetrievingFailedException("Could not execute command.", ex);
		} 
        catch (JSchException ex) {
        	throw new DataRetrievingFailedException("Could not execute command.", ex);
		}
        finally {
        	if (channel != null) {
        		channel.disconnect();
        	} 
        	else if (session != null) {
        		session.disconnect();
        	}
        }
	}
	
	private Reader readToString(InputStream inputStream) throws IOException, DataRetrievingFailedException {
		Reader reader = new InputStreamReader(inputStream);
		BufferedReader bufferedReader = new BufferedReader(reader);
	
		StringWriter writer = new StringWriter();
		BufferedWriter bufferedWriter = new BufferedWriter(writer);
	
		try {
			int secondsWaited = 0;
{'fraction_non_alphanumeric': 0.11327854242994356, 'fraction_numerical': 0.003960788196851173, 'mean_word_length': 2.352140723531364, 'pattern_counts': {'":': 8, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1074100', 'n_tokens_mistral': 3057, 'n_tokens_neox': 2897, 'n_words': 602}