First Commit from Source Code Reply

This commit is contained in:
vincenzofariello
2024-05-09 17:40:24 +02:00
parent 11e3b57c5b
commit 107a016cb9
35225 changed files with 1111346 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
@echo off
set ANT_HOME=../../build/apache-ant-1.9.2
set JAVA_HOME=C:\Programmi\Java\jdk1.6.0_45
set ANT_OPTS=-Xms512m -Xmx512m
set PATH=%ANT_HOME%/bin;%JAVA_HOME%/bin
ant -lib "%JAVA_HOME%/lib/tools.jar" -buildfile build.xml %1

View File

@@ -0,0 +1,8 @@
#home di Java SDK
#sdk.home=D:/progetti/bea92/jdk150_12
#home del progetto,
#se si fa il build dall'alberatura CVS non è necessario modificare la proprietà
project.home=../
#path delle librerie
#se si fa il build dall'alberatura CVS di Ambra non è necessario modificare la proprietà
sfptd.lib.path=../lib

View File

@@ -0,0 +1,83 @@
<project name="SFTPDaemon" default="clean" basedir=".">
<property environment="env"/>
<property file="build.properties"/>
<property environment="env"/>
<property name="build.path" value="${project.home}/build"/>
<property name="building.path" value="${build.path}/building"/>
<property name="sfptd.src.path" value="${project.home}/src"/>
<property name="sfptd.classes.path" value="${building.path}/sftpd-jar"/>
<!-- PATTERN PER sftpd.jar -->
<patternset id="sfptd.jar.classes">
<include name="**/*.class"/>
</patternset>
<!-- LIBRERIE PER LA COMPILAZIONE -->
<filelist id="sfptd.all.classpath" dir="${sfptd.lib.path}">
<file name="commons-logging-1.0.4.jar"/>
<file name="jsch-0.1.54.jar"/>
<file name="log4j-1.2.16.jar"/>
</filelist>
<!-- CLASSPATH PER LA COMPILAZIONE-->
<path id="all.classpath">
<filelist refid="sfptd.all.classpath"/>
</path>
<!-- ################################################################################ -->
<!-- ################################################################################ -->
<!-- ################################################################################ -->
<target name="echo">
<echo>
Starting build using:
project.home= ${project.home}
sfptd.lib.path= ${sfptd.lib.path}
</echo>
</target>
<target name="init" depends="echo">
<tstamp prefix="start"/>
<echo>
Build started at: ${start.DSTAMP} ${start.TSTAMP}
-----------------
project.home= ${project.home}
sfptd.lib.path= ${sfptd.lib.path}
</echo>
<mkdir dir="${building.path}"/>
<mkdir dir="${building.path}/sftpd-jar"/>
<mkdir dir="${project.home}/target"/>
</target>
<target name="compile_sftpd" depends="init">
<echo>
Compiling DBCFX
-------------
</echo>
<javac
srcdir="${sfptd.src.path}"
destdir="${sfptd.classes.path}"
debug="on"
deprecation="off"
failonerror="true">
<classpath refid="all.classpath"/>
</javac>
</target>
<target name="jar_sftpd" depends="compile_sftpd">
<echo>
Creating sfptd.jar
-------------
</echo>
<jar destfile="${building.path}/sftpd.jar">
<fileset dir="${sfptd.classes.path}">
<patternset refid="sfptd.jar.classes"/>
</fileset>
</jar>
</target>
<target name="copyear" depends="jar_sftpd">
<mkdir dir="${project.home}/target/jar_${start.DSTAMP}_${start.TSTAMP}"/>
<copy todir="${build.path}/jar_${start.DSTAMP}_${start.TSTAMP}" file="${building.path}/sftpd.jar"/>
</target>
<target name="clean" depends="copyear">
<delete dir="${building.path}"/>
</target>
</project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,7 @@
log4j.rootLogger = DEBUG, DAILY
# configure the daily rolling file appender
log4j.appender.DAILY=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DAILY.File=/home/serverbea/sftpd/logs/SFTPDaemon.log
log4j.appender.DAILY.DatePattern='.'yyyy-MM-dd
log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout
log4j.appender.DAILY.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] %m%n

View File

