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