|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2010 Composent, Inc., IBM All rights reserved. This |
| 3 |
* program and the accompanying materials are made available under the terms of |
| 4 |
* the Eclipse Public License v1.0 which accompanies this distribution, and is |
| 5 |
* available at http://www.eclipse.org/legal/epl-v10.html |
| 6 |
* |
| 7 |
* Contributors: |
| 8 |
* Composent, Inc. - initial API and implementation |
| 9 |
* Maarten Meijer - bug 237936, added gzip encoded transfer default |
| 10 |
* Henrich Kraemer - bug 263869, testHttpsReceiveFile fails using HTTP proxy |
| 11 |
* Henrich Kraemer - bug 263613, [transport] Update site contacting / downloading is not cancelable |
| 12 |
* Thomas Joiner - HttpClient 4 implementation |
| 13 |
******************************************************************************/ |
| 14 |
package org.eclipse.ecf.provider.filetransfer.httpclient; |
| 15 |
|
| 16 |
import java.io.IOException; |
| 17 |
import java.io.InputStream; |
| 18 |
import java.net.HttpURLConnection; |
| 19 |
import java.net.InetAddress; |
| 20 |
import java.net.Socket; |
| 21 |
import java.net.UnknownHostException; |
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.Collections; |
| 24 |
import java.util.HashMap; |
| 25 |
import java.util.Iterator; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Map; |
| 28 |
import javax.net.SocketFactory; |
| 29 |
import javax.net.ssl.SSLSocketFactory; |
| 30 |
import org.apache.http.Header; |
| 31 |
import org.apache.http.HttpHost; |
| 32 |
import org.apache.http.HttpResponse; |
| 33 |
import org.apache.http.ProtocolVersion; |
| 34 |
import org.apache.http.auth.AuthScope; |
| 35 |
import org.apache.http.auth.Credentials; |
| 36 |
import org.apache.http.auth.NTCredentials; |
| 37 |
import org.apache.http.auth.UsernamePasswordCredentials; |
| 38 |
import org.apache.http.auth.params.AuthPNames; |
| 39 |
import org.apache.http.client.methods.HttpGet; |
| 40 |
import org.apache.http.client.params.AuthPolicy; |
| 41 |
import org.apache.http.client.protocol.RequestAcceptEncoding; |
| 42 |
import org.apache.http.client.protocol.ResponseContentEncoding; |
| 43 |
import org.apache.http.conn.params.ConnRouteParams; |
| 44 |
import org.apache.http.conn.scheme.Scheme; |
| 45 |
import org.apache.http.conn.scheme.SchemeRegistry; |
| 46 |
import org.apache.http.impl.client.DefaultHttpClient; |
| 47 |
import org.apache.http.impl.cookie.DateUtils; |
| 48 |
import org.apache.http.params.CoreConnectionPNames; |
| 49 |
import org.apache.http.protocol.BasicHttpContext; |
| 50 |
import org.apache.http.protocol.HttpContext; |
| 51 |
import org.apache.http.util.EntityUtils; |
| 52 |
import org.eclipse.core.runtime.Assert; |
| 53 |
import org.eclipse.core.runtime.IPath; |
| 54 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 55 |
import org.eclipse.core.runtime.IStatus; |
| 56 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 57 |
import org.eclipse.core.runtime.Path; |
| 58 |
import org.eclipse.core.runtime.Status; |
| 59 |
import org.eclipse.ecf.core.identity.ID; |
| 60 |
import org.eclipse.ecf.core.security.Callback; |
| 61 |
import org.eclipse.ecf.core.security.CallbackHandler; |
| 62 |
import org.eclipse.ecf.core.security.IConnectContext; |
| 63 |
import org.eclipse.ecf.core.security.NameCallback; |
| 64 |
import org.eclipse.ecf.core.security.ObjectCallback; |
| 65 |
import org.eclipse.ecf.core.security.UnsupportedCallbackException; |
| 66 |
import org.eclipse.ecf.core.util.ECFRuntimeException; |
| 67 |
import org.eclipse.ecf.core.util.Proxy; |
| 68 |
import org.eclipse.ecf.core.util.ProxyAddress; |
| 69 |
import org.eclipse.ecf.core.util.Trace; |
| 70 |
import org.eclipse.ecf.filetransfer.BrowseFileTransferException; |
| 71 |
import org.eclipse.ecf.filetransfer.FileTransferJob; |
| 72 |
import org.eclipse.ecf.filetransfer.IFileRangeSpecification; |
| 73 |
import org.eclipse.ecf.filetransfer.IFileTransferPausable; |
| 74 |
import org.eclipse.ecf.filetransfer.IFileTransferRunnable; |
| 75 |
import org.eclipse.ecf.filetransfer.IRetrieveFileTransferOptions; |
| 76 |
import org.eclipse.ecf.filetransfer.IncomingFileTransferException; |
| 77 |
import org.eclipse.ecf.filetransfer.InvalidFileRangeSpecificationException; |
| 78 |
import org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent; |
| 79 |
import org.eclipse.ecf.filetransfer.events.socket.ISocketEventSource; |
| 80 |
import org.eclipse.ecf.filetransfer.events.socket.ISocketListener; |
| 81 |
import org.eclipse.ecf.filetransfer.identity.IFileID; |
| 82 |
import org.eclipse.ecf.internal.provider.filetransfer.httpclient.Activator; |
| 83 |
import org.eclipse.ecf.internal.provider.filetransfer.httpclient.ConnectingSocketMonitor; |
| 84 |
import org.eclipse.ecf.internal.provider.filetransfer.httpclient.DebugOptions; |
| 85 |
import org.eclipse.ecf.internal.provider.filetransfer.httpclient.ECFHttpClientProtocolSocketFactory; |
| 86 |
import org.eclipse.ecf.internal.provider.filetransfer.httpclient.ECFHttpClientSecureProtocolSocketFactory; |
| 87 |
import org.eclipse.ecf.internal.provider.filetransfer.httpclient.HttpClientProxyCredentialProvider; |
| 88 |
import org.eclipse.ecf.internal.provider.filetransfer.httpclient.ISSLSocketFactoryModifier; |
| 89 |
import org.eclipse.ecf.internal.provider.filetransfer.httpclient.Messages; |
| 90 |
import org.eclipse.ecf.provider.filetransfer.events.socket.SocketEventSource; |
| 91 |
import org.eclipse.ecf.provider.filetransfer.identity.FileTransferID; |
| 92 |
import org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer; |
| 93 |
import org.eclipse.ecf.provider.filetransfer.retrieve.HttpHelper; |
| 94 |
import org.eclipse.ecf.provider.filetransfer.util.JREProxyHelper; |
| 95 |
import org.eclipse.ecf.provider.filetransfer.util.ProxySetupHelper; |
| 96 |
import org.eclipse.osgi.util.NLS; |
| 97 |
|
| 98 |
public class HttpClientRetrieveFileTransfer extends AbstractRetrieveFileTransfer { |
| 99 |
|
| 100 |
private static final String USERNAME_PREFIX = Messages.HttpClientRetrieveFileTransfer_Username_Prefix; |
| 101 |
|
| 102 |
// changing to 2 minutes (120000) as per bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=266246 |
| 103 |
// 10/26/2009: Added being able to set with system property with name org.eclipse.ecf.provider.filetransfer.httpclient.retrieve.connectTimeout |
| 104 |
// for https://bugs.eclipse.org/bugs/show_bug.cgi?id=292995 |
| 105 |
protected static final int DEFAULT_CONNECTION_TIMEOUT = HttpClientOptions.RETRIEVE_DEFAULT_CONNECTION_TIMEOUT; |
| 106 |
// changing to 2 minutes (120000) as per bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=266246 |
| 107 |
// 10/26/2009: Added being able to set with system property with name org.eclipse.ecf.provider.filetransfer.httpclient.retrieve.readTimeout |
| 108 |
// for https://bugs.eclipse.org/bugs/show_bug.cgi?id=292995 |
| 109 |
protected static final int DEFAULT_READ_TIMEOUT = HttpClientOptions.RETRIEVE_DEFAULT_READ_TIMEOUT; |
| 110 |
|
| 111 |
protected static final int HTTP_PORT = 80; |
| 112 |
|
| 113 |
protected static final int HTTPS_PORT = 443; |
| 114 |
|
| 115 |
protected static final int MAX_RETRY = 2; |
| 116 |
|
| 117 |
protected static final String HTTPS = Messages.FileTransferNamespace_Https_Protocol; |
| 118 |
|
| 119 |
protected static final String HTTP = Messages.FileTransferNamespace_Http_Protocol; |
| 120 |
|
| 121 |
protected static final String[] supportedProtocols = {HTTP, HTTPS}; |
| 122 |
|
| 123 |
private static final String LAST_MODIFIED_HEADER = "Last-Modified"; //$NON-NLS-1$ |
| 124 |
|
| 125 |
private HttpGet getMethod = null; |
| 126 |
|
| 127 |
private HttpResponse httpResponse = null; |
| 128 |
|
| 129 |
private HttpContext httpContext = null; |
| 130 |
|
| 131 |
private DefaultHttpClient httpClient = null; |
| 132 |
|
| 133 |
private String username; |
| 134 |
|
| 135 |
private String password; |
| 136 |
|
| 137 |
private int responseCode = -1; |
| 138 |
private volatile boolean doneFired = false; |
| 139 |
|
| 140 |
private String remoteFileName; |
| 141 |
|
| 142 |
protected int httpVersion = 1; |
| 143 |
|
| 144 |
protected IFileID fileid = null; |
| 145 |
|
| 146 |
protected JREProxyHelper proxyHelper = null; |
| 147 |
|
| 148 |
private SocketEventSource socketEventSource; |
| 149 |
|
| 150 |
private ConnectingSocketMonitor connectingSockets; |
| 151 |
private FileTransferJob connectJob; |
| 152 |
|
| 153 |
/** |
| 154 |
* @since 5.0 |
| 155 |
*/ |
| 156 |
public HttpClientRetrieveFileTransfer(DefaultHttpClient httpClient) { |
| 157 |
this.httpClient = httpClient; |
| 158 |
Assert.isNotNull(this.httpClient); |
| 159 |
this.httpClient.setCredentialsProvider(new ECFCredentialsProvider()); |
| 160 |
proxyHelper = new JREProxyHelper(); |
| 161 |
connectingSockets = new ConnectingSocketMonitor(1); |
| 162 |
socketEventSource = new SocketEventSource() { |
| 163 |
public Object getAdapter(Class adapter) { |
| 164 |
if (adapter == null) { |
| 165 |
return null; |
| 166 |
} |
| 167 |
if (adapter.isInstance(this)) { |
| 168 |
return this; |
| 169 |
} |
| 170 |
return HttpClientRetrieveFileTransfer.this.getAdapter(adapter); |
| 171 |
} |
| 172 |
|
| 173 |
}; |
| 174 |
|
| 175 |
registerSchemes(socketEventSource, connectingSockets); |
| 176 |
} |
| 177 |
|
| 178 |
private void registerSchemes(ISocketEventSource source, ISocketListener socketListener) { |
| 179 |
SchemeRegistry schemeRegistry = this.httpClient.getConnectionManager().getSchemeRegistry(); |
| 180 |
|
| 181 |
Scheme http = new Scheme(HttpClientRetrieveFileTransfer.HTTP, HTTP_PORT, new ECFHttpClientProtocolSocketFactory(SocketFactory.getDefault(), source, socketListener)); |
| 182 |
|
| 183 |
Trace.trace(Activator.PLUGIN_ID, "registering http scheme"); //$NON-NLS-1$ |
| 184 |
schemeRegistry.register(http); |
| 185 |
|
| 186 |
ISSLSocketFactoryModifier sslSocketFactoryModifier = Activator.getDefault().getSSLSocketFactoryModifier(); |
| 187 |
|
| 188 |
if (sslSocketFactoryModifier == null) { |
| 189 |
sslSocketFactoryModifier = new HttpClientDefaultSSLSocketFactoryModifier(); |
| 190 |
} |
| 191 |
|
| 192 |
SSLSocketFactory sslSocketFactory = null; |
| 193 |
try { |
| 194 |
sslSocketFactory = sslSocketFactoryModifier.getSSLSocketFactory(); |
| 195 |
} catch (IOException e) { |
| 196 |
Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING, ISSLSocketFactoryModifier.class, "getSSLSocketFactory()", e); //$NON-NLS-1$ |
| 197 |
Trace.throwing(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_THROWING, HttpClientRetrieveFileTransfer.class, "registerSchemes()", e); //$NON-NLS-1$ |
| 198 |
throw new ECFRuntimeException("Unable to instantiate schemes for HttpClient.", e); //$NON-NLS-1$ |
| 199 |
} |
| 200 |
|
| 201 |
Scheme https = new Scheme(HttpClientRetrieveFileTransfer.HTTPS, HTTPS_PORT, new ECFHttpClientSecureProtocolSocketFactory(sslSocketFactory, source, socketListener)); |
| 202 |
Trace.trace(Activator.PLUGIN_ID, "registering https scheme; modifier=" + sslSocketFactoryModifier); //$NON-NLS-1$ |
| 203 |
schemeRegistry.register(https); |
| 204 |
|
| 205 |
// SPNEGO is not supported, so remove it from the list |
| 206 |
List authpref = new ArrayList(3); |
| 207 |
authpref.add(AuthPolicy.NTLM); |
| 208 |
authpref.add(AuthPolicy.DIGEST); |
| 209 |
authpref.add(AuthPolicy.BASIC); |
| 210 |
|
| 211 |
httpClient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref); |
| 212 |
httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authpref); |
| 213 |
} |
| 214 |
|
| 215 |
/* (non-Javadoc) |
| 216 |
* @see org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer#getRemoteFileName() |
| 217 |
*/ |
| 218 |
public String getRemoteFileName() { |
| 219 |
return remoteFileName; |
| 220 |
} |
| 221 |
|
| 222 |
public synchronized void cancel() { |
| 223 |
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "cancel"); //$NON-NLS-1$ |
| 224 |
if (isCanceled()) { |
| 225 |
return; // break job cancel recursion |
| 226 |
} |
| 227 |
setDoneCanceled(exception); |
| 228 |
boolean fireDoneEvent = true; |
| 229 |
if (connectJob != null) { |
| 230 |
Trace.trace(Activator.PLUGIN_ID, "calling connectJob.cancel()"); //$NON-NLS-1$ |
| 231 |
connectJob.cancel(); |
| 232 |
} |
| 233 |
synchronized (jobLock) { |
| 234 |
if (job != null) { |
| 235 |
// Its the transfer jobs responsibility to throw the event. |
| 236 |
fireDoneEvent = false; |
| 237 |
Trace.trace(Activator.PLUGIN_ID, "calling transfer job.cancel()"); //$NON-NLS-1$ |
| 238 |
job.cancel(); |
| 239 |
} |
| 240 |
} |
| 241 |
if (getMethod != null) { |
| 242 |
if (!getMethod.isAborted()) { |
| 243 |
Trace.trace(Activator.PLUGIN_ID, "calling getMethod.abort()"); //$NON-NLS-1$ |
| 244 |
getMethod.abort(); |
| 245 |
} |
| 246 |
} |
| 247 |
if (connectingSockets != null) { |
| 248 |
// this should unblock socket connect calls, if any |
| 249 |
for (Iterator iterator = connectingSockets.getConnectingSockets().iterator(); iterator.hasNext();) { |
| 250 |
Socket socket = (Socket) iterator.next(); |
| 251 |
try { |
| 252 |
Trace.trace(Activator.PLUGIN_ID, "Call socket.close() for socket=" + socket.toString()); //$NON-NLS-1$ |
| 253 |
socket.close(); |
| 254 |
} catch (IOException e) { |
| 255 |
Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "cancel", e); //$NON-NLS-1$ |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
hardClose(); |
| 260 |
if (fireDoneEvent) { |
| 261 |
fireTransferReceiveDoneEvent(); |
| 262 |
} |
| 263 |
Trace.exiting(Activator.PLUGIN_ID, DebugOptions.METHODS_EXITING, this.getClass(), "cancel");//$NON-NLS-1$ |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
/* |
| 268 |
* (non-Javadoc) |
| 269 |
* |
| 270 |
* @see org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer#hardClose() |
| 271 |
*/ |
| 272 |
protected void hardClose() { |
| 273 |
super.hardClose(); |
| 274 |
if (getMethod != null) { |
| 275 |
getMethod.abort(); |
| 276 |
getMethod = null; |
| 277 |
} |
| 278 |
responseCode = -1; |
| 279 |
if (proxyHelper != null) { |
| 280 |
proxyHelper.dispose(); |
| 281 |
proxyHelper = null; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* @since 5.0 |
| 287 |
*/ |
| 288 |
protected Credentials getFileRequestCredentials() throws UnsupportedCallbackException, IOException { |
| 289 |
if (connectContext == null) |
| 290 |
return null; |
| 291 |
final CallbackHandler callbackHandler = connectContext.getCallbackHandler(); |
| 292 |
if (callbackHandler == null) |
| 293 |
return null; |
| 294 |
final NameCallback usernameCallback = new NameCallback(USERNAME_PREFIX); |
| 295 |
final ObjectCallback passwordCallback = new ObjectCallback(); |
| 296 |
callbackHandler.handle(new Callback[] {usernameCallback, passwordCallback}); |
| 297 |
username = usernameCallback.getName(); |
| 298 |
password = (String) passwordCallback.getObject(); |
| 299 |
return new UsernamePasswordCredentials(username, password); |
| 300 |
} |
| 301 |
|
| 302 |
protected void setupProxies() { |
| 303 |
// If it's been set directly (via ECF API) then this overrides platform settings |
| 304 |
if (proxy == null) { |
| 305 |
try { |
| 306 |
// give SOCKS priority see https://bugs.eclipse.org/bugs/show_bug.cgi?id=295030#c61 |
| 307 |
proxy = ProxySetupHelper.getSocksProxy(getRemoteFileURL()); |
| 308 |
if (proxy == null) { |
| 309 |
proxy = ProxySetupHelper.getProxy(getRemoteFileURL().toExternalForm()); |
| 310 |
} |
| 311 |
} catch (NoClassDefFoundError e) { |
| 312 |
// If the proxy API is not available a NoClassDefFoundError will be thrown here. |
| 313 |
// If that happens then we just want to continue on. |
| 314 |
Activator.logNoProxyWarning(e); |
| 315 |
} |
| 316 |
} |
| 317 |
if (proxy != null) |
| 318 |
setupProxy(proxy); |
| 319 |
} |
| 320 |
|
| 321 |
protected synchronized void resetDoneAndException() { |
| 322 |
// Doesn't match the description, but since it should be cleared before it is |
| 323 |
// reused, this is the best place. |
| 324 |
clearProxy(); |
| 325 |
|
| 326 |
super.resetDoneAndException(); |
| 327 |
} |
| 328 |
|
| 329 |
protected void setupAuthentication(String urlString) throws UnsupportedCallbackException, IOException { |
| 330 |
Credentials credentials = null; |
| 331 |
if (username == null) { |
| 332 |
credentials = getFileRequestCredentials(); |
| 333 |
} |
| 334 |
|
| 335 |
if (credentials != null && username != null) { |
| 336 |
final AuthScope authScope = new AuthScope(getHostFromURL(urlString), getPortFromURL(urlString), AuthScope.ANY_REALM); |
| 337 |
Trace.trace(Activator.PLUGIN_ID, "retrieve credentials=" + credentials); //$NON-NLS-1$ |
| 338 |
httpClient.getCredentialsProvider().setCredentials(authScope, credentials); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
protected void setRequestHeaderValues() throws InvalidFileRangeSpecificationException { |
| 343 |
final IFileRangeSpecification rangeSpec = getFileRangeSpecification(); |
| 344 |
if (rangeSpec != null) { |
| 345 |
final long startPosition = rangeSpec.getStartPosition(); |
| 346 |
final long endPosition = rangeSpec.getEndPosition(); |
| 347 |
if (startPosition < 0) |
| 348 |
throw new InvalidFileRangeSpecificationException(Messages.HttpClientRetrieveFileTransfer_RESUME_START_POSITION_LESS_THAN_ZERO, rangeSpec); |
| 349 |
if (endPosition != -1L && endPosition <= startPosition) |
| 350 |
throw new InvalidFileRangeSpecificationException(Messages.HttpClientRetrieveFileTransfer_RESUME_ERROR_END_POSITION_LESS_THAN_START, rangeSpec); |
| 351 |
String rangeHeader = "bytes=" + startPosition + "-" + ((endPosition == -1L) ? "" : ("" + endPosition)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
| 352 |
Trace.trace(Activator.PLUGIN_ID, "retrieve range header=" + rangeHeader); //$NON-NLS-1$ |
| 353 |
setRangeHeader(rangeHeader); |
| 354 |
} |
| 355 |
// set max-age for cache control to 0 for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=249990 |
| 356 |
getMethod.addHeader("Cache-Control", "max-age=0"); //$NON-NLS-1$//$NON-NLS-2$ |
| 357 |
setRequestHeaderValuesFromOptions(); |
| 358 |
} |
| 359 |
|
| 360 |
private void setRequestHeaderValuesFromOptions() { |
| 361 |
Map localOptions = getOptions(); |
| 362 |
if (localOptions != null) { |
| 363 |
Object o = localOptions.get(IRetrieveFileTransferOptions.REQUEST_HEADERS); |
| 364 |
if (o != null && o instanceof Map) { |
| 365 |
Map requestHeaders = (Map) o; |
| 366 |
for (Iterator i = requestHeaders.keySet().iterator(); i.hasNext();) { |
| 367 |
Object n = i.next(); |
| 368 |
Object v = requestHeaders.get(n); |
| 369 |
if (n != null && n instanceof String && v != null && v instanceof String) |
| 370 |
getMethod.addHeader((String) n, (String) v); |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
private void setRangeHeader(String value) { |
| 377 |
getMethod.addHeader("Range", value); //$NON-NLS-1$ |
| 378 |
} |
| 379 |
|
| 380 |
private boolean isHTTP11() { |
| 381 |
return (httpVersion >= 1); |
| 382 |
} |
| 383 |
|
| 384 |
public int getResponseCode() { |
| 385 |
if (responseCode != -1) |
| 386 |
return responseCode; |
| 387 |
ProtocolVersion version = getMethod.getProtocolVersion(); |
| 388 |
if (version == null) { |
| 389 |
responseCode = -1; |
| 390 |
httpVersion = 1; |
| 391 |
return responseCode; |
| 392 |
} |
| 393 |
httpVersion = version.getMinor(); |
| 394 |
responseCode = httpResponse.getStatusLine().getStatusCode(); |
| 395 |
return responseCode; |
| 396 |
} |
| 397 |
|
| 398 |
/* |
| 399 |
* (non-Javadoc) |
| 400 |
* |
| 401 |
* @see org.eclipse.ecf.core.identity.IIdentifiable#getID() |
| 402 |
*/ |
| 403 |
public ID getID() { |
| 404 |
return fileid; |
| 405 |
} |
| 406 |
|
| 407 |
private long getLastModifiedTimeFromHeader() throws IOException { |
| 408 |
Header lastModifiedHeader = httpResponse.getLastHeader(LAST_MODIFIED_HEADER); |
| 409 |
if (lastModifiedHeader == null) |
| 410 |
throw new IOException(Messages.HttpClientRetrieveFileTransfer_INVALID_LAST_MODIFIED_TIME); |
| 411 |
|
| 412 |
String lastModifiedString = lastModifiedHeader.getValue(); |
| 413 |
long lastModified = 0; |
| 414 |
if (lastModifiedString != null) { |
| 415 |
try { |
| 416 |
lastModified = DateUtils.parseDate(lastModifiedString).getTime(); |
| 417 |
} catch (Exception e) { |
| 418 |
throw new IOException(Messages.HttpClientRetrieveFileTransfer_EXCEPITION_INVALID_LAST_MODIFIED_FROM_SERVER); |
| 419 |
} |
| 420 |
} |
| 421 |
return lastModified; |
| 422 |
} |
| 423 |
|
| 424 |
protected void getResponseHeaderValues() throws IOException { |
| 425 |
if (getResponseCode() == -1) |
| 426 |
throw new IOException(Messages.HttpClientRetrieveFileTransfer_INVALID_SERVER_RESPONSE_TO_PARTIAL_RANGE_REQUEST); |
| 427 |
Header lastModifiedHeader = httpResponse.getLastHeader(LAST_MODIFIED_HEADER); |
| 428 |
if (lastModifiedHeader != null) { |
| 429 |
setLastModifiedTime(getLastModifiedTimeFromHeader()); |
| 430 |
} |
| 431 |
setFileLength(httpResponse.getEntity().getContentLength()); |
| 432 |
fileid = new FileTransferID(getRetrieveNamespace(), getRemoteFileURL()); |
| 433 |
|
| 434 |
// Get content disposition header and get remote file name from it if possible. |
| 435 |
Header contentDispositionHeader = httpResponse.getLastHeader(HttpHelper.CONTENT_DISPOSITION_HEADER); |
| 436 |
if (contentDispositionHeader != null) { |
| 437 |
remoteFileName = HttpHelper.getRemoteFileNameFromContentDispositionHeader(contentDispositionHeader.getValue()); |
| 438 |
} |
| 439 |
// If still null, get the path from httpclient.getMethod() |
| 440 |
if (remoteFileName == null) { |
| 441 |
// No name could be extracted using Content-Disposition. Let's try the |
| 442 |
// path from the getMethod. |
| 443 |
String pathStr = getMethod.getRequestLine().getUri(); |
| 444 |
if (pathStr != null && pathStr.length() > 0) { |
| 445 |
IPath path = Path.fromPortableString(pathStr); |
| 446 |
if (path.segmentCount() > 0) |
| 447 |
remoteFileName = path.lastSegment(); |
| 448 |
} |
| 449 |
// If still null, use the input file name |
| 450 |
if (remoteFileName == null) |
| 451 |
// Last resort. Use the path of the initial URL request |
| 452 |
remoteFileName = super.getRemoteFileName(); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
final class ECFCredentialsProvider extends HttpClientProxyCredentialProvider { |
| 457 |
|
| 458 |
protected Proxy getECFProxy() { |
| 459 |
return getProxy(); |
| 460 |
} |
| 461 |
|
| 462 |
protected Credentials getNTLMCredentials(Proxy lp) { |
| 463 |
if (hasForceNTLMProxyOption()) |
| 464 |
return HttpClientRetrieveFileTransfer.createNTLMCredentials(lp); |
| 465 |
return null; |
| 466 |
} |
| 467 |
|
| 468 |
} |
| 469 |
|
| 470 |
Proxy getProxy() { |
| 471 |
return proxy; |
| 472 |
} |
| 473 |
|
| 474 |
protected void setInputStream(InputStream ins) { |
| 475 |
remoteFileContents = ins; |
| 476 |
} |
| 477 |
|
| 478 |
protected InputStream wrapTransferReadInputStream(InputStream inputStream, IProgressMonitor monitor) { |
| 479 |
return inputStream; |
| 480 |
} |
| 481 |
|
| 482 |
protected boolean hasForceNTLMProxyOption() { |
| 483 |
Map localOptions = getOptions(); |
| 484 |
if (localOptions != null && localOptions.get(HttpClientOptions.FORCE_NTLM_PROP) != null) |
| 485 |
return true; |
| 486 |
return (System.getProperties().getProperty(HttpClientOptions.FORCE_NTLM_PROP) != null); |
| 487 |
} |
| 488 |
|
| 489 |
protected int getSocketReadTimeout() { |
| 490 |
int result = DEFAULT_READ_TIMEOUT; |
| 491 |
Map localOptions = getOptions(); |
| 492 |
if (localOptions != null) { |
| 493 |
// See if the connect timeout option is present, if so set |
| 494 |
Object o = localOptions.get(IRetrieveFileTransferOptions.READ_TIMEOUT); |
| 495 |
if (o != null) { |
| 496 |
if (o instanceof Integer) { |
| 497 |
result = ((Integer) o).intValue(); |
| 498 |
} else if (o instanceof String) { |
| 499 |
result = new Integer(((String) o)).intValue(); |
| 500 |
} |
| 501 |
return result; |
| 502 |
} |
| 503 |
o = localOptions.get("org.eclipse.ecf.provider.filetransfer.httpclient.retrieve.readTimeout"); //$NON-NLS-1$ |
| 504 |
if (o != null) { |
| 505 |
if (o instanceof Integer) { |
| 506 |
result = ((Integer) o).intValue(); |
| 507 |
} else if (o instanceof String) { |
| 508 |
result = new Integer(((String) o)).intValue(); |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
return result; |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* @since 4.0 |
| 517 |
*/ |
| 518 |
protected int getConnectTimeout() { |
| 519 |
int result = DEFAULT_CONNECTION_TIMEOUT; |
| 520 |
Map localOptions = getOptions(); |
| 521 |
if (localOptions != null) { |
| 522 |
// See if the connect timeout option is present, if so set |
| 523 |
Object o = localOptions.get(IRetrieveFileTransferOptions.CONNECT_TIMEOUT); |
| 524 |
if (o != null) { |
| 525 |
if (o instanceof Integer) { |
| 526 |
result = ((Integer) o).intValue(); |
| 527 |
} else if (o instanceof String) { |
| 528 |
result = new Integer(((String) o)).intValue(); |
| 529 |
} |
| 530 |
return result; |
| 531 |
} |
| 532 |
o = localOptions.get("org.eclipse.ecf.provider.filetransfer.httpclient.retrieve.connectTimeout"); //$NON-NLS-1$ |
| 533 |
if (o != null) { |
| 534 |
if (o instanceof Integer) { |
| 535 |
result = ((Integer) o).intValue(); |
| 536 |
} else if (o instanceof String) { |
| 537 |
result = new Integer(((String) o)).intValue(); |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
return result; |
| 542 |
} |
| 543 |
|
| 544 |
/* (non-Javadoc) |
| 545 |
* @see org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer#openStreams() |
| 546 |
*/ |
| 547 |
protected void openStreams() throws IncomingFileTransferException { |
| 548 |
|
| 549 |
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "openStreams"); //$NON-NLS-1$ |
| 550 |
final String urlString = getRemoteFileURL().toString(); |
| 551 |
this.doneFired = false; |
| 552 |
|
| 553 |
int code = -1; |
| 554 |
|
| 555 |
try { |
| 556 |
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, getSocketReadTimeout()); |
| 557 |
int connectTimeout = getConnectTimeout(); |
| 558 |
httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); |
| 559 |
|
| 560 |
setupAuthentication(urlString); |
| 561 |
|
| 562 |
getMethod = new HttpGet(urlString); |
| 563 |
// Define a CredentialsProvider - found that possibility while debugging in org.apache.commons.httpclient.HttpMethodDirector.processProxyAuthChallenge(HttpMethod) |
| 564 |
// Seems to be another way to select the credentials. |
| 565 |
setRequestHeaderValues(); |
| 566 |
|
| 567 |
Trace.trace(Activator.PLUGIN_ID, "retrieve=" + urlString); //$NON-NLS-1$ |
| 568 |
// Set request header for possible gzip encoding, but only if |
| 569 |
// 1) The file range specification is null (we want the whole file) |
| 570 |
// 2) The target remote file does *not* end in .gz (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=280205) |
| 571 |
if (getFileRangeSpecification() == null && !targetHasGzSuffix(super.getRemoteFileName())) { |
| 572 |
Trace.trace(Activator.PLUGIN_ID, "Accept-Encoding: gzip,deflate added to request header"); //$NON-NLS-1$ |
| 573 |
|
| 574 |
// Add the interceptors to provide the gzip |
| 575 |
httpClient.addRequestInterceptor(new RequestAcceptEncoding()); |
| 576 |
httpClient.addResponseInterceptor(new ResponseContentEncoding()); |
| 577 |
} else { |
| 578 |
Trace.trace(Activator.PLUGIN_ID, "Accept-Encoding NOT added to header"); //$NON-NLS-1$ |
| 579 |
} |
| 580 |
|
| 581 |
fireConnectStartEvent(); |
| 582 |
if (checkAndHandleDone()) { |
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
connectingSockets.clear(); |
| 587 |
// Actually execute get and get response code (since redirect is set to true, then |
| 588 |
// redirect response code handled internally |
| 589 |
if (connectJob == null) { |
| 590 |
performConnect(new NullProgressMonitor()); |
| 591 |
} else { |
| 592 |
connectJob.schedule(); |
| 593 |
connectJob.join(); |
| 594 |
connectJob = null; |
| 595 |
} |
| 596 |
if (checkAndHandleDone()) { |
| 597 |
return; |
| 598 |
} |
| 599 |
|
| 600 |
code = responseCode; |
| 601 |
|
| 602 |
responseHeaders = getResponseHeaders(); |
| 603 |
|
| 604 |
Trace.trace(Activator.PLUGIN_ID, "retrieve resp=" + code); //$NON-NLS-1$ |
| 605 |
|
| 606 |
// Check for NTLM proxy in response headers |
| 607 |
// This check is to deal with bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=252002 |
| 608 |
boolean ntlmProxyFound = NTLMProxyDetector.detectNTLMProxy(httpContext); |
| 609 |
if (ntlmProxyFound && !hasForceNTLMProxyOption()) |
| 610 |
throw new IncomingFileTransferException("HttpClient Provider is not configured to support NTLM proxy authentication.", HttpClientOptions.NTLM_PROXY_RESPONSE_CODE); //$NON-NLS-1$ |
| 611 |
|
| 612 |
if (NTLMProxyDetector.detectSPNEGOProxy(httpContext)) |
| 613 |
throw new BrowseFileTransferException("HttpClient Provider does not support the use of SPNEGO proxy authentication."); //$NON-NLS-1$ |
| 614 |
|
| 615 |
if (code == HttpURLConnection.HTTP_PARTIAL || code == HttpURLConnection.HTTP_OK) { |
| 616 |
getResponseHeaderValues(); |
| 617 |
setInputStream(httpResponse.getEntity().getContent()); |
| 618 |
fireReceiveStartEvent(); |
| 619 |
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) { |
| 620 |
EntityUtils.consume(httpResponse.getEntity()); |
| 621 |
throw new IncomingFileTransferException(NLS.bind("File not found: {0}", urlString), code); //$NON-NLS-1$ |
| 622 |
} else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) { |
| 623 |
EntityUtils.consume(httpResponse.getEntity()); |
| 624 |
throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Unauthorized, code); |
| 625 |
} else if (code == HttpURLConnection.HTTP_FORBIDDEN) { |
| 626 |
EntityUtils.consume(httpResponse.getEntity()); |
| 627 |
throw new IncomingFileTransferException("Forbidden", code); //$NON-NLS-1$ |
| 628 |
} else if (code == HttpURLConnection.HTTP_PROXY_AUTH) { |
| 629 |
EntityUtils.consume(httpResponse.getEntity()); |
| 630 |
throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Proxy_Auth_Required, code); |
| 631 |
} else { |
| 632 |
Trace.trace(Activator.PLUGIN_ID, EntityUtils.toString(httpResponse.getEntity())); |
| 633 |
// EntityUtils.consume(httpResponse.getEntity()); |
| 634 |
throw new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_ERROR_GENERAL_RESPONSE_CODE, new Integer(code)), code); |
| 635 |
} |
| 636 |
} catch (final Exception e) { |
| 637 |
Trace.throwing(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_THROWING, this.getClass(), "openStreams", e); //$NON-NLS-1$ |
| 638 |
if (code == -1) { |
| 639 |
if (!isDone()) { |
| 640 |
setDoneException(e); |
| 641 |
} |
| 642 |
fireTransferReceiveDoneEvent(); |
| 643 |
} else { |
| 644 |
IncomingFileTransferException ex = (IncomingFileTransferException) ((e instanceof IncomingFileTransferException) ? e : new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_EXCEPTION_COULD_NOT_CONNECT, urlString), e, code)); |
| 645 |
throw ex; |
| 646 |
} |
| 647 |
} |
| 648 |
Trace.exiting(Activator.PLUGIN_ID, DebugOptions.METHODS_EXITING, this.getClass(), "openStreams"); //$NON-NLS-1$ |
| 649 |
} |
| 650 |
|
| 651 |
private Map getResponseHeaders() { |
| 652 |
if (getMethod == null) |
| 653 |
return null; |
| 654 |
Header[] headers = httpResponse.getAllHeaders(); |
| 655 |
Map result = null; |
| 656 |
if (headers != null && headers.length > 0) { |
| 657 |
result = new HashMap(); |
| 658 |
for (int i = 0; i < headers.length; i++) { |
| 659 |
String name = headers[i].getName(); |
| 660 |
String val = headers[i].getValue(); |
| 661 |
if (name != null && val != null) |
| 662 |
result.put(name, val); |
| 663 |
} |
| 664 |
} |
| 665 |
return Collections.unmodifiableMap(result); |
| 666 |
} |
| 667 |
|
| 668 |
private boolean checkAndHandleDone() { |
| 669 |
if (isDone()) { |
| 670 |
// for cancel the done event should have been fired always. |
| 671 |
if (!doneFired) { |
| 672 |
fireTransferReceiveDoneEvent(); |
| 673 |
} |
| 674 |
return true; |
| 675 |
} |
| 676 |
return false; |
| 677 |
} |
| 678 |
|
| 679 |
/* |
| 680 |
* (non-Javadoc) |
| 681 |
* |
| 682 |
* @see org.eclipse.ecf.filetransfer.IRetrieveFileTransferContainerAdapter#setConnectContextForAuthentication(org.eclipse.ecf.core.security.IConnectContext) |
| 683 |
*/ |
| 684 |
public void setConnectContextForAuthentication(IConnectContext connectContext) { |
| 685 |
super.setConnectContextForAuthentication(connectContext); |
| 686 |
this.username = null; |
| 687 |
this.password = null; |
| 688 |
} |
| 689 |
|
| 690 |
protected static String getHostFromURL(String url) { |
| 691 |
String result = url; |
| 692 |
final int colonSlashSlash = url.indexOf("://"); //$NON-NLS-1$ |
| 693 |
if (colonSlashSlash < 0) |
| 694 |
return ""; //$NON-NLS-1$ |
| 695 |
if (colonSlashSlash >= 0) { |
| 696 |
result = url.substring(colonSlashSlash + 3); |
| 697 |
} |
| 698 |
|
| 699 |
final int colonPort = result.indexOf(':'); |
| 700 |
final int requestPath = result.indexOf('/'); |
| 701 |
|
| 702 |
int substringEnd; |
| 703 |
|
| 704 |
if (colonPort > 0 && requestPath > 0) |
| 705 |
substringEnd = Math.min(colonPort, requestPath); |
| 706 |
else if (colonPort > 0) |
| 707 |
substringEnd = colonPort; |
| 708 |
else if (requestPath > 0) |
| 709 |
substringEnd = requestPath; |
| 710 |
else |
| 711 |
substringEnd = result.length(); |
| 712 |
|
| 713 |
return result.substring(0, substringEnd); |
| 714 |
|
| 715 |
} |
| 716 |
|
| 717 |
protected static int getPortFromURL(String url) { |
| 718 |
final int colonSlashSlash = url.indexOf("://"); //$NON-NLS-1$ |
| 719 |
if (colonSlashSlash < 0) |
| 720 |
return urlUsesHttps(url) ? HTTPS_PORT : HTTP_PORT; |
| 721 |
// This is wrong as if the url has no colonPort before '?' then it should return the default |
| 722 |
|
| 723 |
final int colonPort = url.indexOf(':', colonSlashSlash + 1); |
| 724 |
if (colonPort < 0) |
| 725 |
return urlUsesHttps(url) ? HTTPS_PORT : HTTP_PORT; |
| 726 |
// Make sure that the colonPort is not from some part of the rest of the URL |
| 727 |
int nextSlash = url.indexOf('/', colonSlashSlash + 3); |
| 728 |
if (nextSlash != -1 && colonPort > nextSlash) |
| 729 |
return urlUsesHttps(url) ? HTTPS_PORT : HTTP_PORT; |
| 730 |
|
| 731 |
final int requestPath = url.indexOf('/', colonPort + 1); |
| 732 |
|
| 733 |
int end; |
| 734 |
if (requestPath < 0) |
| 735 |
end = url.length(); |
| 736 |
else |
| 737 |
end = requestPath; |
| 738 |
|
| 739 |
return Integer.parseInt(url.substring(colonPort + 1, end)); |
| 740 |
} |
| 741 |
|
| 742 |
protected static boolean urlUsesHttps(String url) { |
| 743 |
url = url.trim(); |
| 744 |
return url.startsWith(HTTPS); |
| 745 |
} |
| 746 |
|
| 747 |
/* |
| 748 |
* (non-Javadoc) |
| 749 |
* |
| 750 |
* @see org.eclipse.ecf.internal.provider.filetransfer.AbstractRetrieveFileTransfer#supportsProtocol(java.lang.String) |
| 751 |
*/ |
| 752 |
public static boolean supportsProtocol(String protocolString) { |
| 753 |
for (int i = 0; i < supportedProtocols.length; i++) |
| 754 |
if (supportedProtocols[i].equalsIgnoreCase(protocolString)) |
| 755 |
return true; |
| 756 |
return false; |
| 757 |
} |
| 758 |
|
| 759 |
protected boolean isConnected() { |
| 760 |
return (getMethod != null); |
| 761 |
} |
| 762 |
|
| 763 |
/* |
| 764 |
* (non-Javadoc) |
| 765 |
* |
| 766 |
* @see org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer#doPause() |
| 767 |
*/ |
| 768 |
protected boolean doPause() { |
| 769 |
if (isPaused() || !isConnected() || isDone()) |
| 770 |
return false; |
| 771 |
this.paused = true; |
| 772 |
return this.paused; |
| 773 |
} |
| 774 |
|
| 775 |
/* |
| 776 |
* (non-Javadoc) |
| 777 |
* |
| 778 |
* @see org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer#doResume() |
| 779 |
*/ |
| 780 |
protected boolean doResume() { |
| 781 |
if (!isPaused() || isConnected()) |
| 782 |
return false; |
| 783 |
return openStreamsForResume(); |
| 784 |
} |
| 785 |
|
| 786 |
protected void setResumeRequestHeaderValues() throws IOException { |
| 787 |
if (this.bytesReceived <= 0 || this.fileLength <= this.bytesReceived) |
| 788 |
throw new IOException(Messages.HttpClientRetrieveFileTransfer_RESUME_START_ERROR); |
| 789 |
setRangeHeader("bytes=" + this.bytesReceived + "-"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 790 |
// set max-age for cache control to 0 for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=249990 |
| 791 |
getMethod.addHeader("Cache-Control", "max-age=0"); //$NON-NLS-1$//$NON-NLS-2$ |
| 792 |
setRequestHeaderValuesFromOptions(); |
| 793 |
} |
| 794 |
|
| 795 |
private boolean openStreamsForResume() { |
| 796 |
|
| 797 |
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "openStreamsForResume"); //$NON-NLS-1$ |
| 798 |
final String urlString = getRemoteFileURL().toString(); |
| 799 |
this.doneFired = false; |
| 800 |
|
| 801 |
int code = -1; |
| 802 |
|
| 803 |
try { |
| 804 |
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, getSocketReadTimeout()); |
| 805 |
int connectTimeout = getConnectTimeout(); |
| 806 |
httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); |
| 807 |
|
| 808 |
setupAuthentication(urlString); |
| 809 |
|
| 810 |
getMethod = new HttpGet(urlString); |
| 811 |
// Define a CredentialsProvider - found that possibility while debugging in org.apache.commons.httpclient.HttpMethodDirector.processProxyAuthChallenge(HttpMethod) |
| 812 |
// Seems to be another way to select the credentials. |
| 813 |
setResumeRequestHeaderValues(); |
| 814 |
|
| 815 |
Trace.trace(Activator.PLUGIN_ID, "resume=" + urlString); //$NON-NLS-1$ |
| 816 |
|
| 817 |
// Gzip encoding is not an option for resume |
| 818 |
fireConnectStartEvent(); |
| 819 |
if (checkAndHandleDone()) { |
| 820 |
return false; |
| 821 |
} |
| 822 |
|
| 823 |
connectingSockets.clear(); |
| 824 |
// Actually execute get and get response code (since redirect is set to true, then |
| 825 |
// redirect response code handled internally |
| 826 |
if (connectJob == null) { |
| 827 |
performConnect(new NullProgressMonitor()); |
| 828 |
} else { |
| 829 |
connectJob.schedule(); |
| 830 |
connectJob.join(); |
| 831 |
connectJob = null; |
| 832 |
} |
| 833 |
if (checkAndHandleDone()) { |
| 834 |
return false; |
| 835 |
} |
| 836 |
|
| 837 |
code = responseCode; |
| 838 |
|
| 839 |
responseHeaders = getResponseHeaders(); |
| 840 |
|
| 841 |
Trace.trace(Activator.PLUGIN_ID, "retrieve resp=" + code); //$NON-NLS-1$ |
| 842 |
|
| 843 |
if (code == HttpURLConnection.HTTP_PARTIAL || code == HttpURLConnection.HTTP_OK) { |
| 844 |
getResumeResponseHeaderValues(); |
| 845 |
setInputStream(httpResponse.getEntity().getContent()); |
| 846 |
this.paused = false; |
| 847 |
fireReceiveResumedEvent(); |
| 848 |
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) { |
| 849 |
EntityUtils.consume(httpResponse.getEntity()); |
| 850 |
throw new IncomingFileTransferException(NLS.bind("File not found: {0}", urlString), code, responseHeaders); //$NON-NLS-1$ |
| 851 |
} else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) { |
| 852 |
EntityUtils.consume(httpResponse.getEntity()); |
| 853 |
throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Unauthorized, code, responseHeaders); |
| 854 |
} else if (code == HttpURLConnection.HTTP_FORBIDDEN) { |
| 855 |
EntityUtils.consume(httpResponse.getEntity()); |
| 856 |
throw new IncomingFileTransferException("Forbidden", code, responseHeaders); //$NON-NLS-1$ |
| 857 |
} else if (code == HttpURLConnection.HTTP_PROXY_AUTH) { |
| 858 |
EntityUtils.consume(httpResponse.getEntity()); |
| 859 |
throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Proxy_Auth_Required, code, responseHeaders); |
| 860 |
} else { |
| 861 |
EntityUtils.consume(httpResponse.getEntity()); |
| 862 |
throw new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_ERROR_GENERAL_RESPONSE_CODE, new Integer(code)), code, responseHeaders); |
| 863 |
} |
| 864 |
Trace.exiting(Activator.PLUGIN_ID, DebugOptions.METHODS_EXITING, this.getClass(), "openStreamsForResume", Boolean.TRUE); //$NON-NLS-1$ |
| 865 |
return true; |
| 866 |
} catch (final Exception e) { |
| 867 |
Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "openStreamsForResume", e); //$NON-NLS-1$ |
| 868 |
if (code == -1) { |
| 869 |
if (!isDone()) { |
| 870 |
setDoneException(e); |
| 871 |
} |
| 872 |
} else { |
| 873 |
setDoneException((e instanceof IncomingFileTransferException) ? e : new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_EXCEPTION_COULD_NOT_CONNECT, urlString), e, code, responseHeaders)); |
| 874 |
} |
| 875 |
fireTransferReceiveDoneEvent(); |
| 876 |
Trace.exiting(Activator.PLUGIN_ID, DebugOptions.METHODS_EXITING, this.getClass(), "openStreamsForResume", Boolean.FALSE); //$NON-NLS-1$ |
| 877 |
return false; |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
protected void getResumeResponseHeaderValues() throws IOException { |
| 882 |
if (getResponseCode() != HttpURLConnection.HTTP_PARTIAL) |
| 883 |
throw new IOException(); |
| 884 |
if (lastModifiedTime != getLastModifiedTimeFromHeader()) |
| 885 |
throw new IOException(Messages.HttpClientRetrieveFileTransfer_EXCEPTION_FILE_MODIFIED_SINCE_LAST_ACCESS); |
| 886 |
} |
| 887 |
|
| 888 |
/* |
| 889 |
* (non-Javadoc) |
| 890 |
* |
| 891 |
* @see org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer#getAdapter(java.lang.Class) |
| 892 |
*/ |
| 893 |
public Object getAdapter(Class adapter) { |
| 894 |
if (adapter == null) |
| 895 |
return null; |
| 896 |
if (adapter.equals(IFileTransferPausable.class) && isHTTP11()) |
| 897 |
return this; |
| 898 |
if (adapter.equals(ISocketEventSource.class)) |
| 899 |
return this.socketEventSource; |
| 900 |
return super.getAdapter(adapter); |
| 901 |
} |
| 902 |
|
| 903 |
/* (non-Javadoc) |
| 904 |
* @see org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer#setupProxy(org.eclipse.ecf.core.util.Proxy) |
| 905 |
*/ |
| 906 |
protected void setupProxy(Proxy proxy) { |
| 907 |
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, HttpClientRetrieveFileTransfer.class, "setupProxy " + proxy); //$NON-NLS-1$ |
| 908 |
if (proxy.getType().equals(Proxy.Type.HTTP)) { |
| 909 |
final ProxyAddress address = proxy.getAddress(); |
| 910 |
ConnRouteParams.setDefaultProxy(httpClient.getParams(), new HttpHost(address.getHostName(), address.getPort())); |
| 911 |
// getHostConfiguration().setProxy(address.getHostName(), address.getPort()); |
| 912 |
} else if (proxy.getType().equals(Proxy.Type.SOCKS)) { |
| 913 |
Trace.trace(Activator.PLUGIN_ID, "retrieve socksproxy=" + proxy.getAddress()); //$NON-NLS-1$ |
| 914 |
proxyHelper.setupProxy(proxy); |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
/** |
| 919 |
* This method will clear out the proxy information (so that if this is |
| 920 |
* reused for a request without a proxy, it will work correctly). |
| 921 |
* @since 5.0 |
| 922 |
*/ |
| 923 |
protected void clearProxy() { |
| 924 |
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, HttpClientRetrieveFileTransfer.class, "clearProxy()"); //$NON-NLS-1$ |
| 925 |
ConnRouteParams.setDefaultProxy(httpClient.getParams(), null); |
| 926 |
} |
| 927 |
|
| 928 |
/** |
| 929 |
* @since 5.0 |
| 930 |
*/ |
| 931 |
public static NTCredentials createNTLMCredentials(Proxy p) { |
| 932 |
if (p == null) { |
| 933 |
return null; |
| 934 |
} |
| 935 |
String un = getNTLMUserName(p); |
| 936 |
String domain = getNTLMDomainName(p); |
| 937 |
if (un == null || domain == null) |
| 938 |
return null; |
| 939 |
|
| 940 |
String workstation = null; |
| 941 |
try { |
| 942 |
workstation = InetAddress.getLocalHost().getHostName(); |
| 943 |
} catch (UnknownHostException e) { |
| 944 |
Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING, HttpClientRetrieveFileTransfer.class, "createNTLMCredentials", e); //$NON-NLS-1$ |
| 945 |
} |
| 946 |
|
| 947 |
return new NTCredentials(un, p.getPassword(), workstation, domain); |
| 948 |
} |
| 949 |
|
| 950 |
protected static String getNTLMDomainName(Proxy p) { |
| 951 |
String domainUsername = p.getUsername(); |
| 952 |
if (domainUsername == null) |
| 953 |
return null; |
| 954 |
int slashloc = domainUsername.indexOf('\\'); |
| 955 |
if (slashloc == -1) |
| 956 |
return null; |
| 957 |
return domainUsername.substring(0, slashloc); |
| 958 |
} |
| 959 |
|
| 960 |
protected static String getNTLMUserName(Proxy p) { |
| 961 |
String domainUsername = p.getUsername(); |
| 962 |
if (domainUsername == null) |
| 963 |
return null; |
| 964 |
int slashloc = domainUsername.indexOf('\\'); |
| 965 |
if (slashloc == -1) |
| 966 |
return null; |
| 967 |
return domainUsername.substring(slashloc + 1); |
| 968 |
} |
| 969 |
|
| 970 |
protected void fireConnectStartEvent() { |
| 971 |
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "fireConnectStartEvent"); //$NON-NLS-1$ |
| 972 |
// TODO: should the following be in super.fireReceiveStartEvent(); |
| 973 |
listener.handleTransferEvent(new IFileTransferConnectStartEvent() { |
| 974 |
public IFileID getFileID() { |
| 975 |
return remoteFileID; |
| 976 |
} |
| 977 |
|
| 978 |
public void cancel() { |
| 979 |
HttpClientRetrieveFileTransfer.this.cancel(); |
| 980 |
} |
| 981 |
|
| 982 |
public FileTransferJob prepareConnectJob(FileTransferJob j) { |
| 983 |
return HttpClientRetrieveFileTransfer.this.prepareConnectJob(j); |
| 984 |
} |
| 985 |
|
| 986 |
public void connectUsingJob(FileTransferJob j) { |
| 987 |
HttpClientRetrieveFileTransfer.this.connectUsingJob(j); |
| 988 |
} |
| 989 |
|
| 990 |
public String toString() { |
| 991 |
final StringBuffer sb = new StringBuffer("IFileTransferConnectStartEvent["); //$NON-NLS-1$ |
| 992 |
sb.append(getFileID()); |
| 993 |
sb.append("]"); //$NON-NLS-1$ |
| 994 |
return sb.toString(); |
| 995 |
} |
| 996 |
|
| 997 |
public Object getAdapter(Class adapter) { |
| 998 |
return HttpClientRetrieveFileTransfer.this.getAdapter(adapter); |
| 999 |
} |
| 1000 |
}); |
| 1001 |
} |
| 1002 |
|
| 1003 |
protected String createConnectJobName() { |
| 1004 |
return getRemoteFileURL().toString() + createRangeName() + Messages.HttpClientRetrieveFileTransfer_CONNECTING_JOB_NAME; |
| 1005 |
} |
| 1006 |
|
| 1007 |
protected FileTransferJob prepareConnectJob(FileTransferJob cjob) { |
| 1008 |
if (cjob == null) { |
| 1009 |
// Create our own |
| 1010 |
cjob = new FileTransferJob(createJobName()); |
| 1011 |
} |
| 1012 |
cjob.setFileTransfer(this); |
| 1013 |
cjob.setFileTransferRunnable(fileConnectRunnable); |
| 1014 |
return cjob; |
| 1015 |
} |
| 1016 |
|
| 1017 |
protected void connectUsingJob(FileTransferJob cjob) { |
| 1018 |
Assert.isNotNull(cjob); |
| 1019 |
this.connectJob = cjob; |
| 1020 |
} |
| 1021 |
|
| 1022 |
private IFileTransferRunnable fileConnectRunnable = new IFileTransferRunnable() { |
| 1023 |
public IStatus performFileTransfer(IProgressMonitor monitor) { |
| 1024 |
return performConnect(monitor); |
| 1025 |
} |
| 1026 |
}; |
| 1027 |
|
| 1028 |
private IStatus performConnect(IProgressMonitor monitor) { |
| 1029 |
// there might be more ticks in the future perhaps for |
| 1030 |
// connect socket, certificate validation, send request, authenticate, |
| 1031 |
int ticks = 1; |
| 1032 |
monitor.beginTask(getRemoteFileURL().toString() + Messages.HttpClientRetrieveFileTransfer_CONNECTING_TASK_NAME, ticks); |
| 1033 |
try { |
| 1034 |
if (monitor.isCanceled()) |
| 1035 |
throw newUserCancelledException(); |
| 1036 |
httpContext = new BasicHttpContext(); |
| 1037 |
httpResponse = httpClient.execute(getMethod, httpContext); |
| 1038 |
responseCode = httpResponse.getStatusLine().getStatusCode(); |
| 1039 |
Trace.trace(Activator.PLUGIN_ID, "retrieve resp=" + responseCode); //$NON-NLS-1$ |
| 1040 |
} catch (final Exception e) { |
| 1041 |
Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "performConnect", e); //$NON-NLS-1$ |
| 1042 |
if (!isDone()) { |
| 1043 |
setDoneException(e); |
| 1044 |
} |
| 1045 |
} finally { |
| 1046 |
monitor.done(); |
| 1047 |
} |
| 1048 |
return Status.OK_STATUS; |
| 1049 |
|
| 1050 |
} |
| 1051 |
|
| 1052 |
protected void finalize() throws Throwable { |
| 1053 |
try { |
| 1054 |
if (this.httpClient != null) { |
| 1055 |
this.httpClient.getConnectionManager().shutdown(); |
| 1056 |
} |
| 1057 |
} finally { |
| 1058 |
super.finalize(); |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
protected void fireReceiveResumedEvent() { |
| 1063 |
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "fireReceiveResumedEvent len=" + fileLength + ";rcvd=" + bytesReceived); //$NON-NLS-1$ //$NON-NLS-2$ |
| 1064 |
super.fireReceiveResumedEvent(); |
| 1065 |
} |
| 1066 |
|
| 1067 |
protected void fireTransferReceiveDataEvent() { |
| 1068 |
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "fireTransferReceiveDataEvent len=" + fileLength + ";rcvd=" + bytesReceived); //$NON-NLS-1$ //$NON-NLS-2$ |
| 1069 |
super.fireTransferReceiveDataEvent(); |
| 1070 |
} |
| 1071 |
|
| 1072 |
protected void fireTransferReceiveDoneEvent() { |
| 1073 |
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "fireTransferReceiveDoneEvent len=" + fileLength + ";rcvd=" + bytesReceived); //$NON-NLS-1$ //$NON-NLS-2$ |
| 1074 |
this.doneFired = true; |
| 1075 |
super.fireTransferReceiveDoneEvent(); |
| 1076 |
} |
| 1077 |
|
| 1078 |
protected void fireTransferReceivePausedEvent() { |
| 1079 |
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "fireTransferReceivePausedEvent len=" + fileLength + ";rcvd=" + bytesReceived); //$NON-NLS-1$ //$NON-NLS-2$ |
| 1080 |
super.fireTransferReceivePausedEvent(); |
| 1081 |
} |
| 1082 |
|
| 1083 |
} |