The Stake And Chain
2014-07-10 16:09:01 UTC
Hello,
I'm using jsch-0.1.51, and I'm using ChannelSftp to get and put
PGP-encrypted data to a remote server that I do not control.
The put works perfectly, but with the get I'm having issues.
I initialize a session, and connect like so (some commenting has been
redacted):
private void initSftpConnection() throws JSchException {
initSftpParameters();
JSch jSch = new JSch();
try {
jSch.addIdentity(sftpPrivateKey);
session =
jSch.getSession(userName, remoteHost, Integer.parseInt(port));
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
logger.debug("Session
established.");
sftp = (ChannelSftp)
session.openChannel("sftp");
sftp.connect();
} catch (JSchException e) {
logger.fatal("Error
establishing an SFTP connection", e);
throw e;
}
}
Since I'm dealing with encrypted data, I want to return an InputStream
which I pass to a decryption method, and I do so as described in the API
(once again, I've redeacted some comments and logging):
public InputStream getResponse() {
InputStream encryptedResponse = null;
try {
initSftpConnection();
sftp.cd
(remoteDownloadDirectory);
@SuppressWarnings("unchecked")
Vector<ChannelSftp.LsEntry>
remoteDirectoryEntries = sftp.ls(sftp.pwd());
Iterator<ChannelSftp.LsEntry> itr = remoteDirectoryEntries.iterator();
String downloadFileName =
"";
boolean isDownloadFileFound
= false;
ChannelSftp.LsEntry
remoteEntry = null;
while (itr.hasNext()) {
remoteEntry
= itr.next();
downloadFileName = remoteEntry.getFilename();
if
(downloadFileName.startsWith(classId)) {
isDownloadFileFound = true;
break;
}
}
if (isDownloadFileFound) {
logger.debug("Downloading file " + downloadFileName);
encryptedResponse = sftp.get(downloadFileName);
logger.debug("SFTP get was successful.");
}
} catch (Exception e) {
logger.fatal("Error in
getResponse ", e);
} finally {
sftp.disconnect();
session.disconnect();
}
return encryptedResponse;
}
The problem is, I get the following exception:
java.io.IOException: Pipe closed
at java.io.PipedInputStream.read(PipedInputStream.java:302)
at java.io.PipedInputStream.read(PipedInputStream.java:372)
at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2527)
at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2553)
at
com.jcraft.jsch.ChannelSftp.access$500(ChannelSftp.java:36)
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1253)
at
java.io.BufferedInputStream.fill(BufferedInputStream.java:229)
at
java.io.BufferedInputStream.read(BufferedInputStream.java:248)
at
org.bouncycastle.openpgp.PGPUtil.getDecoderStream(Unknown Source)
The connection is established fine. I find the file, and it begins to
download.
Looking over the PipedInputStream source, it appears the Pipe Closed
exception is thrown in the read method of PipedInputStream if the close
method is invoked.
Is the PipedInputStream still trying to read after the session is closed?
I never explicitly call the close method on the stream, but it appears the
stream is indirectly closed when the disconnect method is called. Also, the
stream is making it to my decryption method. What am I missing?
Peter
I'm using jsch-0.1.51, and I'm using ChannelSftp to get and put
PGP-encrypted data to a remote server that I do not control.
The put works perfectly, but with the get I'm having issues.
I initialize a session, and connect like so (some commenting has been
redacted):
private void initSftpConnection() throws JSchException {
initSftpParameters();
JSch jSch = new JSch();
try {
jSch.addIdentity(sftpPrivateKey);
session =
jSch.getSession(userName, remoteHost, Integer.parseInt(port));
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
logger.debug("Session
established.");
sftp = (ChannelSftp)
session.openChannel("sftp");
sftp.connect();
} catch (JSchException e) {
logger.fatal("Error
establishing an SFTP connection", e);
throw e;
}
}
Since I'm dealing with encrypted data, I want to return an InputStream
which I pass to a decryption method, and I do so as described in the API
(once again, I've redeacted some comments and logging):
public InputStream getResponse() {
InputStream encryptedResponse = null;
try {
initSftpConnection();
sftp.cd
(remoteDownloadDirectory);
@SuppressWarnings("unchecked")
Vector<ChannelSftp.LsEntry>
remoteDirectoryEntries = sftp.ls(sftp.pwd());
Iterator<ChannelSftp.LsEntry> itr = remoteDirectoryEntries.iterator();
String downloadFileName =
"";
boolean isDownloadFileFound
= false;
ChannelSftp.LsEntry
remoteEntry = null;
while (itr.hasNext()) {
remoteEntry
= itr.next();
downloadFileName = remoteEntry.getFilename();
if
(downloadFileName.startsWith(classId)) {
isDownloadFileFound = true;
break;
}
}
if (isDownloadFileFound) {
logger.debug("Downloading file " + downloadFileName);
encryptedResponse = sftp.get(downloadFileName);
logger.debug("SFTP get was successful.");
}
} catch (Exception e) {
logger.fatal("Error in
getResponse ", e);
} finally {
sftp.disconnect();
session.disconnect();
}
return encryptedResponse;
}
The problem is, I get the following exception:
java.io.IOException: Pipe closed
at java.io.PipedInputStream.read(PipedInputStream.java:302)
at java.io.PipedInputStream.read(PipedInputStream.java:372)
at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2527)
at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2553)
at
com.jcraft.jsch.ChannelSftp.access$500(ChannelSftp.java:36)
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1253)
at
java.io.BufferedInputStream.fill(BufferedInputStream.java:229)
at
java.io.BufferedInputStream.read(BufferedInputStream.java:248)
at
org.bouncycastle.openpgp.PGPUtil.getDecoderStream(Unknown Source)
The connection is established fine. I find the file, and it begins to
download.
Looking over the PipedInputStream source, it appears the Pipe Closed
exception is thrown in the read method of PipedInputStream if the close
method is invoked.
Is the PipedInputStream still trying to read after the session is closed?
I never explicitly call the close method on the stream, but it appears the
stream is indirectly closed when the disconnect method is called. Also, the
stream is making it to my decryption method. What am I missing?
Peter