com.novell.sentinel.client.http.java
Class JavaRequestorBase<T>

java.lang.Object
  extended by com.novell.sentinel.client.http.java.JavaRequestorBase<T>
Type Parameters:
T - The type of data interface (e.g., JSONParser or Parser) that will be reported as the result of the HTTP request.
Direct Known Subclasses:
HttpRequestorAsyncJava, HttpRequestorSyncJava

public abstract class JavaRequestorBase<T>
extends Object

Base implementation for synchronous and asynchronous HttpRequestor interfaces.


Nested Class Summary
static class JavaRequestorBase.DefaultHostnameVerifier
          Implementation of HostnameVerifier that returns true regardless of the hostname.
protected static class JavaRequestorBase.NcacFault
           
static class JavaRequestorBase.TestTrustManager
          A TrustManager implementation useful only for testing.
 
Field Summary
static String CHAR_ENCODING
          The character encoding used by HTTP requests/responses issued/received by this class ("UTF-8").
 
Constructor Summary
JavaRequestorBase()
           
 
Method Summary
 void authenticate(String username, String password, UriConstructor url)
          Authenticate to the Sentinel server and obtain a SAML token which will be used for HTTP requests made through this instance.
 void deleteToken(String username, String password, UriConstructor url)
          Delete the SAML token that is contained in this instance.
 HostnameVerifier getHostnameVerifier()
          Get the HostnameVerifier implementation in use for this instance.
 String getLocaleName()
          Implementation of HttpRequestor.getLocaleName().
protected abstract  String getMediaType()
          Derived classes must return the media type to use for requests/responses.
protected abstract  JavaRequestorBase.NcacFault getNcacFault(InputStream source)
          Derived classes must attempt to return Ncac fault information found in the passed InputStream.
protected abstract  T getParser(InputStream source)
          Get a parser (or whatever data source type is being used) that will return data from the passed InputStream.
 String getSAMLToken()
          Get the Sentinel SAML token in use by this instance, or null if there is none.
protected  SecureRandom getSecureRandom()
          Supply a SecureRandom object for use in TLS/SSL.
protected  SSLSocketFactory getSocketFactory()
          Get the SSLSocketFactory to use.
 SSLSocketFactory getSSLSocketFactory()
          Get the SSLSocketFactory implementation in use by this instance.
protected abstract  String getToken(InputStream source)
          Derived classes must return the Base64-encoded Sentinel SAML token returned in response to the POST request issued by the authenticate method.
 TrustManager getTrustManager()
          Get the TrustManager implementation current set in this instance.
protected  byte[] issueOctetRequest(HttpRequestMethod.HttpMethod method, String url, byte[] data)
          Method for derived classes to issue an HTTP request that deals with octet stream data.
protected  T issueRequest(HttpRequestMethod.HttpMethod method, String url, T parser)
          Method for derived classes to issue an HTTP request.
protected abstract  void sendData(OutputStream dest, T parser)
          Given the passed data source (parser), serialize the data from the data source to the passed OutputStream.
 void setHostnameVerifier(HostnameVerifier verifier)
          Set the HostnameVerifier implementation to use in establishing the TLS/SSL connection.
protected  void setLanguageHeaders(HttpURLConnection connection)
           
 void setLocaleName(String localeName)
          Implementation of HttpRequestor.setLocaleName(String).
protected  void setMediaTypeHeaders(HttpURLConnection connection)
           
 void setSAMLToken(String token)
          Set the Sentinel SAML token for use by this instance.
 void setSSLSocketFactory(SSLSocketFactory socketFactory)
          Set the SSLSocketFactory implementation for use by this instance.
 void setTrustManager(TrustManager trustManager)
          Set the TrustManager implementation to be used for the TLS/SSL connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHAR_ENCODING

public static final String CHAR_ENCODING
The character encoding used by HTTP requests/responses issued/received by this class ("UTF-8").

See Also:
Constant Field Values
Constructor Detail

JavaRequestorBase

public JavaRequestorBase()
Method Detail

setLocaleName

public void setLocaleName(String localeName)
Implementation of HttpRequestor.setLocaleName(String).

Parameters:
localeName - Locale name to use in HTTP "Accept-Language" header (may be null).

getLocaleName

public String getLocaleName()
Implementation of HttpRequestor.getLocaleName().

Returns:
Locale name (may be null).

authenticate

public void authenticate(String username,
                         String password,
                         UriConstructor url)
                  throws HttpException
Authenticate to the Sentinel server and obtain a SAML token which will be used for HTTP requests made through this instance.

Parameters:
username - The name of the Sentinel user.
password - The password of the Sentinel user.
url - A UriConstructor instance with at least the scheme, host, and port specified. If no URI path is specified, "SentinelAuthServices/auth/tokens" is appended to the passed information.
Throws:
HttpException - If an error occurs.

deleteToken

public void deleteToken(String username,
                        String password,
                        UriConstructor url)
                 throws HttpException
Delete the SAML token that is contained in this instance. Using this method effectively "logs off" this instance.

Parameters:
username - The name of the Sentinel user.
password - The password of the Sentinel user.
url - A UriConstructor instance with at least the scheme, host, and port specified. If no URI path is specified, "SentinelAuthServices/auth/tokens" is appended to the passed information.
Throws:
HttpException - If an error occurs.

setTrustManager

public void setTrustManager(TrustManager trustManager)
Set the TrustManager implementation to be used for the TLS/SSL connection.

If no TrustManager is set, the default TrustManager used by SSLSocketFactory.getDefault() will be used, which will typically disallow connections to the Sentinel server because Sentinel by default uses self-signed certificates.

