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;
018    
019    import java.io.File;
020    import java.util.List;
021    
022    import org.apache.camel.Processor;
023    import org.apache.camel.util.FileUtil;
024    import org.apache.camel.util.ObjectHelper;
025    
026    /**
027     * File consumer.
028     */
029    public class FileConsumer extends GenericFileConsumer<File> {
030    
031        private String endpointPath;
032    
033        public FileConsumer(GenericFileEndpoint<File> endpoint, Processor processor, GenericFileOperations<File> operations) {
034            super(endpoint, processor, operations);
035            this.endpointPath = endpoint.getConfiguration().getDirectory();
036        }
037    
038        protected void pollDirectory(String fileName, List<GenericFile<File>> fileList) {
039            File directory = new File(fileName);
040    
041            if (!directory.exists() || !directory.isDirectory()) {
042                if (log.isDebugEnabled()) {
043                    log.debug("Cannot poll as directory does not exists or its not a directory: " + directory);
044                }
045                return;
046            }
047    
048            if (log.isTraceEnabled()) {
049                log.trace("Polling directory: " + directory.getPath());
050            }
051            File[] files = directory.listFiles();
052    
053            if (files == null || files.length == 0) {
054                // no files in this directory to poll
055                return;
056            }
057            for (File file : files) {
058                // createa a generic file
059                GenericFile<File> gf = asGenericFile(endpointPath, file);
060    
061                if (file.isDirectory()) {
062                    if (endpoint.isRecursive() && isValidFile(gf, true)) {
063                        // recursive scan and add the sub files and folders
064                        String subDirectory = fileName + File.separator + file.getName();
065                        pollDirectory(subDirectory, fileList);
066                    }
067                } else if (file.isFile()) {
068                    if (isValidFile(gf, false)) {
069                        if (isInProgress(gf)) {
070                            if (log.isTraceEnabled()) {
071                                log.trace("Skipping as file is already in progress: " + gf.getFileName());
072                            }
073                        } else {
074                            // matched file so add
075                            fileList.add(gf);
076                        }
077                    }
078                } else {
079                    log.debug("Ignoring unsupported file type for file: " + file);
080                }
081            }
082        }
083    
084        /**
085         * Creates a new GenericFile<File> based on the given file.
086         *
087         * @param endpointPath the starting directory the endpoint was configued with
088         * @param file the source file
089         * @return wrapped as a GenericFile
090         */
091        public static GenericFile<File> asGenericFile(String endpointPath, File file) {
092            GenericFile<File> answer = new GenericFile<File>();
093            // use file specific binding
094            answer.setBinding(new FileBinding());
095    
096            answer.setEndpointPath(endpointPath);
097            answer.setFile(file);
098            answer.setFileName(file.getName());
099            answer.setFileNameOnly(file.getName());
100            answer.setFileLength(file.length());
101            answer.setAbsolute(file.isAbsolute());
102            answer.setAbsoluteFilePath(file.getAbsolutePath());
103            answer.setLastModified(file.lastModified());
104            if (file.isAbsolute()) {
105                // use absolute path as relative
106                answer.setRelativeFilePath(file.getAbsolutePath());
107            } else {
108                File path;
109                String endpointNormalized = FileUtil.normalizePath(endpointPath);
110                if (file.getPath().startsWith(endpointNormalized)) {
111                    // skip duplicate endpoint path
112                    path = new File(ObjectHelper.after(file.getPath(), endpointNormalized + File.separator));
113                } else {
114                    path = new File(file.getPath());
115                }
116    
117                if (path.getParent() != null) {
118                    answer.setRelativeFilePath(path.getParent() + File.separator + file.getName());
119                } else {
120                    answer.setRelativeFilePath(path.getName());
121                }
122            }
123    
124            // use file as body as we have converters if needed as stream
125            answer.setBody(file);
126            return answer;
127        }
128    }