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; 020 021import org.apache.camel.Exchange; 022import org.apache.camel.component.file.strategy.FileMoveExistingStrategy; 023import org.apache.camel.util.FileUtil; 024import org.apache.camel.util.ObjectHelper; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028import static org.apache.camel.component.file.MoveExistingFileStrategyUtils.completePartialRelativePath; 029 030public class GenericFileDefaultMoveExistingFileStrategy implements FileMoveExistingStrategy { 031 032 private static final Logger LOG = LoggerFactory.getLogger(GenericFileDefaultMoveExistingFileStrategy.class); 033 034 /** 035 * Moves any existing file due fileExists=Move is in use. 036 */ 037 @Override 038 public boolean moveExistingFile(GenericFileEndpoint endpoint, GenericFileOperations operations, String fileName) 039 throws GenericFileOperationFailedException { 040 041 // need to evaluate using a dummy and simulate the file first, to have access to all the file attributes 042 // create a dummy exchange as Exchange is needed for expression evaluation 043 // we support only the following 3 tokens. 044 Exchange dummy = endpoint.createExchange(); 045 String parent = FileUtil.onlyPath(fileName); 046 String onlyName = FileUtil.stripPath(fileName); 047 dummy.getIn().setHeader(Exchange.FILE_NAME, fileName); 048 dummy.getIn().setHeader(Exchange.FILE_NAME_ONLY, onlyName); 049 dummy.getIn().setHeader(Exchange.FILE_PARENT, parent); 050 051 String to = endpoint.getMoveExisting().evaluate(dummy, String.class); 052 053 if (ObjectHelper.isEmpty(to)) { 054 throw new GenericFileOperationFailedException("moveExisting evaluated as empty String, cannot move existing file: " + fileName); 055 } 056 057 to = completePartialRelativePath(to, onlyName, parent); 058 059 // we must normalize it (to avoid having both \ and / in the name which confuses java.io.File) 060 to = FileUtil.normalizePath(to); 061 062 // ensure any paths is created before we rename as the renamed file may be in a different path (which may be non exiting) 063 // use java.io.File to compute the file path 064 File toFile = new File(to); 065 String directory = toFile.getParent(); 066 boolean absolute = FileUtil.isAbsolute(toFile); 067 if (directory != null) { 068 if (!operations.buildDirectory(directory, absolute)) { 069 LOG.debug("Cannot build directory [{}] (could be because of denied permissions)", directory); 070 } 071 } 072 073 // deal if there already exists a file 074 if (operations.existsFile(to)) { 075 if (endpoint.isEagerDeleteTargetFile()) { 076 LOG.trace("Deleting existing file: {}", to); 077 if (!operations.deleteFile(to)) { 078 throw new GenericFileOperationFailedException("Cannot delete file: " + to); 079 } 080 } else { 081 throw new GenericFileOperationFailedException("Cannot move existing file from: " + fileName + " to: " + to + " as there already exists a file: " + to); 082 } 083 } 084 085 LOG.trace("Moving existing file: {} to: {}", fileName, to); 086 if (!operations.renameFile(fileName, to)) { 087 throw new GenericFileOperationFailedException("Cannot rename file from: " + fileName + " to: " + to); 088 } 089 090 return true; 091 } 092 093}