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