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        private String protocol;
029        private String username;
030        private String host;
031        private int port;
032        private String password;
033        private boolean binary;
034        private boolean passiveMode;
035        private int connectTimeout = 10000;
036        private int timeout = 30000;
037        private int soTimeout;
038        private boolean throwExceptionOnConnectFailed;
039        private String siteCommand;
040    
041        public RemoteFileConfiguration() {
042        }
043    
044        public RemoteFileConfiguration(URI uri) {
045            configure(uri);
046        }
047        
048        @Override
049        public boolean needToNormalize() {
050            return false;
051        }
052    
053        @Override
054        public void configure(URI uri) {
055            super.configure(uri);
056            setProtocol(uri.getScheme());
057            setDefaultPort();
058    
059            // UserInfo can contain both username and password as: user:pwd@ftpserver
060            // see: http://en.wikipedia.org/wiki/URI_scheme
061            String username = uri.getUserInfo();
062            String pw = null;
063            if (username != null && username.contains(":")) {
064                pw = ObjectHelper.after(username, ":");
065                username = ObjectHelper.before(username, ":");
066            }
067            if (username != null) {
068                setUsername(username);
069            }
070            if (pw != null) {
071                setPassword(pw);
072            }
073    
074            setHost(uri.getHost());
075            setPort(uri.getPort());
076        }
077    
078        /**
079         * Returns human readable server information for logging purpose
080         */
081        public String remoteServerInformation() {
082            return protocol + "://" + (username != null ? username : "anonymous") + "@" + host + ":" + getPort();
083        }
084    
085        protected abstract void setDefaultPort();
086    
087        public String getHost() {
088            return host;
089        }
090    
091        public void setHost(String host) {
092            this.host = host;
093        }
094    
095        public int getPort() {
096            return port;
097        }
098    
099        public void setPort(int port) {
100            // only set port if provided with a positive number
101            if (port > 0) {
102                this.port = port;
103            }
104        }
105    
106        public String getPassword() {
107            return password;
108        }
109    
110        public void setPassword(String password) {
111            this.password = password;
112        }
113    
114        public String getProtocol() {
115            return protocol;
116        }
117    
118        public void setProtocol(String protocol) {
119            this.protocol = protocol;
120        }
121    
122        public String getUsername() {
123            return username;
124        }
125    
126        public void setUsername(String username) {
127            this.username = username;
128        }
129    
130        public boolean isBinary() {
131            return binary;
132        }
133    
134        public void setBinary(boolean binary) {
135            this.binary = binary;
136        }
137    
138        public boolean isPassiveMode() {
139            return passiveMode;
140        }
141    
142        /**
143         * Sets passive mode connections.
144         * <br/>
145         * Default is active mode connections.
146         */
147        public void setPassiveMode(boolean passiveMode) {
148            this.passiveMode = passiveMode;
149        }
150    
151        public int getConnectTimeout() {
152            return connectTimeout;
153        }
154    
155        /**
156         * Sets the connect timeout for waiting for a connection to be established
157         * <p/>
158         * Used by both FTPClient and JSCH
159         */
160        public void setConnectTimeout(int connectTimeout) {
161            this.connectTimeout = connectTimeout;
162        }
163    
164        public int getTimeout() {
165            return timeout;
166        }
167    
168        /**
169         * Sets the data timeout for waiting for reply
170         * <p/>
171         * Used only by FTPClient
172         */
173        public void setTimeout(int timeout) {
174            this.timeout = timeout;
175        }
176    
177        public int getSoTimeout() {
178            return soTimeout;
179        }
180    
181        /**
182         * Sets the so timeout
183         * <p/>
184         * Used only by FTPClient
185         */
186        public void setSoTimeout(int soTimeout) {
187            this.soTimeout = soTimeout;
188        }
189    
190        public boolean isThrowExceptionOnConnectFailed() {
191            return throwExceptionOnConnectFailed;
192        }
193    
194        /**
195         * Should an exception be thrown if connection failed (exhausted)
196         * <p/>
197         * By default exception is not thrown and a <tt>WARN</tt> is logged.
198         * You can use this to enable exception being thrown and handle the thrown exception
199         * from the {@link org.apache.camel.spi.PollingConsumerPollStrategy} rollback method.
200         */
201        public void setThrowExceptionOnConnectFailed(boolean throwExceptionOnConnectFailed) {
202            this.throwExceptionOnConnectFailed = throwExceptionOnConnectFailed;
203        }
204    
205        public String getSiteCommand() {
206            return siteCommand;
207        }
208    
209        /**
210         * Sets optional site command(s) to be executed after successful login.
211         * <p/>
212         * Multiple site commands can be separated using a new line character (\n).
213         *
214         * @param siteCommand the site command(s).
215         */
216        public void setSiteCommand(String siteCommand) {
217            this.siteCommand = siteCommand;
218        }
219    }