views:

672

answers:

1

I'm building an eclipse plugin that talks to a REST interface which uses Basic Authentication. When the authentication fails i would like to popup my plugin's settings dialog and retry. Normally I could use the static Authenticator.setDefault() to setup an authenticator for all HttpURLConnection's for this but since i am writing a plugin i don't want to overwrite eclipse's default Authenticator (org.eclipse.ui.internal.net.auth);

I thought of setting my custom Authenticator before loading and putting eclipses default back afterwards but i imagine this will cause all sorts of race issues with multithreading so i quickly lost that notion.

Google searches yeild all sorts of results basically telling me its not possible:

The Java URLConnection API should have a setAuthenticator(Authenticator) method for making it easier to use this class in multi-threaded context where authentication is required.

source

If applications contains few third party plugins and each plugin use its own Authenticator what we should do ? Each invocation of "Authenticator.setDefault()" method rewrite previously defined Authenticator...

source

Any different approaches that might help me overcome this issue ?

+1  A: 

If it is not possible with HttpURLConnection I would suggest using the httpclient library from Apache.

A quick example:

HttpClient client = new HttpClient();
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("test","test"));
GetMethod getMethod = new GetMethod("http://www.example.com/mylogin");
client.executeMethod(getMethod);
System.out.println(getMethod.getResponseBodyAsString());
Rich
yeah The apache commoms http client is what i had settled on using if this issue with the JRE default HttpURLConnection is onovercomeable. Thanks for the answer though!
Martijn Laarman
In the end opting for a 3rd party Http class was the only viable solution, so i'll accept your answer.
Martijn Laarman