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    
021    import com.jcraft.jsch.ChannelSftp;
022    import com.jcraft.jsch.SftpException;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    
027    /**
028     * Utility methods for SFTP.
029     */
030    public final class SftpUtils {
031        private static final transient Log LOG = LogFactory.getLog(SftpUtils.class);
032    
033        private SftpUtils() {
034        }
035    
036        public static boolean buildDirectory(ChannelSftp sftpClient, String dirName)
037            throws IOException, SftpException {
038            String originalDirectory = sftpClient.pwd();
039    
040            boolean success = false;
041            try {
042                // maybe the full directory already exsits
043                try {
044                    sftpClient.cd(dirName);
045                    success = true;
046                } catch (SftpException e) {
047                    // ignore, we could not change directory so try to create it instead
048                }
049    
050                if (!success) {
051                    if (LOG.isDebugEnabled()) {
052                        LOG.debug("Trying to build remote directory: " + dirName);
053                    }
054    
055                    try {
056                        sftpClient.mkdir(dirName);
057                        success = true;
058                    } catch (SftpException e) {
059                        // we are here if the server side doesn't create intermediate folders
060                        // so create the folder one by one
061                        success = buildDirectoryChunks(sftpClient, dirName);
062                    }
063                }
064            } finally {
065                // change back to original directory
066                sftpClient.cd(originalDirectory);
067            }
068    
069            return success;
070        }
071    
072        public static boolean buildDirectoryChunks(ChannelSftp sftpClient, String dirName)
073            throws IOException, SftpException {
074            final StringBuilder sb = new StringBuilder(dirName.length());
075            final String[] dirs = dirName.split("\\/");
076    
077            boolean success = false;
078            for (String dir : dirs) {
079                sb.append(dir).append('/');
080                String directory = sb.toString();
081                if (LOG.isTraceEnabled()) {
082                    LOG.trace("Trying to build remote directory: " + directory);
083                }
084    
085                try {
086                    sftpClient.mkdir(directory);
087                    success = true;
088                } catch (SftpException e) {
089                    // ignore keep trying to create the rest of the path
090                }
091            }
092    
093            return success;
094        }
095    }