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.remote;
018    
019    import org.apache.camel.component.file.GenericFile;
020    import org.apache.camel.component.file.GenericFileMessage;
021    import org.apache.camel.util.FileUtil;
022    import org.apache.camel.util.ObjectHelper;
023    
024    /**
025     * Represents a remote file of some sort of backing object
026     *
027     * @param <T> the type of file that these remote endpoints provide
028     */
029    public class RemoteFile<T> extends GenericFile<T> implements Cloneable {
030    
031        private String hostname;
032    
033        /**
034         * Populates the {@link GenericFileMessage} relevant headers
035         *
036         * @param message the message to populate with headers
037         */
038        public void populateHeaders(GenericFileMessage<T> message) {
039            if (message != null) {
040                super.populateHeaders(message);
041                message.setHeader("CamelFileHost", getHostname());
042            }
043        }
044    
045        public String getHostname() {
046            return hostname;
047        }
048    
049        public void setHostname(String hostname) {
050            this.hostname = hostname;
051        }
052        
053        @Override
054        public char getFileSeparator() {
055            // always use / as separator for FTP
056            return '/';
057        }
058        
059        @Override
060        protected boolean isAbsolute(String name) {
061            return name.startsWith("" + getFileSeparator());
062        }
063        
064        @Override
065        protected String normalizePath(String name) {        
066            return name;
067        }
068        
069        @SuppressWarnings("unchecked")
070        public RemoteFile<T> copyFrom(RemoteFile<T> source) {
071            RemoteFile<T> result;
072            try {
073                result = source.getClass().newInstance();
074            } catch (Exception e) {
075                throw ObjectHelper.wrapRuntimeCamelException(e);
076            }
077    
078            // align these setters with GenericFile
079            result.setEndpointPath(source.getEndpointPath());
080            result.setAbsolute(source.isAbsolute());
081            result.setAbsoluteFilePath(source.getAbsoluteFilePath());
082            result.setRelativeFilePath(source.getRelativeFilePath());
083            result.setFileName(source.getFileName());
084            result.setFileNameOnly(source.getFileNameOnly());
085            result.setFileLength(source.getFileLength());
086            result.setLastModified(source.getLastModified());
087            result.setFile(source.getFile());
088            result.setBody(source.getBody());
089            result.setBinding(source.getBinding());
090    
091            result.setHostname(source.getHostname());
092            return result;
093        }
094    
095        protected String normalizePathToProtocol(String path) {        
096            path = super.normalizePathToProtocol(path);        
097            // strip leading / for FTP protocol to avoid files with absolute paths
098            return FileUtil.stripLeadingSeparator(path);
099        }
100    }