FTPSClient.listFiles() not working for NonStop/Tandem System - java

I am writing a small FTPS client that will download Enscribe files from NonStop/Tandem and will be processes in Windows. I am using the Apache Commons Net API to achieve this.
I am able to download and upload files from and to NonStop/Tandem. But I am not able to list the files and the directories using the listFiles() and/or mlistDir() methods present under org.apache.commons.net.ftp.FTPClient class.
Below is my code to list the files present in the current working directory.
FTPSClient client = new FTPSClient(false);
try {
client.connect(serverAddress, serverPort);
if (FTPReply.isPositiveCompletion(client.getReplyCode())) {
if (client.login(userName, passwd)) {
System.out.println(client.getReplyString());
// Set protection buffer size
client.execPBSZ(0);
// Set data channel protection to private
client.execPROT("P");
// Enter local passive mode
client.enterLocalPassiveMode();
// Get Current Working Directory
client.printWorkingDirectory();
System.out.println(client.getReplyString());
FTPFile[] files = client.listFiles();
// Logout
client.logout();
System.out.println(client.getReplyString());
} else {
System.out.println("Login failed...");
}
// Disconnect from Server
client.disconnect();
System.out.println("Disconnected from Host...");
} else {
System.out.println("Connection to Host failed...");
System.out.println("Error Code - " + reply);
}
} catch (Exception e) {
e.printStackTrace();
}
I get the following error while executing the code:
org.apache.commons.net.ftp.parser.ParserInitializationException: Unknown parser type: Nonstop J-series Server : J06.19.
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:169)
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:94)
at org.apache.commons.net.ftp.FTPClient.__createParser(FTPClient.java:3377)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3334)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3012)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3065)
at com.connect.ssl.FTPSTest.main(FTPSTest.java:57)
I even tried to set the FTPClient configuration as UNIX as below, but it didn't helped.
client.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
Can anyone help me with this.

Related

Unable to connect to Oracle database using the JAR file

I'm using eclipse 2020 edition and I've added all libraries I need to connect to Oracle server like ojdbc7.jar and my code is like this:
public Connection SetDatabaseConnection() {
writeInLog("Connecting to IRB", 0);
if(openConnection()){
try {
productionPool.setDriverType("thin");
productionPool.setUser(username);
productionPool.setPassword(password);
productionPool.setPortNumber(Integer.parseInt(port));
productionPool.setServerName(IP);
productionPool.setServiceName(serviceName);
productionPool.setURL("jdbc:oracle:thin:#"+ _connStr.substring(_connStr.indexOf(":")+1));
productionPooledConnection = productionPool.getPooledConnection();
if (productionPooledConnection != null) {
//return true;
currentConnection = productionPooledConnection.getConnection();
logger.info("Connected to IRB server");
return currentConnection;
}
} catch (SQLException ex) {
logger.info("Unable to connect to IRB server, SQLException: "+ex.getMessage());
System.out.println(" (IRB-Exception) DB Exception: \n"+ ex);
}
}
}
my problem is: i can connect to the server while debugging or running the application in the eclipse but when I exported a JAR file the application stopped in this step.
in addition:
my code to open a connection:
private boolean openConnection(){
try {
productionPool = new OracleConnectionPoolDataSource();
productionPooledConnection = new OraclePooledConnection();
logger.info("openConnection(): Connected to IRB server \n");
return true;
} catch (SQLException e) {
e.printStackTrace();
logger.info("Unable to connect to IRB server , SQLException: "+e.getMessage());
}
logger.info("openConnection(): Unable to connect to IRB server \n");
return false;
}
The application never throws any excption it only write in the log file this statment: writeInLog("Connecting to IRB", 0);
I couldn't find the exact reason why this happened but I removed the JARs that cause the error FAT Jar Export: couldn't find the class-path for ...etc and import them again. It worked successfully.

FTPClient How to get around org.apache.commons.net.io.CopyStreamException: IOException caught while copying

