001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.component.file.remote; 018 019 import java.io.IOException; 020 import java.io.InputStream; 021 022 import org.apache.camel.Exchange; 023 import org.apache.camel.RuntimeCamelException; 024 import org.apache.commons.net.ftp.FTPClient; 025 026 public class FtpProducer extends RemoteFileProducer<RemoteFileExchange> { 027 FtpEndpoint endpoint; 028 private final FTPClient client; 029 030 public FtpProducer(FtpEndpoint endpoint, FTPClient client) { 031 super(endpoint); 032 this.endpoint = endpoint; 033 this.client = client; 034 } 035 036 public void process(Exchange exchange) throws Exception { 037 process(endpoint.toExchangeType(exchange)); 038 } 039 040 public void process(RemoteFileExchange exchange) throws Exception { 041 final String fileName; 042 InputStream payload = exchange.getIn().getBody(InputStream.class); 043 final String endpointFile = endpoint.getConfiguration().getFile(); 044 client.changeWorkingDirectory(endpointFile); // TODO this line might 045 // not be needed... 046 // check after finish 047 // writing unit tests 048 if (endpointFile == null) { 049 throw new NullPointerException("Null Endpoint File"); 050 } else { 051 if (endpoint.getConfiguration().isDirectory()) { 052 fileName = endpointFile + "/" + exchange.getIn().getMessageId(); 053 } else { 054 fileName = endpointFile; 055 } 056 } 057 buildDirectory(client, fileName.substring(0, fileName.lastIndexOf('/'))); 058 final boolean success = client.storeFile(fileName, payload); 059 if (!success) { 060 throw new RuntimeCamelException("error sending file"); 061 } 062 } 063 064 @Override 065 protected void doStart() throws Exception { 066 super.doStart(); 067 // client.connect(endpoint.getConfiguration().getHost()); 068 // client.login(endpoint.getConfiguration().getUsername(), 069 // endpoint.getConfiguration().getPassword()); 070 // client.setFileType(endpoint.getConfiguration().isBinary() ? 071 // FTPClient.BINARY_FILE_TYPE : FTPClient.ASCII_FILE_TYPE); 072 } 073 074 @Override 075 protected void doStop() throws Exception { 076 client.disconnect(); 077 super.doStop(); 078 } 079 080 protected static boolean buildDirectory(FTPClient ftpClient, String dirName) throws IOException { 081 boolean atLeastOneSuccess = false; 082 final StringBuilder sb = new StringBuilder(dirName.length()); 083 final String[] dirs = dirName.split("\\/"); 084 for (String dir : dirs) { 085 sb.append('/').append(dir); 086 final boolean success = ftpClient.makeDirectory(sb.toString()); 087 System.out.println(sb.toString() + " = " + success); 088 if (!atLeastOneSuccess && success) { 089 atLeastOneSuccess = true; 090 } 091 } 092 return atLeastOneSuccess; 093 } 094 }