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.ArrayList;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.camel.CamelContext;
025import org.apache.camel.ComponentConfiguration;
026import org.apache.camel.spi.EndpointCompleter;
027import org.apache.camel.util.FileUtil;
028import org.apache.camel.util.ObjectHelper;
029import org.apache.camel.util.StringHelper;
030
031/**
032 *  * The <a href="http://camel.apache.org/file.html">File Component</a> provides access to file systems.
033 */
034public class FileComponent extends GenericFileComponent<File> implements EndpointCompleter {
035    /**
036     * GenericFile property on Camel Exchanges.
037     */
038    public static final String FILE_EXCHANGE_FILE = "CamelFileExchangeFile";
039    
040    /**
041     * Default camel lock filename postfix
042     */
043    public static final String DEFAULT_LOCK_FILE_POSTFIX = ".camelLock";
044
045    public FileComponent() {
046        setEndpointClass(FileEndpoint.class);
047    }
048
049    public FileComponent(CamelContext context) {
050        super(context);
051        setEndpointClass(FileEndpoint.class);
052    }
053
054    protected GenericFileEndpoint<File> buildFileEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
055        // the starting directory must be a static (not containing dynamic expressions)
056        if (StringHelper.hasStartToken(remaining, "simple")) {
057            throw new IllegalArgumentException("Invalid directory: " + remaining
058                    + ". Dynamic expressions with ${ } placeholders is not allowed."
059                    + " Use the fileName option to set the dynamic expression.");
060        }
061
062        File file = new File(remaining);
063
064        FileEndpoint result = new FileEndpoint(uri, this);
065        result.setFile(file);
066
067        GenericFileConfiguration config = new GenericFileConfiguration();
068        config.setDirectory(FileUtil.isAbsolute(file) ? file.getAbsolutePath() : file.getPath());
069        result.setConfiguration(config);
070
071        return result;
072    }
073
074    protected void afterPropertiesSet(GenericFileEndpoint<File> endpoint) throws Exception {
075        // noop
076    }
077
078    public List<String> completeEndpointPath(ComponentConfiguration configuration, String completionText) {
079        boolean empty = ObjectHelper.isEmpty(completionText);
080        String pattern = completionText;
081        File file = new File(completionText);
082        String prefix = completionText;
083        if (file.exists()) {
084            pattern = "";
085        } else {
086            String startPath = ".";
087            if (!empty) {
088                int idx = completionText.lastIndexOf('/');
089                if (idx >= 0) {
090                    startPath = completionText.substring(0, idx);
091                    if (startPath.length() == 0) {
092                        startPath = "/";
093                    }
094                    pattern = completionText.substring(idx + 1);
095                }
096            }
097            file = new File(startPath);
098            prefix = startPath;
099        }
100        if (prefix.length() > 0 && !prefix.endsWith("/")) {
101            prefix += "/";
102        }
103        if (prefix.equals("./")) {
104            prefix = "";
105        }
106        File[] list = file.listFiles();
107        List<String> answer = new ArrayList<String>();
108        for (File aFile : list) {
109            String name = aFile.getName();
110            if (pattern.length() == 0 || name.contains(pattern)) {
111                if (isValidEndpointCompletion(configuration, completionText, aFile)) {
112                    answer.add(prefix + name);
113                }
114            }
115        }
116        return answer;
117    }
118
119    /**
120     * Returns true if this is a valid file for completion. By default we should ignore files that start with a "."
121     */
122    protected boolean isValidEndpointCompletion(ComponentConfiguration configuration, String completionText,
123                                           File file) {
124        return !file.getName().startsWith(".");
125    }
126}