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.FileUtil; 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 036 public RemoteFileConfiguration() { 037 } 038 039 public RemoteFileConfiguration(URI uri) { 040 configure(uri); 041 } 042 043 @Override 044 public boolean needToNormalize() { 045 return false; 046 } 047 048 @Override 049 public void configure(URI uri) { 050 super.configure(uri); 051 setProtocol(uri.getScheme()); 052 setDefaultPort(); 053 setUsername(uri.getUserInfo()); 054 setHost(uri.getHost()); 055 setPort(uri.getPort()); 056 } 057 058 @Override 059 public void setDirectory(String directory) { 060 // let super do its work first 061 super.setDirectory(directory); 062 063 // for FTP we must not start with a / root, so skip it if its there 064 super.setDirectory(FileUtil.stripLeadingSeparator(getDirectory())); 065 } 066 067 /** 068 * Returns human readable server information for logging purpose 069 */ 070 public String remoteServerInformation() { 071 return protocol + "://" + (username != null ? username : "anonymous") + "@" + host + ":" + getPort(); 072 } 073 074 protected abstract void setDefaultPort(); 075 076 public String getHost() { 077 return host; 078 } 079 080 public void setHost(String host) { 081 this.host = host; 082 } 083 084 public int getPort() { 085 return port; 086 } 087 088 public void setPort(int port) { 089 // only set port if provided with a positive number 090 if (port > 0) { 091 this.port = port; 092 } 093 } 094 095 public String getPassword() { 096 return password; 097 } 098 099 public void setPassword(String password) { 100 this.password = password; 101 } 102 103 public String getProtocol() { 104 return protocol; 105 } 106 107 public void setProtocol(String protocol) { 108 this.protocol = protocol; 109 } 110 111 public String getUsername() { 112 return username; 113 } 114 115 public void setUsername(String username) { 116 this.username = username; 117 } 118 119 public boolean isBinary() { 120 return binary; 121 } 122 123 public void setBinary(boolean binary) { 124 this.binary = binary; 125 } 126 127 public boolean isPassiveMode() { 128 return passiveMode; 129 } 130 131 /** 132 * Sets passive mode connections. 133 * <br/> 134 * Default is active mode connections. 135 */ 136 public void setPassiveMode(boolean passiveMode) { 137 this.passiveMode = passiveMode; 138 } 139 140 }