001: /**
002: *
003: * Copyright (C) 2007 Enterprise Distributed Technologies Ltd
004: *
005: * www.enterprisedt.com
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Bug fixes, suggestions and comments should be should posted on
022: * http://www.enterprisedt.com/forums/index.php
023: *
024: * Change Log:
025: *
026: * $Log: ConnectionContext.java,v $
027: * Revision 1.2 2007-12-20 00:40:16 bruceb
028: * autologin
029: *
030: * Revision 1.1 2007-12-18 07:52:06 bruceb
031: * 2.0 changes
032: *
033: *
034: */package com.enterprisedt.net.ftp;
035:
036: import java.util.Locale;
037:
038: /**
039: * Holds various parameters pertaining to the context of the connection. Used
040: * internally.
041: *
042: * @author Bruce Blackshaw
043: * @version $Revision: 1.2 $
044: */
045: public class ConnectionContext implements Cloneable {
046:
047: private String remoteHost;
048:
049: private String controlEncoding = FTPClient.DEFAULT_ENCODING;
050:
051: private int remotePort = FTPControlSocket.CONTROL_PORT;
052:
053: private int timeout = FTPClient.DEFAULT_TIMEOUT;
054:
055: private int transferNotifyInterval = FTPClient.DEFAULT_MONITOR_INTERVAL;
056:
057: /**
058: * Size of transfer buffers
059: */
060: protected int transferBufferSize = FTPClient.DEFAULT_BUFFER_SIZE;
061:
062: private String username;
063:
064: private String password;
065:
066: private FTPTransferType transferType = FTPTransferType.BINARY;
067:
068: private FTPConnectMode connectMode = FTPConnectMode.ACTIVE;
069:
070: private Locale[] parserLocales = FTPClient.DEFAULT_LISTING_LOCALES;
071:
072: private int lowPort = -1;
073:
074: private int highPort = -1;
075:
076: /**
077: * Use strict return codes if true
078: */
079: private boolean strictReturnCodes = false;
080:
081: /**
082: * If true, uses the original host IP if an internal IP address
083: * is returned by the server in PASV mode
084: */
085: private boolean autoPassiveIPSubstitution = true;
086:
087: /**
088: * Delete partial files on transfer failure?
089: */
090: private boolean deleteOnFailure = true;
091:
092: /**
093: * IP address to force to use in active mode
094: */
095: private String activeIP = null;
096:
097: /**
098: * If true, filetypes are autodetected and transfer mode changed to binary/ASCII as
099: * required
100: */
101: private boolean detectContentType = false;
102:
103: /**
104: * Listen to all interfaces in active mode
105: */
106: private boolean listenOnAllInterfaces = true;
107:
108: /**
109: * No explicit login required after connect
110: */
111: private boolean autoLogin = true;
112:
113: /**
114: * Matcher for directory empty
115: */
116: private DirectoryEmptyStrings dirEmptyStrings = new DirectoryEmptyStrings();
117:
118: /**
119: * Matcher for transfer complete
120: */
121: private TransferCompleteStrings transferCompleteStrings = new TransferCompleteStrings();
122:
123: /**
124: * Matcher for permission denied
125: */
126: private FileNotFoundStrings fileNotFoundStrings = new FileNotFoundStrings();
127:
128: public ConnectionContext() {
129: }
130:
131: public Object clone() {
132: // no mutable fields so just grab a copy
133: try {
134: return super .clone();
135: } catch (CloneNotSupportedException ignore) {
136: }
137: return null;
138: }
139:
140: /**
141: * Determine if auto login is switched on
142: *
143: * @return true if auto login
144: */
145: public boolean isAutoLogin() {
146: return autoLogin;
147: }
148:
149: /**
150: * Set the autoLogin flag
151: *
152: * @param autoLogin true if logging in automatically
153: */
154: public void setAutoLogin(boolean autoLogin) {
155: this .autoLogin = autoLogin;
156: }
157:
158: /**
159: * Set autodetect of filetypes on or off. If on, the transfer mode is
160: * switched from ASCII to binary and vice versa depending on the extension
161: * of the file. After the transfer, the mode is always returned to what it
162: * was before the transfer was performed. The default is off.
163: *
164: * If the filetype is unknown, the transfer mode is unchanged
165: *
166: * @param detectContentType true if detecting transfer mode, false if not
167: */
168: public synchronized void setDetectContentType(
169: boolean detectContentType) {
170: this .detectContentType = detectContentType;
171: }
172:
173: /**
174: * Get the detect content type flag
175: *
176: * @return true if we are detecting binary and ASCII transfers from the file type
177: */
178: public synchronized boolean getDetectContentType() {
179: return detectContentType;
180: }
181:
182: /**
183: * We can force PORT to send a fixed IP address, which can be useful with certain
184: * NAT configurations.
185: *
186: * @param activeIP IP address to force, in 192.168.1.0 form or in IPV6 form, e.g.
187: * 1080::8:800:200C:417A
188: */
189: public synchronized void setActiveIPAddress(String activeIP) {
190:
191: this .activeIP = activeIP;
192: }
193:
194: /**
195: * The active IP address being used, or null if not used
196: * @return IP address as a string or null
197: */
198: public synchronized String getActiveIPAddress() {
199: return activeIP;
200: }
201:
202: /**
203: * Force a certain range of ports to be used in active mode. This is
204: * generally so that a port range can be configured in a firewall. Note
205: * that if lowest == highest, a single port will be used. This works well
206: * for uploads, but downloads generally require multiple ports, as most
207: * servers fail to create a connection repeatedly for the same port.
208: *
209: * @param lowest Lower limit of range.
210: * @param highest Upper limit of range.
211: */
212: public synchronized void setActivePortRange(int lowest, int highest) {
213: this .lowPort = lowest;
214: this .highPort = highest;
215: }
216:
217: /**
218: * Get the lower limit of the port range for active mode.
219: *
220: * @return lower limit, or -1 if not set
221: */
222: public synchronized int getActiveLowPort() {
223: return lowPort;
224: }
225:
226: /**
227: * Get the upper limit of the port range for active mode.
228: *
229: * @return upper limit, or -1 if not set
230: */
231: public synchronized int getActiveHighPort() {
232: return highPort;
233: }
234:
235: /**
236: * Set strict checking of FTP return codes. If strict
237: * checking is on (the default) code must exactly match the expected
238: * code. If strict checking is off, only the first digit must match.
239: *
240: * @param strict true for strict checking, false for loose checking
241: */
242: public synchronized void setStrictReturnCodes(boolean strict) {
243: this .strictReturnCodes = strict;
244: }
245:
246: /**
247: * Determine if strict checking of return codes is switched on. If it is
248: * (the default), all return codes must exactly match the expected code.
249: * If strict checking is off, only the first digit must match.
250: *
251: * @return true if strict return code checking, false if non-strict.
252: */
253: public synchronized boolean isStrictReturnCodes() {
254: return strictReturnCodes;
255: }
256:
257: /**
258: * Is automatic substitution of the remote host IP set to
259: * be on for passive mode connections?
260: *
261: * @return true if set on, false otherwise
262: */
263: public synchronized boolean isAutoPassiveIPSubstitution() {
264: return autoPassiveIPSubstitution;
265: }
266:
267: /**
268: * Set automatic substitution of the remote host IP on if
269: * in passive mode
270: *
271: * @param autoPassiveIPSubstitution true if set to on, false otherwise
272: */
273: public synchronized void setAutoPassiveIPSubstitution(
274: boolean autoPassiveIPSubstitution) {
275: this .autoPassiveIPSubstitution = autoPassiveIPSubstitution;
276: }
277:
278: /**
279: * Listen on all interfaces for active mode transfers (the default).
280: *
281: * @param listenOnAll true if listen on all interfaces, false to listen on the control interface
282: */
283: public synchronized void setListenOnAllInterfaces(
284: boolean listenOnAll) {
285: listenOnAllInterfaces = listenOnAll;
286: }
287:
288: /**
289: * Are we listening on all interfaces in active mode, which is the default?
290: *
291: * @return true if listening on all interfaces, false if listening just on the control interface
292: */
293: public synchronized boolean getListenOnAllInterfaces() {
294: return listenOnAllInterfaces;
295: }
296:
297: /**
298: * If true, delete partially written files when exceptions are thrown
299: * during a download
300: *
301: * @return true if delete local file on error
302: */
303: public synchronized boolean isDeleteOnFailure() {
304: return deleteOnFailure;
305: }
306:
307: /**
308: * Switch on or off the automatic deletion of partially written files
309: * that are left when an exception is thrown during a download
310: *
311: * @param deleteOnFailure true if delete when a failure occurs
312: */
313: public synchronized void setDeleteOnFailure(boolean deleteOnFailure) {
314: this .deleteOnFailure = deleteOnFailure;
315: }
316:
317: public synchronized FTPConnectMode getConnectMode() {
318: return connectMode;
319: }
320:
321: public synchronized void setConnectMode(FTPConnectMode connectMode) {
322: this .connectMode = connectMode;
323: }
324:
325: public synchronized void setPassword(String password) {
326: this .password = password;
327: }
328:
329: public synchronized void setRemoteHost(String remoteHost) {
330: this .remoteHost = remoteHost;
331: }
332:
333: public synchronized void setRemotePort(int remotePort) {
334: this .remotePort = remotePort;
335: }
336:
337: public synchronized void setUserName(String username) {
338: this .username = username;
339: }
340:
341: public synchronized void setContentType(FTPTransferType transferType) {
342: this .transferType = transferType;
343: }
344:
345: public synchronized FTPTransferType getContentType() {
346: return transferType;
347: }
348:
349: public synchronized String getPassword() {
350: return password;
351: }
352:
353: public synchronized String getRemoteHost() {
354: return remoteHost;
355: }
356:
357: public synchronized int getRemotePort() {
358: return remotePort;
359: }
360:
361: /**
362: * Set the list of locales to be tried for date parsing of dir listings
363: *
364: * @param locales locales to use
365: */
366: public synchronized void setParserLocales(Locale[] locales) {
367: this .parserLocales = locales;
368: }
369:
370: public synchronized int getTimeout() {
371: return timeout;
372: }
373:
374: public synchronized void setTimeout(int timeout) {
375: this .timeout = timeout;
376: }
377:
378: public synchronized String getUserName() {
379: return username;
380: }
381:
382: public synchronized Locale[] getParserLocales() {
383: return parserLocales;
384: }
385:
386: /**
387: * Get the encoding used for the control connection
388: *
389: * @return Returns the current controlEncoding.
390: */
391: public synchronized String getControlEncoding() {
392: return controlEncoding;
393: }
394:
395: /**
396: * Set the encoding used for control channel messages
397: *
398: * @param controlEncoding The controlEncoding to set, which is the name of a Charset
399: */
400: public synchronized void setControlEncoding(String controlEncoding) {
401: this .controlEncoding = controlEncoding;
402: }
403:
404: /**
405: * Set the size of the buffers used in writing to and reading from
406: * the data sockets
407: *
408: * @param size new size of buffer in bytes
409: */
410: public void setTransferBufferSize(int size) {
411: transferBufferSize = size;
412: }
413:
414: /**
415: * Get the size of the buffers used in writing to and reading from
416: * the data sockets
417: *
418: * @return transfer buffer size
419: */
420: public int getTransferBufferSize() {
421: return transferBufferSize;
422: }
423:
424: /**
425: * Get the interval used for progress notification of transfers.
426: *
427: * @return number of bytes between each notification.
428: */
429: public synchronized int getTransferNotifyInterval() {
430: return transferNotifyInterval;
431: }
432:
433: /**
434: * Set the interval used for progress notification of transfers.
435: *
436: * @param notifyInterval number of bytes between each notification
437: */
438: public synchronized void setTransferNotifyInterval(
439: int notifyInterval) {
440: this .transferNotifyInterval = notifyInterval;
441: }
442:
443: /**
444: * Get class that holds fragments of server messages that indicate a file was
445: * not found. New messages can be added.
446: * <p>
447: * The fragments are used when it is necessary to examine the message
448: * returned by a server to see if it is saying a file was not found.
449: * If an FTP server is returning a different message that still clearly
450: * indicates a file was not found, use this property to add a new server
451: * fragment to the repository via the add method. It would be helpful to
452: * email support at enterprisedt dot com to inform us of the message so
453: * it can be added to the next build.
454: *
455: * @return messages class
456: */
457: public synchronized FileNotFoundStrings getFileNotFoundMessages() {
458: return fileNotFoundStrings;
459: }
460:
461: /**
462: * Get class that holds fragments of server messages that indicate a transfer completed.
463: * New messages can be added.
464: * <p>
465: * The fragments are used when it is necessary to examine the message
466: * returned by a server to see if it is saying a transfer completed.
467: * If an FTP server is returning a different message that still clearly
468: * indicates a transfer failed, use this property to add a new server
469: * fragment to the repository via the add method. It would be helpful to
470: * email support at enterprisedt dot com to inform us of the message so
471: * it can be added to the next build.
472: *
473: * @return messages class
474: */
475: public synchronized TransferCompleteStrings getTransferCompleteMessages() {
476: return transferCompleteStrings;
477: }
478:
479: /**
480: * Get class that holds fragments of server messages that indicate a
481: * directory is empty. New messages can be added.
482: * <p>
483: * The fragments are used when it is necessary to examine the message
484: * returned by a server to see if it is saying a directory is empty.
485: * If an FTP server is returning a different message that still clearly
486: * indicates a directory is empty, use this property to add a new server
487: * fragment to the repository via the add method. It would be helpful to
488: * email support at enterprisedt dot com to inform us of the message so
489: * it can be added to the next build.
490: *
491: * @return messages class
492: */
493: public synchronized DirectoryEmptyStrings getDirectoryEmptyMessages() {
494: return dirEmptyStrings;
495: }
496: }
|