private ExecutionContext resolveIdentitiesIfRequired(final ExecutionContext ctx) {
if (ctx instanceof ExecutionContextWithTokens) {
ExecutionContextWithTokens contextWithTokens = (ExecutionContextWithTokens) ctx;
if (identityResolver == null) {
contextWithTokens.setIdentityChain(new IdentityChainImpl(new ArrayList<Identity>()));
// if there's no identity resolver then it can't tokenise any tokens back to the transport..
contextWithTokens.getIdentityTokens().clear();
return contextWithTokens;
}
else {
IdentityChain chain = new IdentityChainImpl();
try {
identityResolver.resolve(chain, contextWithTokens);
}
catch (InvalidCredentialsException e) {
if (e.getCredentialFaultCode() != null) { // Check if a custom error code should be used
ServerFaultCode sfc = ServerFaultCode.getByCredentialFaultCode(e.getCredentialFaultCode());
throw new CougarFrameworkException(sfc, "Credentials supplied were invalid", e);
}
throw new CougarFrameworkException(ServerFaultCode.SecurityException, "Credentials supplied were invalid", e);
}
// ensure the identity chain set in the context is immutable
contextWithTokens.setIdentityChain(new IdentityChainImpl(chain.getIdentities()));
contextWithTokens.getIdentityTokens().clear();
List<IdentityToken> tokens = identityResolver.tokenise(contextWithTokens.getIdentity());
if (tokens != null) {
contextWithTokens.getIdentityTokens().addAll(tokens);
}