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