Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorslewis2007-12-06 07:50:52 +0000
committerslewis2007-12-06 07:50:52 +0000
commite06af1fb1e96817c0f7aa4d8dc1162dec247bd1d (patch)
tree424d3b7c18c8fe5081d870c458de053d9bac265b /providers
parente44e611cac7d725a3e0460df926a95bdc73db3b9 (diff)
downloadorg.eclipse.ecf-e06af1fb1e96817c0f7aa4d8dc1162dec247bd1d.tar.gz
org.eclipse.ecf-e06af1fb1e96817c0f7aa4d8dc1162dec247bd1d.tar.xz
org.eclipse.ecf-e06af1fb1e96817c0f7aa4d8dc1162dec247bd1d.zip
Added support for using platform proxy settings for both URLConnection-based and httpclient-based file transfer
Diffstat (limited to 'providers')
-rw-r--r--providers/bundles/org.eclipse.ecf.provider.filetransfer/META-INF/MANIFEST.MF3
-rw-r--r--providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/internal/provider/filetransfer/Activator.java15
-rw-r--r--providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/AbstractRetrieveFileTransfer.java45
-rw-r--r--providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/JREProxyHelper.java77
-rw-r--r--providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/UrlConnectionRetrieveFileTransfer.java20
5 files changed, 157 insertions, 3 deletions
diff --git a/providers/bundles/org.eclipse.ecf.provider.filetransfer/META-INF/MANIFEST.MF b/providers/bundles/org.eclipse.ecf.provider.filetransfer/META-INF/MANIFEST.MF
index 40244f92d..0f65aa530 100644
--- a/providers/bundles/org.eclipse.ecf.provider.filetransfer/META-INF/MANIFEST.MF
+++ b/providers/bundles/org.eclipse.ecf.provider.filetransfer/META-INF/MANIFEST.MF
@@ -13,7 +13,8 @@ Require-Bundle: org.eclipse.equinox.common,
Eclipse-LazyStart: true
Bundle-RequiredExecutionEnvironment: CDC-1.0/Foundation-1.0,
J2SE-1.3
-Import-Package: org.eclipse.core.runtime.jobs,
+Import-Package: org.eclipse.core.net.proxy;resolution:=optional,
+ org.eclipse.core.runtime.jobs,
org.eclipse.osgi.util;version="1.0.0",
org.osgi.framework;version="1.3.0",
org.osgi.service.log;version="1.3.0",
diff --git a/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/internal/provider/filetransfer/Activator.java b/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/internal/provider/filetransfer/Activator.java
index a3951adf7..c37502556 100644
--- a/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/internal/provider/filetransfer/Activator.java
+++ b/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/internal/provider/filetransfer/Activator.java
@@ -14,6 +14,7 @@ import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
+import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.core.runtime.*;
import org.eclipse.ecf.core.util.LogHelper;
import org.eclipse.ecf.core.util.PlatformHelper;
@@ -60,6 +61,8 @@ public class Activator implements BundleActivator {
private ServiceTracker adapterManagerTracker = null;
+ private ServiceTracker proxyServiceTracker = null;
+
/**
* The constructor
*/
@@ -75,6 +78,14 @@ public class Activator implements BundleActivator {
return (LogService) logServiceTracker.getService();
}
+ public IProxyService getProxyService() {
+ if (proxyServiceTracker == null) {
+ proxyServiceTracker = new ServiceTracker(this.context, IProxyService.class.getName(), null);
+ proxyServiceTracker.open();
+ }
+ return (IProxyService) proxyServiceTracker.getService();
+ }
+
public void log(IStatus status) {
final LogService logService = getLogService();
if (logService != null) {
@@ -122,6 +133,10 @@ public class Activator implements BundleActivator {
adapterManagerTracker.close();
adapterManagerTracker = null;
}
+ if (proxyServiceTracker != null) {
+ proxyServiceTracker.close();
+ proxyServiceTracker = null;
+ }
this.context = null;
this.fileTransferProtocolMap = null;
}
diff --git a/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/AbstractRetrieveFileTransfer.java b/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/AbstractRetrieveFileTransfer.java
index 7c06685c8..4aae88146 100644
--- a/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/AbstractRetrieveFileTransfer.java
+++ b/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/AbstractRetrieveFileTransfer.java
@@ -12,10 +12,14 @@ import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
+import org.eclipse.core.net.proxy.IProxyData;
+import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.identity.Namespace;
+import org.eclipse.ecf.core.util.Proxy;
+import org.eclipse.ecf.core.util.ProxyAddress;
import org.eclipse.ecf.filetransfer.*;
import org.eclipse.ecf.filetransfer.events.*;
import org.eclipse.ecf.filetransfer.identity.IFileID;
@@ -61,6 +65,8 @@ public abstract class AbstractRetrieveFileTransfer implements IIncomingFileTrans
protected IFileRangeSpecification rangeSpecification = null;
+ protected Proxy proxy;
+
protected URL getRemoteFileURL() {
return remoteFileURL;
}
@@ -447,6 +453,45 @@ public abstract class AbstractRetrieveFileTransfer implements IIncomingFileTrans
throw new IncomingFileTransferException(NLS.bind(Messages.AbstractRetrieveFileTransfer_MalformedURLException, rFileID), e);
}
this.listener = transferListener;
+ setupProxies();
openStreams();
}
+
+ /**
+ * Setup ECF proxy. Subclasses must override this method to do appropriate proxy setup. This method will be called
+ * from within {@link #sendRetrieveRequest(IFileID, IFileTransferListener, Map)} and {@link #sendRetrieveRequest(IFileID, IFileRangeSpecification, IFileTransferListener, Map)},
+ * prior to the actual call to {@link #openStreams()}.
+ * @param proxy the proxy to be setup. Will not be <code>null</code>.
+ */
+ protected abstract void setupProxy(Proxy proxy);
+
+ protected void setupProxies() {
+ // If it's been set directly (via ECF API) then this overrides platform settings
+ try {
+ if (proxy == null) {
+ IProxyService proxyService = Activator.getDefault().getProxyService();
+ // Only do this if platform service exists
+ if (proxyService != null) {
+ // Setup via proxyService entry
+ URL target = getRemoteFileURL();
+ String type = IProxyData.SOCKS_PROXY_TYPE;
+ if (target.getProtocol().equalsIgnoreCase(IProxyData.HTTP_PROXY_TYPE)) {
+ type = IProxyData.HTTP_PROXY_TYPE;
+ } else if (target.getProtocol().equalsIgnoreCase(IProxyData.HTTPS_PROXY_TYPE)) {
+ type = IProxyData.HTTPS_PROXY_TYPE;
+ }
+ final IProxyData proxyData = proxyService.getProxyDataForHost(target.getHost(), type);
+ if (proxyData != null) {
+ proxy = new Proxy(((type.equalsIgnoreCase(IProxyData.SOCKS_PROXY_TYPE)) ? Proxy.Type.SOCKS : Proxy.Type.HTTP), new ProxyAddress(proxyData.getHost(), proxyData.getPort()), proxyData.getUserId(), proxyData.getPassword());
+ }
+ }
+ }
+ if (proxy != null)
+ setupProxy(proxy);
+ } catch (Exception e) {
+ // If we don't even have the classes for this (i.e. the org.eclipse.core.net plugin not available)
+ // then we simply log and ignore
+ Activator.getDefault().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.ERROR, "Warning: Platform proxy API not available", e)); //$NON-NLS-1$
+ }
+ }
}
diff --git a/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/JREProxyHelper.java b/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/JREProxyHelper.java
new file mode 100644
index 000000000..8bddd2475
--- /dev/null
+++ b/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/JREProxyHelper.java
@@ -0,0 +1,77 @@
+/****************************************************************************
+ * Copyright (c) 2007 Composent, Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Composent, Inc. - initial API and implementation
+ *****************************************************************************/
+
+package org.eclipse.ecf.provider.filetransfer.retrieve;
+
+import java.net.Authenticator;
+import java.net.PasswordAuthentication;
+import java.util.Properties;
+import org.eclipse.ecf.core.util.Proxy;
+
+/**
+ * Helper class for setting the JRE proxy value (http and socks).
+ */
+public class JREProxyHelper {
+
+ private static final String SOCKS_PROXY_PORT_SYSTEM_PROPERTY = "socksProxyPort"; //$NON-NLS-1$
+
+ private static final String SOCKS_PROXY_HOST_SYSTEM_PROPERTY = "socksProxyHost"; //$NON-NLS-1$
+
+ private static final String HTTP_PROXY_PORT_SYSTEM_PROPERTY = "http.proxyPort"; //$NON-NLS-1$
+
+ private static final String HTTP_PROXY_HOST_SYSTEM_PROPERTY = "http.proxyHost"; //$NON-NLS-1$
+
+ private String proxyHostProperty;
+ private String proxyPortProperty;
+
+ private String oldHost;
+ private String oldPort;
+
+ public void setupProxy(final Proxy proxy2) {
+ Properties systemProperties = System.getProperties();
+ proxyHostProperty = (proxy2.getType().equals(Proxy.Type.HTTP)) ? HTTP_PROXY_HOST_SYSTEM_PROPERTY : SOCKS_PROXY_HOST_SYSTEM_PROPERTY;
+ proxyPortProperty = (proxy2.getType().equals(Proxy.Type.HTTP)) ? HTTP_PROXY_PORT_SYSTEM_PROPERTY : SOCKS_PROXY_PORT_SYSTEM_PROPERTY;
+ oldHost = systemProperties.getProperty(proxyHostProperty);
+ if (oldHost != null) {
+ oldPort = systemProperties.getProperty(proxyPortProperty);
+ }
+ systemProperties.setProperty(proxyHostProperty, proxy2.getAddress().getHostName());
+ int proxyPort = proxy2.getAddress().getPort();
+ if (proxyPort != -1)
+ systemProperties.setProperty(proxyPortProperty, proxyPort + ""); //$NON-NLS-1$
+ final String username = proxy2.getUsername();
+ if (username != null && !username.equals("")) { //$NON-NLS-1$
+ final String password = (proxy2.getPassword() == null) ? "" : proxy2.getPassword(); //$NON-NLS-1$
+ if (proxy2.hasCredentials()) {
+ Authenticator.setDefault(new Authenticator() {
+ /* (non-Javadoc)
+ * @see java.net.Authenticator#getPasswordAuthentication()
+ */
+ protected PasswordAuthentication getPasswordAuthentication() {
+ return new PasswordAuthentication(username, password.toCharArray());
+ }
+ });
+ }
+ }
+ }
+
+ public void dispose() {
+ // reset old values
+ if (oldHost != null) {
+ System.getProperties().setProperty(proxyHostProperty, oldHost);
+ oldHost = null;
+ if (oldPort != null) {
+ System.getProperties().setProperty(proxyPortProperty, oldPort);
+ oldPort = null;
+ }
+ }
+ }
+}
diff --git a/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/UrlConnectionRetrieveFileTransfer.java b/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/UrlConnectionRetrieveFileTransfer.java
index f20ac27bf..dffde6f07 100644
--- a/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/UrlConnectionRetrieveFileTransfer.java
+++ b/providers/bundles/org.eclipse.ecf.provider.filetransfer/src/org/eclipse/ecf/provider/filetransfer/retrieve/UrlConnectionRetrieveFileTransfer.java
@@ -28,8 +28,6 @@ public class UrlConnectionRetrieveFileTransfer extends AbstractRetrieveFileTrans
// XXX currently unused
protected IConnectContext connectContext;
- // XXX currently unused
- protected Proxy proxy;
protected long lastModifiedTime = 0L;
@@ -41,6 +39,13 @@ public class UrlConnectionRetrieveFileTransfer extends AbstractRetrieveFileTrans
protected IFileID fileid = null;
+ private JREProxyHelper proxyHelper = null;
+
+ public UrlConnectionRetrieveFileTransfer() {
+ super();
+ proxyHelper = new JREProxyHelper();
+ }
+
protected void connect() throws IOException {
urlConnection = getRemoteFileURL().openConnection();
}
@@ -134,6 +139,13 @@ public class UrlConnectionRetrieveFileTransfer extends AbstractRetrieveFileTrans
throw new IOException(Messages.UrlConnectionRetrieveFileTransfer_EXCEPTION_FILE_MODIFIED_SINCE_LAST_ACCESS);
}
+ /**
+ * @param proxy2 the ECF proxy to setup
+ */
+ protected void setupProxy(final Proxy proxy2) {
+ proxyHelper.setupProxy(proxy2);
+ }
+
/*
* (non-Javadoc)
*
@@ -161,6 +173,10 @@ public class UrlConnectionRetrieveFileTransfer extends AbstractRetrieveFileTrans
super.hardClose();
urlConnection = null;
responseCode = -1;
+ if (proxyHelper != null) {
+ proxyHelper.dispose();
+ proxyHelper = null;
+ }
}
/*

Back to the top