View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  
25  package com.mindtree.techworks.insight.download.ftpbrowse.test;
26  
27  import java.awt.Dimension;
28  import java.awt.HeadlessException;
29  import java.awt.Rectangle;
30  import java.awt.event.WindowAdapter;
31  import java.awt.event.WindowEvent;
32  import java.io.File;
33  import java.io.FileOutputStream;
34  import java.io.IOException;
35  import java.net.InetAddress;
36  import java.net.MalformedURLException;
37  import java.net.PasswordAuthentication;
38  import java.net.SocketException;
39  import java.net.URL;
40  import java.net.UnknownHostException;
41  import java.util.logging.LogManager;
42  import java.util.logging.Logger;
43  
44  import javax.swing.JFileChooser;
45  import javax.swing.JFrame;
46  
47  import org.apache.commons.net.ftp.FTPClient;
48  import org.apache.commons.net.ftp.FTPReply;
49  
50  import com.mindtree.techworks.insight.download.ftpbrowse.FTPBrowseException;
51  import com.mindtree.techworks.insight.download.ftpbrowse.FTPFileFile;
52  import com.mindtree.techworks.insight.download.ftpbrowse.FTPRemoteFileSystemView;
53  import com.mindtree.techworks.insight.gui.widgets.InsightRemoteFileChooser;
54  
55  /**
56   * TODO
57   * 
58   * 
59   * 
60   * @author Bindul Bhowmik
61   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
62   *  
63   */
64  public class FileChooserTestFrame extends JFrame {
65  	
66  	/**
67  	 * Used for object serialization
68  	 */
69  	private static final long serialVersionUID = 7240401258426606510L;
70  	
71  	/**
72  	 * Logger
73  	 */
74  	private static final Logger logger = Logger.getLogger(FileChooserTestFrame.class.getName());
75  
76  	/**
77  	 * @throws java.awt.HeadlessException
78  	 * @throws MalformedURLException
79  	 */
80  	public FileChooserTestFrame() throws HeadlessException,
81  			MalformedURLException {
82  
83  		super();
84  		//        this.setSize(500, 300);
85  		addWindowListener(new WindowAdapter() {
86  			public void windowClosing(WindowEvent aEvent) {
87  				System.exit(0);
88  			}
89  		});
90  		Dimension dim = getToolkit().getScreenSize();
91  		Rectangle abounds = getBounds();
92  		setLocation((dim.width - abounds.width) / 2,
93  				(dim.height - abounds.height) / 2);
94  		setVisible(true);
95  
96  		URL url = new URL("ftp://cendantstp/");
97  //		URL url = new URL("ftp://dt1241-bindulb");
98  		char[] password = "spnr".toCharArray();
99  		PasswordAuthentication passwordAuthentication = new PasswordAuthentication("spnr", password);
100 //		PasswordAuthentication passwordAuthentication = new PasswordAuthentication("anonymous", "abc".toCharArray());
101 
102 		FTPRemoteFileSystemView remoteFileSystemView = new FTPRemoteFileSystemView(
103 				url, passwordAuthentication);
104 
105 		JFileChooser fileChooser = new InsightRemoteFileChooser(remoteFileSystemView);
106 		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
107 		fileChooser.setMultiSelectionEnabled(true);
108 //		fileChooser.showOpenDialog(this);
109 		File[] selectedFiles = null;
110 		if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
111 			selectedFiles = fileChooser.getSelectedFiles();
112 			for (int i = 0; i < selectedFiles.length; i++ ) {
113 				if (selectedFiles[i] instanceof FTPFileFile) {
114 					FTPFileFile ftpFile = (FTPFileFile) selectedFiles[i];
115 					logger.fine(ftpFile.getName());
116 					logger.fine(ftpFile.getPath());
117 				} else {
118 					logger.fine(selectedFiles[i].toString());
119 					logger.fine(selectedFiles[i].getAbsolutePath());
120 				}
121 			}
122 		}
123 		remoteFileSystemView.disconnect();
124 		try {
125 		if (null != selectedFiles) {
126 			FTPClient ftpClient = new FTPClient();
127 			InetAddress inetAddress = InetAddress.getByName(url.getHost());
128 			ftpClient.connect(inetAddress);
129 			if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
130 				throw new FTPBrowseException(ftpClient.getReplyString());
131 			}
132 			if (null != passwordAuthentication) {
133 				ftpClient.login(passwordAuthentication.getUserName(),
134 						new StringBuffer().append(
135 								passwordAuthentication.getPassword())
136 								.toString());
137 			}
138 //			String parentDir = System.getProperty("user.dir");
139 			for (int i = 0; i < selectedFiles.length; i++ ) {
140 				FTPFileFile file = (FTPFileFile)selectedFiles[i];
141 				logger.fine(file.getPath());
142 				FileOutputStream fos = new FileOutputStream(new File("d:/junk/ftp/test.txt" ));
143 				logger.fine("" + ftpClient.retrieveFile(file.getPath().replaceAll("\\\\","/"), fos));
144 				fos.close();
145 			}
146 		}
147 		} catch (UnknownHostException e) {
148 			e.printStackTrace();
149 		} catch (SocketException e) {
150 			e.printStackTrace();
151 		} catch (IOException e) {
152 			e.printStackTrace();
153 		}
154 		
155 		System.exit(0);
156 	}
157 
158 	/**
159 	 * Summa method
160 	 * @param args
161 	 */
162 	public static void main(String[] args) {
163 		/*
164 		 * Logging properties
165 		 */
166 		LogManager logManager = LogManager.getLogManager();
167 		try {
168 			logManager
169 					.readConfiguration(FileChooserTestFrame.class.getClassLoader()
170 							.getResourceAsStream("com/mindtree/logging/download/ftpbrowse/test/logging.properties"));
171 		} catch (SecurityException e1) {
172 			// TODO Auto-generated catch block
173 			e1.printStackTrace();
174 		} catch (IOException e1) {
175 			// TODO Auto-generated catch block
176 			e1.printStackTrace();
177 		}
178 		//		FTPClient ftpClient = new FTPClient();
179 		//		try {
180 		//			ftpClient.connect("127.0.0.1");
181 		//// ftpClient.login("mindtree\bindulbhowmik", "!MindTree+");
182 		//			ftpClient.login("spnr", "spnr");
183 		//			System.out.println(ftpClient.getSystemName());
184 		//// ftpClient.sendCommand(FTPCommand.PWD);
185 		//// System.out.println(ftpClient.getReplyString());
186 		//			String root= "/";
187 		//			ftpClient.changeWorkingDirectory(root);
188 		//			ftpClient.changeToParentDirectory();
189 		//			System.out.println(ftpClient.printWorkingDirectory());
190 		//// ftpClient.changeWorkingDirectory("/cvs_backup");
191 		//			System.out.println(ftpClient.getReplyString());
192 		//// ftpClient.changeWorkingDirectory("/var/ftp/build/jars");
193 		//			FTPListParseEngine ftpListParseEngine =
194 		// ftpClient.initiateListParsing();
195 		//			FTPFile [] files = ftpListParseEngine.getFiles();
196 		//			for (int i = 0; i < files.length; i++ ) {
197 		//				System.out.println(files[i].toString());
198 		//				System.out.println(files[i].isDirectory() + "\t" +
199 		// files[i].isFile());
200 		//			}
201 		//			ftpClient.disconnect();
202 		//		} catch (SocketException e) {
203 		//			// TODO Auto-generated catch block
204 		//			e.printStackTrace();
205 		//		} catch (IOException e) {
206 		//			// TODO Auto-generated catch block
207 		//			e.printStackTrace();
208 		//		}
209 
210 		try {
211 			FileChooserTestFrame fileChooserTestFrame = new FileChooserTestFrame();
212 			fileChooserTestFrame.getHeight();
213 		} catch (HeadlessException e) {
214 			// TODO Auto-generated catch block
215 			e.printStackTrace();
216 		} catch (MalformedURLException e) {
217 			// TODO Auto-generated catch block
218 			e.printStackTrace();
219 		}
220 	}
221 }