{
if (username == null) throw new NullPointerException("username is null");
if (password == null) throw new NullPointerException("password is null");
if (tokenFactory == null) throw new NullPointerException("idp is null");
final Access access = obtainToken(projectName);
ClientRequestFilter tokenFilter = new ClientRequestFilter() {
volatile Access token = access;
@Override
public void filter(ClientRequestContext requestContext) throws IOException
{
Access tmp = token;
if (tmp.getToken().expired())
{
synchronized (this)
{
tmp = token;
if (tmp.getToken().expired())
{
token = tmp = obtainToken(projectName);
}
}
}
requestContext.getHeaders().putSingle("X-Auth-Token", tmp.getToken().getId());
}
};
target.register(tokenFilter);
return access;