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 com.jcraft.jsch.ChannelSftp;
020    import com.jcraft.jsch.JSch;
021    import com.jcraft.jsch.JSchException;
022    import com.jcraft.jsch.Session;
023    import com.jcraft.jsch.UserInfo;
024    
025    import org.apache.camel.Processor;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    import static org.apache.camel.util.ObjectHelper.isNotNullAndNonEmpty;
029    
030    public class SftpEndpoint extends RemoteFileEndpoint<RemoteFileExchange> {
031        protected final transient Log log = LogFactory.getLog(getClass());
032        
033        public SftpEndpoint(String uri, RemoteFileComponent remoteFileComponent, RemoteFileConfiguration configuration) {
034            super(uri, remoteFileComponent, configuration);
035        }
036    
037        public SftpEndpoint(String endpointUri) {
038            super(endpointUri);
039        }
040    
041        public SftpProducer createProducer() throws Exception {
042            return new SftpProducer(this, createSession());
043        }
044    
045        public SftpConsumer createConsumer(Processor processor) throws Exception {
046            final SftpConsumer consumer = new SftpConsumer(this, processor, createSession());
047            configureConsumer(consumer);
048            return consumer;
049        }
050    
051        protected Session createSession() throws JSchException {
052            final JSch jsch = new JSch();
053    
054            String privateKeyFile = getConfiguration().getPrivateKeyFile();
055            if (isNotNullAndNonEmpty(privateKeyFile)) {
056                log.debug("Using private keyfile: " + privateKeyFile);
057                String privateKeyFilePassphrase = getConfiguration().getPrivateKeyFilePassphrase(); 
058                if (isNotNullAndNonEmpty(privateKeyFilePassphrase)) {
059                    jsch.addIdentity(privateKeyFile, privateKeyFilePassphrase);
060                } else {
061                    jsch.addIdentity(privateKeyFile);
062                }
063            }
064    
065            String knownHostsFile = getConfiguration().getKnownHosts();
066            if (isNotNullAndNonEmpty(knownHostsFile)) {
067                log.debug("Using knownHosts: " + knownHostsFile);
068                jsch.setKnownHosts(knownHostsFile);
069            }
070          
071            final Session session = jsch.getSession(getConfiguration().getUsername(), getConfiguration().getHost(), getConfiguration().getPort());
072            session.setUserInfo(new UserInfo() {
073                public String getPassphrase() {
074                    return null;
075                }
076    
077                public String getPassword() {
078                    return getConfiguration().getPassword();
079                }
080    
081                public boolean promptPassword(String string) {
082                    return true;
083                }
084    
085                public boolean promptPassphrase(String string) {
086                    return true;
087                }           
088    
089                public boolean promptYesNo(String string) {
090                    log.error(string);
091                    // Return 'false' indicating modification of the hosts file is disabled.
092                    return false;
093                }
094    
095                public void showMessage(String string) {
096                }
097            });
098            return session;
099        }
100    
101        public ChannelSftp createChannelSftp(Session session) throws JSchException {
102            final ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
103            return channel;
104        }
105    }