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 */ 017package org.apache.camel.component.file; 018 019import java.io.File; 020import java.io.FileNotFoundException; 021import java.nio.file.Files; 022import java.nio.file.attribute.PosixFilePermission; 023import java.util.Arrays; 024import java.util.HashSet; 025import java.util.Set; 026 027import org.apache.camel.Component; 028import org.apache.camel.Exchange; 029import org.apache.camel.Message; 030import org.apache.camel.PollingConsumer; 031import org.apache.camel.Processor; 032import org.apache.camel.processor.idempotent.MemoryIdempotentRepository; 033import org.apache.camel.spi.Metadata; 034import org.apache.camel.spi.UriEndpoint; 035import org.apache.camel.spi.UriParam; 036import org.apache.camel.spi.UriPath; 037import org.apache.camel.util.FileUtil; 038import org.apache.camel.util.ObjectHelper; 039 040/** 041 * The file component is used for reading or writing files. 042 */ 043@UriEndpoint(firstVersion = "1.0.0", scheme = "file", title = "File", syntax = "file:directoryName", consumerClass = FileConsumer.class, label = "core,file") 044public class FileEndpoint extends GenericFileEndpoint<File> { 045 046 private static final Integer CHMOD_WRITE_MASK = 02; 047 private static final Integer CHMOD_READ_MASK = 04; 048 private static final Integer CHMOD_EXECUTE_MASK = 01; 049 050 private final FileOperations operations = new FileOperations(this); 051 052 @UriPath(name = "directoryName") @Metadata(required = "true") 053 private File file; 054 @UriParam(label = "advanced", defaultValue = "true") 055 private boolean copyAndDeleteOnRenameFail = true; 056 @UriParam(label = "advanced") 057 private boolean renameUsingCopy; 058 @UriParam(label = "producer,advanced", defaultValue = "true") 059 private boolean forceWrites = true; 060 @UriParam(label = "consumer,advanced") 061 private boolean probeContentType; 062 @UriParam(label = "consumer,advanced") 063 private String extendedAttributes; 064 @UriParam(label = "producer,advanced") 065 private String chmod; 066 @UriParam(label = "producer,advanced") 067 private String chmodDirectory; 068 069 public FileEndpoint() { 070 } 071 072 public FileEndpoint(String endpointUri, Component component) { 073 super(endpointUri, component); 074 } 075 076 public FileConsumer createConsumer(Processor processor) throws Exception { 077 ObjectHelper.notNull(operations, "operations"); 078 ObjectHelper.notNull(file, "file"); 079 080 // auto create starting directory if needed 081 if (!file.exists() && !file.isDirectory()) { 082 if (isAutoCreate()) { 083 log.debug("Creating non existing starting directory: {}", file); 084 boolean absolute = FileUtil.isAbsolute(file); 085 boolean created = operations.buildDirectory(file.getPath(), absolute); 086 if (!created) { 087 log.warn("Cannot auto create starting directory: {}", file); 088 } 089 } else if (isStartingDirectoryMustExist()) { 090 throw new FileNotFoundException("Starting directory does not exist: " + file); 091 } 092 } 093 094 FileConsumer result = newFileConsumer(processor, operations); 095 096 if (isDelete() && getMove() != null) { 097 throw new IllegalArgumentException("You cannot set both delete=true and move options"); 098 } 099 100 // if noop=true then idempotent should also be configured 101 if (isNoop() && !isIdempotentSet()) { 102 log.info("Endpoint is configured with noop=true so forcing endpoint to be idempotent as well"); 103 setIdempotent(true); 104 } 105 106 // if idempotent and no repository set then create a default one 107 if (isIdempotentSet() && isIdempotent() && idempotentRepository == null) { 108 log.info("Using default memory based idempotent repository with cache max size: {}", DEFAULT_IDEMPOTENT_CACHE_SIZE); 109 idempotentRepository = MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IDEMPOTENT_CACHE_SIZE); 110 } 111 112 if (ObjectHelper.isNotEmpty(getReadLock())) { 113 // check if its a valid 114 String valid = "none,markerFile,fileLock,rename,changed,idempotent,idempotent-changed,idempotent-rename"; 115 String[] arr = valid.split(","); 116 boolean matched = Arrays.stream(arr).anyMatch(n -> n.equals(getReadLock())); 117 if (!matched) { 118 throw new IllegalArgumentException("ReadLock invalid: " + getReadLock() + ", must be one of: " + valid); 119 } 120 } 121 122 // set max messages per poll 123 result.setMaxMessagesPerPoll(getMaxMessagesPerPoll()); 124 result.setEagerLimitMaxMessagesPerPoll(isEagerMaxMessagesPerPoll()); 125 126 configureConsumer(result); 127 return result; 128 } 129 130 @Override 131 public PollingConsumer createPollingConsumer() throws Exception { 132 ObjectHelper.notNull(operations, "operations"); 133 ObjectHelper.notNull(file, "file"); 134 135 if (log.isDebugEnabled()) { 136 log.debug("Creating GenericFilePollingConsumer with queueSize: {} blockWhenFull: {} blockTimeout: {}", 137 getPollingConsumerQueueSize(), isPollingConsumerBlockWhenFull(), getPollingConsumerBlockTimeout()); 138 } 139 GenericFilePollingConsumer result = new GenericFilePollingConsumer(this); 140 // should not call configurePollingConsumer when its GenericFilePollingConsumer 141 result.setBlockWhenFull(isPollingConsumerBlockWhenFull()); 142 result.setBlockTimeout(getPollingConsumerBlockTimeout()); 143 144 return result; 145 } 146 147 public GenericFileProducer<File> createProducer() throws Exception { 148 ObjectHelper.notNull(operations, "operations"); 149 150 // you cannot use temp file and file exists append 151 if (getFileExist() == GenericFileExist.Append && ((getTempPrefix() != null) || (getTempFileName() != null))) { 152 throw new IllegalArgumentException("You cannot set both fileExist=Append and tempPrefix/tempFileName options"); 153 } 154 155 // ensure fileExist and moveExisting is configured correctly if in use 156 if (getFileExist() == GenericFileExist.Move && getMoveExisting() == null) { 157 throw new IllegalArgumentException("You must configure moveExisting option when fileExist=Move"); 158 } else if (getMoveExisting() != null && getFileExist() != GenericFileExist.Move) { 159 throw new IllegalArgumentException("You must configure fileExist=Move when moveExisting has been set"); 160 } 161 162 return new GenericFileProducer<File>(this, operations); 163 } 164 165 public Exchange createExchange(GenericFile<File> file) { 166 Exchange exchange = createExchange(); 167 if (file != null) { 168 file.bindToExchange(exchange, probeContentType); 169 } 170 return exchange; 171 } 172 173 /** 174 * Strategy to create a new {@link FileConsumer} 175 * 176 * @param processor the given processor 177 * @param operations file operations 178 * @return the created consumer 179 */ 180 protected FileConsumer newFileConsumer(Processor processor, GenericFileOperations<File> operations) { 181 return new FileConsumer(this, processor, operations); 182 } 183 184 public File getFile() { 185 return file; 186 } 187 188 /** 189 * The starting directory 190 */ 191 public void setFile(File file) { 192 this.file = file; 193 // update configuration as well 194 getConfiguration().setDirectory(FileUtil.isAbsolute(file) ? file.getAbsolutePath() : file.getPath()); 195 } 196 197 @Override 198 public String getScheme() { 199 return "file"; 200 } 201 202 @Override 203 protected String createEndpointUri() { 204 return getFile().toURI().toString(); 205 } 206 207 @Override 208 public char getFileSeparator() { 209 return File.separatorChar; 210 } 211 212 @Override 213 public boolean isAbsolute(String name) { 214 // relative or absolute path? 215 return FileUtil.isAbsolute(new File(name)); 216 } 217 218 public boolean isCopyAndDeleteOnRenameFail() { 219 return copyAndDeleteOnRenameFail; 220 } 221 222 /** 223 * Whether to fallback and do a copy and delete file, in case the file could not be renamed directly. This option is not available for the FTP component. 224 */ 225 public void setCopyAndDeleteOnRenameFail(boolean copyAndDeleteOnRenameFail) { 226 this.copyAndDeleteOnRenameFail = copyAndDeleteOnRenameFail; 227 } 228 229 public boolean isRenameUsingCopy() { 230 return renameUsingCopy; 231 } 232 233 /** 234 * Perform rename operations using a copy and delete strategy. 235 * This is primarily used in environments where the regular rename operation is unreliable (e.g. across different file systems or networks). 236 * This option takes precedence over the copyAndDeleteOnRenameFail parameter that will automatically fall back to the copy and delete strategy, 237 * but only after additional delays. 238 */ 239 public void setRenameUsingCopy(boolean renameUsingCopy) { 240 this.renameUsingCopy = renameUsingCopy; 241 } 242 243 public boolean isForceWrites() { 244 return forceWrites; 245 } 246 247 /** 248 * Whether to force syncing writes to the file system. 249 * You can turn this off if you do not want this level of guarantee, for example if writing to logs / audit logs etc; this would yield better performance. 250 */ 251 public void setForceWrites(boolean forceWrites) { 252 this.forceWrites = forceWrites; 253 } 254 255 public boolean isProbeContentType() { 256 return probeContentType; 257 } 258 259 /** 260 * Whether to enable probing of the content type. If enable then the consumer uses {@link Files#probeContentType(java.nio.file.Path)} to 261 * determine the content-type of the file, and store that as a header with key {@link Exchange#FILE_CONTENT_TYPE} on the {@link Message}. 262 */ 263 public void setProbeContentType(boolean probeContentType) { 264 this.probeContentType = probeContentType; 265 } 266 267 public String getExtendedAttributes() { 268 return extendedAttributes; 269 } 270 271 /** 272 * To define which file attributes of interest. Like posix:permissions,posix:owner,basic:lastAccessTime, 273 * it supports basic wildcard like posix:*, basic:lastAccessTime 274 */ 275 public void setExtendedAttributes(String extendedAttributes) { 276 this.extendedAttributes = extendedAttributes; 277 } 278 279 /** 280 * Chmod value must be between 000 and 777; If there is a leading digit like in 0755 we will ignore it. 281 */ 282 public boolean chmodPermissionsAreValid(String chmod) { 283 if (chmod == null || chmod.length() < 3 || chmod.length() > 4) { 284 return false; 285 } 286 String permissionsString = chmod.trim().substring(chmod.length() - 3); // if 4 digits chop off leading one 287 for (int i = 0; i < permissionsString.length(); i++) { 288 Character c = permissionsString.charAt(i); 289 if (!Character.isDigit(c) || Integer.parseInt(c.toString()) > 7) { 290 return false; 291 } 292 } 293 return true; 294 } 295 296 public Set<PosixFilePermission> getPermissions() { 297 Set<PosixFilePermission> permissions = new HashSet<PosixFilePermission>(); 298 if (ObjectHelper.isEmpty(chmod)) { 299 return permissions; 300 } 301 302 String chmodString = chmod.substring(chmod.length() - 3); // if 4 digits chop off leading one 303 304 Integer ownerValue = Integer.parseInt(chmodString.substring(0, 1)); 305 Integer groupValue = Integer.parseInt(chmodString.substring(1, 2)); 306 Integer othersValue = Integer.parseInt(chmodString.substring(2, 3)); 307 308 if ((ownerValue & CHMOD_WRITE_MASK) > 0) { 309 permissions.add(PosixFilePermission.OWNER_WRITE); 310 } 311 if ((ownerValue & CHMOD_READ_MASK) > 0) { 312 permissions.add(PosixFilePermission.OWNER_READ); 313 } 314 if ((ownerValue & CHMOD_EXECUTE_MASK) > 0) { 315 permissions.add(PosixFilePermission.OWNER_EXECUTE); 316 } 317 318 if ((groupValue & CHMOD_WRITE_MASK) > 0) { 319 permissions.add(PosixFilePermission.GROUP_WRITE); 320 } 321 if ((groupValue & CHMOD_READ_MASK) > 0) { 322 permissions.add(PosixFilePermission.GROUP_READ); 323 } 324 if ((groupValue & CHMOD_EXECUTE_MASK) > 0) { 325 permissions.add(PosixFilePermission.GROUP_EXECUTE); 326 } 327 328 if ((othersValue & CHMOD_WRITE_MASK) > 0) { 329 permissions.add(PosixFilePermission.OTHERS_WRITE); 330 } 331 if ((othersValue & CHMOD_READ_MASK) > 0) { 332 permissions.add(PosixFilePermission.OTHERS_READ); 333 } 334 if ((othersValue & CHMOD_EXECUTE_MASK) > 0) { 335 permissions.add(PosixFilePermission.OTHERS_EXECUTE); 336 } 337 338 return permissions; 339 } 340 341 public String getChmod() { 342 return chmod; 343 } 344 345 /** 346 * Specify the file permissions which is sent by the producer, the chmod value must be between 000 and 777; 347 * If there is a leading digit like in 0755 we will ignore it. 348 */ 349 public void setChmod(String chmod) throws Exception { 350 if (ObjectHelper.isNotEmpty(chmod) && chmodPermissionsAreValid(chmod)) { 351 this.chmod = chmod.trim(); 352 } else { 353 throw new IllegalArgumentException("chmod option [" + chmod + "] is not valid"); 354 } 355 } 356 357 public Set<PosixFilePermission> getDirectoryPermissions() { 358 Set<PosixFilePermission> permissions = new HashSet<PosixFilePermission>(); 359 if (ObjectHelper.isEmpty(chmodDirectory)) { 360 return permissions; 361 } 362 363 String chmodString = chmodDirectory.substring(chmodDirectory.length() - 3); // if 4 digits chop off leading one 364 365 Integer ownerValue = Integer.parseInt(chmodString.substring(0, 1)); 366 Integer groupValue = Integer.parseInt(chmodString.substring(1, 2)); 367 Integer othersValue = Integer.parseInt(chmodString.substring(2, 3)); 368 369 if ((ownerValue & CHMOD_WRITE_MASK) > 0) { 370 permissions.add(PosixFilePermission.OWNER_WRITE); 371 } 372 if ((ownerValue & CHMOD_READ_MASK) > 0) { 373 permissions.add(PosixFilePermission.OWNER_READ); 374 } 375 if ((ownerValue & CHMOD_EXECUTE_MASK) > 0) { 376 permissions.add(PosixFilePermission.OWNER_EXECUTE); 377 } 378 379 if ((groupValue & CHMOD_WRITE_MASK) > 0) { 380 permissions.add(PosixFilePermission.GROUP_WRITE); 381 } 382 if ((groupValue & CHMOD_READ_MASK) > 0) { 383 permissions.add(PosixFilePermission.GROUP_READ); 384 } 385 if ((groupValue & CHMOD_EXECUTE_MASK) > 0) { 386 permissions.add(PosixFilePermission.GROUP_EXECUTE); 387 } 388 389 if ((othersValue & CHMOD_WRITE_MASK) > 0) { 390 permissions.add(PosixFilePermission.OTHERS_WRITE); 391 } 392 if ((othersValue & CHMOD_READ_MASK) > 0) { 393 permissions.add(PosixFilePermission.OTHERS_READ); 394 } 395 if ((othersValue & CHMOD_EXECUTE_MASK) > 0) { 396 permissions.add(PosixFilePermission.OTHERS_EXECUTE); 397 } 398 399 return permissions; 400 } 401 402 public String getChmodDirectory() { 403 return chmodDirectory; 404 } 405 406 /** 407 * Specify the directory permissions used when the producer creates missing directories, the chmod value must be between 000 and 777; 408 * If there is a leading digit like in 0755 we will ignore it. 409 */ 410 public void setChmodDirectory(String chmodDirectory) throws Exception { 411 if (ObjectHelper.isNotEmpty(chmodDirectory) && chmodPermissionsAreValid(chmodDirectory)) { 412 this.chmodDirectory = chmodDirectory.trim(); 413 } else { 414 throw new IllegalArgumentException("chmodDirectory option [" + chmodDirectory + "] is not valid"); 415 } 416 } 417 418}