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