<commit_before>package gov.nih.nci.cacis.ip.mirthconnect.ftp;

import gov.nih.nci.cacis.common.exception.ApplicationRuntimeException;
import gov.nih.nci.cacis.sa.mirthconnect.SemanticAdapter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.Selectors;
import org.apache.commons.vfs.impl.StandardFileSystemManager;
import org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

public class SFTPSender {

    private static final Logger LOG = Logger.getLogger(SFTPSender.class.getName());

    @Autowired
    private FTPMapping ftpMapping;

    @Value("${sftp.file.directory}")
    private String sftpFileDirectory;

    public void sendDocument(InputStream file, String ftpAddress, String extension) {

        LOG.error("SFTP FILE DIRECTORY: " + sftpFileDirectory);
        StandardFileSystemManager standardFileSystemManager = new StandardFileSystemManager();
        try {
            final FTPInfo ftpInfo = ftpMapping.getFTPInfo(ftpAddress);
            if (ftpInfo == null) {
                throw new ApplicationRuntimeException("No server config exists for address[ " + ftpAddress + " ]");
            }

            String sftpURI = "sftp://" + ftpInfo.getUserName() + ":" + ftpInfo.getPassword() + "@" + ftpInfo.getSite()
                    + "/" + ftpInfo.getRootDirectory();
            String fileName = "IHEXIPFTP-" + UUID.randomUUID() + extension;

            FileSystemOptions fileSystemOptions = new FileSystemOptions();
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fileSystemOptions, "no");
            SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fileSystemOptions, true);

            standardFileSystemManager.init();

            FileObject fileObject = standardFileSystemManager.resolveFile(sftpURI + "/" + fileName, fileSystemOptions);

            long timestamp = new Date().getTime();
            OutputStream out = new FileOutputStream(new File(sftpFileDirectory + timestamp + ".xml"));

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = file.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            file.close();
            out.flush();
            out.close();

            FileObject localFileObject = standardFileSystemManager.resolveFile(sftpFileDirectory +"/"+ timestamp + ".xml");

            fileObject.copyFrom(localFileObject, Selectors.SELECT_SELF);

            FileUtils.forceDelete(new File(sftpFileDirectory +"/"+ timestamp + ".xml"));

        } catch (Exception e) {
            throw new ApplicationRuntimeException("Error sending SFTP. " + e.getMessage());
        } finally {
            standardFileSystemManager.close();
        }
    }
}<commit_msg>Refactored code to cleanup a bug.<commit_after>package gov.nih.nci.cacis.ip.mirthconnect.ftp;

import gov.nih.nci.cacis.common.exception.ApplicationRuntimeException;
import gov.nih.nci.cacis.sa.mirthconnect.SemanticAdapter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.Selectors;
import org.apache.commons.vfs.impl.StandardFileSystemManager;
import org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

public class SFTPSender {

    private static final Logger LOG = Logger.getLogger(SFTPSender.class.getName());

    @Autowired
    private FTPMapping ftpMapping;

    @Value("${sftp.file.directory}")
    private String sftpFileDirectory;

    public void sendDocument(InputStream file, String ftpAddress, String extension) {

        LOG.error("SFTP FILE DIRECTORY: " + sftpFileDirectory);
        StandardFileSystemManager standardFileSystemManager = new StandardFileSystemManager();
        try {
            final FTPInfo ftpInfo = ftpMapping.getFTPInfo(ftpAddress);
            if (ftpInfo == null) {
                throw new ApplicationRuntimeException("No server config exists for address[ " + ftpAddress + " ]");
            }

            String sftpURI = "sftp://" + ftpInfo.getUserName() + ":" + ftpInfo.getPassword() + "@" + ftpInfo.getSite()
                    + "/" + ftpInfo.getRootDirectory();
            String fileName = "IHEXIPFTP-" + UUID.randomUUID() + extension;

            FileSystemOptions fileSystemOptions = new FileSystemOptions();
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fileSystemOptions, "no");
            SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(fileSystemOptions, true);

            standardFileSystemManager.init();

            FileObject fileObject = standardFileSystemManager.resolveFile(sftpURI + "/" + fileName, fileSystemOptions);

            long timestamp = new Date().getTime();
            String tempSftpFile = sftpFileDirectory +"/"+ timestamp + ".xml";
            OutputStream out = new FileOutputStream(new File(tempSftpFile));

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = file.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            file.close();
            out.flush();
            out.close();

            FileObject localFileObject = standardFileSystemManager.resolveFile(tempSftpFile);

            fileObject.copyFrom(localFileObject, Selectors.SELECT_SELF);

            FileUtils.forceDelete(new File(tempSftpFile));

        } catch (Exception e) {
            throw new ApplicationRuntimeException("Error sending SFTP. " + e.getMessage());
        } finally {
            standardFileSystemManager.close();
        }
    }
}
{'fraction_non_alphanumeric': 0.10863552550706822, 'fraction_numerical': 0.002458512599877074, 'mean_word_length': 3.1511479591836733, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '681786', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1782, 'n_words': 336}