uk.ac.starlink.util
Class PropertyAuthenticator

java.lang.Object
  extended by java.net.Authenticator
      extended by uk.ac.starlink.util.PropertyAuthenticator

public class PropertyAuthenticator
extends java.net.Authenticator

Authenticator implementation which uses system properties to set fixed username and passwords for HTTP Basic Authentication. The username and password are acquired from the system properties "star.basicauth.user" and "star.basicauth.password". A convenience method installInstance(boolean) is provided to set this authenticator up for use in all HTTP connection attempts.

Since this uses the same username and password for all web sites, it's obviously a bit of a blunt instrument. It may be refined at some point in the future.

Since:
10 Aug 2011

Nested Class Summary
 
Nested classes/interfaces inherited from class java.net.Authenticator
java.net.Authenticator.RequestorType
 
Field Summary
static java.lang.String PASSWORD_PROP
          System property supplying basic authentication password ("star.basicauth.password").
static java.lang.String USER_PROP
          System property supplying basic authentication username ("star.basicauth.user").
 
Constructor Summary
PropertyAuthenticator()
          Constructor.
 
Method Summary
static java.net.PasswordAuthentication createAuthentication()
          Returns a PasswordAuthentication instance suitable for use with PropertyAuthenticator.
 java.net.PasswordAuthentication getPasswordAuthentication()
           
static boolean installInstance(boolean offerAdvice)
          Installs an instance of PropertyAuthenticator so that it is used automatically in response to all 401 Unauthorized HTTP responses.
 
Methods inherited from class java.net.Authenticator
getRequestingHost, getRequestingPort, getRequestingPrompt, getRequestingProtocol, getRequestingScheme, getRequestingSite, getRequestingURL, getRequestorType, requestPasswordAuthentication, requestPasswordAuthentication, requestPasswordAuthentication, setDefault
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

USER_PROP

public static final java.lang.String USER_PROP
System property supplying basic authentication username ("star.basicauth.user").

See Also:
Constant Field Values

PASSWORD_PROP

public static final java.lang.String PASSWORD_PROP
System property supplying basic authentication password ("star.basicauth.password").

See Also:
Constant Field Values
Constructor Detail

PropertyAuthenticator

public PropertyAuthenticator()
Constructor.

Method Detail

getPasswordAuthentication

public java.net.PasswordAuthentication getPasswordAuthentication()
Overrides:
getPasswordAuthentication in class java.net.Authenticator

createAuthentication

public static java.net.PasswordAuthentication createAuthentication()
Returns a PasswordAuthentication instance suitable for use with PropertyAuthenticator. It reads the current values of the properties and keeps them for later.

Returns:
authentication

installInstance

public static boolean installInstance(boolean offerAdvice)
Installs an instance of PropertyAuthenticator so that it is used automatically in response to all 401 Unauthorized HTTP responses. The authenticator is only installed if the properties are present. If the offerAdvice parameter is true, then if the properties are not set up, an authenticator is installed which issues a message advising how to use system properties to get the authenticator working next time.

Parameters:
offerAdvice - if true, install an advising authenticator if the property one isn't going to work
Returns:
true iff an authenticator was installed; if offerAdvice is true, will always return true
See Also:
Authenticator.setDefault(java.net.Authenticator)