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 if (isInProgress(gf)) { 068 if (log.isTraceEnabled()) { 069 log.trace("Skipping as file is already in progress: " + gf.getFileName()); 070 } 071 } else { 072 // matched file so add 073 fileList.add(gf); 074 } 075 } 076 } else { 077 log.debug("Ignoring unsupported file type for file: " + file); 078 } 079 } 080 } 081 082 /** 083 * Creates a new GenericFile<File> based on the given file. 084 * 085 * @param endpointPath the starting directory the endpoint was configued with 086 * @param file the source file 087 * @return wrapped as a GenericFile 088 */ 089 public static GenericFile<File> asGenericFile(String endpointPath, File file) { 090 GenericFile<File> answer = new GenericFile<File>(); 091 // use file specific binding 092 answer.setBinding(new FileBinding()); 093 094 answer.setEndpointPath(endpointPath); 095 answer.setFile(file); 096 answer.setFileName(file.getName()); 097 answer.setFileNameOnly(file.getName()); 098 answer.setFileLength(file.length()); 099 answer.setAbsolute(file.isAbsolute()); 100 answer.setAbsoluteFilePath(file.getAbsolutePath()); 101 answer.setLastModified(file.lastModified()); 102 if (file.isAbsolute()) { 103 // use absolute path as relative 104 answer.setRelativeFilePath(file.getAbsolutePath()); 105 } else { 106 File path; 107 String endpointNormalized = FileUtil.normalizePath(endpointPath); 108 if (file.getPath().startsWith(endpointNormalized)) { 109 // skip duplicate endpoint path 110 path = new File(ObjectHelper.after(file.getPath(), endpointNormalized + File.separator)); 111 } else { 112 path = new File(file.getPath()); 113 } 114 115 if (path.getParent() != null) { 116 answer.setRelativeFilePath(path.getParent() + File.separator + file.getName()); 117 } else { 118 answer.setRelativeFilePath(path.getName()); 119 } 120 } 121 122 // use file as body as we have converters if needed as stream 123 answer.setBody(file); 124 return answer; 125 } 126 }