List files on SFTP server within Docker - java

I'm trying to access files directly from an SFTP server, using Docker.
The following works:
import static java.nio.file.Paths.get;
public File[] copyExtractFiles() {
String command = "sftp -i case-loader/./docker/config -o StrictHostKeyChecking=no -P 2222 sftp#localhost:incoming/*.xml src/test/resources/extract";
Process p = new ProcessBuilder("bash", "-c", command).start();
p.waitFor();
BufferedReader stdOutput = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.forName(CHARSET_NAME)));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream(), Charset.forName(CHARSET_NAME)));
return get("src/test/resources/extract").toFile().listFiles();
}
This transfers an XML file from the incoming directory on the Docker image to the src/test/resources/extract directory, and then lists the files.
However, I do not have access to the local file system and so want to access the files directly on the SFTP server. Is this possible? What do I need to change?

Use SFTP library, like JSch, instead of driving an external console application. Then you will be able to download the file list to memory.
Java - download from SFTP directly to memory without ever writing to file.

Related

How to know if a file is closed using java Process

I'm executing this command in order to open the log file with default file viewer:
Process p = Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler F:/Download/MSI3ca79.LOG");
I would like to know when the file is closed. Is it possible?
p.waitFor(); // doesn't work because the process is terminated just after the execution.
Solution:
ProcessBuilder pb =
new ProcessBuilder("c:/windows/notepad.exe", "F:/Download/MSI3ca79.LOG");
File log = new File(LogFactory.getLogFactory(TestExternProcess.class).getName()); pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
int exitVal = p.waitFor();
Plainly, it's not possible. I would not go this way.
It depends on a particular app (Notepad) opening file. The system doesn't know in general when an app stops viewing a file. Because what is closing a file? Is it closing a tab in a viewer UI? Or cleaning memory allocated for the file by an app? Removing lock file in case of closing doc, xls files?
In order to do it, you would need to write a program controlling the OS and any app that can view the file.

How to retrieve files from cloud foundry?

I currently have a spring boot web application. The application writes to a file every time the web app is refreshed. Locally I am able to see the files in the root path directory. But when I upload my .jar file to cloud foundry how would I be able to obtain those files that are being written?
Script snippet writing to file
try{
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
File file = new File(dateFormat.format(date) + "data.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("Some Data is being written");
out.close;
}
I am able to find data.txt in my root folder. But How can I get those files after I package my application to a jar, and push it to cloud foundry.
Cf push command
-cf push testapp -p target/webapp.jar
I hope this doc will be useful for you:
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html#howto-convert-an-existing-application-to-spring-boot
Try this:
cf ssh-code and then:
scp -P 2222 -o User=cf:<APP_GUID>/0 ssh.<system domain>:/<path>/<file name> <LOCAL_FILE_SYS>
Thanks,
Chandan

Heroku properties file

I am using a properties file to hardcode a list of valid usernames in a Java webapp.
The file is placed in the same package as the class (which is a struts action) and is read as follows:
Properties prop = new Properties();
InputStream input = null;
input = getClass().getResourceAsStream("login.properties");
prop.load(input);
String[] aprovedUsers = prop.getProperty("approvedUsers").split(",");
It works ok when I test it locally (in an Apache server running as localhost), but when I deploy the webapp to heroku, it seems that it can't read the propoerties file as input is null.
Is there a special consideration that needs to be taken into account when using properties files in heroku?
EDIT: My Procfile is
web: java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war
I am using Jetty as container.
Your local classpath and heroku classpath are different. Refer to ClassLoader.getSystemResource in order to find file.
FileInputStream stream = new FileInputStream(ClassLoader.getSystemResource("login.properties").getPath());

Run linux command in remote machine from java

I´m working with application in java.
I can execute linux command (bash) on my machine host, but i want to execute this command in a remote machine like ssh.
I´m ussing this code
Process process = Runtime.getRuntime().exec(script);
process = Runtime.getRuntime().exec(script);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
How i can execute linux shell in remote machine with java code?
Luis: as Eric suggested one possible solution is to run a local script that performs an SSH itself on the remote server.
For instance if you have a Linux->Linux environment, your script you could have something like:
ssh remoteuser#remotehost 'bash -s' < localscripttoexecuteremotely.sh
In a Windows->Linux scenario you could do:
plink remoteuser#remotehost -m localscripttoexecuteremotely.sh
Take a look at this thread for additional information.

wget file download ftp waitfor() hangs

I am trying to download a XML file from a FTP server with wget in my Java programm.
I have to wait until it finishes the download.
String command = "WGET -O "
+props.getProperty("xmlFolder")+""+
+ rs.getString("software")
+ ".xml ftp://"+props.getProperty("ftpUser")
+":"+props.getProperty("ftpPasswort")+"#"+rs.getString("xmlPfad");
System.out.println(command);
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
System.out.println("downloaded!");
Without the waitfor() it works perfectly, but with this function it stucks after 2-3 MB are downloaded. Any suggestions?
Have you tried to use the --quiet option for wget?
EDIT 1:
The pipe's write side (child process) might be full.
EDIT 2:
From openjdk-6-src-b20-21_jun_2010
In jdk/src/solaris/native/java/lang/UNIXProcess_md.c (at least for a UNIX system) we can see how Java launches a new child process and how it is using pipe to redirect stdout and stderr from child (wget) to parent process (Java)

Categories

Resources