@@ -0,0 +1,323 @@
############### SFTP PUT DAEMON ###############
# CONFIGURAZIONE PER PUT SU PITAGORA REGOLATORIO
PUT.LOCAL.PATH.REG=/gnpapp/batch/ftpout/pitagora
PUT.LOCAL.BACKUP.PATH.REG=/gnpapp/batch/backup_out/pitagora
# Estensione files da inviare verso Pitagora Reg. (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
PUT.EXTENSION.REG=.csv;.xml
PUT.IDENTITY.REG=/home/bea/.ssh/id_rsa
PUT.PASSPHRASE.REG=
PUT.USERNAME.REG=dbcfx
#PUT.HOST.REG=10.119.3.119
# PORTALE WHOLESALE WINDOWS
#PUT.HOST.REG=172.30.16.99
# PORTALE WHOLESALE LINUX
PUT.HOST.REG=172.30.16.178
PUT.PORT.REG=22
PUT.KNOWN_HOSTS.REG=/home/bea/.ssh/known_hosts
PUT.REMOTE_PATH.REG=/HOME/regolatorio/reg_TLC/ull_TLC
#modifica cristiano per X11
PUT.RENAMECTR.REG=YES
PUT.RENAMECTR.SUFFIX.REG=_f.xml
PUT_SENDCTR.REG=YES
#fine modifica cristiano X11
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
PUT.CONNECT.TIMEOUT.REG=30000
##############
# CONFIGURAZIONE PER PUT SU PITAGORA XDSL
PUT.LOCAL.PATH.XDSL=/gnpapp/batch/ftpout/pitagoraxdsl
PUT.LOCAL.BACKUP.PATH.XDSL=/gnpapp/batch/backup_out/pitagoraxdsl
# Estensione files da inviare verso Pitagora Xsdl (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
#aggiunta estensione bad per X1 2013
PUT.EXTENSION.XDSL=.xml;
PUT.IDENTITY.XDSL=/home/bea/.ssh/id_rsa
PUT.PASSPHRASE.XDSL=
PUT.USERNAME.XDSL=dbcfx
#PUT.HOST.XDSL=10.119.3.119
# PORTALE WHOLESALE WINDOWS
#PUT.HOST.XDSL=172.30.16.99
# PORTALE WHOLESALE LINUX
PUT.HOST.REG=172.30.16.178
PUT.PORT.XDSL=22
PUT.KNOWN_HOSTS.XDSL=/home/bea/.ssh/known_hosts
PUT.REMOTE_PATH.XDSL=/HOME/xdsl/adsl_002
PUT.RENAMECTR.XDSL=YES
PUT.RENAMECTR.SUFFIX.XDSL=f.xml
PUT_SENDCTR.XDSL=YES
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
PUT.CONNECT.TIMEOUT.XDSL=30000
############### SFTP GET DAEMON ###############
# CONFIGURAZIONE PER GET DA PITAGORA REGOLATORIO
GET.LOCAL.PATH.REG=/gnpapp/batch/ftpin/pitagora
GET.IDENTITY.REG=/home/bea/.ssh/id_rsa
GET.PASSPHRASE.REG=
GET.USERNAME.REG=dbcfx
#GET.HOST.REG=10.119.3.119
# PORTALE WHOLESALE WINDOWS
#GET.HOST.REG=172.30.16.99
# PORTALE WHOLESALE LINUX
GET.HOST.REG=172.30.16.178
GET.PORT.REG=22
GET.KNOWN_HOSTS.REG=/home/bea/.ssh/known_hosts
GET.REMOTE_PATH.REG=/HOME/regolatorio/reg_TLC/ull_TLC/riscontri
# Estensione files da prelevare da Pitagora Reg. (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
GET.EXTENSION.REG=.csv;._NEW;.bad;.xml
# Pattern di inizio del nome file da prelevare
GET.NOMEFILE_PATTERN.REG=N_TLC_*_N_CRM*
# Abilita o disabilita decompressione GZip per estensione .gz (YES|NO)
GET.UNCOMPRESS.REG=NO
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
GET.CONNECT.TIMEOUT.REG=30000
# FILE PER GESTIONE STORICO FILE LAVORATI
GET.FILENAME.LAVORATI.REG=/gnpapp/properties/N_TLC_STORICO.txt
##############
# CONFIGURAZIONE PER GET DA PITAGORA XDSL
GET.LOCAL.PATH.XDSL=/gnpapp/batch/ftpin/pitagoraxdsl
GET.IDENTITY.XDSL=/home/bea/.ssh/id_rsa
GET.PASSPHRASE.XDSL=
GET.USERNAME.XDSL=dbcfx
#GET.HOST.XDSL=10.119.3.119
# PORTALE WHOLESALE WINDOWS
#GET.HOST.XDSL=172.30.16.99
# PORTALE WHOLESALE LINUX
GET.HOST.REG=172.30.16.178
GET.PORT.XDSL=22
GET.KNOWN_HOSTS.XDSL=/home/bea/.ssh/known_hosts
GET.REMOTE_PATH.XDSL=/HOME/xdsl/adsl_002/adsl_002_riscontri
# Estensione files da prelevare da Pitagora XDSL (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
GET.EXTENSION.XDSL=.xml.gz;.bad
# Pattern di inizio del nome file da prelevare
GET.NOMEFILE_PATTERN.XDSL=002_*_xDSL_BTS*
# Abilita o disabilita decompressione GZip per estensione .gz (YES|NO)
GET.UNCOMPRESS.XDSL=YES
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
GET.CONNECT.TIMEOUT.XDSL=30000
# FILE PER GESTIONE STORICO FILE LAVORATI (in /gnpapp/
GET.FILENAME.LAVORATI.XDSL=/gnpapp/properties/002_TLC_STORICO.txt
###################################################################
####### CONFIGURAZIONE GET DA NOW #######
###################################################################
#####
##### CONFIGURAZIONE PER GET DA NOW_REG
#####
GET.LOCAL.PATH.NOW_REG=/gnpapp/batch/ftpin/now/reg
GET.IDENTITY.NOW_REG=/home/bea/.ssh/id_rsa
GET.PASSPHRASE.NOW_REG=
GET.USERNAME.NOW_REG=dbcfx
#GET.HOST.REG=10.119.3.119
# PORTALE WHOLESALE WINDOWS
#GET.HOST.NOW_REG=172.30.16.99
# PORTALE WHOLESALE LINUX
GET.HOST.REG=172.30.16.178
GET.PORT.NOW_REG=22
GET.KNOWN_HOSTS.NOW_REG=/home/bea/.ssh/known_hosts
GET.REMOTE_PATH.NOW_REG=/HOME/regolatorio/reg_TLC/ull_TLC/riscontri
# DBCFX_SM_392 BEGIN i files sono compressi
# Estensione files da prelevare da Pitagora Reg. (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
GET.EXTENSION.NOW_REG=.xml.gz
# Pattern di inizio del nome file da prelevare
GET.NOMEFILE_PATTERN.NOW_REG=N_TLC_*_N_??[.]xml[.]gz;TLC*TLCN_??[.]xml[.]gz
# Abilita o disabilita decompressione GZip per estensione .gz (YES|NO)
GET.UNCOMPRESS.NOW_REG=YES
# DBCFX_SM_392 END i files sono compressi
# Estensione files da prelevare da Pitagora Reg. (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
GET.EXTENSION.NOW_REG=.xml
# Pattern di inizio del nome file da prelevare
#GET.NOMEFILE_PATTERN.NOW_REG=N_TLC_*_N_??[.]xml
# Abilita o disabilita decompressione GZip per estensione .gz (YES|NO)
GET.UNCOMPRESS.NOW_REG=NO
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
GET.CONNECT.TIMEOUT.NOW_REG=30000
# FILE PER GESTIONE STORICO FILE LAVORATI
GET.FILENAME.LAVORATI.NOW_REG=/gnpapp/properties/N_TLC_STORICO_NOW.txt
#####
##### CONFIGURAZIONE PER GET DA NOW_XDSL
#####
GET.LOCAL.PATH.NOW_XDSL=/gnpapp/batch/ftpin/now/xdsl
GET.IDENTITY.NOW_XDSL=/home/bea/.ssh/id_rsa
GET.PASSPHRASE.NOW_XDSL=
GET.USERNAME.NOW_XDSL=dbcfx
#GET.HOST.XDSL=10.119.3.119
# PORTALE WHOLESALE WINDOWS
#GET.HOST.NOW_XDSL=172.30.16.99
# PORTALE WHOLESALE LINUX
GET.HOST.REG=172.30.16.178
GET.PORT.NOW_XDSL=22
GET.KNOWN_HOSTS.NOW_XDSL=/home/bea/.ssh/known_hosts
GET.REMOTE_PATH.NOW_XDSL=/HOME/xdsl/adsl_002/adsl_002_riscontri
# Estensione files da prelevare da Pitagora XDSL (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
GET.EXTENSION.NOW_XDSL=.xml.gz;.bad
# Pattern di inizio del nome file da prelevare
GET.NOMEFILE_PATTERN.NOW_XDSL=002_*_xDSL[.]*
# Abilita o disabilita decompressione GZip per estensione .gz (YES|NO)
GET.UNCOMPRESS.NOW_XDSL=YES
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
GET.CONNECT.TIMEOUT.NOW_XDSL=30000
# FILE PER GESTIONE STORICO FILE LAVORATI (in /gnpapp/
GET.FILENAME.LAVORATI.NOW_XDSL=/gnpapp/properties/002_TLC_STORICO_NOW.txt
#####
##### CONFIGURAZIONE PER GET DA NOW_NGA
#####
GET.LOCAL.PATH.NOW_NGA=/gnpapp/batch/ftpin/now/nga
GET.IDENTITY.NOW_NGA=/home/bea/.ssh/id_rsa
GET.PASSPHRASE.NOW_NGA=
GET.USERNAME.NOW_NGA=dbcfx
#GET.HOST.XDSL=10.119.3.119
# PORTALE WHOLESALE WINDOWS
#GET.HOST.NOW_NGA=172.30.16.99
# PORTALE WHOLESALE LINUX
GET.HOST.REG=172.30.16.178
GET.PORT.NOW_NGA=22
GET.KNOWN_HOSTS.NOW_NGA=/home/bea/.ssh/known_hosts
GET.REMOTE_PATH.NOW_NGA=/HOME/xdsl/adsl_002/adsl_002_riscontri
# Estensione files da prelevare da Pitagora XDSL (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
GET.EXTENSION.NOW_NGA=.xml.gz;.bad
# Pattern di inizio del nome file da prelevare
GET.NOMEFILE_PATTERN.NOW_NGA=002_*_NGA[.]*
# Abilita o disabilita decompressione GZip per estensione .gz (YES|NO)
GET.UNCOMPRESS.NOW_NGA=YES
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
GET.CONNECT.TIMEOUT.NOW_NGA=30000
# FILE PER GESTIONE STORICO FILE LAVORATI (in /gnpapp/
GET.FILENAME.LAVORATI.NOW_NGA=/gnpapp/properties/002_TLC_STORICO_NOW.txt
##################### FINE CONFIGURAZIONI NOW #####################
#####################################################################
##############
#OPENACCESS
##############
# CONFIGURAZIONE PER PUT SU OPENACCESS
PUT.LOCAL.PATH.OPEN=/gnpapp/batch/ftpout/open
PUT.LOCAL.BACKUP.PATH.OPEN=/gnpapp/batch/backup_out/open
# Estensione files da inviare verso OpenAccess (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
PUT.EXTENSION.OPEN=.txt
#PUT.IDENTITY.OPEN=/bea/.ssh/id_rsa
PUT.IDENTITY.OPEN=/home/bea/.ssh/id_rsa
PUT.PASSPHRASE.OPEN=beaopen
PUT.USERNAME.OPEN=rientriOLO
PUT.HOST.OPEN=10.192.65.18
PUT.PORT.OPEN=22
#PUT.KNOWN_HOSTS.OPEN=/bea/.ssh/known_hosts
PUT.KNOWN_HOSTS.OPEN=/home/bea/.ssh/known_hosts
PUT.REMOTE_PATH.OPEN=/
PUT.RENAMECTR.OPEN=NO
PUT.RENAMECTR.SUFFIX.OPEN=
PUT.SENDCTR.OPEN=NO
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
PUT.CONNECT.TIMEOUT.OPEN=30000
##############
#MPA
##############
# CONFIGURAZIONE PER PUT SU MPA
PUT.LOCAL.PATH.MPA=/gnpapp/batch/ftpout/mpa
PUT.LOCAL.BACKUP.PATH.MPA=/gnpapp/batch/backup_out/mpa
# Estensione files da inviare verso MPA (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
PUT.EXTENSION.MPA=.csv
PUT.IDENTITY.MPA=/bea/.ssh/id_rsa
PUT.PASSPHRASE.MPA=bea
PUT.USERNAME.MPA=bea
PUT.HOST.MPA=10.192.65.18
PUT.PORT.MPA=22
PUT.KNOWN_HOSTS.MPA=/bea/.ssh/known_hosts
PUT.REMOTE_PATH.MPA=/home/bea/temp/mpa
PUT.RENAMECTR.MPA=NO
PUT.RENAMECTR.SUFFIX.MPA=
PUT_SENDCTR.MPA=YES
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
PUT.CONNECT.TIMEOUT.MPA=30000
##############
#DWHE2E
##############
# CONFIGURAZIONE PER PUT SU DWHE2E
PUT.LOCAL.PATH.DWHE2E=/gnpapp/batch/ftpout/dwhe2e
PUT.LOCAL.BACKUP.PATH.DWHE2E=/gnpapp/batch/backup_out/dwhe2e
# Estensione files da inviare verso DWHE2E (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
PUT.EXTENSION.DWHE2E=.csv
PUT.IDENTITY.DWHE2E=/bea/.ssh/id_rsa
PUT.PASSPHRASE.DWHE2E=bea
PUT.USERNAME.DWHE2E=bea
PUT.HOST.DWHE2E=10.192.65.18
PUT.PORT.DWHE2E=22
PUT.KNOWN_HOSTS.DWHE2E=/bea/.ssh/known_hosts
PUT.REMOTE_PATH.DWHE2E=/home/bea/temp/dwhe2e
PUT.RENAMECTR.DWHE2E=NO
PUT.RENAMECTR.SUFFIX.DWHE2E=
PUT_SENDCTR.DWHE2E=YES
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
PUT.CONNECT.TIMEOUT.DWHE2E=30000
##############
##############
#PDC
##############
# CONFIGURAZIONE PER PUT SU TOOL PDC
PUT.LOCAL.PATH.PDC=/gnpapp/batch/ftpout/open
PUT.LOCAL.BACKUP.PATH.PDC=/gnpapp/batch/backup_out/open
# Estensione files da inviare verso PDC (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
PUT.EXTENSION.PDC=.txt
PUT.IDENTITY.PDC=/home/bea/.ssh/id_rsa
PUT.PASSPHRASE.PDC=
PUT.USERNAME.PDC=rientriOLO
PUT.HOST.PDC=10.192.65.18
PUT.PORT.PDC=22
PUT.KNOWN_HOSTS.PDC=/home/bea/.ssh/known_hosts
PUT.REMOTE_PATH.PDC=/
PUT.RENAMECTR.PDC=NO
PUT.RENAMECTR.SUFFIX.PDC=
PUT.SENDCTR.PDC=NO
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
PUT.CONNECT.TIMEOUT.PDC=30000
##############
##############
#CRMB
##############
# CONFIGURAZIONE PER PUT SU CRMB
PUT.LOCAL.PATH.CRMB=/gnpapp/batch/ftpout/open
PUT.LOCAL.BACKUP.PATH.CRMB=/gnpapp/batch/backup_out/open
# Estensione files da inviare verso CRMB (POSSONO ESSERE PIU' DI UNA DELIMITATE DA ";")
PUT.EXTENSION.CRMB=.txt
PUT.IDENTITY.CRMB=/home/bea/.ssh/id_rsa
PUT.PASSPHRASE.CRMB=
PUT.USERNAME.CRMB=rientriOLO
PUT.HOST.CRMB=10.192.65.18
PUT.PORT.CRMB=22
PUT.KNOWN_HOSTS.CRMB=/home/bea/.ssh/known_hosts
PUT.REMOTE_PATH.CRMB=/
PUT.RENAMECTR.CRMB=NO
PUT.RENAMECTR.SUFFIX.CRMB=
PUT.SENDCTR.CRMB=NO
# TIMEOUT IN MILLISECONDI (10000 = 10sec, 1800000 = 30min)
PUT.CONNECT.TIMEOUT.CRMB=30000
##############

