use of java.io.File in project camel by apache.
the class FileConsumer method pollDirectory.
@Override
protected boolean pollDirectory(String fileName, List<GenericFile<File>> fileList, int depth) {
log.trace("pollDirectory from fileName: {}", fileName);
depth++;
File directory = new File(fileName);
if (!directory.exists() || !directory.isDirectory()) {
log.debug("Cannot poll as directory does not exists or its not a directory: {}", directory);
if (getEndpoint().isDirectoryMustExist()) {
throw new GenericFileOperationFailedException("Directory does not exist: " + directory);
}
return true;
}
log.trace("Polling directory: {}", directory.getPath());
File[] dirFiles = directory.listFiles();
if (dirFiles == null || dirFiles.length == 0) {
// no files in this directory to poll
if (log.isTraceEnabled()) {
log.trace("No files found in directory: {}", directory.getPath());
}
return true;
} else {
// we found some files
if (log.isTraceEnabled()) {
log.trace("Found {} in directory: {}", dirFiles.length, directory.getPath());
}
}
List<File> files = Arrays.asList(dirFiles);
for (File file : dirFiles) {
// check if we can continue polling in files
if (!canPollMoreFiles(fileList)) {
return false;
}
// trace log as Windows/Unix can have different views what the file is?
if (log.isTraceEnabled()) {
log.trace("Found file: {} [isAbsolute: {}, isDirectory: {}, isFile: {}, isHidden: {}]", new Object[] { file, file.isAbsolute(), file.isDirectory(), file.isFile(), file.isHidden() });
}
// creates a generic file
GenericFile<File> gf = asGenericFile(endpointPath, file, getEndpoint().getCharset(), getEndpoint().isProbeContentType());
if (file.isDirectory()) {
if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() && isValidFile(gf, true, files)) {
// recursive scan and add the sub files and folders
String subDirectory = fileName + File.separator + file.getName();
boolean canPollMore = pollDirectory(subDirectory, fileList, depth);
if (!canPollMore) {
return false;
}
}
} else {
// Windows can report false to a file on a share so regard it always as a file (if its not a directory)
if (depth >= endpoint.minDepth && isValidFile(gf, false, files)) {
log.trace("Adding valid file: {}", file);
// matched file so add
if (extendedAttributes != null) {
Path path = file.toPath();
Map<String, Object> allAttributes = new HashMap<>();
for (String attribute : extendedAttributes) {
try {
String prefix = null;
if (attribute.endsWith(":*")) {
prefix = attribute.substring(0, attribute.length() - 1);
} else if (attribute.equals("*")) {
prefix = "basic:";
}
if (ObjectHelper.isNotEmpty(prefix)) {
Map<String, Object> attributes = Files.readAttributes(path, attribute);
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
allAttributes.put(prefix + entry.getKey(), entry.getValue());
}
}
} else if (!attribute.contains(":")) {
allAttributes.put("basic:" + attribute, Files.getAttribute(path, attribute));
} else {
allAttributes.put(attribute, Files.getAttribute(path, attribute));
}
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug("Unable to read attribute {} on file {}", attribute, file, e);
}
}
}
gf.setExtendedAttributes(allAttributes);
}
fileList.add(gf);
}
}
}
return true;
}
use of java.io.File in project camel by apache.
the class GenericFile method changeFileName.
/**
* Changes the name of this remote file. This method alters the absolute and
* relative names as well.
*
* @param newName the new name
*/
public void changeFileName(String newName) {
LOG.trace("Changing name to: {}", newName);
// Make sure the names is normalized.
String newFileName = FileUtil.normalizePath(newName);
String newEndpointPath = FileUtil.normalizePath(endpointPath.endsWith("" + File.separatorChar) ? endpointPath : endpointPath + File.separatorChar);
LOG.trace("Normalized endpointPath: {}", newEndpointPath);
LOG.trace("Normalized newFileName: ()", newFileName);
File file = new File(newFileName);
if (!absolute) {
// for relative then we should avoid having the endpoint path duplicated so clip it
if (ObjectHelper.isNotEmpty(newEndpointPath) && newFileName.startsWith(newEndpointPath)) {
// in this logic here
if (newEndpointPath.endsWith("" + File.separatorChar)) {
newFileName = ObjectHelper.after(newFileName, newEndpointPath);
} else {
newFileName = ObjectHelper.after(newFileName, newEndpointPath + File.separatorChar);
}
// reconstruct file with clipped name
file = new File(newFileName);
}
}
// store the file name only
setFileNameOnly(file.getName());
setFileName(file.getName());
// relative path
if (file.getParent() != null) {
setRelativeFilePath(file.getParent() + getFileSeparator() + file.getName());
} else {
setRelativeFilePath(file.getName());
}
// absolute path
if (isAbsolute(newFileName)) {
setAbsolute(true);
setAbsoluteFilePath(newFileName);
} else {
setAbsolute(false);
// construct a pseudo absolute filename that the file operations uses even for relative only
String path = ObjectHelper.isEmpty(endpointPath) ? "" : endpointPath + getFileSeparator();
setAbsoluteFilePath(path + getRelativeFilePath());
}
if (LOG.isTraceEnabled()) {
LOG.trace("FileNameOnly: {}", getFileNameOnly());
LOG.trace("FileName: {}", getFileName());
LOG.trace("Absolute: {}", isAbsolute());
LOG.trace("Relative path: {}", getRelativeFilePath());
LOG.trace("Absolute path: {}", getAbsoluteFilePath());
LOG.trace("Name changed to: {}", this);
}
}
use of java.io.File in project camel by apache.
the class GenericFile method populateHeaders.
/**
* Populates the {@link GenericFileMessage} relevant headers
*
* @param message the message to populate with headers
*/
public void populateHeaders(GenericFileMessage<T> message) {
if (message != null) {
message.setHeader(Exchange.FILE_NAME_ONLY, getFileNameOnly());
message.setHeader(Exchange.FILE_NAME, getFileName());
message.setHeader(Exchange.FILE_NAME_CONSUMED, getFileName());
message.setHeader("CamelFileAbsolute", isAbsolute());
message.setHeader("CamelFileAbsolutePath", getAbsoluteFilePath());
if (extendedAttributes != null) {
message.setHeader("CamelFileExtendedAttributes", extendedAttributes);
}
if (probeContentType && file instanceof File) {
File f = (File) file;
Path path = f.toPath();
try {
message.setHeader(Exchange.FILE_CONTENT_TYPE, Files.probeContentType(path));
} catch (Throwable e) {
// just ignore the exception
}
}
if (isAbsolute()) {
message.setHeader(Exchange.FILE_PATH, getAbsoluteFilePath());
} else {
// we must normalize path according to protocol if we build our own paths
String path = normalizePathToProtocol(getEndpointPath() + File.separator + getRelativeFilePath());
message.setHeader(Exchange.FILE_PATH, path);
}
message.setHeader("CamelFileRelativePath", getRelativeFilePath());
message.setHeader(Exchange.FILE_PARENT, getParent());
if (getFileLength() >= 0) {
message.setHeader(Exchange.FILE_LENGTH, getFileLength());
}
if (getLastModified() > 0) {
message.setHeader(Exchange.FILE_LAST_MODIFIED, getLastModified());
}
}
}
use of java.io.File in project camel by apache.
the class FileStateRepository method loadStore.
/**
* Loads the given file store into the 1st level cache
*/
protected void loadStore() throws IOException {
// auto create starting directory if needed
if (!fileStore.exists()) {
LOG.debug("Creating filestore: {}", fileStore);
File parent = fileStore.getParentFile();
if (parent != null) {
parent.mkdirs();
}
boolean created = FileUtil.createNewFile(fileStore);
if (!created) {
throw new IOException("Cannot create filestore: " + fileStore);
}
}
LOG.trace("Loading to 1st level cache from state filestore: {}", fileStore);
cache.clear();
Scanner scanner = null;
try {
scanner = new Scanner(fileStore);
scanner.useDelimiter(STORE_DELIMITER);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
int separatorIndex = line.indexOf(KEY_VALUE_DELIMITER);
String key = line.substring(0, separatorIndex);
String value = line.substring(separatorIndex + KEY_VALUE_DELIMITER.length());
cache.put(key, value);
}
} catch (IOException e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
} finally {
if (scanner != null) {
scanner.close();
}
}
LOG.debug("Loaded {} to the 1st level cache from state filestore: {}", cache.size(), fileStore);
}
use of java.io.File in project camel by apache.
the class FileStateRepository method appendToStore.
/**
* Appends the {@code <key,value>} pair to the file store
*
* @param key the state key
*/
private void appendToStore(String key, String value) {
if (LOG.isDebugEnabled()) {
LOG.debug("Appending {}={} to state filestore: {}", new Object[] { key, value, fileStore });
}
FileOutputStream fos = null;
try {
// create store parent directory if missing
File storeParentDirectory = fileStore.getParentFile();
if (storeParentDirectory != null && !storeParentDirectory.exists()) {
LOG.info("Parent directory of file store {} doesn't exist. Creating.", fileStore);
if (fileStore.getParentFile().mkdirs()) {
LOG.info("Parent directory of file store {} successfully created.", fileStore);
} else {
LOG.warn("Parent directory of file store {} cannot be created.", fileStore);
}
}
// create store if missing
if (!fileStore.exists()) {
FileUtil.createNewFile(fileStore);
}
// append to store
fos = new FileOutputStream(fileStore, true);
fos.write(key.getBytes());
fos.write(KEY_VALUE_DELIMITER.getBytes());
fos.write(value.getBytes());
fos.write(STORE_DELIMITER.getBytes());
} catch (IOException e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
} finally {
IOHelper.close(fos, "Appending to file state repository", LOG);
}
}
Aggregations