Hi Mattias

    I've just read your javaway blog
(http://blog.jayway.com/2010/08/12/dynamic-ftp-client-using-apache-camel-and-spring/)
and is a great and well written blog entry to explore dynamically ftping
files.

    I was also recently asked to develop an FTP client that could transmit
files to various FTP servers as a part of a delivery system. I discovered
"Apache Camel" is the best suited framework for integration. I started
learning Camel from few days ago.

I just followed you as explained above. The only different is that I don't
use Spring. I created the camel context in a java class (in a main method)
and started the context (context.start()). Then I set the FtpProperties and
called ftpSender.sendFile(ftpProperties, file, producer).

Plz note the producer is context.createProducerTemplate(). 

The main java class is as follows
-------------------------------------------------------------------------
public static void main(String[] args) {
  CamelContext context = new DefaultCamelContext();
  try {
    context.start();
    File file = new File("message1.xml");
    FtpProperties ftpProperties = new FtpPropertiesImpl("ftp", "dfs",
"******", "dev.e-xxxxx.com", "images", true);
    FtpSender ftpSender = new FtpSenderImpl();
    ProducerTemplate producer = context.createProducerTemplate();       
    // Thread.sleep(10000);
    // context.stop();  
    ftpSender.sendFile(ftpProperties, file, producer);
  } catch (Exception e) {
    System.out.println("Error while Starting Camel " + e);
  }             
}
-------------------------------------------------------------------------

When I ran the above program I got the following error.

Dec 19, 2010 5:07:20 PM
org.apache.camel.component.file.remote.RemoteFileProducer connectIfNecessary
INFO: Connected and logged in to:
Endpoint[ftp://d...@dev.e-xxxxx.com/images?passiveMode=true&password=******]
Dec 19, 2010 5:07:21 PM
org.apache.camel.component.file.remote.RemoteFileProducer handleFailedWrite
WARNING: Writing file failed with: Cannot store file: images/message1.xml
Error while Starting Camel org.apache.camel.CamelExecutionException:
Exception occurred during execution on the exchange: Exchange[Message:
message1.xml]


But I can able to ftp files when I use static router (Created at the coding
time) as follows
----------------------------------------------------------------------
public static void main(String[] args) {public static void main(String[]
args) {
  CamelContext context = new DefaultCamelContext();
  context.addRoutes(new RouteBuilder() {
    public void configure() {
         
from("file:D:/Niranjan/Books/Camel/camelinaction-source/chapter1/file-copy/data/inbox?noop=true")
         
.to("ftp://d...@dev.e-xxxxx.com/images?password=*******&passiveMode=true";);     
          
    }
  });
  context.start();
  Thread.sleep(10000);
  context.stop();
}
--------------------------------------------------------------------
Am I missing that Spring suppose to do by default??
Any help would be appreciated.

My Environment
The development machine runs Windows-7 and the remote FTP server runs on
some Linux distribution (Linux 215402.215402-web1.decmar.co.uk
2.6.18-194.26.1.el5 #1 SMP Fri Oct 29 14:21:16 EDT 2010 x86_64 x86_64 x86_64
GNU/Linux).

Camel version 2.5.0


MY Full Code
-----------------------------------------------------------------------------------------


public class FtpPropertiesImpl implements FtpProperties {

    private String protocol = null;
    private String userName = null;
    private String password = null;
    private String host = null;
    private String remoteDirectory = null;
    private boolean passiveMode = false;

    public FtpPropertiesImpl(String protocol, String userName, String
password, String host, String remoteDirectory, boolean passiveMode) {
        this.protocol = protocol;
        this.userName = userName;
        this.password = password;
        this.host = host;
        this.remoteDirectory = remoteDirectory;
        this.passiveMode = passiveMode;
    }

    /**
     * Gets the protocol
     * @return One of {...@code ftp}, {...@code ftps} or {...@code sftp}
     */
    public String getProtocol() {
        return protocol;
    }

    /**
     * Gets the user name
     * @return The name of the user
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Gets the password
     * @return The password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Gets the FTP host
     * @return The FTP host
     */
    public String getHost() {
        return host;
    }

    /**
     * Gets the name of the directory on the server where the file will be
transferred
     * @return The name of the remote directory
     */
    public String getRemoteDirectory() {
        return remoteDirectory;
    }

    /**
     * Gets the passive mode, e.g. if the server is behind a firewall
     * @return whether or not passive mode should be used
     */
    public boolean getPassiveMode() {
        return passiveMode;
    }
}


public class FtpSenderImpl implements FtpSender {

    /** Camel URI, format is
ftp://u...@host/fileName?password=secret&passiveMode=true */
    private static final String CAMEL_FTP_PATTERN =
"{0}://{...@{2}/{3}?password={4}&passiveMode={5}";

    @Override
    public void sendFile(FtpProperties ftpProperties, File file,
ProducerTemplate producerTemplate) throws RuntimeException {
         producerTemplate.sendBodyAndHeader(createFtpUri(ftpProperties) +
"&binary=true", file, Exchange.FILE_NAME, file.getName());
    }

    /**
      * Creates a Camel FTP URI based on the provided FTP properties
      * @param ftpProperties The properties to be used
      */
    private String createFtpUri(FtpProperties ftpProperties) {
        return MessageFormat.format(CAMEL_FTP_PATTERN,
                ftpProperties.getProtocol(),
                ftpProperties.getUserName(),
                ftpProperties.getHost(),
                ftpProperties.getRemoteDirectory(),
                ftpProperties.getPassword(),
                ftpProperties.getPassiveMode());
    }
}


Regards,
Niranjan
-- 
View this message in context: 
http://camel.465427.n5.nabble.com/Dynamic-origin-file-components-tp473618p3311118.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to