View File

@@ -0,0 +1,16 @@
#!/bin/ksh
THIS_PATH=`dirname $0`
. $THIS_PATH/setEnv.sh
SFTPD_PROPERTIES="${GNP_HOME}/properties/sftpd.properties"
CLASSPATH="${GNP_LIB_HOME}/sftpd.jar:${GNP_LIB_HOME}/log4j-1.2.16.jar:${GNP_LIB_HOME}/commons-logging-1.0.4.jar:${GNP_LIB_HOME}/jsch-0.1.54.jar:${GNP_HOME}/properties"
check_node=`$THIS_PATH/TestNodo.sh`
if [ $? -eq 0 ]
then
echo "Nodo abilitato ad eseguire lo script"
${JAVA_HOME}/bin/java -classpath $CLASSPATH -Dfile.encoding=UTF-8 -Dproperties_path="${SFTPD_PROPERTIES}" it.alescar.sftp.daemon.SFTPDaemon $1 $2
else
echo "(WARNING) Nodo non abilitato ad eseguire script"
fi

View File

@@ -0,0 +1,39 @@
package it.alescar.sftp.daemon;
import it.alescar.sftp.daemon.get.SFTPGetDaemon;
import it.alescar.sftp.daemon.put.SFTPPutDaemon;
import org.apache.log4j.Logger;
import java.util.Date;
public class SFTPDaemon {
private static Logger log = Logger.getLogger(SFTPDaemon.class);
private final static String PAR_PUT = "-put";
private final static String PAR_GET = "-get";
public static void main(String[] args) {
log.info("**** START: " + new Date() + " ****");
try {
if (args == null || args.length != 2) {
log.info("SINTASSI: SFTPDaemon -tipo <sistema>");
log.info(" tipo: -put per upload, -get per download");
log.info(" sistema: REG per REGOLATORIO, XDSL per XDSL, OPEN per OPENACCESS");
} else if (args[0].equalsIgnoreCase(PAR_PUT)) {
SFTPPutDaemon daemon = new SFTPPutDaemon(args[1]);
daemon.doTheWork();
} else if (args[0].equalsIgnoreCase(PAR_GET)) {
SFTPGetDaemon daemon = new SFTPGetDaemon(args[1]);
daemon.doTheWork();
} else {
log.info("SINTASSI: SFTPDaemon -tipo <sistema>");
log.info(" tipo: -put per upload, -get per download");
log.info(" sistema: reg per REGOLATORIO, xdsl per XDSL, OPEN per OPENACCESS");
}
} catch (Exception t) {
log.fatal(t.getMessage());
t.printStackTrace();
}
log.info("**** END : " + new Date() + " ****");
}
}