I am trying to upload a .zip File with my Java application to an FTP server using the Apache Commons Net FTPSClient.
<pre><code>import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPSClient;
public class FTPUploader {
/**
* Zips the source file first and then uploads it to the ftp server
* #param source - The file to be uploaded
* #param target - The file on the ftp server to upload to
*/
public static void upload(String source, String target){
BufferedInputStream stream = null;
File zippedFile = new File("/temp/"+UUID.randomUUID()+".zip");
File targetFile = new File(target);
FTPSClient client = new FTPSClient();
System.out.println("[FTPClient] Starting Upload of "+source+" to "+target);
try {
//Establish connection to FTP Server
client.connect("localhost");
client.login("user", "password"); //changed
client.setFileType(FTP.BINARY_FILE_TYPE);
client.enterLocalPassiveMode();
client.execPBSZ(0);
client.execPROT("P");
//Get the original File
File file = new File(source);
//Zip the file before uploading
if(file.isDirectory()){
ZipUtil.zipDirectory(source, zippedFile.getPath());
}
else{
ZipUtil.zipFile(source, zippedFile.getPath());
}
//Go to the directory on the ftp server
String directoryPath = targetFile.getParentFile().getPath();
//If it doesn't exist create it
if(!client.changeWorkingDirectory(directoryPath)){
client.makeDirectory(directoryPath);
client.changeWorkingDirectory(directoryPath);
}
//Create an InputStream of the zipped file to be uploaded
stream = new BufferedInputStream(new FileInputStream(zippedFile));
//Store zipped file to server
if(client.storeFile(targetFile.getName(), stream)){
System.out.println("[FTPClient] Done!");
}
else{
System.out.println("[FTPClient] Upload failed: "+client.getReplyString());
}
//Finish up
client.logout();
} catch (IOException e) {
System.out.println("[FTPClient] Error! Last Reply: "+client.getReplyString());
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
client.disconnect();
zippedFile.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
at org.apache.commons.net.io.Util.copyStream(Util.java:136)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:675)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:639)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:2030)
... 2 more
Caused by: java.net.SocketException: Broken pipe (Write failed)
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at sun.security.ssl.OutputRecord.writeBuffer(OutputRecord.java:431)
at sun.security.ssl.OutputRecord.write(OutputRecord.java:417)
at sun.security.ssl.SSLSocketImpl.writeRecordInternal(SSLSocketImpl.java:886)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:857)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:126)
at org.apache.commons.net.io.Util.copyStream(Util.java:124)
... 5 more</code></pre>
Things I checked:
Connection to FTP Server is working
Login is successful
Directory on FTP Server is created successfully
Empty .zip File with correct name is created on FTP Server
Permission on remote Directory is 755 (writing possible)
Both local and remote File Paths are correct and local file exists
Anyone know how to solve the error?
The problem was caused by the FTP Server. It had a setting that forces FTP clients to reuse their SSL session from the control channel on the data channel, which the apache commons FTP client apparently does not do. Disabling the requirement on the server using 'TLSOptions NoSessionReuseRequired' solved the issue.
It is worth noting though that the requirement seems to have a security background as the data channel may be hijacked when it does not need to use the same SSL session. See this answer: https://stackoverflow.com/a/32404418/10173457

How to create a "FTPS" Mock Server to unit test File Transfer in Java

