use of org.eclipse.ecf.filetransfer.BrowseFileTransferException in project ecf by eclipse.
the class HttpClientRetrieveFileTransfer method openStreams.
/* (non-Javadoc)
* @see org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer#openStreams()
*/
protected void openStreams() throws IncomingFileTransferException {
// $NON-NLS-1$
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "openStreams");
final String urlString = getRemoteFileURL().toString();
this.doneFired = false;
int code = -1;
try {
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, getSocketReadTimeout());
int connectTimeout = getConnectTimeout();
httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
setupAuthentication(urlString);
getMethod = new HttpGet(urlString);
// Define a CredentialsProvider - found that possibility while debugging in org.apache.commons.httpclient.HttpMethodDirector.processProxyAuthChallenge(HttpMethod)
// Seems to be another way to select the credentials.
setRequestHeaderValues();
// $NON-NLS-1$
Trace.trace(Activator.PLUGIN_ID, "retrieve=" + urlString);
// 2) The target remote file does *not* end in .gz (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=280205)
if (getFileRangeSpecification() == null && !targetHasGzSuffix(super.getRemoteFileName())) {
// $NON-NLS-1$
Trace.trace(Activator.PLUGIN_ID, "Accept-Encoding: gzip,deflate added to request header");
// Add the interceptors to provide the gzip
httpClient.addRequestInterceptor(new RequestAcceptEncoding());
httpClient.addResponseInterceptor(new ResponseContentEncoding());
} else {
// $NON-NLS-1$
Trace.trace(Activator.PLUGIN_ID, "Accept-Encoding NOT added to header");
}
fireConnectStartEvent();
if (checkAndHandleDone()) {
return;
}
connectingSockets.clear();
// redirect response code handled internally
if (connectJob == null) {
performConnect(new NullProgressMonitor());
} else {
connectJob.schedule();
connectJob.join();
connectJob = null;
}
if (checkAndHandleDone()) {
return;
}
code = responseCode;
responseHeaders = getResponseHeaders();
// $NON-NLS-1$
Trace.trace(Activator.PLUGIN_ID, "retrieve resp=" + code);
// Check for NTLM proxy in response headers
// This check is to deal with bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=252002
boolean ntlmProxyFound = NTLMProxyDetector.detectNTLMProxy(httpContext);
if (ntlmProxyFound && !hasForceNTLMProxyOption())
// $NON-NLS-1$
throw new IncomingFileTransferException("HttpClient Provider is not configured to support NTLM proxy authentication.", HttpClientOptions.NTLM_PROXY_RESPONSE_CODE);
if (NTLMProxyDetector.detectSPNEGOProxy(httpContext))
// $NON-NLS-1$
throw new BrowseFileTransferException("HttpClient Provider does not support the use of SPNEGO proxy authentication.");
if (code == HttpURLConnection.HTTP_PARTIAL || code == HttpURLConnection.HTTP_OK) {
getResponseHeaderValues();
setInputStream(httpResponse.getEntity().getContent());
fireReceiveStartEvent();
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
EntityUtils.consume(httpResponse.getEntity());
// $NON-NLS-1$
throw new IncomingFileTransferException(NLS.bind("File not found: {0}", urlString), code);
} else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
EntityUtils.consume(httpResponse.getEntity());
throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Unauthorized, code);
} else if (code == HttpURLConnection.HTTP_FORBIDDEN) {
EntityUtils.consume(httpResponse.getEntity());
// $NON-NLS-1$
throw new IncomingFileTransferException("Forbidden", code);
} else if (code == HttpURLConnection.HTTP_PROXY_AUTH) {
EntityUtils.consume(httpResponse.getEntity());
throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Proxy_Auth_Required, code);
} else {
Trace.trace(Activator.PLUGIN_ID, EntityUtils.toString(httpResponse.getEntity()));
// EntityUtils.consume(httpResponse.getEntity());
throw new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_ERROR_GENERAL_RESPONSE_CODE, new Integer(code)), code);
}
} catch (final Exception e) {
// $NON-NLS-1$
Trace.throwing(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_THROWING, this.getClass(), "openStreams", e);
if (code == -1) {
if (!isDone()) {
setDoneException(e);
}
fireTransferReceiveDoneEvent();
} else {
IncomingFileTransferException ex = (IncomingFileTransferException) ((e instanceof IncomingFileTransferException) ? e : new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_EXCEPTION_COULD_NOT_CONNECT, urlString), e, code));
throw ex;
}
}
// $NON-NLS-1$
Trace.exiting(Activator.PLUGIN_ID, DebugOptions.METHODS_EXITING, this.getClass(), "openStreams");
}
use of org.eclipse.ecf.filetransfer.BrowseFileTransferException in project ecf by eclipse.
the class HttpClientFileSystemBrowser method runRequest.
/* (non-Javadoc)
* @see org.eclipse.ecf.provider.filetransfer.browse.AbstractFileSystemBrowser#runRequest()
*/
protected void runRequest() throws Exception {
// $NON-NLS-1$
Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "runRequest");
setupProxies();
// set timeout
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
String urlString = directoryOrFile.toString();
// setup authentication
setupAuthentication(urlString);
headMethod = new HttpHead(urlString);
// $NON-NLS-1$
int maxAge = Integer.getInteger("org.eclipse.ecf.http.cache.max-age", 0).intValue();
// fix the fix for bug 249990 with bug 410813
if (maxAge == 0) {
// $NON-NLS-1$//$NON-NLS-2$
headMethod.addHeader("Cache-Control", "max-age=0");
} else if (maxAge > 0) {
// $NON-NLS-1$//$NON-NLS-2$
headMethod.addHeader("Cache-Control", "max-age=" + maxAge);
}
long lastModified = 0;
long fileLength = -1;
connectingSockets.clear();
int code = -1;
try {
// $NON-NLS-1$
Trace.trace(Activator.PLUGIN_ID, "browse=" + urlString);
httpContext = new BasicHttpContext();
httpResponse = httpClient.execute(headMethod, httpContext);
code = httpResponse.getStatusLine().getStatusCode();
// $NON-NLS-1$
Trace.trace(Activator.PLUGIN_ID, "browse resp=" + code);
// Check for NTLM proxy in response headers
// This check is to deal with bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=252002
boolean ntlmProxyFound = NTLMProxyDetector.detectNTLMProxy(httpContext);
if (ntlmProxyFound && !hasForceNTLMProxyOption())
// $NON-NLS-1$
throw new BrowseFileTransferException("HttpClient Provider is not configured to support NTLM proxy authentication.", HttpClientOptions.NTLM_PROXY_RESPONSE_CODE);
if (NTLMProxyDetector.detectSPNEGOProxy(httpContext))
// $NON-NLS-1$
throw new BrowseFileTransferException("HttpClient Provider does not support the use of SPNEGO proxy authentication.");
if (code == HttpURLConnection.HTTP_OK) {
Header contentLength = httpResponse.getLastHeader(CONTENT_LENGTH_HEADER);
if (contentLength != null) {
fileLength = Integer.parseInt(contentLength.getValue());
}
lastModified = getLastModifiedTimeFromHeader();
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
// $NON-NLS-1$
throw new BrowseFileTransferException(NLS.bind("File not found: {0}", urlString), code);
} else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
throw new BrowseFileTransferException(Messages.HttpClientRetrieveFileTransfer_Unauthorized, code);
} else if (code == HttpURLConnection.HTTP_FORBIDDEN) {
// $NON-NLS-1$
throw new BrowseFileTransferException("Forbidden", code);
} else if (code == HttpURLConnection.HTTP_PROXY_AUTH) {
throw new BrowseFileTransferException(Messages.HttpClientRetrieveFileTransfer_Proxy_Auth_Required, code);
} else {
throw new BrowseFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_ERROR_GENERAL_RESPONSE_CODE, new Integer(code)), code);
}
remoteFiles = new IRemoteFile[1];
remoteFiles[0] = new URLRemoteFile(lastModified, fileLength, fileID);
} catch (Exception e) {
// $NON-NLS-1$
Trace.throwing(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_THROWING, this.getClass(), "runRequest", e);
BrowseFileTransferException ex = (BrowseFileTransferException) ((e instanceof BrowseFileTransferException) ? e : new BrowseFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_EXCEPTION_COULD_NOT_CONNECT, urlString), e, code));
throw ex;
}
}
use of org.eclipse.ecf.filetransfer.BrowseFileTransferException in project ecf by eclipse.
the class URLFileSystemBrowser method runRequest.
/* (non-Javadoc)
* @see org.eclipse.ecf.provider.filetransfer.browse.AbstractFileSystemBrowser#runRequest()
*/
protected void runRequest() throws Exception {
int code = -1;
try {
setupProxies();
setupAuthentication();
setupTimeouts();
URLConnection urlConnection = directoryOrFile.openConnection();
// this is for addressing bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=235933
if (directoryOrFile.getProtocol().equalsIgnoreCase("jar")) {
// $NON-NLS-1$
urlConnection.setUseCaches(false);
}
// Add http 1.1 'Connection: close' header in order to potentially avoid
// server issue described here https://bugs.eclipse.org/bugs/show_bug.cgi?id=234916#c13
// See bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=247197
// also see http 1.1 rfc section 14-10 in http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
// $NON-NLS-1$ //$NON-NLS-2$
urlConnection.setRequestProperty("Connection", "close");
IURLConnectionModifier connectionModifier = Activator.getDefault().getURLConnectionModifier();
if (connectionModifier != null) {
connectionModifier.setSocketFactoryForConnection(urlConnection);
}
if (urlConnection instanceof HttpURLConnection) {
HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
// $NON-NLS-1$
httpConnection.setRequestMethod("HEAD");
httpConnection.connect();
} else {
InputStream ins = urlConnection.getInputStream();
ins.close();
}
code = getResponseCode(urlConnection);
if (isHTTP()) {
if (code == HttpURLConnection.HTTP_OK) {
// do nothing
} else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
// $NON-NLS-1$
throw new BrowseFileTransferException(NLS.bind("File not found: {0}", directoryOrFile.toString()), code);
} else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
// $NON-NLS-1$
throw new BrowseFileTransferException("Unauthorized", code);
} else if (code == HttpURLConnection.HTTP_FORBIDDEN) {
// $NON-NLS-1$
throw new BrowseFileTransferException("Forbidden", code);
} else if (code == HttpURLConnection.HTTP_PROXY_AUTH) {
// $NON-NLS-1$
throw new BrowseFileTransferException("Proxy auth required", code);
} else {
// $NON-NLS-1$
throw new BrowseFileTransferException(NLS.bind("General connection error with response code={0} and header(0)={1}", new Integer(code), urlConnection.getHeaderField(0)), code);
}
}
remoteFiles = new IRemoteFile[1];
remoteFiles[0] = new URLRemoteFile(urlConnection.getLastModified(), urlConnection.getContentLength(), fileID);
} catch (final FileNotFoundException e) {
// $NON-NLS-1$
throw new IncomingFileTransferException(NLS.bind("File not found: {0}", directoryOrFile.toString()), 404);
} catch (Exception e) {
// $NON-NLS-1$
Exception except = (e instanceof BrowseFileTransferException) ? e : new BrowseFileTransferException(NLS.bind("Could not connect to {0}", directoryOrFile), e, code);
throw except;
}
}
use of org.eclipse.ecf.filetransfer.BrowseFileTransferException in project ecf by eclipse.
the class URLBrowseTest method testBrowseUnknownHost.
public void testBrowseUnknownHost() throws Exception {
testBrowse(new URL(URLRetrieveTestUnknownHost.HTTP_UNKNOWN_HOST_URL));
Thread.sleep(3000);
assertHasEventCount(events, IRemoteFileSystemBrowseEvent.class, 1);
IRemoteFileSystemBrowseEvent event = (IRemoteFileSystemBrowseEvent) events.get(0);
assertNotNull(event);
final IRemoteFile[] remoteFiles = event.getRemoteFiles();
assertNull(remoteFiles);
Exception e = event.getException();
assertNotNull(e);
if (e instanceof BrowseFileTransferException) {
BrowseFileTransferException ifte = (BrowseFileTransferException) e;
assertTrue(ifte.getCause() instanceof UnknownHostException);
} else
fail("Event exception is not instance of BrowseFileTransferException");
}
Aggregations