The following demonstrates how to re-use the Eclipse proxy preferences in an Eclipse RCP application.
Create a project “de.vogella.rcp.net.proxy” and select the “RCP application with a view” as template.
Add the preferences command to your application. See Eclipse Preferences Tutorial for details.
Add the plugin “org.eclipse.ui.net” and “org.eclipse.core.net” as dependency to your plugin.
Run your plugin and open preferences.
Fantastic! You already have the preference dialog.
Now change your view to the following.
package de.vogella.rcp.net.proxy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.osgi.framework.FrameworkUtil;
import org.osgi.util.tracker.ServiceTracker;
public class View extends ViewPart {
public static final String ID = "de.vogella.rcp.net.proxy.view";
private final ServiceTracker proxyTracker;
public View() {
proxyTracker = new ServiceTracker(FrameworkUtil.getBundle(
this.getClass()).getBundleContext(), IProxyService.class
.getName(), null);
proxyTracker.open();
}
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
StyledText text = new StyledText(parent, SWT.NONE);
text.setText(readWebpage());
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
}
private String readWebpage() {
BufferedReader in = null;
StringBuffer sb = new StringBuffer();
try {
URI uri = new URI("http://www.vogella.de");
IProxyService proxyService = getProxyService();
IProxyData[] proxyDataForHost = proxyService.select(uri);
for (IProxyData data : proxyDataForHost) {
if (data.getHost() != null) {
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", data.getHost());
}
if (data.getHost() != null) {
System.setProperty("http.proxyPort", String.valueOf(data
.getPort()));
}
}
// Close the service and close the service tracker
proxyService = null;
URL url;
url = uri.toURL();
in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
// Process each line.
sb.append(inputLine + "\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
public IProxyService getProxyService() {
return (IProxyService) proxyTracker.getService();
}
@Override
public void dispose() {
proxyTracker.close();
super.dispose();
}
}
If you run your application behind a firewall and if you have maintained the proxy correctly then the View should display the HTML code.
Hope this helps!

In getProxyService() you open a tracker without closing it. Everytime you call getProxyService(), this adds a new service listener to the bundle context without removing it.
Also, as far as I know, the line
if (data.getType() == IProxyData.HTTP_PROXY_TYPE) {
is not necessary. ProxyService.select(uri) returns only ProxyData that match the URI, i.e. in this case they all have the type HTTP_PROXY_TYPE. I usually just pick the first element of the array (if there is one), but I also have never seen more than one proxy data returned by select.
And as a bonus, if this line was removed, the code would also work for URIs with other protocols like HTTPS.
@George: Thanks a lot for the feedback. I tried to adjust the code according to your suggestions. I now use
/ Close the service and close the service tracker
proxyService=null;
proxyTracker.close();
and removed the check for IProxyData.HTTP_PROXY_TYPE.
Thanks a lot.
Lars, I always like reading your site/blog! Good work!
A little tip for the use of ServiceTracker. Just create it as a final class field. Then you can call getService() as often as you want and just close the tracker in the views dispose() method.
Although if your bundle is stopped, the service tracker will be automatically unbound.
@Philipp: Sorry for taking so long to response. I adjusted my code example accoording your advice. Thank you very much for the tip.
If you have taht .net plugins in your configuration the system properties are already set right as you can verify in the about dialogs configuration tab:
http.nonProxyHosts=xx
http.proxyHost=.xx
https.nonProxyHosts=…
Unless you do not use libraries that explicitly require proxy configuration do not modify those system properties. If you have to set system properties distinguish http and https protocolls.