use of com.b2international.snowowl.core.identity.Token in project snow-owl by b2ihealthcare.
the class TransportClient method connect.
public User connect(final String username, final String password) throws SnowowlServiceException {
try {
this.user = username;
this.password = password;
// initialize connectors first
initConnection();
// try to log in with the specified username and password using the non-authorized bus instance
final Token token = UserRequests.prepareLogin().setUsername(username).setPassword(password).buildAsync().execute(bus).getSync();
// if successfully logged in replace the event bus with an authorized one
env.services().registerService(IEventBus.class, new AuthorizedEventBus(bus, ImmutableMap.of("Authorization", token.getToken())));
env.services().registerService(TransportClient.class, this);
return env.service(AuthorizationHeaderVerifier.class).toUser(token.getToken());
} catch (UnauthorizedException e) {
throw new SnowowlServiceException(e.getMessage());
} catch (final Throwable t) {
final Throwable rootCause = Throwables.getRootCause(t);
final String message = Strings.nullToEmpty(StringUtils.getLine(rootCause.getMessage(), "\n", 0)).replace("\r", "");
LOG.error("Exception caught while connecting to the server.", t);
// FIXME: "Sentiment analysis" for exception messages
if (message.startsWith(COULD_NOT_ACTIVATE_PREFIX)) {
throw new SnowowlServiceException("The server could not be reached. Please verify the connection URL.");
} else if (message.startsWith(ALREADY_LOGGED_IN_PREFIX)) {
throw new SnowowlServiceException("Another client with the same user is already connected to the server.");
} else if (message.startsWith(INCORRECT_USER_NAME_OR_PASSWORD)) {
throw new SnowowlServiceException(message);
} else if (message.startsWith(LOGIN_DISABLED)) {
throw new SnowowlServiceException(message);
} else if (message.startsWith(LDAP_CONNECTION_REFUSED)) {
throw new SnowowlServiceException("The LDAP server could not be reached for authentication. Please contact the administrator.");
} else {
throw new SnowowlServiceException("An unexpected error occurred while connecting to the server. Please contact the administrator.");
}
}
}
Aggregations