View File

@@ -0,0 +1,241 @@
package it.alescar.sftp.daemon.get;
import it.alescar.sftp.utils.ApplicationConstants;
import it.alescar.sftp.utils.Config;
import it.alescar.sftp.utils.Func;
import it.alescar.sftp.utils.SftpManager;
import org.apache.log4j.Logger;
import java.io.*;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
/**
* @version 2.0 DBCFX_RU_253 riduzione verbosita' log.
* Problema: a ciascuna chiamata la classe scandisce (ed elenca) TUTTI i files che trova
* nella cartella remota SFTP, da cui NON vengono mai cancellati,
* quindi scrive centinaia di righe di log "inutili" piu' volte ogni ora.
* Soluzione: eliminare i messaggi di log relativi a:
* 1. file ignorato in quanto non corrisponde al pattern della chiamata corrente (60% log)
* 2. ripetizione file ignorato in quanto pertinente ma gia' elaborato (20% log)
*
* @version 2.1 DBCFX_RU_256 gestione inventario download passati.
* Problema: nel caso che il processo non riesca a leggere l'inventario dei files gia' acquisiti,
* scarica tutto quello che trova, saturando il file system sia di dati che di log.
* Soluzione: se non riesce a leggere l'inventario, esce in errore senza scaricare dati.
* Suluzione accessoria: abbassato da info a debug i log di normale elaborazione file.
*
*/
public class SFTPGetDaemon {
private static Logger log = Logger.getLogger(SFTPGetDaemon.class);
//PARAMETRI PATH
private String localPath;
//PARAMETRI CONNESSIONE
private String identity;
private String passphrase;
private String username;
private String host;
private String port;
private String knownHosts;
private String remotePath;
private String[] fileExtension;
private boolean unZip;
private String nomeFilePattern;
private int timeout;
//MANAGER DI CONNESSIONE
private SftpManager ftpManager = null;
//FILENAME PER MEMORIZZARE I FILE LAVORATI
private String processedFilename = null;
public SFTPGetDaemon(String idSistemaRemoto) throws Exception {
if (!idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.PIT_REGOLATORIO)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.PIT_XDSL)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.OPENACCESS)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.MPA)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.DWHE2E)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.NOW_REGOLATORIO)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.NOW_XDSL)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.NOW_NGA)){
log.fatal("Sistema remoto sconosciuto: " + idSistemaRemoto);
}
initConfig(idSistemaRemoto);
}
//SISTEMA=REG per pitagora regolatorio, XDSL per pitagora xdsl
private void initConfig(String sistema) throws Exception {
localPath = Config.getString(ApplicationConstants.GET_PROPERTIES.localPath + sistema);
identity = Config.getString(ApplicationConstants.GET_PROPERTIES.identity + sistema);
passphrase = Config.getString(ApplicationConstants.GET_PROPERTIES.passphrase + sistema);
username = Config.getString(ApplicationConstants.GET_PROPERTIES.username + sistema);
host = Config.getString(ApplicationConstants.GET_PROPERTIES.host + sistema);
port = Config.getString(ApplicationConstants.GET_PROPERTIES.port + sistema);
knownHosts = Config.getString(ApplicationConstants.GET_PROPERTIES.knownHosts + sistema);
remotePath = Config.getString(ApplicationConstants.GET_PROPERTIES.remotePath + sistema);
String cfgExtension = Config.getString(ApplicationConstants.GET_PROPERTIES.extension + sistema);
fileExtension = cfgExtension.split(";");
nomeFilePattern = Config.getString(ApplicationConstants.GET_PROPERTIES.nomefile + sistema);
unZip = Config.getYesOrNo(ApplicationConstants.GET_PROPERTIES.uncompress + sistema);
timeout = Config.getInt(ApplicationConstants.GET_PROPERTIES.timeout + sistema);
processedFilename = Config.getString(ApplicationConstants.GET_PROPERTIES.processedFilename + sistema);
log.info("** SYSTEM ID = [" + sistema + "]");
log.info("** LOCAL DESTINATION PATH = [" + localPath + "]");
log.info("** FILE EXTENSIONS = [" + cfgExtension + "]");
log.info("** FILENAME PATTERN = [" + nomeFilePattern + "]");
log.info("** UNCOMPRESS = [" + (unZip ? "YES" : "NO") + "]");
log.info("** TIMEOUT CONNESSIONE = [" + timeout + "]");
log.info("** PROCESSED FILENAME = [" + processedFilename + "]");
File f = new File(localPath);
if (!f.exists()) {
throw new Exception("Attenzione manca la directory locale di destinazione [" + localPath + "]");
}
}
public void doTheWork() throws Exception {
Func function = new Func(); // per l'unzip
//CONNESSIONE SFTP
try {
ftpManager = new SftpManager(identity, passphrase, username, host, port, knownHosts, timeout);
String[] listaFiles = ftpManager.getListaFiles(remotePath);
// Recupera dal file locale, i filename gia' lavorati
TreeSet<String> fileLavorati = readFromFile(processedFilename);
// DBCFX_RU_256 gestione inventario download passati.
// se non riesce a leggere l'inventario, esce in errore senza scaricare dati.
if (null == fileLavorati || fileLavorati.isEmpty()) {
log.error("FAIL - could not read download archive file ["+processedFilename+"] - if absent or empty, please initialize with dummy content [placeholder]");
throw new NullPointerException("could not read download archive file ["+processedFilename+"]");
}
// DBCFX_RU_256 END
for (String fileName : listaFiles) {
// aggiunta per X1 2013
StringTokenizer st = new StringTokenizer(nomeFilePattern, ";");
boolean isOk = false;
while (st.hasMoreTokens() && !isOk) {
String app = st.nextToken();
//isOk = fileName.substring(0, app.length()).equalsIgnoreCase(app);
// [S4 NOW] trasforma il pattern Unix like in una regular expression
String regexPattern = app.replace("?", ".?").replace("*", ".*?");
isOk = fileName.matches(regexPattern);
}
if (isOk) { //se inizia col pattern corretto
// DBCFX_RU_253 riduzione verbosita' log.
//log.info("Elaborazione del file : " + fileName);
int gotFile = 0;
for (String aFileExtension : fileExtension) {
String fileExt = fileName.substring(fileName.length() - aFileExtension.length());
//log.debug("fileExt="+fileExt);//fileName.substring(fileName.lastIndexOf("."));
if (fileExt.equalsIgnoreCase(aFileExtension)) { //e se l'estensione corrisponde a una di quelle configurate
if (!fileLavorati.contains(fileName)) { // [S4 NOW] Se non ho ancora lavorato il file
// DBCFX_RU_253 riduzione verbosita' log.
log.debug("Elaborazione file [" + localPath + "/" + fileName + "]");
File outFile = new File(localPath + "/" + fileName);
// [S4 NOW] Tolto il remove del file remoto
//int retCode = ftpManager.getAndRm(remotePath, fileName, outFile); //allora lo riceve
int retCode = ftpManager.get(remotePath, fileName, outFile); //allora lo riceve
gotFile = gotFile + retCode;
if (unZip //SE unzip abilitato e il file ricevuto e' un .gz
&& retCode > 0
&& aFileExtension.substring(aFileExtension.lastIndexOf(".") + 1).equalsIgnoreCase(ApplicationConstants.EXTENSION_GZ)
) {
function.unZipAndRm(new File(localPath + "/" + fileName), new File(localPath + "/" + fileName.substring(0, fileName.length() - 3)));
}
File ctrFile = new File(localPath + "/" + fileName.substring(0, fileName.length() - aFileExtension.length()) + "." + ApplicationConstants.EXTENSION_CTR);
ctrFile.createNewFile();
// DBCFX_RU_256 abbassato da info a debug
log.debug("Creato ctr [" + localPath + "/" + fileName.substring(0, fileName.length() - aFileExtension.length()) + "." + ApplicationConstants.EXTENSION_CTR + "]");
// Aggiorno la lista dei file lavorati
fileLavorati.add(fileName);
// Memorizzo subito nel file in caso di fault
saveToFile(fileLavorati, processedFilename);
} else {
gotFile = -1;
// DBCFX_RU_256 abbassato da info a debug
log.debug("SKIP - File [" + fileName + "] gia' lavorato!");
}
}
}
if (gotFile == 0) {
// DBCFX_RU_253 riduzione verbosita' log.
//log.warn("Il file " + fileName + " non verra' elaborato in quanto non rispetta la nomenclatura prevista");
}
} else {
// DBCFX_RU_253 riduzione verbosita' log.
//log.warn("Il file " + fileName + " non verra' elaborato in quanto non rispetta la nomenclatura prevista");
}
}
if (ftpManager != null) {
ftpManager.release();
}
} catch (Exception e) {
if (ftpManager != null) {
ftpManager.release();
}
throw e;
}
}
/**
* Legge i file lavorati precedentemente memorizzati nel file in input
* Ogni riga e' un nome file.
* Restituisce un TreeSet
*
* @param filename to be listed
* @pa
*/
public TreeSet<String> readFromFile(String filename) throws Exception {
TreeSet<String> output = new TreeSet<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File(filename)));
String line;
// if no more lines the readLine() returns null
while ((line = br.readLine()) != null) {
// reading lines until the end of the file
output.add(line);
}
return output;
} finally {
if (br != null) {
br.close();
}
}
}
/**
* Salva il TreeSet nel file indicato
*
* @param obj to be listed
* @param path
*/
public void saveToFile(Set<String> obj, String filename) throws Exception {
PrintWriter pw = null;
try {
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"));
for (String s : obj) {
pw.println(s);
}
pw.flush();
} finally {
if (pw != null) {
pw.close();
}
}
}
}