For testing, an instance of TestTrustManager can be used, but TestTrustManager should not be used in production because no certificate checking is performed at all.

Parameters:
trustManager - The implementation to use.

getTrustManager

public TrustManager getTrustManager()
Get the TrustManager implementation current set in this instance.

Returns:
The implementation or null.

setHostnameVerifier

public void setHostnameVerifier(HostnameVerifier verifier)
Set the HostnameVerifier implementation to use in establishing the TLS/SSL connection. If no implementation is explicitly set, this instance will use an instance of DefaultHostnameVerifier which will accept any hostname.

Parameters:
verifier - The implementation to use (must not be null).

getHostnameVerifier

public HostnameVerifier getHostnameVerifier()
Get the HostnameVerifier implementation in use for this instance.

Returns:
The implementation (will not be null).

setSSLSocketFactory

public void setSSLSocketFactory(SSLSocketFactory socketFactory)
Set the SSLSocketFactory implementation for use by this instance. If none is explicitly set using this method then the method getSocketFactory() is called to obtain a default implementation.

Parameters:
socketFactory -

getSSLSocketFactory

public SSLSocketFactory getSSLSocketFactory()
Get the SSLSocketFactory implementation in use by this instance.

Returns:
This implementation instance in use.

getSAMLToken

public String getSAMLToken()
Get the Sentinel SAML token in use by this instance, or null if there is none.

Returns:
Base64-encoded SAML token, or null.

setSAMLToken

public void setSAMLToken(String token)
Set the Sentinel SAML token for use by this instance. Calling authenticate will implicitly set the SAML token.

Parameters:
token -
See Also:
authenticate(String, String, UriConstructor)

issueRequest

protected T issueRequest(HttpRequestMethod.HttpMethod method,
                         String url,
                         T parser)
                  throws HttpException
Method for derived classes to issue an HTTP request.

Parameters:
method - The request type (HttpMethod.GET, HttpMethod.PUT, etc.)
url - The URL of the request.
parser - The source from which any request content will be obtained (may be null for requests without content).
Returns:
The source from which any result content can be obtained.
Throws:
HttpException - if an error occurs making the request.

issueOctetRequest

protected byte[] issueOctetRequest(HttpRequestMethod.HttpMethod method,
                                   String url,
                                   byte[] data)
                            throws HttpException
Method for derived classes to issue an HTTP request that deals with octet stream data.

Parameters:
method - The request type (HttpMethod.GET, HttpMethod.PUT, etc.)
url - The URL of the request.
data - The request content, if any (may be null).
Returns:
The source from which any result content can be obtained.
Throws:
HttpException - if an error occurs making the request.

getSocketFactory

protected SSLSocketFactory getSocketFactory()
Get the SSLSocketFactory to use. This base implementation returns the value set by setSSLSocketFactory, or if none has been explicitly set, creates an instance using either one of two methods:
  1. If no TrustManager implementation has been set using setTrustManager then SSLSocketFactory.getDefault() is called
  2. If a TrustManager implementat has been set using setTrustManager then SSLContext.getInstance("TLS") is called and the returned SSLContext instance is initialized with no KeyManager, the TrustManager instance set by setTrustManager, and the SecureRandom object returned by getSecureRandom. The SSLSocketFactory is then obtained by calling SSLContext.getSocketFactory on the initialized SSLContext instance.
  3. Returns:
    The SSLSocketFactory implementation to use.

getSecureRandom

protected SecureRandom getSecureRandom()
Supply a SecureRandom object for use in TLS/SSL. This will only be called if a TrustManager implementation has been set for this instance.

The default implementation returns a default SecureRandom instance (constructed with the default constructor) that is shared among all instances of this class.

Returns:
A SecureRandom instance for use in TLS/SSL connections.

setMediaTypeHeaders

protected void setMediaTypeHeaders(HttpURLConnection connection)

setLanguageHeaders

protected void setLanguageHeaders(HttpURLConnection connection)

getMediaType

protected abstract String getMediaType()
Derived classes must return the media type to use for requests/responses. This will depend on the parameter used for (e.g., "application/json" or application/xml").

Returns:
The media type string.

getToken

protected abstract String getToken(InputStream source)
                            throws IOException
Derived classes must return the Base64-encoded Sentinel SAML token returned in response to the POST request issued by the authenticate method.

Parameters:
source - The response data from the server.
Returns:
The Base64-encoded SAML token.
Throws:
IOException - If an error occurs reading the token.

getParser

protected abstract T getParser(InputStream source)
Get a parser (or whatever data source type is being used) that will return data from the passed InputStream. The parser will typically be passed to an HttpResultHandler's onSuccess method.

Parameters:
source - The InputStream.
Returns:
An implementation that can be used to obtain data.

sendData

protected abstract void sendData(OutputStream dest,
                                 T parser)
                          throws IOException
Given the passed data source (parser), serialize the data from the data source to the passed OutputStream.

Parameters:
dest - The OutputStream to which to write the data.
parser - The data source containing the data.
Throws:
IOException - If an error occurs writing the data.

getNcacFault

protected abstract JavaRequestorBase.NcacFault getNcacFault(InputStream source)
                                                     throws IOException
Derived classes must attempt to return Ncac fault information found in the passed InputStream. If no valid Ncac fault information can be parsed, then the implementation must throw an exception.

Parameters:
source - The error stream from the Http request.
Returns:
An NcacFault instance.
Throws:
IOException - If any error occurs attempting to parse Ncac fault information.