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    import org.apache.camel.Processor;
025    
026    public class SftpEndpoint extends RemoteFileEndpoint<RemoteFileExchange> {
027        public SftpEndpoint(String uri, RemoteFileComponent remoteFileComponent, RemoteFileConfiguration configuration) {
028            super(uri, remoteFileComponent, configuration);
029        }
030    
031        public SftpProducer createProducer() throws Exception {
032            return new SftpProducer(this, createChannelSftp());
033        }
034    
035        public SftpConsumer createConsumer(Processor processor) throws Exception {
036            final SftpConsumer consumer = new SftpConsumer(this, processor, createChannelSftp());
037            configureConsumer(consumer);
038            return consumer;
039        }
040    
041        protected ChannelSftp createChannelSftp() throws JSchException {
042            final JSch jsch = new JSch();
043            final Session session = jsch.getSession(getConfiguration().getUsername(), getConfiguration().getHost());
044            // TODO there's got to be a better way to deal with accepting new hosts...
045            session.setUserInfo(new UserInfo() {
046                public String getPassphrase() {
047                    return null;
048                }
049    
050                public String getPassword() {
051                    return SftpEndpoint.this.getConfiguration().getPassword();
052                }
053    
054                public boolean promptPassword(String string) {
055                    return true;
056                }
057    
058                public boolean promptPassphrase(String string) {
059                    return true;
060                }
061    
062                public boolean promptYesNo(String string) {
063                    return true;
064                }
065    
066                public void showMessage(String string) {
067                }
068            });
069            session.connect();
070            final ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
071            channel.connect();
072            return channel;
073        }
074    }