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    
023    /**
024     * Configuration of the FTP server
025     */
026    public abstract class RemoteFileConfiguration extends GenericFileConfiguration {
027        private String protocol;
028        private String username;
029        private String host;
030        private int port;
031        private String password;
032        private boolean binary;
033        private boolean passiveMode;
034        private int connectTimeout = 10000;
035        private int timeout = 30000;
036        private int soTimeout;
037    
038        public RemoteFileConfiguration() {
039        }
040    
041        public RemoteFileConfiguration(URI uri) {
042            configure(uri);
043        }
044        
045        @Override
046        public boolean needToNormalize() {
047            return false;
048        }
049    
050        @Override
051        public void configure(URI uri) {
052            super.configure(uri);
053            setProtocol(uri.getScheme());
054            setDefaultPort();
055            setUsername(uri.getUserInfo());
056            setHost(uri.getHost());
057            setPort(uri.getPort());
058        }
059    
060        /**
061         * Returns human readable server information for logging purpose
062         */
063        public String remoteServerInformation() {
064            return protocol + "://" + (username != null ? username : "anonymous") + "@" + host + ":" + getPort();
065        }
066    
067        protected abstract void setDefaultPort();
068    
069        public String getHost() {
070            return host;
071        }
072    
073        public void setHost(String host) {
074            this.host = host;
075        }
076    
077        public int getPort() {
078            return port;
079        }
080    
081        public void setPort(int port) {
082            // only set port if provided with a positive number
083            if (port > 0) {
084                this.port = port;
085            }
086        }
087    
088        public String getPassword() {
089            return password;
090        }
091    
092        public void setPassword(String password) {
093            this.password = password;
094        }
095    
096        public String getProtocol() {
097            return protocol;
098        }
099    
100        public void setProtocol(String protocol) {
101            this.protocol = protocol;
102        }
103    
104        public String getUsername() {
105            return username;
106        }
107    
108        public void setUsername(String username) {
109            this.username = username;
110        }
111    
112        public boolean isBinary() {
113            return binary;
114        }
115    
116        public void setBinary(boolean binary) {
117            this.binary = binary;
118        }
119    
120        public boolean isPassiveMode() {
121            return passiveMode;
122        }
123    
124        /**
125         * Sets passive mode connections.
126         * <br/>
127         * Default is active mode connections.
128         */
129        public void setPassiveMode(boolean passiveMode) {
130            this.passiveMode = passiveMode;
131        }
132    
133        public int getConnectTimeout() {
134            return connectTimeout;
135        }
136    
137        /**
138         * Sets the connect timeout for waiting for a connection to be established
139         * <p/>
140         * Used by both FTPClient and JSCH
141         */
142        public void setConnectTimeout(int connectTimeout) {
143            this.connectTimeout = connectTimeout;
144        }
145    
146        public int getTimeout() {
147            return timeout;
148        }
149    
150        /**
151         * Sets the data timeout for waiting for reply
152         * <p/>
153         * Used only by FTPClient
154         */
155        public void setTimeout(int timeout) {
156            this.timeout = timeout;
157        }
158    
159        public int getSoTimeout() {
160            return soTimeout;
161        }
162    
163        /**
164         * Sets the so timeout
165         * <p/>
166         * Used only by FTPClient
167         */
168        public void setSoTimeout(int soTimeout) {
169            this.soTimeout = soTimeout;
170        }
171    }