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 */ 017package org.apache.commons.vfs2.provider.ftp; 018 019import java.net.Proxy; 020 021import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory; 022import org.apache.commons.vfs2.FileSystem; 023import org.apache.commons.vfs2.FileSystemConfigBuilder; 024import org.apache.commons.vfs2.FileSystemOptions; 025 026/** 027 * The config builder for various ftp configuration options. 028 */ 029public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { 030 private static final String _PREFIX = FtpFileSystemConfigBuilder.class.getName(); 031 032 private static final FtpFileSystemConfigBuilder BUILDER = new FtpFileSystemConfigBuilder(); 033 034 private static final String CONNECT_TIMEOUT = _PREFIX + ".CONNECT_TIMEOUT"; 035 private static final String DATA_TIMEOUT = _PREFIX + ".DATA_TIMEOUT"; 036 private static final String DEFAULT_DATE_FORMAT = _PREFIX + ".DEFAULT_DATE_FORMAT"; 037 private static final String ENCODING = _PREFIX + ".ENCODING"; 038 private static final String FACTORY_KEY = FTPFileEntryParserFactory.class.getName() + ".KEY"; 039 private static final String FILE_TYPE = _PREFIX + ".FILE_TYPE"; 040 private static final String PASSIVE_MODE = _PREFIX + ".PASSIVE"; 041 private static final String PROXY = _PREFIX + ".PROXY"; 042 private static final String RECENT_DATE_FORMAT = _PREFIX + ".RECENT_DATE_FORMAT"; 043 private static final String REMOTE_VERIFICATION = _PREFIX + ".REMOTE_VERIFICATION"; 044 private static final String SERVER_LANGUAGE_CODE = _PREFIX + ".SERVER_LANGUAGE_CODE"; 045 private static final String SERVER_TIME_ZONE_ID = _PREFIX + ".SERVER_TIME_ZONE_ID"; 046 private static final String SHORT_MONTH_NAMES = _PREFIX + ".SHORT_MONTH_NAMES"; 047 private static final String SO_TIMEOUT = _PREFIX + ".SO_TIMEOUT"; 048 private static final String USER_DIR_IS_ROOT = _PREFIX + ".USER_DIR_IS_ROOT"; 049 050 private FtpFileSystemConfigBuilder() { 051 super("ftp."); 052 } 053 054 /** 055 * Create new config builder with specified prefix string. 056 * 057 * @param prefix prefix string to use for parameters of this config builder. 058 * @since 2.1 059 */ 060 protected FtpFileSystemConfigBuilder(final String prefix) { 061 super(prefix); 062 } 063 064 /** 065 * Gets the singleton instance. 066 * 067 * @return the singleton instance. 068 */ 069 public static FtpFileSystemConfigBuilder getInstance() { 070 return BUILDER; 071 } 072 073 @Override 074 protected Class<? extends FileSystem> getConfigClass() { 075 return FtpFileSystem.class; 076 } 077 078 /** 079 * Gets the timeout in milliseconds to use for the socket connection. 080 * 081 * @param opts The FileSystemOptions. 082 * @return The timeout in milliseconds to use for the socket connection. 083 * @since 2.1 084 */ 085 public Integer getConnectTimeout(final FileSystemOptions opts) { 086 return getInteger(opts, CONNECT_TIMEOUT); 087 } 088 089 /** 090 * @param opts The FileSystemOptions. 091 * @return The encoding. 092 * @since 2.0 093 */ 094 public String getControlEncoding(final FileSystemOptions opts) { 095 return getString(opts, ENCODING); 096 } 097 098 /** 099 * @param opts The FileSystemOptions. 100 * @return The timeout for opening the data channel in milliseconds. 101 * @see #setDataTimeout 102 */ 103 public Integer getDataTimeout(final FileSystemOptions opts) { 104 return getInteger(opts, DATA_TIMEOUT); 105 } 106 107 /** 108 * Get the default date format used by the server. See {@link org.apache.commons.net.ftp.FTPClientConfig} for 109 * details and examples. 110 * 111 * @param opts The FileSystemOptions 112 * @return The default date format. 113 */ 114 public String getDefaultDateFormat(final FileSystemOptions opts) { 115 return getString(opts, DEFAULT_DATE_FORMAT); 116 } 117 118 /** 119 * @param opts The FileSystemOptions. 120 * @see #setEntryParser 121 * @return the key to the EntryParser. 122 */ 123 public String getEntryParser(final FileSystemOptions opts) { 124 return getString(opts, FACTORY_KEY); 125 } 126 127 /** 128 * @param opts The FlleSystemOptions. 129 * @see #setEntryParserFactory 130 * @return An FTPFileEntryParserFactory. 131 */ 132 public FTPFileEntryParserFactory getEntryParserFactory(final FileSystemOptions opts) { 133 return (FTPFileEntryParserFactory) getParam(opts, FTPFileEntryParserFactory.class.getName()); 134 } 135 136 /** 137 * Gets the file type parameter. 138 * 139 * @param opts The FileSystemOptions. 140 * @return A FtpFileType 141 * @since 2.1 142 */ 143 public FtpFileType getFileType(final FileSystemOptions opts) { 144 return getEnum(FtpFileType.class, opts, FILE_TYPE); 145 } 146 147 /** 148 * @param opts The FileSystemOptions. 149 * @return true if passive mode is set. 150 * @see #setPassiveMode 151 */ 152 public Boolean getPassiveMode(final FileSystemOptions opts) { 153 return getBoolean(opts, PASSIVE_MODE); 154 } 155 156 /** 157 * Gets the Proxy. 158 * 159 * @param opts The FileSystemOptions. 160 * @return the Proxy 161 * @since 2.1 162 */ 163 public Proxy getProxy(final FileSystemOptions opts) { 164 return (Proxy) this.getParam(opts, PROXY); 165 } 166 167 /** 168 * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples. 169 * 170 * @param opts The FileSystemOptions. 171 * @return The recent date format. 172 */ 173 public String getRecentDateFormat(final FileSystemOptions opts) { 174 return getString(opts, RECENT_DATE_FORMAT); 175 } 176 177 /** 178 * Gets whether to use remote verification. 179 * 180 * @param opts The FileSystemOptions. 181 * @return True if remote verification should be done. 182 */ 183 public Boolean getRemoteVerification(final FileSystemOptions opts) { 184 return getBoolean(opts, REMOTE_VERIFICATION); 185 } 186 187 /** 188 * Get the language code used by the server. See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and 189 * examples. 190 * 191 * @param opts The FilesystemOptions. 192 * @return The language code of the server. 193 */ 194 public String getServerLanguageCode(final FileSystemOptions opts) { 195 return getString(opts, SERVER_LANGUAGE_CODE); 196 } 197 198 /** 199 * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples. 200 * 201 * @param opts The FileSystemOptions. 202 * @return The server timezone id. 203 */ 204 public String getServerTimeZoneId(final FileSystemOptions opts) { 205 return getString(opts, SERVER_TIME_ZONE_ID); 206 } 207 208 /** 209 * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples. 210 * 211 * @param opts The FileSystemOptions. 212 * @return An array of short month names. 213 */ 214 public String[] getShortMonthNames(final FileSystemOptions opts) { 215 return (String[]) getParam(opts, SHORT_MONTH_NAMES); 216 } 217 218 /** 219 * @param opts The FileSystem options. 220 * @return The timeout value in milliseconds. 221 * @see #getDataTimeout 222 * @since 2.0 223 */ 224 public Integer getSoTimeout(final FileSystemOptions opts) { 225 return getInteger(opts, SO_TIMEOUT); 226 } 227 228 /** 229 * Returns {@link Boolean#TRUE} if VFS should treat the user directory as the root directory. Defaults to 230 * <code>Boolean.TRUE</code> if the method {@link #setUserDirIsRoot(FileSystemOptions, boolean)} has not been 231 * invoked. 232 * 233 * @param opts The FileSystemOptions. 234 * @return <code>Boolean.TRUE</code> if VFS treats the user directory as the root directory. 235 * @see #setUserDirIsRoot 236 */ 237 public Boolean getUserDirIsRoot(final FileSystemOptions opts) { 238 return getBoolean(opts, USER_DIR_IS_ROOT, Boolean.TRUE); 239 } 240 241 /** 242 * Sets the timeout for the initial control connection. 243 * <p> 244 * If you set the connectTimeout to {@code null} no connectTimeout will be set. 245 * 246 * @param opts The FileSystemOptions. 247 * @param connectTimeout the timeout value in milliseconds 248 * @since 2.1 249 */ 250 public void setConnectTimeout(final FileSystemOptions opts, final Integer connectTimeout) { 251 setParam(opts, CONNECT_TIMEOUT, connectTimeout); 252 } 253 254 /** 255 * See {@link org.apache.commons.net.ftp.FTP#setControlEncoding} for details and examples. 256 * 257 * @param opts The FileSystemOptions. 258 * @param encoding the encoding to use 259 * @since 2.0 260 */ 261 public void setControlEncoding(final FileSystemOptions opts, final String encoding) { 262 setParam(opts, ENCODING, encoding); 263 } 264 265 /** 266 * Set the data timeout for the ftp client. 267 * <p> 268 * If you set the {@code dataTimeout} to {@code null}, no dataTimeout will be set on the ftp client. 269 * 270 * @param opts The FileSystemOptions. 271 * @param dataTimeout The timeout value. 272 */ 273 public void setDataTimeout(final FileSystemOptions opts, final Integer dataTimeout) { 274 setParam(opts, DATA_TIMEOUT, dataTimeout); 275 } 276 277 /** 278 * Set the default date format used by the server. See {@link org.apache.commons.net.ftp.FTPClientConfig} for 279 * details and examples. 280 * 281 * @param opts The FileSystemOptions. 282 * @param defaultDateFormat The default date format. 283 */ 284 public void setDefaultDateFormat(final FileSystemOptions opts, final String defaultDateFormat) { 285 setParam(opts, DEFAULT_DATE_FORMAT, defaultDateFormat); 286 } 287 288 /** 289 * Set the FQCN of your FileEntryParser used to parse the directory listing from your server. 290 * <p> 291 * If you do not use the default commons-net FTPFileEntryParserFactory e.g. by using {@link #setEntryParserFactory} 292 * this is the "key" parameter passed as argument into your custom factory. 293 * 294 * @param opts The FileSystemOptions. 295 * @param key The key. 296 */ 297 public void setEntryParser(final FileSystemOptions opts, final String key) { 298 setParam(opts, FACTORY_KEY, key); 299 } 300 301 /** 302 * FTPFileEntryParserFactory which will be used for ftp-entry parsing. 303 * 304 * @param opts The FileSystemOptions. 305 * @param factory instance of your factory 306 */ 307 public void setEntryParserFactory(final FileSystemOptions opts, final FTPFileEntryParserFactory factory) { 308 setParam(opts, FTPFileEntryParserFactory.class.getName(), factory); 309 } 310 311 /** 312 * Sets the file type parameter. 313 * 314 * @param opts The FileSystemOptions. 315 * @param ftpFileType A FtpFileType 316 * @since 2.1 317 */ 318 public void setFileType(final FileSystemOptions opts, final FtpFileType ftpFileType) { 319 setParam(opts, FILE_TYPE, ftpFileType); 320 } 321 322 /** 323 * Enter into passive mode. 324 * 325 * @param opts The FileSystemOptions. 326 * @param passiveMode true if passive mode should be used. 327 */ 328 public void setPassiveMode(final FileSystemOptions opts, final boolean passiveMode) { 329 setParam(opts, PASSIVE_MODE, passiveMode ? Boolean.TRUE : Boolean.FALSE); 330 } 331 332 /** 333 * Sets the Proxy. 334 * <p> 335 * You might need to make sure that {@link #setPassiveMode(FileSystemOptions, boolean) passive mode} is activated. 336 * 337 * @param opts the FileSystem options. 338 * @param proxy the Proxy 339 * @since 2.1 340 */ 341 public void setProxy(final FileSystemOptions opts, final Proxy proxy) { 342 setParam(opts, PROXY, proxy); 343 } 344 345 /** 346 * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples. 347 * 348 * @param opts The FileSystemOptions. 349 * @param recentDateFormat The recent date format. 350 */ 351 public void setRecentDateFormat(final FileSystemOptions opts, final String recentDateFormat) { 352 setParam(opts, RECENT_DATE_FORMAT, recentDateFormat); 353 } 354 355 /** 356 * Sets whether to use remote verification. 357 * 358 * @param opts The FileSystemOptions. 359 * @param remoteVerification True if verification should be done. 360 */ 361 public void setRemoteVerification(final FileSystemOptions opts, final boolean remoteVerification) { 362 setParam(opts, REMOTE_VERIFICATION, remoteVerification); 363 } 364 365 /** 366 * Set the language code used by the server. See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and 367 * examples. 368 * 369 * @param opts The FileSystemOptions. 370 * @param serverLanguageCode The servers language code. 371 */ 372 public void setServerLanguageCode(final FileSystemOptions opts, final String serverLanguageCode) { 373 setParam(opts, SERVER_LANGUAGE_CODE, serverLanguageCode); 374 } 375 376 /** 377 * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples. 378 * 379 * @param opts The FileSystemOptions. 380 * @param serverTimeZoneId The server timezone id. 381 */ 382 public void setServerTimeZoneId(final FileSystemOptions opts, final String serverTimeZoneId) { 383 setParam(opts, SERVER_TIME_ZONE_ID, serverTimeZoneId); 384 } 385 386 /** 387 * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples. 388 * 389 * @param opts The FileSystemOptions. 390 * @param shortMonthNames an array of short month name Strings. 391 */ 392 public void setShortMonthNames(final FileSystemOptions opts, final String[] shortMonthNames) { 393 String[] clone = null; 394 if (shortMonthNames != null) { 395 clone = new String[shortMonthNames.length]; 396 System.arraycopy(shortMonthNames, 0, clone, 0, shortMonthNames.length); 397 } 398 399 setParam(opts, SHORT_MONTH_NAMES, clone); 400 } 401 402 /** 403 * Sets the socket timeout for the FTP client. 404 * <p> 405 * If you set the {@code soTimeout} to {@code null}, no socket timeout will be set on the ftp client. 406 * 407 * @param opts The FileSystem options. 408 * @param soTimeout The timeout value in milliseconds. 409 * @since 2.0 410 */ 411 public void setSoTimeout(final FileSystemOptions opts, final Integer soTimeout) { 412 setParam(opts, SO_TIMEOUT, soTimeout); 413 } 414 415 /** 416 * Use user directory as root (do not change to fs root). 417 * 418 * @param opts The FileSystemOptions. 419 * @param userDirIsRoot true if the user directory should be treated as the root. 420 */ 421 public void setUserDirIsRoot(final FileSystemOptions opts, final boolean userDirIsRoot) { 422 setParam(opts, USER_DIR_IS_ROOT, userDirIsRoot ? Boolean.TRUE : Boolean.FALSE); 423 } 424 425}