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;
26  
27  import java.net.MalformedURLException;
28  import java.net.PasswordAuthentication;
29  import java.net.URL;
30  
31  
32  /**
33   * This is a specialization of the <code>Fileset</code> class to contain
34   * information such as the host, port, etc., about a SFTPFileset.
35   * 
36   * @see com.mindtree.techworks.insight.download.Fileset Fileset
37   * @author Regunath B
38   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
39   */
40  public class SFTPFileset extends Fileset {
41  
42  	//
43  	// Constants
44  	//
45  
46  	/**
47  	 * The name of the SFTP Protocol
48  	 */
49  	public static final String SFTP_PROTOCOL = "sftp";
50  
51  	/**
52  	 * The default SFTP Port (22)
53  	 */
54  	public static final int SFTP_DEFAULT_PORT = 22;
55  
56  	//
57  	// Instance variables
58  	//
59  
60  	/**
61  	 * The host to connect to
62  	 */
63  	private String host;
64  
65  	/**
66  	 * The port to connect to the remote machine. By default, this is
67  	 * initialized to the SFTP Default Port 22.
68  	 */
69  	private int port = SFTP_DEFAULT_PORT;
70  
71  	/**
72  	 * The default directory on the remote machine.
73  	 */
74  	private String defaultDirectory;
75  
76  	/**
77  	 * Specifies if authentication is required to connect to SFTP location
78  	 */
79  	private boolean isAuthenticationRequired;
80  
81  	/**
82  	 * The user name that will be used when connecting to the specified SFTP
83  	 * location
84  	 */
85  	private String userName;
86  
87  	/**
88  	 * The password that will be used when connecting to the specified SFTP
89  	 * location
90  	 */
91  	private String password;
92  
93  	/**
94  	 * Specifies if the SFTP location is to be accessed over the Proxy
95  	 */
96  	private boolean isProxyRequired;
97  
98  	//
99  	// Constructors
100 	//
101 
102 	/**
103 	 * Constructs a new FTPFileset object with just the name of the fileset.
104 	 * 
105 	 * @param filesetName The name of the fileset.
106 	 */
107 	public SFTPFileset (String filesetName) {
108 
109 		super(filesetName, Fileset.SFTP_FILESET, "SFTP_CLIENT");
110 	}
111 
112 
113 	/**
114 	 * Constructs a new SFTPFileSet object with the parameters supplied.
115 	 * 
116 	 * @param filesetName The name of the fileset.
117 	 * @param host The host to connect to
118 	 * @param port The port to connect to. (Default: 22)
119 	 * @param defaultDirectory The default directory on the remote machine.
120 	 * @param isAuthenticationRequired If authentication is required.
121 	 * @param userName The username to use
122 	 * @param password The password for the username
123 	 * @param isProxyRequired If proxy is required
124 	 */
125 	public SFTPFileset (String filesetName, String host, int port,
126 			String defaultDirectory, boolean isAuthenticationRequired,
127 			String userName, String password, boolean isProxyRequired) {
128 
129 		super(filesetName, Fileset.SFTP_FILESET, "SFTP_CLIENT");
130 		this.host = host;
131 		this.port = port;
132 		this.defaultDirectory = defaultDirectory;
133 		this.isAuthenticationRequired = isAuthenticationRequired;
134 		this.userName = userName;
135 		this.password = password;
136 		this.isProxyRequired = isProxyRequired;
137 	}
138 
139 	//
140 	// Accessor methods
141 	//
142 
143 	/**
144 	 * @return Returns the defaultDirectory.
145 	 */
146 	public String getDefaultDirectory () {
147 
148 		return defaultDirectory;
149 	}
150 
151 	/**
152 	 * @param defaultDirectory The defaultDirectory to set.
153 	 */
154 	public void setDefaultDirectory (String defaultDirectory) {
155 
156 		this.defaultDirectory = defaultDirectory;
157 	}
158 
159 	/**
160 	 * @return Returns the host.
161 	 */
162 	public String getHost () {
163 
164 		return host;
165 	}
166 
167 	/**
168 	 * @param host The host to set.
169 	 */
170 	public void setHost (String host) {
171 
172 		this.host = host;
173 	}
174 
175 	/**
176 	 * @return Returns the isAuthenticationRequired.
177 	 */
178 	public boolean isAuthenticationRequired () {
179 
180 		return isAuthenticationRequired;
181 	}
182 
183 	/**
184 	 * @param isAuthenticationRequired The isAuthenticationRequired to set.
185 	 */
186 	public void setAuthenticationRequired (boolean isAuthenticationRequired) {
187 
188 		this.isAuthenticationRequired = isAuthenticationRequired;
189 	}
190 
191 	/**
192 	 * @return Returns the isProxyRequired.
193 	 */
194 	public boolean isProxyRequired () {
195 
196 		return isProxyRequired;
197 	}
198 
199 	/**
200 	 * @param isProxyRequired The isProxyRequired to set.
201 	 */
202 	public void setProxyRequired (boolean isProxyRequired) {
203 
204 		this.isProxyRequired = isProxyRequired;
205 	}
206 
207 	/**
208 	 * @return Returns the password.
209 	 */
210 	public String getPassword () {
211 
212 		return password;
213 	}
214 
215 	/**
216 	 * @param password The password to set.
217 	 */
218 	public void setPassword (String password) {
219 
220 		this.password = password;
221 	}
222 
223 	/**
224 	 * @return Returns the port.
225 	 */
226 	public int getPort () {
227 
228 		return port;
229 	}
230 
231 	/**
232 	 * @param port The port to set.
233 	 */
234 	public void setPort (int port) {
235 
236 		this.port = port;
237 	}
238 
239 	/**
240 	 * @return Returns the userName.
241 	 */
242 	public String getUserName () {
243 
244 		return userName;
245 	}
246 
247 	/**
248 	 * @param userName The userName to set.
249 	 */
250 	public void setUserName (String userName) {
251 
252 		this.userName = userName;
253 	}
254 
255 	//
256 	// Utility Methods
257 	//
258 
259 	/**
260 	 * Returns the URL form of the host information
261 	 * 
262 	 * @return An url for the host information
263 	 * @throws MalformedURLException If the URL cannot be formed.
264 	 */
265 	public URL getHostURL () throws MalformedURLException {
266 
267 		URL hostURL = null;
268 		if (null == defaultDirectory) {
269 			hostURL = new URL(SFTP_PROTOCOL, host, port, "");
270 		} else {
271 			hostURL = new URL(SFTP_PROTOCOL, host, port, defaultDirectory);
272 		}
273 		return hostURL;
274 	}
275 
276 	/**
277 	 * Returns the authentication information encapsulated in a
278 	 * <code>PasswordAuthentication</code> object. It returns null, if
279 	 * authentication is not required or either userName or password are null.
280 	 * 
281 	 * @return A <code>PasswordAuthentication</code> object for the
282 	 *         <code>userName</code> and <code>password</code> supplied.
283 	 */
284 	public PasswordAuthentication getPasswordAuthentication () {
285 
286 		if (isAuthenticationRequired && null != userName && null != password) {
287 			return new PasswordAuthentication(userName, password.toCharArray());
288 		} else {
289 			return null;
290 		}
291 	}
292 }