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.util.concurrent.locks.Lock;
021import java.util.concurrent.locks.ReentrantLock;
022
023import org.apache.camel.Exchange;
024import org.apache.camel.Expression;
025import org.apache.camel.impl.DefaultExchange;
026import org.apache.camel.impl.DefaultProducer;
027import org.apache.camel.util.FileUtil;
028import org.apache.camel.util.LRUCache;
029import org.apache.camel.util.ObjectHelper;
030import org.apache.camel.util.ServiceHelper;
031import org.apache.camel.util.StringHelper;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035/**
036 * Generic file producer
037 */
038public class GenericFileProducer<T> extends DefaultProducer {
039    protected final Logger log = LoggerFactory.getLogger(getClass());
040    protected final GenericFileEndpoint<T> endpoint;
041    protected GenericFileOperations<T> operations;
042    // assume writing to 100 different files concurrently at most for the same file producer
043    private final LRUCache<String, Lock> locks = new LRUCache<String, Lock>(100);
044
045    protected GenericFileProducer(GenericFileEndpoint<T> endpoint, GenericFileOperations<T> operations) {
046        super(endpoint);
047        this.endpoint = endpoint;
048        this.operations = operations;
049    }
050    
051    public String getFileSeparator() {
052        return File.separator;
053    }
054
055    public String normalizePath(String name) {
056        return FileUtil.normalizePath(name);
057    }
058
059    public void process(Exchange exchange) throws Exception {
060        // store any existing file header which we want to keep and propagate
061        final String existing = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
062
063        // create the target file name
064        String target = createFileName(exchange);
065
066        // use lock for same file name to avoid concurrent writes to the same file
067        // for example when you concurrently append to the same file
068        Lock lock;
069        synchronized (locks) {
070            lock = locks.get(target);
071            if (lock == null) {
072                lock = new ReentrantLock();
073                locks.put(target, lock);
074            }
075        }
076
077        lock.lock();
078        try {
079            processExchange(exchange, target);
080        } finally {
081            // do not remove as the locks cache has an upper bound
082            // this ensure the locks is appropriate reused
083            lock.unlock();
084            // and remove the write file name header as we only want to use it once (by design)
085            exchange.getIn().removeHeader(Exchange.OVERRULE_FILE_NAME);
086            // and restore existing file name
087            exchange.getIn().setHeader(Exchange.FILE_NAME, existing);
088        }
089    }
090
091    /**
092     * Sets the operations to be used.
093     * <p/>
094     * Can be used to set a fresh operations in case of recovery attempts
095     *
096     * @param operations the operations
097     */
098    public void setOperations(GenericFileOperations<T> operations) {
099        this.operations = operations;
100    }
101
102    /**
103     * Perform the work to process the fileExchange
104     *
105     * @param exchange fileExchange
106     * @param target   the target filename
107     * @throws Exception is thrown if some error
108     */
109    protected void processExchange(Exchange exchange, String target) throws Exception {
110        log.trace("Processing file: {} for exchange: {}", target, exchange);
111
112        try {
113            preWriteCheck();
114
115            // should we write to a temporary name and then afterwards rename to real target
116            boolean writeAsTempAndRename = ObjectHelper.isNotEmpty(endpoint.getTempFileName());
117            String tempTarget = null;
118            // remember if target exists to avoid checking twice
119            Boolean targetExists;
120            if (writeAsTempAndRename) {
121                // compute temporary name with the temp prefix
122                tempTarget = createTempFileName(exchange, target);
123
124                log.trace("Writing using tempNameFile: {}", tempTarget);
125               
126                //if we should eager delete target file before deploying temporary file
127                if (endpoint.getFileExist() != GenericFileExist.TryRename && endpoint.isEagerDeleteTargetFile()) {
128                    
129                    // cater for file exists option on the real target as
130                    // the file operations code will work on the temp file
131
132                    // if an existing file already exists what should we do?
133                    targetExists = operations.existsFile(target);
134                    if (targetExists) {
135                        
136                        log.trace("EagerDeleteTargetFile, target exists");
137                        
138                        if (endpoint.getFileExist() == GenericFileExist.Ignore) {
139                            // ignore but indicate that the file was written
140                            log.trace("An existing file already exists: {}. Ignore and do not override it.", target);
141                            return;
142                        } else if (endpoint.getFileExist() == GenericFileExist.Fail) {
143                            throw new GenericFileOperationFailedException("File already exist: " + target + ". Cannot write new file.");
144                        } else if (endpoint.getFileExist() == GenericFileExist.Move) {
145                            // move any existing file first
146                            doMoveExistingFile(target);
147                        } else if (endpoint.isEagerDeleteTargetFile() && endpoint.getFileExist() == GenericFileExist.Override) {
148                            // we override the target so we do this by deleting it so the temp file can be renamed later
149                            // with success as the existing target file have been deleted
150                            log.trace("Eagerly deleting existing file: {}", target);
151                            if (!operations.deleteFile(target)) {
152                                throw new GenericFileOperationFailedException("Cannot delete file: " + target);
153                            }
154                        }
155                    }
156                }
157
158                // delete any pre existing temp file
159                if (operations.existsFile(tempTarget)) {
160                    log.trace("Deleting existing temp file: {}", tempTarget);
161                    if (!operations.deleteFile(tempTarget)) {
162                        throw new GenericFileOperationFailedException("Cannot delete file: " + tempTarget);
163                    }
164                }
165            }
166
167            // write/upload the file
168            writeFile(exchange, tempTarget != null ? tempTarget : target);
169
170            // if we did write to a temporary name then rename it to the real
171            // name after we have written the file
172            if (tempTarget != null) {
173                // if we did not eager delete the target file
174                if (endpoint.getFileExist() != GenericFileExist.TryRename && !endpoint.isEagerDeleteTargetFile()) {
175
176                    // if an existing file already exists what should we do?
177                    targetExists = operations.existsFile(target);
178                    if (targetExists) {
179
180                        log.trace("Not using EagerDeleteTargetFile, target exists");
181
182                        if (endpoint.getFileExist() == GenericFileExist.Ignore) {
183                            // ignore but indicate that the file was written
184                            log.trace("An existing file already exists: {}. Ignore and do not override it.", target);
185                            return;
186                        } else if (endpoint.getFileExist() == GenericFileExist.Fail) {
187                            throw new GenericFileOperationFailedException("File already exist: " + target + ". Cannot write new file.");
188                        } else if (endpoint.getFileExist() == GenericFileExist.Override) {
189                            // we override the target so we do this by deleting it so the temp file can be renamed later
190                            // with success as the existing target file have been deleted
191                            log.trace("Deleting existing file: {}", target);
192                            if (!operations.deleteFile(target)) {
193                                throw new GenericFileOperationFailedException("Cannot delete file: " + target);
194                            }
195                        }
196                    }
197                }
198
199                // now we are ready to rename the temp file to the target file
200                log.trace("Renaming file: [{}] to: [{}]", tempTarget, target);
201                boolean renamed = operations.renameFile(tempTarget, target);
202                if (!renamed) {
203                    throw new GenericFileOperationFailedException("Cannot rename file from: " + tempTarget + " to: " + target);
204                }
205            }
206
207            // any done file to write?
208            if (endpoint.getDoneFileName() != null) {
209                String doneFileName = endpoint.createDoneFileName(target);
210                ObjectHelper.notEmpty(doneFileName, "doneFileName", endpoint);
211
212                // create empty exchange with empty body to write as the done file
213                Exchange empty = new DefaultExchange(exchange);
214                empty.getIn().setBody("");
215
216                log.trace("Writing done file: [{}]", doneFileName);
217                // delete any existing done file
218                if (operations.existsFile(doneFileName)) {
219                    if (!operations.deleteFile(doneFileName)) {
220                        throw new GenericFileOperationFailedException("Cannot delete existing done file: " + doneFileName);
221                    }
222                }
223                writeFile(empty, doneFileName);
224            }
225
226            // let's store the name we really used in the header, so end-users
227            // can retrieve it
228            exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, target);
229        } catch (Exception e) {
230            handleFailedWrite(exchange, e);
231        }
232
233        postWriteCheck(exchange);
234    }
235
236    private void doMoveExistingFile(String fileName) throws GenericFileOperationFailedException {
237        // need to evaluate using a dummy and simulate the file first, to have access to all the file attributes
238        // create a dummy exchange as Exchange is needed for expression evaluation
239        // we support only the following 3 tokens.
240        Exchange dummy = endpoint.createExchange();
241        String parent = FileUtil.onlyPath(fileName);
242        String onlyName = FileUtil.stripPath(fileName);
243        dummy.getIn().setHeader(Exchange.FILE_NAME, fileName);
244        dummy.getIn().setHeader(Exchange.FILE_NAME_ONLY, onlyName);
245        dummy.getIn().setHeader(Exchange.FILE_PARENT, parent);
246
247        String to = endpoint.getMoveExisting().evaluate(dummy, String.class);
248        // we must normalize it (to avoid having both \ and / in the name which confuses java.io.File)
249        to = FileUtil.normalizePath(to);
250        if (ObjectHelper.isEmpty(to)) {
251            throw new GenericFileOperationFailedException("moveExisting evaluated as empty String, cannot move existing file: " + fileName);
252        }
253
254        boolean renamed = operations.renameFile(fileName, to);
255        if (!renamed) {
256            throw new GenericFileOperationFailedException("Cannot rename file from: " + fileName + " to: " + to);
257        }
258    }
259
260    /**
261     * If we fail writing out a file, we will call this method. This hook is
262     * provided to disconnect from servers or clean up files we created (if needed).
263     */
264    public void handleFailedWrite(Exchange exchange, Exception exception) throws Exception {
265        throw exception;
266    }
267
268    /**
269     * Perform any actions that need to occur before we write such as connecting to an FTP server etc.
270     */
271    public void preWriteCheck() throws Exception {
272        // nothing needed to check
273    }
274
275    /**
276     * Perform any actions that need to occur after we are done such as disconnecting.
277     */
278    public void postWriteCheck(Exchange exchange) {
279        // nothing needed to check
280    }
281
282    public void writeFile(Exchange exchange, String fileName) throws GenericFileOperationFailedException {
283        // build directory if auto create is enabled
284        if (endpoint.isAutoCreate()) {
285            // we must normalize it (to avoid having both \ and / in the name which confuses java.io.File)
286            String name = FileUtil.normalizePath(fileName);
287
288            // use java.io.File to compute the file path
289            File file = new File(name);
290            String directory = file.getParent();
291            boolean absolute = FileUtil.isAbsolute(file);
292            if (directory != null) {
293                if (!operations.buildDirectory(directory, absolute)) {
294                    log.debug("Cannot build directory [{}] (could be because of denied permissions)", directory);
295                }
296            }
297        }
298
299        // upload
300        if (log.isTraceEnabled()) {
301            log.trace("About to write [{}] to [{}] from exchange [{}]", new Object[]{fileName, getEndpoint(), exchange});
302        }
303
304        boolean success = operations.storeFile(fileName, exchange);
305        if (!success) {
306            throw new GenericFileOperationFailedException("Error writing file [" + fileName + "]");
307        }
308        log.debug("Wrote [{}] to [{}]", fileName, getEndpoint());
309    }
310
311    public String createFileName(Exchange exchange) {
312        String answer;
313
314        // overrule takes precedence
315        Object value;
316
317        Object overrule = exchange.getIn().getHeader(Exchange.OVERRULE_FILE_NAME);
318        if (overrule != null) {
319            if (overrule instanceof Expression) {
320                value = overrule;
321            } else {
322                value = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, overrule);
323            }
324        } else {
325            value = exchange.getIn().getHeader(Exchange.FILE_NAME);
326        }
327
328        // if we have an overrule then override the existing header to use the overrule computed name from this point forward
329        if (overrule != null) {
330            exchange.getIn().setHeader(Exchange.FILE_NAME, value);
331        }
332
333        if (value != null && value instanceof String && StringHelper.hasStartToken((String) value, "simple")) {
334            log.warn("Simple expression: {} detected in header: {} of type String. This feature has been removed (see CAMEL-6748).", value, Exchange.FILE_NAME);
335        }
336
337        // expression support
338        Expression expression = endpoint.getFileName();
339        if (value != null && value instanceof Expression) {
340            expression = (Expression) value;
341        }
342
343        // evaluate the name as a String from the value
344        String name;
345        if (expression != null) {
346            log.trace("Filename evaluated as expression: {}", expression);
347            name = expression.evaluate(exchange, String.class);
348        } else {
349            name = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, value);
350        }
351
352        // flatten name
353        if (name != null && endpoint.isFlatten()) {
354            // check for both windows and unix separators
355            int pos = Math.max(name.lastIndexOf("/"), name.lastIndexOf("\\"));
356            if (pos != -1) {
357                name = name.substring(pos + 1);
358            }
359        }
360
361        // compute path by adding endpoint starting directory
362        String endpointPath = endpoint.getConfiguration().getDirectory();
363        String baseDir = "";
364        if (endpointPath.length() > 0) {
365            // Its a directory so we should use it as a base path for the filename
366            // If the path isn't empty, we need to add a trailing / if it isn't already there
367            baseDir = endpointPath;
368            boolean trailingSlash = endpointPath.endsWith("/") || endpointPath.endsWith("\\");
369            if (!trailingSlash) {
370                baseDir += getFileSeparator();
371            }
372        }
373        if (name != null) {
374            answer = baseDir + name;
375        } else {
376            // use a generated filename if no name provided
377            answer = baseDir + endpoint.getGeneratedFileName(exchange.getIn());
378        }
379
380        if (endpoint.getConfiguration().needToNormalize()) {
381            // must normalize path to cater for Windows and other OS
382            answer = normalizePath(answer);
383        }
384
385        return answer;
386    }
387
388    public String createTempFileName(Exchange exchange, String fileName) {
389        String answer = fileName;
390
391        String tempName;
392        if (exchange.getIn().getHeader(Exchange.FILE_NAME) == null) {
393            // its a generated filename then add it to header so we can evaluate the expression
394            exchange.getIn().setHeader(Exchange.FILE_NAME, FileUtil.stripPath(fileName));
395            tempName = endpoint.getTempFileName().evaluate(exchange, String.class);
396            // and remove it again after evaluation
397            exchange.getIn().removeHeader(Exchange.FILE_NAME);
398        } else {
399            tempName = endpoint.getTempFileName().evaluate(exchange, String.class);
400        }
401
402        // check for both windows and unix separators
403        int pos = Math.max(answer.lastIndexOf("/"), answer.lastIndexOf("\\"));
404        if (pos == -1) {
405            // no path so use temp name as calculated
406            answer = tempName;
407        } else {
408            // path should be prefixed before the temp name
409            StringBuilder sb = new StringBuilder(answer.substring(0, pos + 1));
410            sb.append(tempName);
411            answer = sb.toString();
412        }
413
414        if (endpoint.getConfiguration().needToNormalize()) {
415            // must normalize path to cater for Windows and other OS
416            answer = normalizePath(answer);
417        }
418
419        // stack path in case the temporary file uses .. paths
420        answer = FileUtil.compactPath(answer, getFileSeparator());
421
422        return answer;
423    }
424
425    @Override
426    protected void doStart() throws Exception {
427        super.doStart();
428        ServiceHelper.startService(locks);
429    }
430
431    @Override
432    protected void doStop() throws Exception {
433        ServiceHelper.stopService(locks);
434        super.doStop();
435    }
436}