View File

@@ -0,0 +1,177 @@
package it.alescar.sftp.daemon.put;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.FileInputStream;
import it.alescar.sftp.utils.ApplicationConstants;
import it.alescar.sftp.utils.Config;
import it.alescar.sftp.utils.Func;
import it.alescar.sftp.utils.SftpManager;
public class SFTPPutDaemon {
private static Logger log = Logger.getLogger(SFTPPutDaemon.class);
//PARAMETRI PATH
private String localSourcePath;
private String localBackupPath;
private String[] fileExtension;
//PARAMETRI CONNESSIONE
private String identity;
private String passphrase;
private String username;
private String host;
private String port;
private String knownHosts;
private String remotePath;
private boolean sendCtr;
private boolean renameCtr;
private String ctrSuffix;
private int timeout;
//MANAGER DI CONNESSIONE
private SftpManager ftpManager = null;
public SFTPPutDaemon(String idSistemaRemoto) throws Exception{
if(!idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.PIT_REGOLATORIO)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.PIT_XDSL)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.OPENACCESS)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.MPA)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.DWHE2E)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.NOW_REGOLATORIO)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.NOW_XDSL)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.NOW_NGA)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.RAES)
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.MFENP) //ADD FENP RU_279
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.PDC) //ADD PDC RU_352
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.CRMB)){ //ADD CRMB RU_XXX
log.fatal("Sistema remoto sconosciuto: "+idSistemaRemoto);
}
initConfig(idSistemaRemoto);
}
//SISTEMA=REG per pitagora regolatorio, XDSL per pitagora xdsl
private void initConfig(String sistema) throws Exception{
localSourcePath=Config.getString(ApplicationConstants.PUT_PROPERTIES.localPath+sistema);
localBackupPath=Config.getString(ApplicationConstants.PUT_PROPERTIES.localBackupPath+sistema);
String cfgExtension=Config.getString(ApplicationConstants.PUT_PROPERTIES.extension+sistema);
fileExtension=cfgExtension.split(";");
identity=Config.getString(ApplicationConstants.PUT_PROPERTIES.identity + sistema);
passphrase=Config.getString(ApplicationConstants.PUT_PROPERTIES.passphrase+sistema);
username=Config.getString(ApplicationConstants.PUT_PROPERTIES.username+sistema);
host=Config.getString(ApplicationConstants.PUT_PROPERTIES.host+sistema);
port=Config.getString(ApplicationConstants.PUT_PROPERTIES.port+sistema);
knownHosts=Config.getString(ApplicationConstants.PUT_PROPERTIES.knownHosts+sistema);
remotePath=Config.getString(ApplicationConstants.PUT_PROPERTIES.remotePath+sistema);
sendCtr=Config.getYesOrNo(ApplicationConstants.PUT_PROPERTIES.sendCtr + sistema);
renameCtr=Config.getYesOrNo(ApplicationConstants.PUT_PROPERTIES.renameCtr+sistema);
ctrSuffix=Config.getString(ApplicationConstants.PUT_PROPERTIES.ctrSuffix + sistema);
timeout = Config.getInt(ApplicationConstants.PUT_PROPERTIES.timeout+sistema);
log.info("** SYSTEM ID = "+sistema);
log.info("** LOCAL SOURCE PATH = "+localSourcePath);
log.info("** LOCAL BACKUP PATH = "+localBackupPath);
log.info("** FILE EXTENSIONS = "+cfgExtension);
log.info("** SEND CTR = " +(sendCtr ?"YES":"NO"));
log.info("** TIMEOUT CONNESSIONE = " + timeout);
if(sendCtr){
log.info("** RENAME CTR = " +(renameCtr ?"YES"+" - "+ctrSuffix:"NO"));
}
}
public void doTheWork() throws Exception {
File f = new File(localSourcePath);
String[] aFile = f.list();
for (int i = 0;i<aFile.length;i++){
File fileTrovato=new File(localSourcePath+"/"+aFile[i]);
String fileExt=aFile[i].substring(aFile[i].lastIndexOf(".")+1);
//String postfixFile = aFile[i].substring(aFile[i].lastIndexOf("_"));
if(!fileTrovato.isDirectory() && sendCtr && (ApplicationConstants.EXTENSION_CTR.equalsIgnoreCase(fileExt)// cristiano aggiunta per x11 2012
//||ApplicationConstants.POSTFIX_FILE_PLUG.equalsIgnoreCase(postfixFile)
)) {
boolean sended=false;
String fileNameCtr=aFile[i];
log.info("Elaborazione del file di controllo: "+fileNameCtr);
for(int j=0;j<fileExtension.length;j++){
String fileNameToSend=aFile[i].substring(0,aFile[i].length()-4)+fileExtension[j];
log.info("fileNameToSend: "+fileNameToSend);
File fileToSend= new File(localSourcePath+"/"+fileNameToSend);
log.info("fileToSend: "+localSourcePath+fileNameToSend);
if(fileToSend.isFile()){
log.info("fileToSend.isFile()");
put(fileNameToSend,fileNameToSend);
doBackup(fileNameToSend);
sended=true;
}
}
if(sended){
log.info("sended");
if(sendCtr){
log.info("sendCtr"+sendCtr);
String remoteCtrName=fileNameCtr;
if(renameCtr){
remoteCtrName=fileNameCtr.substring(0,fileNameCtr.length()-4)+ctrSuffix;
}
log.info("remoteCtrName" +remoteCtrName);
put(fileNameCtr,remoteCtrName);
}
doBackup(fileNameCtr);
}
} else if (!fileTrovato.isDirectory() && !sendCtr) { // CASO SENZA CTR (p.e. OPENACCESS)
String fileName = aFile[i];
log.info("Elaborazione del file : " + fileName + " per host[" + host + "]");
for (String aFileExtension : fileExtension) {
File fileToSend = new File(localSourcePath + "/" + fileName);
if (fileToSend.isFile() && fileName.endsWith(aFileExtension)) {
put(fileName, fileName);
doBackup(fileName);
}
}
}
}
if(ftpManager!=null) {
ftpManager.release();
}
}
public void put(String fileNameToSend,String remoteFileName) throws Exception{
log.info("put");
if(ftpManager==null) {
log.info("ftpManager null");
ftpManager=new SftpManager(identity,passphrase,username,host,port,knownHosts, timeout);
log.info("arrivo");
}
try{
log.info("local source " +localSourcePath);
log.info("fileNameToSend " +fileNameToSend);
log.info("all " +localSourcePath+"/"+fileNameToSend);
FileInputStream fis = new FileInputStream(localSourcePath+"/"+fileNameToSend);
log.info("local source" +localSourcePath+"/"+fileNameToSend);
ftpManager.put(fis, remotePath, remoteFileName);
log.info("ftpManager");
}
catch(Exception e){
if(ftpManager!=null){
ftpManager.release();
}
throw e;
}
}
public void doBackup(String fileNameToBck) throws Exception{
Func function = new Func();
File filebck = new File(localBackupPath);
if (!filebck.exists()) {
throw new Exception("Attenzione manca la directory di backup: " + localBackupPath);
}
function.backupFile(new File(localSourcePath+"/"+fileNameToBck),new File(localBackupPath+"/"+fileNameToBck));
}
}

