Author: davsclaus
Date: Wed Oct 31 13:00:39 2012
New Revision: 1404119
URL: http://svn.apache.org/viewvc?rev=1404119&view=rev
Log:
CAMEL-5750: camel-jsch should close stream after usage when writing. Also
support the allowEmptyNusage. Also support the allowEmptyBody option.
Modified:
camel/branches/camel-2.10.x/ (props changed)
camel/branches/camel-2.10.x/components/camel-jsch/ (props changed)
camel/branches/camel-2.10.x/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
Merged /camel/trunk:r1403725
Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Propchange: camel/branches/camel-2.10.x/components/camel-jsch/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Oct 31 13:00:39 2012
@@ -14,3 +14,4 @@ eclipse-classes
*.ipr
*.iml
*.iws
+*.idea
Modified:
camel/branches/camel-2.10.x/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
URL:
http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java?rev=1404119&r1=1404118&r2=1404119&view=diff
==============================================================================
---
camel/branches/camel-2.10.x/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
(original)
+++
camel/branches/camel-2.10.x/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
Wed Oct 31 13:00:39 2012
@@ -17,6 +17,7 @@
package org.apache.camel.component.jsch;
import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
@@ -32,8 +33,8 @@ import com.jcraft.jsch.Session;
import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;
-import org.apache.camel.CamelExchangeException;
import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
import org.apache.camel.component.file.GenericFileEndpoint;
import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.apache.camel.component.file.remote.RemoteFileConfiguration;
@@ -94,9 +95,24 @@ public class ScpOperations implements Re
ScpConfiguration cfg = endpoint.getConfiguration();
int timeout = cfg.getConnectTimeout();
- LOG.trace("Opening channel to {} with {} timeout...",
cfg.remoteServerInformation(),
- timeout > 0 ? (Integer.toString(timeout) + " ms") : "no");
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Opening channel to {} with {} timeout...",
cfg.remoteServerInformation(),
+ timeout > 0 ? (Integer.toString(timeout) + " ms") : "no");
+ }
String file = getRemoteFile(name, cfg);
+
+
+ InputStream is = null;
+ if (exchange.getIn().getBody() == null) {
+ // Do an explicit test for a null body and decide what to do
+ if (endpoint.isAllowNullBody()) {
+ LOG.trace("Writing empty file.");
+ is = new ByteArrayInputStream(new byte[]{});
+ } else {
+ throw new GenericFileOperationFailedException("Cannot write
null body to file: " + name);
+ }
+ }
+
try {
channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(getScpCommand(cfg, file));
@@ -104,15 +120,20 @@ public class ScpOperations implements Re
LOG.trace("Channel connected to {}",
cfg.remoteServerInformation());
try {
+ if (is == null) {
+ is = exchange.getIn().getMandatoryBody(InputStream.class);
+ }
write(channel, file,
exchange.getIn().getMandatoryBody(InputStream.class), cfg);
- } catch (CamelExchangeException e) {
- throw new GenericFileOperationFailedException("Failed extract
message body as InputStream", e);
+ } catch (InvalidPayloadException e) {
+ throw new GenericFileOperationFailedException("Cannot store
file: " + name, e);
} catch (IOException e) {
throw new GenericFileOperationFailedException("Failed to write
file " + file, e);
+ } finally {
+ // must close stream after usage
+ IOHelper.close(is);
}
} catch (JSchException e) {
- LOG.warn("Failed to secure copy file " + file, e);
- return false;
+ throw new GenericFileOperationFailedException("Failed to write
file " + file, e);
} finally {
if (channel != null) {
LOG.trace("Disconnecting 'exec' scp channel");