1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package com.mindtree.techworks.insight.download;
26
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.net.MalformedURLException;
33 import java.net.URL;
34
35 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
36 import org.apache.commons.httpclient.HostConfiguration;
37 import org.apache.commons.httpclient.HttpClient;
38 import org.apache.commons.httpclient.HttpException;
39 import org.apache.commons.httpclient.HttpStatus;
40 import org.apache.commons.httpclient.NTCredentials;
41 import org.apache.commons.httpclient.auth.AuthScope;
42 import org.apache.commons.httpclient.methods.GetMethod;
43 import org.apache.commons.httpclient.params.HttpMethodParams;
44
45 import com.mindtree.techworks.insight.download.ProxyInfo.ProtocolProxyInfo;
46 import com.mindtree.techworks.insight.preferences.util.PreferenceInterpreter;
47
48
49
50
51
52
53
54
55
56 public class HTTPRemoteClient extends RemoteClient {
57
58
59
60
61 static {
62
63 RemoteClientFactory.registerClient("HTTP_CLIENT",
64 HTTPRemoteClient.class);
65 }
66
67
68
69
70
71
72
73
74 private HTTPFileset fileset;
75
76
77
78
79 private HttpClient httpClient;
80
81
82
83
84
85
86
87
88 protected void setFileset (Fileset fileset) throws RemoteClientException {
89
90 if (fileset.getType() != Fileset.HTTP_FILESET
91 || !(fileset instanceof HTTPFileset)) {
92 throw new RemoteClientException(
93 "This client can handle only HTTPFileset");
94 }
95 this.fileset = (HTTPFileset) fileset;
96
97
98 initialize();
99
100 }
101
102
103
104
105
106
107
108 protected String[] downloadFile (String fileName)
109 throws RemoteClientException {
110
111
112 isBusy = true;
113
114
115 File destinationFile = null;
116 FileOutputStream fos = null;
117 InputStream is = null;
118
119
120 setAuthorization(fileName);
121
122
123 try {
124 destinationFile = getDestinationFile(fileName, '/');
125 fos = new FileOutputStream(destinationFile);
126 } catch (FileNotFoundException e) {
127
128 throw new RemoteClientException(
129 "Could not write to temporary file.", e);
130 } catch (IOException e) {
131 throw new RemoteClientException("Could not create temporary file.",
132 e);
133 }
134
135
136 GetMethod getMethod = new GetMethod(fileName);
137
138
139 DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(0, false);
140 httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);
141
142 try {
143
144 int statusCode = httpClient.executeMethod(getMethod);
145
146
147 if (statusCode != HttpStatus.SC_OK) {
148 throw new RemoteClientException(
149 "Could not complete HTTP GET : "
150 + getMethod.getStatusLine());
151 }
152
153 is = getMethod.getResponseBodyAsStream();
154 int byteToRead;
155 byteToRead = is.read();
156 while (byteToRead != -1) {
157 fos.write(byteToRead);
158 byteToRead = is.read();
159 }
160
161 } catch (HttpException e) {
162 throw new RemoteClientException("Could not complete HTTP GET : "
163 + e.getMessage(), e);
164 } catch (IOException e) {
165 throw new RemoteClientException("Could not complete HTTP GET : "
166 + e.getMessage(), e);
167 } finally {
168 getMethod.releaseConnection();
169 try {
170 fos.close();
171 } catch (IOException ioe) {
172
173 ioe.printStackTrace(System.err);
174 }
175 try {
176 if (null != is) {
177 is.close();
178 }
179 } catch (IOException ioe) {
180
181 ioe.printStackTrace(System.err);
182 }
183 isBusy = false;
184 }
185
186
187
188 String[] stringArray = new String[1];
189 stringArray[0] = destinationFile.getAbsolutePath();
190
191 return stringArray;
192 }
193
194
195
196
197 protected void closeConnection () throws RemoteClientException {
198
199
200
201 }
202
203
204
205
206
207
208
209
210 private void initialize () {
211
212 HostConfiguration hostConfiguration = new HostConfiguration();
213 httpClient = new HttpClient();
214
215
216 if (!fileset.isIgnoreProxy()) {
217 ProxyInfo proxyInfo = PreferenceInterpreter.getProxyInfo();
218 ProtocolProxyInfo httpProxyInfo = proxyInfo.getHttpProxyInfo();
219
220 if (null != httpProxyInfo && httpProxyInfo.isSet()) {
221 hostConfiguration.setProxy(httpProxyInfo.getHost(),
222 httpProxyInfo.getPort());
223
224 if (proxyInfo.isAuthenticationRequired()) {
225
226 NTCredentials credentials = getNTCredentials(proxyInfo
227 .getPasswordInformation().getUserName(),
228 new StringBuffer().append(
229 proxyInfo.getPasswordInformation()
230 .getPassword()).toString(),
231 httpProxyInfo.getHost());
232
233
234 AuthScope authscope = new AuthScope(httpProxyInfo.getHost(), httpProxyInfo.getPort());
235 httpClient.getState().setProxyCredentials(authscope, credentials);
236 }
237 }
238 }
239
240 httpClient.setHostConfiguration(hostConfiguration);
241 }
242
243
244
245
246
247
248
249
250
251
252
253 private void setAuthorization (String url) {
254
255
256 if (fileset.isAuthenticationRequired()) {
257 try {
258 URL theURL = new URL(url);
259 NTCredentials credentials = getNTCredentials(fileset
260 .getUserName(), fileset.getPassword(), theURL.getHost());
261 AuthScope authScope = new AuthScope(theURL.getHost(), (theURL
262 .getPort() == -1) ? theURL.getDefaultPort() : theURL
263 .getPort());
264 httpClient.getState().setCredentials(authScope, credentials);
265 } catch (MalformedURLException e) {
266
267 e.printStackTrace(System.err);
268 }
269 }
270 }
271
272
273
274
275
276
277
278
279
280
281
282 private NTCredentials getNTCredentials (String user, String password,
283 String host) {
284
285 String domain = null;
286 String userName = user;
287
288
289
290
291 if (userName.indexOf('\\') > -1) {
292 domain = userName.substring(0, userName.indexOf('\\'));
293 userName = userName.substring(userName.indexOf('\\') + 1);
294 }
295
296 return new NTCredentials(userName, password, host, domain);
297 }
298
299 }