View File

@@ -0,0 +1,60 @@
package it.alescar.sftp.utils;
public class ApplicationConstants {
public static final String PIT_REGOLATORIO = "REG"; // Utilizzato per la GET ad CRMWS dei servizi regolamentati
public static final String PIT_XDSL = "XDSL"; // Utilizzato per la GET ad CRMWS dei servizi xDSL
public static final String OPENACCESS = "OPEN";
public static final String MPA = "MPA";
public static final String DWHE2E = "DWHE2E";
public static final String RAES = "RAES";
//COSTANTE PER REPORT FENP RU_279
public static final String MFENP="MFENP";
//COSTANTE PER REPORT FENP RU_352
public static final String PDC="PDC";
//COSTANTE PER REPORT FENP RU_XXX
public static final String CRMB="CRMB";
public static final String NOW_REGOLATORIO = "NOW_REG"; // Utilizzato per la GET da NOW dei servizi regolamentati
public static final String NOW_XDSL = "NOW_XDSL"; // Utilizzato per la GET da NOW dei servizi xDSL
public static final String NOW_NGA = "NOW_NGA"; // Utilizzato per la GET da NOW dei servizi NGA
public static final String EXTENSION_CTR = "ctr";
public static final String EXTENSION_GZ = "gz";
public static final String POSTFIX_FILE_PLUG = "_f.xml";
public class PUT_PROPERTIES {
public static final String localPath = "PUT.LOCAL.PATH.";
public static final String localBackupPath = "PUT.LOCAL.BACKUP.PATH.";
public static final String extension = "PUT.EXTENSION.";
public static final String identity = "PUT.IDENTITY.";
public static final String passphrase = "PUT.PASSPHRASE.";
public static final String username = "PUT.USERNAME.";
public static final String host = "PUT.HOST.";
public static final String port = "PUT.PORT.";
public static final String knownHosts = "PUT.KNOWN_HOSTS.";
public static final String remotePath = "PUT.REMOTE_PATH.";
public static final String sendCtr = "PUT_SENDCTR.";
public static final String renameCtr = "PUT.RENAMECTR.";
public static final String ctrSuffix = "PUT.RENAMECTR.SUFFIX.";
public static final String timeout = "PUT.CONNECT.TIMEOUT.";
}
public class GET_PROPERTIES {
public static final String localPath = "GET.LOCAL.PATH.";
public static final String extension = "GET.EXTENSION.";
public static final String identity = "GET.IDENTITY.";
public static final String passphrase = "GET.PASSPHRASE.";
public static final String username = "GET.USERNAME.";
public static final String host = "GET.HOST.";
public static final String port = "GET.PORT.";
public static final String knownHosts = "GET.KNOWN_HOSTS.";
public static final String remotePath = "GET.REMOTE_PATH.";
public static final String nomefile = "GET.NOMEFILE_PATTERN.";
public static final String uncompress = "GET.UNCOMPRESS.";
public static final String timeout = "GET.CONNECT.TIMEOUT.";
public static final String processedFilename = "GET.FILENAME.LAVORATI.";
}
}

