Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.xxxxxx.xmlparser.libs;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Hashtable;
- import com.jcraft.jsch.*;
- import org.apache.log4j.Logger;
- /**
- * Eine Klasse zum Auslesen von Dateien über SFTP per InputStream
- *
- * @author Patrick Trautmann
- * @version 1.0, 03.08.2010
- */
- public class SftpPull {
- private static Logger logger = Logger.getLogger( SftpPull.class );
- //Variablen für den SFTP Zugriff
- private JSch sftpPool = null;
- private ChannelSftp sftpChannel = null;
- private Session sftpSession = null;
- //SFTP Connection Defaults
- private String sftpHostname = "";
- private String sftpUsername = "";
- private String sftpPassword = "";
- private int sftpPort = 22;
- private InputStream result = null;
- /**
- * Konstruktor der Klasse.
- * @param Hostname
- * @param Username
- * @param Password
- */
- public SftpPull(String Hostname, String Username, String Password) {
- this.sftpHostname = Hostname;
- this.sftpUsername = Username;
- this.sftpPassword = Password;
- }
- /**
- * Konstruktor der Klasse.
- *
- * @param Hostname
- * @param Username
- * @param Password
- * @param Port
- */
- public SftpPull(String Hostname, String Username, String Password, int Port) {
- this.sftpHostname = Hostname;
- this.sftpUsername = Username;
- this.sftpPassword = Password;
- this.sftpPort = Port;
- }
- /**
- * Initialisieren der SFTP Variablen um eine erfolgereiche Verbindung herstellen zu können.
- *
- * @throws JSchException Wird bei SFTP Problemen ausgelöst
- */
- @SuppressWarnings("unchecked")
- public void init_SFTP() {
- this.sftpPool = new JSch();
- try {
- //Verbindung herstellen
- sftpSession = this.sftpPool.getSession(this.sftpUsername, this.sftpHostname, this.sftpPort);
- //Config aufbauen
- @SuppressWarnings("rawtypes")
- Hashtable config = new Hashtable();
- config.put("StrictHostKeyChecking", "no");
- sftpSession.setConfig(config);
- sftpSession.setPassword(this.sftpPassword);
- //Verbindung aufbauen
- sftpSession.connect();
- sftpChannel = (ChannelSftp) sftpSession.openChannel("sftp");
- sftpChannel.connect();
- //Logger
- logger.debug("SFTP Verbidnungs wurden ausgeführt.");
- }
- catch(JSchException e) {
- logger.error(e.toString());
- }
- }
- /**
- * Liest die XML Datei per SFTP aus und gibt sie als InputStream wieder.
- *
- * @param filepath Der Dateipfad auf dem Unixsystem zur XML Datei
- * @param filename Der Name der XML Datei
- * @throws SftpException Fehler die beim Areiten mit SFTP auftreten
- * @return InputStream Die XML Datei des gesuchten Dateipfades
- */
- public InputStream readOut(String filepath, String filename) throws SftpException {
- if(this.sftpChannel.isConnected()) {
- logger.debug("Die Verbindung zum SFTP Server wurde erfolgreich hergestellt.");
- sftpChannel.cd(filepath);
- this.result = sftpChannel.get(filename);
- }
- logger.debug("InputStream wurde zurückgegeben");
- return this.result;
- }
- /**
- * SFTP Verbindung und InputStream schließen
- *
- * @throws IOException Fehler die beim schließen des InputStreams auftreten
- */
- public void close_SFTP() {
- try {
- this.sftpSession.disconnect();
- logger.debug("SFTP Verbindung wurde geschlossen.");
- this.result.close();
- logger.debug("Der InputStream wurde geschlossen");
- }
- catch(IOException e) {
- logger.error(e.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement