use of io.gravitee.rest.api.service.exceptions.TokenNameAlreadyExistsException in project gravitee-management-rest-api by gravitee-io.
the class TokenServiceImpl method create.
@Override
public TokenEntity create(NewTokenEntity newToken) {
try {
final String username = getAuthenticatedUsername();
// check if name already exists
final List<TokenEntity> tokens = findByUser(username);
final boolean nameAlreadyExists = tokens.stream().anyMatch(token -> newToken.getName().equalsIgnoreCase(token.getName()));
if (nameAlreadyExists) {
throw new TokenNameAlreadyExistsException(newToken.getName());
}
final String decodedToken = UUID.toString(UUID.random());
final Token token = convert(newToken, TokenReferenceType.USER, username, passwordEncoder.encode(decodedToken));
auditService.createEnvironmentAuditLog(Collections.singletonMap(TOKEN, token.getId()), TOKEN_CREATED, token.getCreatedAt(), null, token);
return convert(tokenRepository.create(token), decodedToken);
} catch (TechnicalException e) {
final String error = "An error occurs while trying to create a token " + newToken;
LOGGER.error(error, e);
throw new TechnicalManagementException(error, e);
}
}
Aggregations