camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1403725 - in /camel/trunk/components/camel-jsch: ./ src/main/java/org/apache/camel/component/jsch/ScpOperations.java
Date Tue, 30 Oct 2012 14:51:44 GMT
Author: davsclaus
Date: Tue Oct 30 14:51:44 2012
New Revision: 1403725

URL: http://svn.apache.org/viewvc?rev=1403725&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/trunk/components/camel-jsch/   (props changed)
    camel/trunk/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java

Propchange: camel/trunk/components/camel-jsch/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Tue Oct 30 14:51:44 2012
@@ -14,3 +14,4 @@ eclipse-classes
 *.ipr
 *.iml
 *.iws
+*.idea

Modified: camel/trunk/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java?rev=1403725&r1=1403724&r2=1403725&view=diff
==============================================================================
--- camel/trunk/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
(original)
+++ camel/trunk/components/camel-jsch/src/main/java/org/apache/camel/component/jsch/ScpOperations.java
Tue Oct 30 14:51:44 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");



Mime
View raw message