Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2011 Claudius Teodorescu
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* $Id$
*/
package ro.kuberam.libs.java.ftclient.SFTP;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URI;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.Vector;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.stream.StreamResult;
import org.apache.log4j.Logger;
import ro.kuberam.libs.java.ftclient.AbstractConnection;
import ro.kuberam.libs.java.ftclient.ErrorMessages;
import ro.kuberam.libs.java.ftclient.ExpathFTClientModule;
import ro.kuberam.libs.java.ftclient.utils.GenerateResourceElement;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
/**
* Implements a public interface for a SFTP connection.
*
* @author Claudius Teodorescu
*/
public class SFTP extends AbstractConnection {
private static final Logger log = Logger.getLogger(SFTP.class);
private static String moduleNsUri = "";
static {
moduleNsUri = ExpathFTClientModule.NAMESPACE_URI;
}
private static String modulePrefix = "";
static {
modulePrefix = ExpathFTClientModule.PREFIX;
}
public X connect(URI remoteHostURI, String username, String password, String remoteHost,
int remotePort, String clientPrivateKey) throws Exception {
long startTime = new Date().getTime();
X abstractConnection = null;
remotePort = (remotePort == -1) ? (int) 22 : remotePort;
// JSch.setLogger(new MyLogger());
JSch jSch = new JSch();
Session sftpConnection = null;
try {
if (clientPrivateKey.length() != 0) {
try {
String uuid = UUID.randomUUID().toString();
File clientPrivateKeyTempFile = File.createTempFile("SFTPprivateKey" + uuid, ".pem");
BufferedWriter out = new BufferedWriter(new FileWriter(
clientPrivateKeyTempFile.getAbsolutePath()));
out.write(clientPrivateKey);
out.close();
// OutputStream out = new
// FileOutputStream(clientPrivateKeyTempFile);
// byte buf[] = new byte[1024];
// int len;
// while((len = clientPrivateKey.read(buf))>0) {
// out.write(buf,0,len);
// }
// out.close();
// clientPrivateKey.close();
jSch.addIdentity(clientPrivateKeyTempFile.getCanonicalPath());
clientPrivateKeyTempFile.delete();
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
}
}
sftpConnection = jSch.getSession(username, remoteHost, remotePort);
sftpConnection.setConfig("StrictHostKeyChecking", "no");
sftpConnection.setConfig("PreferredAuthentications", "publickey,password");
sftpConnection.setTimeout(15000);
sftpConnection.setPassword(password);
// System.out.println(SFTPconnection.getServerVersion());
sftpConnection.connect();
abstractConnection = (X) sftpConnection;
log.info("The SFTP sub-module connected to '" + remoteHostURI + "' in "
+ (new Date().getTime() - startTime) + " ms.");
} catch (JSchException ex) {
log.error(ex.getMessage(), ex);
throw new Exception(ErrorMessages.err_FTC005);
}
return abstractConnection;
}
public StreamResult listResources(Object abstractConnection, String remoteResourcePath)
throws Exception {
long startTime = new Date().getTime();
log.info("The SFTP sub-module is preparing to retrieve the list of resources for '"
+ remoteResourcePath + "'.");
if (!checkIsDirectory(remoteResourcePath)) {
throw new Exception(ErrorMessages.err_FTC008);
}
Session session = (Session) abstractConnection;
if (!session.isConnected()) {
throw new Exception(ErrorMessages.err_FTC002);
}
Channel SFTPchannel = null;
ChannelSftp connection = null;
try {
SFTPchannel = session.openChannel("sftp");
connection = (ChannelSftp) SFTPchannel;
SFTPchannel.connect();
} catch (JSchException ex) {
log.error(ex.getMessage(), ex);
}
List