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 log.warn("Cannot poll as directory does not exists or its not a directory: " + directory);
043 return;
044 }
045
046 if (log.isTraceEnabled()) {
047 log.trace("Polling directory: " + directory.getPath());
048 }
049 File[] files = directory.listFiles();
050
051 if (files == null || files.length == 0) {
052 // no files in this directory to poll
053 return;
054 }
055 for (File file : files) {
056 // createa a generic file
057 GenericFile<File> gf = asGenericFile(endpointPath, file);
058
059 if (file.isDirectory()) {
060 if (endpoint.isRecursive() && isValidFile(gf, true)) {
061 // recursive scan and add the sub files and folders
062 String subDirectory = fileName + File.separator + file.getName();
063 pollDirectory(subDirectory, fileList);
064 }
065 } else if (file.isFile()) {
066 if (isValidFile(gf, false)) {
067 // matched file so add
068 fileList.add(gf);
069 }
070 } else {
071 log.debug("Ignoring unsupported file type for file: " + file);
072 }
073 }
074 }
075
076 /**
077 * Creates a new GenericFile<File> based on the given file.
078 *
079 * @param endpointPath the starting directory the endpoint was configued with
080 * @param file the source file
081 * @return wrapped as a GenericFile
082 */
083 public static GenericFile<File> asGenericFile(String endpointPath, File file) {
084 GenericFile<File> answer = new GenericFile<File>();
085 // use file specific binding
086 answer.setBinding(new FileBinding());
087
088 answer.setEndpointPath(endpointPath);
089 answer.setFile(file);
090 answer.setFileName(file.getName());
091 answer.setFileNameOnly(file.getName());
092 answer.setFileLength(file.length());
093 answer.setAbsolute(file.isAbsolute());
094 answer.setAbsoluteFilePath(file.getAbsolutePath());
095 answer.setLastModified(file.lastModified());
096 if (file.isAbsolute()) {
097 // use absolute path as relative
098 answer.setRelativeFilePath(file.getAbsolutePath());
099 } else {
100 File path;
101 String endpointNormalized = FileUtil.normalizePath(endpointPath);
102 if (file.getPath().startsWith(endpointNormalized)) {
103 // skip duplicate endpoint path
104 path = new File(ObjectHelper.after(file.getPath(), endpointNormalized + File.separator));
105 } else {
106 path = new File(file.getPath());
107 }
108
109 if (path.getParent() != null) {
110 answer.setRelativeFilePath(path.getParent() + File.separator + file.getName());
111 } else {
112 answer.setRelativeFilePath(path.getName());
113 }
114 }
115
116 // use file as body as we have converters if needed as stream
117 answer.setBody(file);
118 return answer;
119 }
120 }