View File

@@ -0,0 +1,91 @@
package it.alescar.sftp.utils;
import java.io.*;
import java.util.*;
public class Config {
private static final String PATH_PROPERTIES="properties_path";
private static Properties props = null;
private static String propertiesPath = null;
static {
init();
}
private Config() {
}
private static void init(){
FileInputStream f = null;
propertiesPath = System.getProperty(PATH_PROPERTIES);
System.out.println("Path delle Properties ottenuto per SFTPD: " +
propertiesPath);
try {
f = new FileInputStream(propertiesPath);
//load del file di Properties
props = new Properties();
props.load(f);
} catch (Exception ex) {
System.out.println("ERRORE nella creazione del properties:" +ex.getMessage());
} finally {
try {
if (f != null) {
f.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* CARLO 03-02-2003
* aggiunto metodo Trim(String) richiemato da tutti i metodi
* Se null->null altrimenti trimmo
* */
private static final String Trim(String valore) {
if (valore != null) {
return valore.trim();
} else {
return null;
}
}
public static String getString(String property) {
if (props == null) {
init();
}
String ret = Trim(props.getProperty(property));
return ret;
}
public static int getInt(String property) {
if (props == null) {
init();
}
int ret;
try {
ret = Integer.parseInt(Trim(props.getProperty(property)));
} catch (NumberFormatException nfe) {
// ret = -1;
System.out.println("ERRORE nel leggere il valore numerico della property ["+property+"]");
ret = -1;
}
return ret;
}
public static boolean getYesOrNo(String property) {
boolean retCode=false;
if (props == null) {
init();
}
String ret = Trim(props.getProperty(property));
if( ret!=null && ret.equalsIgnoreCase("YES")){
retCode=true;
}
return retCode;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,63 @@
package it.alescar.sftp.utils;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class Func {
private static Logger log = Logger.getLogger(Func.class);
/* archivia il file e cancella il file sorgente
*
*/
public void backupFile(File in, File out) throws Exception {
log.info("Backup del file " + in.getAbsolutePath() + " sul file " + out.getAbsolutePath());
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(in);
fos = new FileOutputStream(out);
byte[] buf = new byte[1024];
int i = 0;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
in.delete();
} catch (IOException io) {
log.error(io);
} finally {
if (fis != null) fis.close();
if (fos != null) fos.close();
}
}
public void unZipAndRm(File zipFile, File outFile) throws Exception {
GZIPInputStream gzipInputStream = null;
FileOutputStream out = null;
try {
gzipInputStream = new GZIPInputStream(new FileInputStream(zipFile));
out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = gzipInputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
//out.close();
log.info("Unzip del file " + outFile.getAbsolutePath());
zipFile.delete();
} catch (Exception t) {
log.warn("Unzip del file " + zipFile.getAbsolutePath() + " non riuscito!");
throw t;
} finally {
if (out != null) out.close();
if (gzipInputStream != null) gzipInputStream.close();
}
}
}

View File

@@ -0,0 +1,233 @@
package it.alescar.sftp.utils;
import com.jcraft.jsch.*;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;
public class SftpManager {
private JSch jsch = null;
private Session session = null;
private Channel channel = null;
private ChannelSftp c = null;
private String identity;
private String passphrase;
private String username;
private String host;
private String port;
private String knownHosts;
private static Logger log = Logger.getLogger(SftpManager.class);
public SftpManager(String identity, String passphrase, String username,
String host, String port, String knownHosts, int timeout) throws Exception {
this.identity = identity;
this.passphrase = passphrase;
this.username = username;
this.host = host;
this.port = port;
this.knownHosts = knownHosts;
log.info("identity "+identity);
log.info("passphrase "+passphrase);
log.info("username "+username);
log.info("host "+host);
log.info("port "+port);
log.info("knownHosts "+knownHosts);
log.info("timeout "+timeout);
connect(timeout);
}
/**
* metodo che esegue l'upload sul file system remoto
*
* @param inputStream input stream del file da uploadare
* @param destFolder cartella sul fs remoto
* @param filePath percorso su cui salvare il file (all'interno di destFolder)
* @param fileName nome del file
* @throws SftpAFException
*/
public void put(InputStream inputStream, String destFolder, String fileName) throws Exception {
try {
try {
c.cd(c.getHome());
//log.info("cd dir destFolder = " + destFolder);
c.cd(destFolder);
} catch (SftpException e) {
//log.debug("creo la cartella destFolder " + destFolder);
c.mkdir(destFolder);
c.cd(destFolder);
}
c.put(inputStream, fileName);
log.info("File " + fileName + " inviato con successo ....");
} catch (SftpException e) {
log.error("Errore nell'invio del file " + fileName + " via SFTP: " + e.getMessage());
throw e;
}
}
/**
* metodo che ritorna la lista dei files nella cartella specificata
*
* @param destFolder cartella sul fs remoto
* @throws SftpAFException
*/
public String[] getListaFiles(String destFolder) throws Exception {
try {
c.cd(c.getHome());
log.debug("cd dir destFolder = " + destFolder);
c.cd(destFolder);
Vector listaFilesTrovati = c.ls(destFolder);
ArrayList<String> lista = new ArrayList<String>();
for (int i = 0; i < listaFilesTrovati.size(); i++) {
com.jcraft.jsch.ChannelSftp.LsEntry fFound = (com.jcraft.jsch.ChannelSftp.LsEntry) listaFilesTrovati.get(i);
if (!fFound.getAttrs().isDir()) {
lista.add(fFound.getFilename());
}
}
String[] risultati = new String[lista.size()];
risultati = (String[]) lista.toArray(risultati);
return risultati;
} catch (SftpException e) {
log.error("Errore nell'ls del path " + destFolder + " via SFTP: " + e.getMessage());
throw e;
}
}
public int get(String destFolder, String fileName, File outFile) throws Exception {
return getAndRm(destFolder, fileName, outFile, false);
}
/**
* metodo che esegue la move (get+rm) da fs remoto a fs locale
*
* @param destFolder cartella sul fs remoto
* @param fileName nome del file da trasferire
* @param outFile File locale di output
* @param deleteRemoteFile indica se cancellare il file remoto
* @throws SftpAFException
*/
public int getAndRm(String destFolder, String fileName, File outFile, boolean deleteRemoteFile) throws Exception {
int retCode = 0;
try {
c.cd(c.getHome());
c.cd(destFolder);
log.info("Ricevo il file remoto " + destFolder + "/" + fileName + " ....");
FileOutputStream fos = new FileOutputStream(outFile);
c.get(fileName, fos);
fos.close();
if (deleteRemoteFile) {
log.info("File remoto " + destFolder + "/" + fileName + " cancellato ....");
c.rm(fileName);
}
log.info("File " + destFolder + "/" + fileName + " ricevuto con successo ....");
retCode = 1;
} catch (SftpException e) {
if (e.id != 2) {
log.error("Errore nella get del file " + destFolder + "/" + fileName + " in SFTP: " + e.getMessage() + "e.id=" + e.id);
throw e;
} else {//per file non trovato tento di cancellare il file locale creato vuoto..
try {
outFile.delete();
} catch (Throwable t) {
}
}
}
return retCode;
}
/**
* metodo da invocare per ottenere la connessione sul canale sftp
*
* @param timeout timeout per la connessione sftp
* @return true se la connessione e' riuscita, altrimenti false
*/
public void connect(int timeout) throws Exception {
this.getChannelSftpEnv(timeout);
}
/**
* metodo privato per la connessione al canale sftp
*
* @param timeout timeout per la connessione sftp
* @return true se la connessione e' riuscita, altrimenti false
*/
private void getChannelSftpEnv(int timeout) throws Exception {
log.info("getChannelSftpEnv ");
jsch = new JSch();
jsch.addIdentity(identity, passphrase);
jsch.setKnownHosts(knownHosts);
session = jsch.getSession(username, host, Integer.parseInt(port));
session.setTimeout(timeout);
log.info("session "+session);
Hashtable<String, String> prop = new Hashtable<String, String>();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
log.info("channel "+channel);
c = (ChannelSftp) channel;
log.info("Connesso in SFTP con " + username + "@" + host + ":" + port);
}
/**
* metodo per il rilascio delle risorse
* utilizzato a fine connessione
*/
public void release() {
try {
if (c != null) {
c.quit();
c.disconnect();
c = null;
}
} catch (Exception e) {
log.warn(e.getMessage());
}
try {
if (channel != null) {
channel.disconnect();
channel = null;
}
} catch (Exception e) {
log.warn(e.getMessage());
}
try {
if (session != null) {
session.disconnect();
session = null;
}
} catch (Exception e) {
log.warn(e.getMessage());
}
try {
if (jsch != null) {
jsch.removeAllIdentity();
jsch = null;
}
} catch (Exception e) {
log.warn(e.getMessage());
}
log.info("Connessione SFTP chiusa");
}
}