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 java.net.URI;
020    
021    import org.apache.camel.Expression;
022    import org.apache.camel.RuntimeCamelException;
023    import org.apache.camel.language.simple.FileLanguage;
024    import org.apache.commons.net.ftp.FTPClientConfig;
025    
026    public class RemoteFileConfiguration implements Cloneable {
027        private String protocol;
028        private String username;
029        private String host;
030        private int port;
031        private String password;
032        private String file;
033        private boolean binary;
034        private boolean directory = true;
035        private FTPClientConfig ftpClientConfig;
036        private Expression expression;
037        private boolean passiveMode;
038        private String knownHosts;
039        private String privateKeyFile;
040        private String privateKeyFilePassphrase;
041    
042        public RemoteFileConfiguration() {
043        }
044    
045        public RemoteFileConfiguration(URI uri) {
046            configure(uri);
047        }
048    
049        public RemoteFileConfiguration copy() {
050            try {
051                return (RemoteFileConfiguration)clone();
052            } catch (CloneNotSupportedException e) {
053                throw new RuntimeCamelException(e);
054            }
055        }
056    
057        public String toString() {
058            return remoteServerInformation() + "/" + file;
059        }
060    
061        /**
062         * Returns human readable server information for logging purpose
063         */
064        public String remoteServerInformation() {
065            return protocol + "://" + (username != null ? username : "anonymous") + "@" + host + ":" + port;
066        }
067    
068        public void configure(URI uri) {
069            setProtocol(uri.getScheme());
070            setDefaultPort();
071            setUsername(uri.getUserInfo());
072            setHost(uri.getHost());
073            setPort(uri.getPort());
074            setFile(uri.getPath());
075        }
076    
077        protected void setDefaultPort() {
078            if ("ftp".equalsIgnoreCase(protocol)) {
079                setPort(21);
080            } else if ("sftp".equalsIgnoreCase(protocol)) {
081                setPort(22);
082            }
083        }
084    
085        public String getFile() {
086            return file;
087        }
088    
089        public void setFile(String file) {
090            // Avoid accidentally putting everything in root on
091            // servers that expose the full filesystem
092            if (file.startsWith("/")) {
093                file = file.substring(1);
094            }
095            this.file = file;
096        }
097    
098        public String getKnownHosts() {
099            return knownHosts;
100        }
101    
102        public void setKnownHosts(String knownHosts) {
103            this.knownHosts = knownHosts;
104        }
105    
106        public String getHost() {
107            return host;
108        }
109    
110        public void setHost(String host) {
111            this.host = host;
112        }
113    
114        public int getPort() {
115            return port;
116        }
117    
118        public void setPort(int port) {
119            if (port != -1) { // use default
120                this.port = port;
121            }
122        }
123    
124        public String getPassword() {
125            return password;
126        }
127    
128        public void setPassword(String password) {
129            this.password = password;
130        }
131    
132        public String getProtocol() {
133            return protocol;
134        }
135    
136        public void setProtocol(String protocol) {
137            this.protocol = protocol;
138        }
139    
140        public String getUsername() {
141            return username;
142        }
143    
144        public void setUsername(String username) {
145            this.username = username;
146        }
147    
148        public boolean isBinary() {
149            return binary;
150        }
151    
152        public void setBinary(boolean binary) {
153            this.binary = binary;
154        }
155    
156        public boolean isDirectory() {
157            return directory;
158        }
159    
160        public void setDirectory(boolean directory) {
161            this.directory = directory;
162        }
163    
164        public FTPClientConfig getFtpClientConfig() {
165            return ftpClientConfig;
166        }
167    
168        public void setFtpClientConfig(FTPClientConfig ftpClientConfig) {
169            this.ftpClientConfig = ftpClientConfig;
170        }
171    
172    
173        public Expression getExpression() {
174            return expression;
175        }
176    
177        public void setExpression(Expression expression) {
178            this.expression = expression;
179        }
180    
181        /**
182         * Sets the expression based on {@link org.apache.camel.language.simple.FileLanguage}
183         */
184        public void setExpression(String fileLanguageExpression) {
185            this.expression = FileLanguage.file(fileLanguageExpression);
186        }
187    
188        public boolean isPassiveMode() {
189            return passiveMode;
190        }
191    
192        /**
193         * Sets passive mode connections.
194         * <br/>
195         * Default is active mode connections.
196         */
197        public void setPassiveMode(boolean passiveMode) {
198            this.passiveMode = passiveMode;
199        }
200    
201        public String getPrivateKeyFile() {
202            return privateKeyFile;
203        }
204    
205        public void setPrivateKeyFile(String privateKeyFile) {
206            this.privateKeyFile = privateKeyFile;
207        }
208    
209        public String getPrivateKeyFilePassphrase() {
210            return privateKeyFilePassphrase;
211        }
212    
213        public void setPrivateKeyFilePassphrase(String privateKeyFilePassphrase) {
214            this.privateKeyFilePassphrase = privateKeyFilePassphrase;
215        }
216        
217    }