001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011:
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016:
017: //TODO: Add SFTP port number here (convert potmp to a string and pass it
018: package net.sf.jftp.net;
019:
020: import net.sf.jftp.JFtp;
021: import net.sf.jftp.config.Settings;
022: import net.sf.jftp.gui.tasks.LastConnections;
023: import net.sf.jftp.system.logging.Log;
024:
025: // This class is used to initiate connections of all types (FTP, SFTP, SMB, NFS
026: // are currently supported.) Any time the user tries to open a connection using
027: // any protocol, this class is the intermediary between the GUI and the actual
028: // connection establishing classes. This puts much common functionality into
029: // one method (so in creating this I did some code cleanup.)
030: public class StartConnection {
031: public static FtpConnection con = null;
032: public static com.sshtools.j2ssh.configuration.SshConnectionProperties properties = new com.sshtools.j2ssh.configuration.SshConnectionProperties();
033: public static String keyfile = null;
034:
035: public static void setSshProperties(
036: com.sshtools.j2ssh.configuration.SshConnectionProperties props) {
037: properties = props;
038: }
039:
040: public static void setSshKeyfile(String file) {
041: keyfile = file;
042: }
043:
044: //data sent to startCon: protocol: (ie. FTP, SFTP, etc.)
045: // htmp: hostname
046: // utmp: username
047: // ptmp: password
048: // potmp: port
049: // dtmp: domain
050: // (null data is sent if it is ever not applicable)
051: //maybe it should just take an array of strings instead? (What's
052: //stored in the array can then be determined by reading the 1st
053: //entry, which is the protocol name)
054: public static boolean startCon(String protocol, String htmp,
055: String utmp, String ptmp, int potmp, String dtmp,
056: boolean useLocal) {
057: // I may need a series of If statements following this one
058: // declaring con for each of the possible types of connections
059: //FtpConnection con = null;
060: // ie.: if (protocol=="SFTP) SftpConnection con = null;
061: //NfsConnection con = null;
062: String conType; // this is for error msgs: may not be needed
063:
064: String[] searchValue = new String[JFtp.CONNECTION_DATA_LENGTH];
065:
066: Integer potmpInt = new Integer(potmp);
067:
068: String potmpString = potmpInt.toString();
069: String useLocalString = new String("false");
070:
071: if (useLocal) {
072: useLocalString = "true";
073: }
074:
075: //*** FTP section: deprecated
076: if (protocol.equals("FTP")) {
077: /*
078:
079: */
080: } else if (protocol.equals("SFTP")) {
081: SftpConnection con;
082:
083: //Log.debug(htmp+":"+properties.getHost());
084: //properties.setHost(htmp);
085: con = new SftpConnection(properties, keyfile);
086:
087: //con.setLocalPath(JFtp.localDir.getCon().getPWD());
088: //con.addConnectionListener((ConnectionListener) JFtp.localDir);
089: //con.addConnectionListener((ConnectionListener) JFtp.remoteDir);
090: //JFtp.statusP.jftp.addConnection(htmp, con);
091: if (con.login(utmp, ptmp)) {
092: if (useLocal) {
093: JFtp.statusP.jftp.addLocalConnection(htmp, con);
094: } else {
095: JFtp.statusP.jftp.addConnection(htmp, con);
096: }
097:
098: //JFtp.remoteDir.setCon(con);
099: if (con.chdir(con.getPWD()) || con.chdir("/")) {
100: ;
101: }
102:
103: //BUGFIX
104: searchValue[0] = "SFTP";
105: searchValue[1] = htmp;
106: searchValue[2] = utmp;
107:
108: if (Settings.getStorePasswords()) {
109: searchValue[3] = ptmp;
110: } else {
111: searchValue[3] = "";
112: }
113:
114: //BUGFIX 1.40: now accepts string
115: //for port number
116: searchValue[4] = potmpString;
117:
118: searchValue[5] = useLocalString;
119:
120: searchValue[6] = LastConnections.SENTINEL;
121:
122: //searchValue = "SFTP " + htmp + " " + utmp + " " +
123: // ptmp + " " + useLocalString;
124: updateFileMenu(searchValue);
125:
126: return true;
127: }
128:
129: //if
130: } else if (protocol.equals("SMB")) {
131: SmbConnection con = null;
132:
133: try {
134: con = new SmbConnection(htmp, dtmp, utmp, ptmp,
135: ((ConnectionListener) JFtp.remoteDir));
136:
137: //JFtp.statusP.jftp.addConnection(htmp, con);
138: if (useLocal) {
139: JFtp.statusP.jftp.addLocalConnection(htmp, con);
140: JFtp.localDir.fresh();
141: } else {
142: JFtp.statusP.jftp.addConnection(htmp, con);
143: JFtp.remoteDir.fresh();
144: }
145:
146: //JFtp.remoteDir.setCon(con);
147: //con.setLocalPath(JFtp.localDir.getCon().getPWD());
148: //con.addConnectionListener((ConnectionListener) JFtp.localDir);
149: //con.addConnectionListener((ConnectionListener) JFtp.remoteDir);
150: //JFtp.remoteDir.fresh();
151: //BUGFIX
152: searchValue[0] = "SMB";
153: searchValue[1] = htmp;
154: searchValue[2] = utmp;
155:
156: if (Settings.getStorePasswords()) {
157: searchValue[3] = ptmp;
158: } else {
159: searchValue[3] = "";
160: }
161:
162: searchValue[4] = dtmp;
163: searchValue[5] = useLocalString;
164: searchValue[6] = LastConnections.SENTINEL;
165:
166: //searchValue = "SMB " + htmp + " " + utmp + " " +
167: // ptmp + " " + dtmp + " " + useLocalString;
168: updateFileMenu(searchValue);
169:
170: return true;
171: } catch (Exception ex) {
172: Log
173: .debug("Could not create SMBConnection, does this distribution come with jcifs?");
174: }
175: }
176:
177: //can assume any other connection is NFS for now
178: else {
179: NfsConnection con;
180:
181: //***
182: boolean status = true;
183:
184: //***
185: con = new NfsConnection(htmp);
186:
187: //JFtp.remoteDir.setCon(con);
188: //con.addConnectionListener(((ConnectionListener)JFtp.remoteDir));
189: //JFtp.statusP.jftp.addConnection(htmp, con);
190: if (!utmp.equals("<anonymous>")) {
191: status = ((NfsConnection) con).login(utmp, ptmp);
192: }
193:
194: if (useLocal) {
195: con.setLocalPath("/");
196: JFtp.statusP.jftp.addLocalConnection(htmp, con);
197: } else {
198: JFtp.statusP.jftp.addConnection(htmp, con);
199: }
200:
201: con.chdir(htmp);
202:
203: //con.setLocalPath(JFtp.localDir.getCon().getPWD());
204: //con.addConnectionListener((ConnectionListener) JFtp.localDir);
205: //BUGFIX
206: searchValue[0] = "NFS";
207: searchValue[1] = htmp;
208: searchValue[2] = utmp;
209:
210: if (Settings.getStorePasswords()) {
211: searchValue[3] = ptmp;
212: } else {
213: searchValue[3] = "";
214: }
215:
216: searchValue[4] = useLocalString;
217: searchValue[5] = LastConnections.SENTINEL;
218:
219: //searchValue = "NFS " + htmp + " " + utmp + " " +
220: // ptmp + " " + useLocalString;
221: /*
222: searchValue = "NFS " + htmp + " " + utmp + " " +
223: ptmp + " " + potmpString + " " + dtmp
224: + " " + useLocalString;
225: */
226: updateFileMenu(searchValue);
227:
228: return status;
229: }
230:
231: /*
232: //Common functionality goes here
233:
234: try {
235: //if statement here, select connection type (or should
236: // a superclass be used?)
237: con = new NfsConnection(htmp);
238:
239: JFtp.statusP.jftp.addConnection(htmp, con);
240: } catch (Exception ex) {
241: Log.debug("Could not create " + protocol + "Connection!");
242: }
243:
244: */
245:
246: //return true only if all has executed as expected
247: return true;
248: }
249:
250: public static int startFtpCon(String htmp, String utmp,
251: String ptmp, int potmp, String dtmp, boolean useLocal) {
252: return startFtpCon(htmp, utmp, ptmp, potmp, dtmp, useLocal,
253: null);
254: }
255:
256: //startCon
257: public static int startFtpCon(String htmp, String utmp,
258: String ptmp, int potmp, String dtmp, boolean useLocal,
259: String crlf) {
260: boolean pasv = Settings.getFtpPasvMode();
261: boolean threads = Settings.getEnableMultiThreading();
262:
263: //BUGFIX: this is now an array
264: //String searchValue = new String("");
265: String[] searchValue = new String[JFtp.CONNECTION_DATA_LENGTH];
266:
267: //BELOW IS GUI-related stuff... should put in separate
268: //method in GUI dir?
269: /*
270: if(!pasv && threads)
271: { // only passive ftp threading works
272: //Settings.setProperty("jftp.enableMultiThreading", false);
273:
274: JDialog j = new JDialog();
275: j.setTitle("Warning");
276: j.setLocation(150, 150);
277: j.setSize(450, 100);
278: j.getContentPane().add(new JLabel(" Multithreading in active mode is EXPERIMENTAL"));
279: j.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
280: j.setModal(true);
281: j.show();
282: }
283: */
284: //***also need to get max connections!!
285: con = new FtpConnection(htmp, potmp, dtmp, crlf);
286:
287: //con.addConnectionListener((ConnectionListener) JFtp.localDir);
288: //con.addConnectionListener((ConnectionListener) JFtp.remoteDir);
289: //JFtp.remoteDir.setCon(con);
290: if (useLocal) {
291: JFtp.statusP.jftp.addLocalConnection(htmp, con);
292: } else {
293: JFtp.statusP.jftp.addConnection(htmp, con);
294: }
295:
296: //System.out.println("**" + htmp + potmp + dtmp);
297: //System.out.println("**" + utmp + ptmp);
298: int response = con.login(utmp, ptmp);
299:
300: //boolean isConnected = false;
301: if (response == FtpConnection.LOGIN_OK) {
302: //System.out.println(htmp + " " + potmp + " " + dtmp);
303:
304: /*
305: if (useLocal)
306: JFtp.statusP.jftp.addLocalConnection(
307: htmp, con);
308: else
309: JFtp.statusP.jftp.addConnection(htmp, con);
310:
311: */
312:
313: //String searchValue = new String("");
314: Integer potmpInt = new Integer(potmp);
315:
316: String potmpString = potmpInt.toString();
317: String useLocalString = new String("false");
318:
319: if (useLocal) {
320: useLocalString = "true";
321: }
322:
323: //BUGFIX
324: /*
325: searchValue = "FTP " + htmp + " " + utmp + " " +
326: ptmp + " " + potmpString + " " + dtmp
327: + " " + useLocalString;
328: */
329: searchValue[0] = "FTP";
330: searchValue[1] = htmp;
331: searchValue[2] = utmp;
332:
333: if (Settings.getStorePasswords()) {
334: searchValue[3] = ptmp;
335: } else {
336: searchValue[3] = "";
337: }
338:
339: searchValue[4] = potmpString;
340: searchValue[5] = dtmp;
341: searchValue[6] = useLocalString;
342:
343: searchValue[7] = LastConnections.SENTINEL;
344: updateFileMenu(searchValue);
345: }
346:
347: //*** JUST FOR NOW: if not connected, take out the tabs
348: else {
349: if (useLocal) {
350: JFtp.statusP.jftp.closeCurrentLocalTab();
351: } else {
352: JFtp.statusP.jftp.closeCurrentTab();
353: }
354: }
355:
356: return response; //*** code that indicates successful connection
357: }
358:
359: //startFtpCon
360: private static void updateFileMenu(String[] searchValue) {
361: int position;
362:
363: position = LastConnections.findString(searchValue,
364: JFtp.CAPACITY);
365:
366: //bugfix: now a 2D array
367: String[][] newVals = new String[JFtp.CAPACITY][JFtp.CONNECTION_DATA_LENGTH];
368:
369: if (position >= 0) {
370: //System.out.println(JFtp.CAPACITY);
371: //System.out.println("IT WAS FOUND");
372: //System.out.println(position);
373: newVals = LastConnections.moveToFront(position,
374: JFtp.CAPACITY);
375: } else {
376: //System.out.println("IT WASN'T FOUND");
377: newVals = LastConnections.prepend(searchValue,
378: JFtp.CAPACITY, true);
379: }
380: }
381:
382: //updateFileMenu
383: }
384:
385: //StartConnection
|