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.component.file.GenericFileConfiguration;
022    import org.apache.camel.util.ObjectHelper;
023    
024    /**
025     * Configuration of the FTP server
026     */
027    public abstract class RemoteFileConfiguration extends GenericFileConfiguration {
028    
029        /**
030         * Path separator as either unix or windows style.
031         * <p/>
032         * UNIX = Path separator / is used
033         * Windows = Path separator \ is used
034         * Auto = Use existing path separator in file name
035         */
036        public enum PathSeparator { UNIX, Windows, Auto };
037    
038        private String protocol;
039        private String username;
040        private String host;
041        private int port;
042        private String password;
043        private boolean binary;
044        private boolean passiveMode;
045        private int connectTimeout = 10000;
046        private int timeout = 30000;
047        private int soTimeout;
048        private boolean throwExceptionOnConnectFailed;
049        private String siteCommand;
050        private boolean stepwise = true;
051        private PathSeparator separator = PathSeparator.Auto;
052    
053        public RemoteFileConfiguration() {
054        }
055    
056        public RemoteFileConfiguration(URI uri) {
057            configure(uri);
058        }
059        
060        @Override
061        public boolean needToNormalize() {
062            return false;
063        }
064    
065        @Override
066        public void configure(URI uri) {
067            super.configure(uri);
068            setProtocol(uri.getScheme());
069            setDefaultPort();
070    
071            // UserInfo can contain both username and password as: user:pwd@ftpserver
072            // see: http://en.wikipedia.org/wiki/URI_scheme
073            String username = uri.getUserInfo();
074            String pw = null;
075            if (username != null && username.contains(":")) {
076                pw = ObjectHelper.after(username, ":");
077                username = ObjectHelper.before(username, ":");
078            }
079            if (username != null) {
080                setUsername(username);
081            }
082            if (pw != null) {
083                setPassword(pw);
084            }
085    
086            setHost(uri.getHost());
087            setPort(uri.getPort());
088        }
089    
090        /**
091         * Returns human readable server information for logging purpose
092         */
093        public String remoteServerInformation() {
094            return protocol + "://" + (username != null ? username : "anonymous") + "@" + host + ":" + getPort();
095        }
096    
097        protected abstract void setDefaultPort();
098    
099        public String getHost() {
100            return host;
101        }
102    
103        public void setHost(String host) {
104            this.host = host;
105        }
106    
107        public int getPort() {
108            return port;
109        }
110    
111        public void setPort(int port) {
112            // only set port if provided with a positive number
113            if (port > 0) {
114                this.port = port;
115            }
116        }
117    
118        public String getPassword() {
119            return password;
120        }
121    
122        public void setPassword(String password) {
123            this.password = password;
124        }
125    
126        public String getProtocol() {
127            return protocol;
128        }
129    
130        public void setProtocol(String protocol) {
131            this.protocol = protocol;
132        }
133    
134        public String getUsername() {
135            return username;
136        }
137    
138        public void setUsername(String username) {
139            this.username = username;
140        }
141    
142        public boolean isBinary() {
143            return binary;
144        }
145    
146        public void setBinary(boolean binary) {
147            this.binary = binary;
148        }
149    
150        public boolean isPassiveMode() {
151            return passiveMode;
152        }
153    
154        /**
155         * Sets passive mode connections.
156         * <br/>
157         * Default is active mode connections.
158         */
159        public void setPassiveMode(boolean passiveMode) {
160            this.passiveMode = passiveMode;
161        }
162    
163        public int getConnectTimeout() {
164            return connectTimeout;
165        }
166    
167        /**
168         * Sets the connect timeout for waiting for a connection to be established
169         * <p/>
170         * Used by both FTPClient and JSCH
171         */
172        public void setConnectTimeout(int connectTimeout) {
173            this.connectTimeout = connectTimeout;
174        }
175    
176        public int getTimeout() {
177            return timeout;
178        }
179    
180        /**
181         * Sets the data timeout for waiting for reply
182         * <p/>
183         * Used only by FTPClient
184         */
185        public void setTimeout(int timeout) {
186            this.timeout = timeout;
187        }
188    
189        public int getSoTimeout() {
190            return soTimeout;
191        }
192    
193        /**
194         * Sets the so timeout
195         * <p/>
196         * Used only by FTPClient
197         */
198        public void setSoTimeout(int soTimeout) {
199            this.soTimeout = soTimeout;
200        }
201    
202        public boolean isThrowExceptionOnConnectFailed() {
203            return throwExceptionOnConnectFailed;
204        }
205    
206        /**
207         * Should an exception be thrown if connection failed (exhausted)
208         * <p/>
209         * By default exception is not thrown and a <tt>WARN</tt> is logged.
210         * You can use this to enable exception being thrown and handle the thrown exception
211         * from the {@link org.apache.camel.spi.PollingConsumerPollStrategy} rollback method.
212         */
213        public void setThrowExceptionOnConnectFailed(boolean throwExceptionOnConnectFailed) {
214            this.throwExceptionOnConnectFailed = throwExceptionOnConnectFailed;
215        }
216    
217        public String getSiteCommand() {
218            return siteCommand;
219        }
220    
221        /**
222         * Sets optional site command(s) to be executed after successful login.
223         * <p/>
224         * Multiple site commands can be separated using a new line character (\n).
225         *
226         * @param siteCommand the site command(s).
227         */
228        public void setSiteCommand(String siteCommand) {
229            this.siteCommand = siteCommand;
230        }
231    
232        public boolean isStepwise() {
233            return stepwise;
234        }
235    
236        /**
237         * Sets whether we should stepwise change directories while traversing file structures
238         * when downloading files, or as well when uploading a file to a directory.
239         * <p/>
240         * You can disable this if you for example are in a situation where you cannot change directory
241         * on the FTP server due security reasons.
242         *
243         * @param stepwise whether to use change directory or not
244         */
245        public void setStepwise(boolean stepwise) {
246            this.stepwise = stepwise;
247        }
248    
249        public PathSeparator getSeparator() {
250            return separator;
251        }
252    
253        /**
254         * Sets the path separator to be used.
255         * <p/>
256         * UNIX = Path separator / is used
257         * Windows = Path separator \ is used
258         * Auto = (is default) Use existing path separator in file name
259         */
260        public void setSeparator(PathSeparator separator) {
261            this.separator = separator;
262        }
263    
264        /**
265         * Normalizes the given path according to the configured path separator.
266         *
267         * @param path  the given path
268         * @return the normalized path
269         */
270        public String normalizePath(String path) {
271            if (ObjectHelper.isEmpty(path) || separator == PathSeparator.Auto) {
272                return path;
273            }
274    
275            if (separator == PathSeparator.UNIX) {
276                // unix style
277                return path.replace('\\', '/');
278            } else {
279                // windows style
280                return path.replace('/', '\\');
281            }
282        }
283    }