I have a CreateFTPConnection class which create a FTPS connection. Using this connection, files are transferred. Here is the code of TransferFile class
public class TransferFile
{
private CreateFTPConnection ftpConnection;
private FTPSClient client;
public TransferFile(CreateFTPConnection ftpConnection) {
this.ftpConnection = ftpConnection;
this.client = ftpConnection.getClient();
}
public void transfer(Message<?> msg)
{
InputStream inputStream = null;
try
{
if(!client.isConnected()){
ftpConnection.init();
client = ftpConnection.getClient();
}
File file = (File) msg.getPayload();
inputStream = new FileInputStream(file);
client.storeFile(file.getName(), inputStream);
client.sendNoOp();
} catch (Exception e) {
try
{
client.disconnect();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
finally
{
try {
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
I have to write jUnit Testcase for this class. For this, I have to create a FTPS Mock Server connection and have to use that connection to test the File Transfer. So can anyone plz give me any idea of how to make FTPS Mock Server and do the test case. I googled on this, but what I get is on FTP or SFTP, not FTPS. Please help me.
You might find this useful MockFTPServer
The issue is that these mock servers don't implement the TLS portion from what I can see. You may need to do a little work to allow connections via TLS.
You should be able to search around and find some articles here on SO about dealing with certificates, (or in some cases, bypassing them) for the sake of your testing.
Here's another Article that goes through the steps of creating a basic FTP server Test.
Short of a full blown FTP server (Apache http w/ mod_ftp add on), there doesn't seem to be anything useful to do this.

Simple way to keep files updated from remote server using java?

I was hoping to use Java to access a remote server, and access the files and keep my files updated, or update the files on the server.
Is there a simple way to access a remote server using the username#host and password, which would allow me to upload and download files?
Thanks
You can use JSch to access files via ssh remotely.
Use the proper tool for the job: rsync
If you like to connect to a machine while opening a ssh connection ,
to run OS command you can use trilead.
This is an example to a method that will open a connection.
public static Connection newConnectionNoPassword(String host, String username, File privateKey) {
Connection newConn = new Connection(host);
try {
newConn.connect(); // Ignoring ConnectionInfo returned value.
//If the authentication was successful the authenticated connection will be returend
if ( newConn.authenticateWithPublicKey(username, privateKey, null)){
return newConn;
}else{
newConn.close();
return null;
}
} catch (IOException ioe) {
newConn.close();
ioe.printStackTrace();
return null;
}
}
If you use maven you can get it by adding the following dependency to your pom.xml:
<dependency>
<groupId>com.trilead</groupId>
<artifactId>trilead-ssh2</artifactId>
<version>build213-svnkit-1.3-patch</version>
</dependency>
In order to upload\download files from\to a server you can use trilead SCPClient.
Here is an example of downloading files from a remote server to a local folder:
public void downloadFiles(String[] remoteFiles, String localDir) throws IllegalArgumentException, IOException {
checkNotEmpty(localDir);
checkNotEmpty(remoteFiles);
File dir = new File(localDir);
if (!dir.exists() || !dir.mkdirs()) {
throw new IOException("Failed to create local directory : " + localDir);
}
SCPClient scp = new SCPClient(this.conn);
try {
scp.get(remoteFiles, localDir);
} catch (IOException e) {
throw new IOException("Failed to copy remote files to local folder", e);
}
}
Hope it helps..

How to pass mainframe file name in enterprisedt.net.ftp.FileTransferClient

I am trying to download a file from ftp server using com.enterprisedt.net.ftp.FileTransferClient and the file name is "ABC.DEF.GHI.JKL(0)", This is a mainframe file and it is a valid file name(checked with mainframe admin).
public static void main(String[] args) {
// extract command-line arguments
String host = "111.111.111.111";
String username = "bbbbbbbb";
String password = "cccccccccp";
String filename = "ABC.DEF.GHI.JKL(0)";
// set up logger so that we get some output
Logger log = Logger.getLogger(ConnectToServer.class);
Logger.setLevel(Level.INFO);
FileTransferClient ftp = null;
try {
// create client
log.info("Creating FTP client");
ftp = new FileTransferClient();
// set remote host
log.info("Setting remote host");
ftp.setRemoteHost(host);
ftp.setUserName(username);
ftp.setPassword(password);
// connect to the server
log.info("Connecting to server " + host);
ftp.connect();
log.info("Connected and logged in to server " + host);
//Downloading file from server
log.info("Downloading file");
ftp.downloadFile(filename+".copy", filename);
log.info("File downloaded");
// Shut down client
log.info("Quitting client");
ftp.disconnect();
log.info("Example complete");
} catch (Exception e) {
e.printStackTrace();
}
}
The error i am receiving is :
ERROR [FTPClient] 2 Mar 2012 21:44:08.359 : Caught and rethrowing exception in initGet() : Invalid data set name "ABC.DEF.GHI.JKL(0)". Use MVS Dsname conventions.
com.enterprisedt.net.ftp.FTPException: 501 Invalid data set name "ABC.DEF.GHI.JKL(0)". Use MVS Dsname conventions.
at com.enterprisedt.net.ftp.FTPControlSocket.validateReply(FTPControlSocket.java:1223)
at com.enterprisedt.net.ftp.FTPClient.initGet(FTPClient.java:3109)
at com.enterprisedt.net.ftp.FTPClient.getData(FTPClient.java:3156)
at com.enterprisedt.net.ftp.FTPClient.getFile(FTPClient.java:2970)
at com.enterprisedt.net.ftp.FTPClient.get(FTPClient.java:2356)
at com.enterprisedt.net.ftp.FileTransferClient.downloadFile(FileTransferClient.java:703)
at com.enterprisedt.net.ftp.FileTransferClient.downloadFile(FileTransferClient.java:683)
at com.bluecrossma.ConnectToServer.main(ConnectToServer.java:47)
Please suggest me on how to resolve this problem.
Thanks in Advance
Found the answer in this link: torsas.ca/attachments/File/03012008/FTP_Fileref.pdf Just needed to add single quotes inside the double quotes..Thanks guys.

Categories