First Commit - Source Code from Reply
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding//src/main/java=UTF-8
|
||||
encoding/<project>=UTF-8
|
||||
9
dbcm/dbcm-sftp/.settings/org.eclipse.jdt.core.prefs
Normal file
9
dbcm/dbcm-sftp/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,9 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.release=disabled
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
4
dbcm/dbcm-sftp/.settings/org.eclipse.m2e.core.prefs
Normal file
4
dbcm/dbcm-sftp/.settings/org.eclipse.m2e.core.prefs
Normal file
@@ -0,0 +1,4 @@
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
||||
<wb-module deploy-name="dbcm-sftp">
|
||||
<wb-resource deploy-path="/" source-path="/src/main/java"/>
|
||||
</wb-module>
|
||||
</project-modules>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<installed facet="java" version="1.6"/>
|
||||
<installed facet="jst.utility" version="1.0"/>
|
||||
</faceted-project>
|
||||
@@ -0,0 +1,2 @@
|
||||
disabled=06target
|
||||
eclipse.preferences.version=1
|
||||
39
dbcm/dbcm-sftp/pom.xml
Normal file
39
dbcm/dbcm-sftp/pom.xml
Normal file
@@ -0,0 +1,39 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>dbcm-sftp</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>dbcm</artifactId>
|
||||
<groupId>dbcm</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.6</maven.compiler.source>
|
||||
<maven.compiler.target>1.6</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.jcraft</groupId>
|
||||
<artifactId>jsch</artifactId>
|
||||
<version>0.1.55</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.7</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>dbcm</groupId>
|
||||
<artifactId>dbcm-core</artifactId>
|
||||
<type>jar</type>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,19 @@
|
||||
package dbcm.sftp.factory;
|
||||
|
||||
import dbcm.sftp.manager.SftpManager;
|
||||
import dbcm.sftp.manager.SftpManagerI;
|
||||
|
||||
public class SftpManagerFactory {
|
||||
|
||||
public static SftpManagerI create(SftpManagerType sftpManagerType,String privateKeyPath,
|
||||
String privateKeyPassPhrase, String user, String host,String port, String knowHosts,
|
||||
boolean strictHostKeyChecking, int timeout) throws Exception{
|
||||
switch(sftpManagerType){
|
||||
case JSCH:
|
||||
return new SftpManager(privateKeyPath, privateKeyPassPhrase, user, host, port, knowHosts, strictHostKeyChecking, timeout);
|
||||
default:
|
||||
return new SftpManager(privateKeyPath, privateKeyPassPhrase, user, host, port, knowHosts, strictHostKeyChecking, timeout);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package dbcm.sftp.factory;
|
||||
|
||||
public enum SftpManagerType {
|
||||
JSCH
|
||||
}
|
||||
130
dbcm/dbcm-sftp/src/main/java/dbcm/sftp/handler/SftpHandler.java
Normal file
130
dbcm/dbcm-sftp/src/main/java/dbcm/sftp/handler/SftpHandler.java
Normal file
@@ -0,0 +1,130 @@
|
||||
package dbcm.sftp.handler;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import dbcm.sftp.factory.SftpManagerFactory;
|
||||
import dbcm.sftp.factory.SftpManagerType;
|
||||
import dbcm.sftp.manager.SftpManagerI;
|
||||
import dbcm.utilities.FileUitls;
|
||||
|
||||
public class SftpHandler{
|
||||
|
||||
private SftpManagerI sftpManager;
|
||||
private Logger logger;
|
||||
|
||||
public SftpHandler(String privateKeyPath,String privateKeyPassPhrase, String user, String host,
|
||||
String port, String knowHosts, boolean strictHostKeyChecking, int timeout) throws Exception {
|
||||
try {
|
||||
setLog();
|
||||
generateSftpSession(privateKeyPath, privateKeyPassPhrase, user, host, port, knowHosts, strictHostKeyChecking, timeout);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
System.out.println("Errore nella creazione del SftpFileHandler " +ex.toString());
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
private void generateSftpSession(String privateKeyPath, String privateKeyPassPhrase, String user, String host,
|
||||
String port, String knowHosts, boolean strictHostKeyChecking, int timeout) throws Exception {
|
||||
logger.info( "GenerateSftpSession user - "+ user+ " host "+host);
|
||||
|
||||
// Apre la sessione
|
||||
try {
|
||||
sftpManager = SftpManagerFactory.create(SftpManagerType.JSCH, privateKeyPath, privateKeyPassPhrase,
|
||||
user, host, port, knowHosts,strictHostKeyChecking, timeout);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// C - PROBLEMI DI CONNESSIONE
|
||||
logger.error( "Exception in generateSftpSession - "+ ex.toString());
|
||||
throw ex;
|
||||
}
|
||||
logger.info( "Connessione stabilita Correttamente");
|
||||
}
|
||||
|
||||
public List<String> getFileListFromPattern(String remoteGetPath, String pattern) throws Exception {
|
||||
logger.info( "Start getFileListFromPattern");
|
||||
try{
|
||||
List<String> fileNameList=sftpManager.getFileListFromPattern(remoteGetPath, pattern);
|
||||
logger.info( "lista file ottenuta correttamente "+fileNameList);
|
||||
return fileNameList;
|
||||
}catch(Exception ex) {
|
||||
logger.error( "Exception in getFileListFromPattern");
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public void getAndStoreFile(String remoteGetPath, String fileName, String localStorePath) throws Exception {
|
||||
logger.info( "Start getAndStoreFile "+remoteGetPath + " fileName "+fileName+" localStorePath "+localStorePath);
|
||||
|
||||
try {
|
||||
sftpManager.get(remoteGetPath, fileName, new File(localStorePath+fileName));
|
||||
logger.info( "Operazione di get e save avvenuta correttamente creato "+localStorePath+fileName);
|
||||
|
||||
}catch(Exception ex) {
|
||||
logger.error( "Exception in getAndStoreFile "+ex.getMessage());
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public void backup(String localDir, String fname, String remoteDir) throws Exception {
|
||||
logger.info("Start backup "+localDir + " remoteDir"+remoteDir +" fname "+fname);
|
||||
try {
|
||||
File f = new File(localDir + "/" + fname);
|
||||
checkExistRemoteDir(remoteDir);
|
||||
backupFile(fname, remoteDir, f);
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error( "ATTENZIONE!!Problemi nel backup del file " + fname +" - "+ e.toString());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private void backupFile(String fname, String remoteDir, File f) throws Exception {
|
||||
logger.info( "Start backupFile");
|
||||
|
||||
try {
|
||||
File copiadif = new File(remoteDir + "BACKUP_" + fname);
|
||||
|
||||
//backuppo se il file non esiste
|
||||
if(!copiadif.exists()) {
|
||||
logger.info( "Il file non esiste procedo al backup");
|
||||
|
||||
FileUitls fileUtils = new FileUitls();
|
||||
fileUtils.copyFile(f, copiadif);
|
||||
}else {
|
||||
logger.info( "Il file è già presente");
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.error( "Exception in backupFile " +e.toString());
|
||||
throw new Exception("ATTENZIONE!!Impossibile eseguire il backup del file: " + fname + "." + " - "+ e.toString());
|
||||
}
|
||||
|
||||
logger.info( "End backupFile");
|
||||
}
|
||||
|
||||
private void checkExistRemoteDir(String remoteDir) throws Exception {
|
||||
logger.info( "Start checkExistRemoteDir");
|
||||
|
||||
File filebck = new File(remoteDir);
|
||||
|
||||
if (!filebck.exists()) {
|
||||
logger.info( "Directorty di backup inesistente");
|
||||
throw new Exception("ATTENZIONE!!Impossibile creare il backup perche' manca la directory di backup: '" + remoteDir + "'");
|
||||
}
|
||||
logger.info( "End checkExistRemoteDir");
|
||||
}
|
||||
|
||||
public void release() {
|
||||
if(sftpManager!=null) {
|
||||
sftpManager.release();
|
||||
}
|
||||
}
|
||||
|
||||
private void setLog() {
|
||||
logger = Logger.getLogger(this.getClass().getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package dbcm.sftp.logger;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
|
||||
public class JSCHSftpLogger implements com.jcraft.jsch.Logger{
|
||||
private Logger logger;
|
||||
|
||||
public JSCHSftpLogger() throws Exception {
|
||||
setLog();
|
||||
}
|
||||
|
||||
private void setLog() {
|
||||
logger = Logger.getLogger(this.getClass().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled(int pLevel) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(int pLevel, String pMessage) {
|
||||
|
||||
switch (pLevel) {
|
||||
case DEBUG:
|
||||
logger.debug(pMessage);
|
||||
break;
|
||||
case INFO:
|
||||
logger.info(pMessage);
|
||||
break;
|
||||
case WARN:
|
||||
logger.warn(pMessage);
|
||||
break;
|
||||
case ERROR:
|
||||
logger.error(pMessage);
|
||||
break;
|
||||
case FATAL:
|
||||
logger.fatal(pMessage);
|
||||
break;
|
||||
default:
|
||||
logger.info(pMessage);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
176
dbcm/dbcm-sftp/src/main/java/dbcm/sftp/manager/SftpManager.java
Normal file
176
dbcm/dbcm-sftp/src/main/java/dbcm/sftp/manager/SftpManager.java
Normal file
@@ -0,0 +1,176 @@
|
||||
package dbcm.sftp.manager;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.jcraft.jsch.*;
|
||||
|
||||
import dbcm.sftp.logger.JSCHSftpLogger;
|
||||
|
||||
public class SftpManager implements SftpManagerI {
|
||||
|
||||
private JSch jsch = null;
|
||||
private Session session = null;
|
||||
private Channel channel = null;
|
||||
private ChannelSftp channelSftp = null;
|
||||
|
||||
private static Logger log;
|
||||
|
||||
public SftpManager(String identity, String passphrase, String username, String host, String port, String knownHosts, boolean strictHostKeyChecking, int timeout) throws Exception {
|
||||
initLog();
|
||||
connect(timeout, identity, passphrase, username, host, port, knownHosts, strictHostKeyChecking);
|
||||
}
|
||||
|
||||
/**
|
||||
* metodo da invocare per ottenere la connessione sul canale sftp
|
||||
*
|
||||
* @param timeout timeout per la connessione sftp
|
||||
* @param knownHosts
|
||||
* @param port
|
||||
* @param host
|
||||
* @param username
|
||||
* @param passphrase
|
||||
* @param identity
|
||||
* @return true se la connessione e' riuscita, altrimenti false
|
||||
*/
|
||||
public void connect(int timeout, String identity, String passphrase, String username, String host, String port, String knownHosts, boolean StrictHostKeyChecking) throws Exception {
|
||||
log.info("Start connect timeout " + timeout + " identity " + identity + " username " + username + " host " + host + " port " + port + " StrictHostKeyChecking " + StrictHostKeyChecking);
|
||||
|
||||
jsch = new JSch();
|
||||
|
||||
// Inizializzazione logger JSCH per visualizzazione info dettagliate della fase di connessione con l'host
|
||||
JSch.setLogger(new JSCHSftpLogger());
|
||||
|
||||
jsch.addIdentity(identity, passphrase);
|
||||
jsch.setKnownHosts(knownHosts);
|
||||
|
||||
setSession(timeout, username, host, port, StrictHostKeyChecking);
|
||||
setChannel(session);
|
||||
|
||||
log.info("Connesso in SFTP con " + username + "@" + host + ":" + port);
|
||||
}
|
||||
|
||||
private void setSession(int timeout, String username, String host, String port, boolean StrictHostKeyChecking) throws JSchException {
|
||||
session = jsch.getSession(username, host, Integer.parseInt(port));
|
||||
session.setTimeout(timeout);
|
||||
|
||||
if (!StrictHostKeyChecking) {
|
||||
disableHostKeyChecking();
|
||||
}
|
||||
|
||||
session.connect();
|
||||
}
|
||||
|
||||
private void disableHostKeyChecking() {
|
||||
Hashtable<String, String> prop = new Hashtable<String, String>();
|
||||
prop.put("StrictHostKeyChecking", "no");
|
||||
session.setConfig(prop);
|
||||
}
|
||||
|
||||
private void setChannel(Session session) throws JSchException {
|
||||
channel = session.openChannel("sftp");
|
||||
channel.connect();
|
||||
channelSftp = (ChannelSftp) channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void get(String remoteFolder, String fileName, File outFile) throws Exception {
|
||||
log.info("Start get remoteFolder " + remoteFolder + " fileName " + fileName);
|
||||
|
||||
try {
|
||||
channelSftp.cd(channelSftp.getHome());
|
||||
channelSftp.cd(remoteFolder);
|
||||
FileOutputStream fos = new FileOutputStream(outFile);
|
||||
channelSftp.get(fileName, fos);
|
||||
fos.close();
|
||||
log.info("File " + remoteFolder + "/" + fileName + " ricevuto con successo ....");
|
||||
} catch (SftpException e) {
|
||||
log.error("Errore nella get del file " + remoteFolder + "/" + fileName + " in SFTP: " + e.toString() + "e.id=" + e.id);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getFileListFromPattern(String destFolder, String pattern) throws Exception {
|
||||
try {
|
||||
cdInDestFolder(destFolder);
|
||||
return getFileNameListFromPattern(pattern);
|
||||
} catch (SftpException e) {
|
||||
log.error("Errore nell'ls del path " + destFolder + " via SFTP: " + e.toString());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private ArrayList<String> getFileNameListFromPattern(String pattern) throws SftpException {
|
||||
ArrayList<String> fileNameList = new ArrayList<String>();
|
||||
Vector<ChannelSftp.LsEntry> list = channelSftp.ls(pattern);
|
||||
|
||||
for (ChannelSftp.LsEntry entry : list) {
|
||||
log.debug("fileName = " + entry.getFilename());
|
||||
if (!entry.getAttrs().isDir()) {
|
||||
fileNameList.add(entry.getFilename());
|
||||
}
|
||||
}
|
||||
return fileNameList;
|
||||
}
|
||||
|
||||
private void cdInDestFolder(String destFolder) throws SftpException {
|
||||
channelSftp.cd(channelSftp.getHome());
|
||||
log.debug("cd dir destFolder = " + destFolder);
|
||||
channelSftp.cd(destFolder);
|
||||
}
|
||||
|
||||
/**
|
||||
* metodo per il rilascio delle risorse utilizzato a fine connessione
|
||||
*/
|
||||
@Override
|
||||
public void release() {
|
||||
try {
|
||||
if (channelSftp != null) {
|
||||
channelSftp.quit();
|
||||
channelSftp.disconnect();
|
||||
channelSftp = null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn(e.toString());
|
||||
}
|
||||
|
||||
try {
|
||||
if (channel != null) {
|
||||
channel.disconnect();
|
||||
channel = null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn(e.toString());
|
||||
}
|
||||
|
||||
try {
|
||||
if (session != null) {
|
||||
session.disconnect();
|
||||
session = null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn(e.toString());
|
||||
}
|
||||
try {
|
||||
if (jsch != null) {
|
||||
jsch.removeAllIdentity();
|
||||
jsch = null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn(e.toString());
|
||||
}
|
||||
log.info("Connessione SFTP chiusa");
|
||||
}
|
||||
|
||||
private void initLog() {
|
||||
log = Logger.getLogger(SftpManager.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package dbcm.sftp.manager;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
public interface SftpManagerI {
|
||||
public void get(String remoteFolder, String fileName, File outFile) throws Exception;
|
||||
public void release();
|
||||
public List<String> getFileListFromPattern(String destFolder, String reg) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package dbcm.sftp.utils;
|
||||
|
||||
public class SftpUtils {
|
||||
public static String sftpPort= "22";
|
||||
public static char specialCharStar= '*';
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
dbcm/dbcm-sftp/target/classes/dbcm/sftp/utils/SftpUtils.class
Normal file
BIN
dbcm/dbcm-sftp/target/classes/dbcm/sftp/utils/SftpUtils.class
Normal file
Binary file not shown.
BIN
dbcm/dbcm-sftp/target/dbcm-sftp-1.0.jar
Normal file
BIN
dbcm/dbcm-sftp/target/dbcm-sftp-1.0.jar
Normal file
Binary file not shown.
5
dbcm/dbcm-sftp/target/maven-archiver/pom.properties
Normal file
5
dbcm/dbcm-sftp/target/maven-archiver/pom.properties
Normal file
@@ -0,0 +1,5 @@
|
||||
#Generated by Maven
|
||||
#Fri Jun 10 14:06:51 CEST 2022
|
||||
version=1.0
|
||||
groupId=dbcm
|
||||
artifactId=dbcm-sftp
|
||||
@@ -0,0 +1,8 @@
|
||||
dbcm\sftp\utils\SftpUtils.class
|
||||
dbcm\sftp\logger\JSCHSftpLogger.class
|
||||
dbcm\sftp\handler\SftpHandler.class
|
||||
dbcm\sftp\factory\SftpManagerFactory$1.class
|
||||
dbcm\sftp\manager\SftpManagerI.class
|
||||
dbcm\sftp\factory\SftpManagerFactory.class
|
||||
dbcm\sftp\factory\SftpManagerType.class
|
||||
dbcm\sftp\manager\SftpManager.class
|
||||
@@ -0,0 +1,7 @@
|
||||
C:\dev\TIM\DBCMNP\dbcmnp_workspace\dbcm\dbcm-sftp\src\main\java\dbcm\sftp\factory\SftpManagerFactory.java
|
||||
C:\dev\TIM\DBCMNP\dbcmnp_workspace\dbcm\dbcm-sftp\src\main\java\dbcm\sftp\manager\SftpManagerI.java
|
||||
C:\dev\TIM\DBCMNP\dbcmnp_workspace\dbcm\dbcm-sftp\src\main\java\dbcm\sftp\manager\SftpManager.java
|
||||
C:\dev\TIM\DBCMNP\dbcmnp_workspace\dbcm\dbcm-sftp\src\main\java\dbcm\sftp\factory\SftpManagerType.java
|
||||
C:\dev\TIM\DBCMNP\dbcmnp_workspace\dbcm\dbcm-sftp\src\main\java\dbcm\sftp\utils\SftpUtils.java
|
||||
C:\dev\TIM\DBCMNP\dbcmnp_workspace\dbcm\dbcm-sftp\src\main\java\dbcm\sftp\handler\SftpHandler.java
|
||||
C:\dev\TIM\DBCMNP\dbcmnp_workspace\dbcm\dbcm-sftp\src\main\java\dbcm\sftp\logger\JSCHSftpLogger.java
|
||||
Reference in New Issue
Block a user