Setting outbox calls on user updates / creations, removing not needed inbox events

This commit is contained in:
Thomas Georgios Giannos 2024-01-29 16:51:29 +02:00
parent 953fa2710e
commit 80a3aa5616
17 changed files with 731 additions and 1033 deletions

View File

@ -1,30 +0,0 @@
package eu.eudat.commons.enums.notification;
import com.fasterxml.jackson.annotation.JsonValue;
import eu.eudat.commons.enums.EnumUtils;
import eu.eudat.data.converters.enums.DatabaseEnum;
import java.util.Map;
public enum TentativeUserProfile implements DatabaseEnum<Short> {
Complete((short) 0), Tentative((short) 1);
private final Short value;
TentativeUserProfile(Short value) {
this.value = value;
}
@JsonValue
public Short getValue() {
return value;
}
private static final Map<Short, TentativeUserProfile> map = EnumUtils.getEnumValueMap(TentativeUserProfile.class);
public static TentativeUserProfile of(Short i) {
return map.get(i);
}
}

View File

@ -9,7 +9,6 @@ import gr.cite.rabbitmq.consumer.InboxCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
@ -19,7 +18,9 @@ import java.util.List;
@EnableConfigurationProperties({OutboxProperties.class, InboxProperties.class})
@ConditionalOnProperty(prefix = "queue.rabbitmq", name = "listenerEnabled")
public class AppRabbitConfigurer extends RabbitConfigurer {
private ApplicationContext applicationContext;
private InboxProperties inboxProperties;
public AppRabbitConfigurer(ApplicationContext applicationContext, InboxProperties inboxProperties) {
@ -30,13 +31,10 @@ public class AppRabbitConfigurer extends RabbitConfigurer {
// @Bean
public InboxBindings inboxBindingsCreator() {
List<String> bindingItems = new ArrayList<>();
bindingItems.addAll(this.inboxProperties.getUserRemovalTopic());
bindingItems.addAll(this.inboxProperties.getUserTouchedTopic());
return new InboxBindings(bindingItems);
}
// @Bean
public InboxCreator inboxCreator() {
return (params) -> {

View File

@ -1,42 +1,25 @@
package eu.eudat.integrationevent.inbox;
import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.validation.annotation.Validated;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
@Validated
@ConfigurationProperties(prefix = "queue.task.listener.options")
//@ConstructorBinding
public class InboxProperties {
@NotNull
private final String exchange;
@NotNull
private final List<String> userRemovalTopic;
@NotNull
private final List<String> userTouchedTopic;
public InboxProperties(
String exchange,
List<String> userRemovalTopic,
List<String> userTouchedTopic) {
String exchange
) {
this.exchange = exchange;
this.userRemovalTopic = userRemovalTopic;
this.userTouchedTopic = userTouchedTopic;
}
public List<String> getUserRemovalTopic() {
return userRemovalTopic;
}
public List<String> getUserTouchedTopic() {
return userTouchedTopic;
}
public String getExchange() {
return exchange;
}
}

View File

@ -1,13 +1,10 @@
package eu.eudat.integrationevent.inbox;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.fake.FakeRequestScope;
import eu.eudat.data.QueueInboxEntity;
import eu.eudat.integrationevent.TrackedEvent;
import eu.eudat.integrationevent.inbox.userremoval.UserRemovalIntegrationEventHandler;
import eu.eudat.integrationevent.inbox.usertouched.UserTouchedIntegrationEventHandler;
import eu.eudat.query.QueueInboxQuery;
import gr.cite.queueinbox.entity.QueueInbox;
import gr.cite.queueinbox.entity.QueueInboxStatus;
@ -18,11 +15,13 @@ import gr.cite.rabbitmq.consumer.InboxCreatorParams;
import gr.cite.tools.data.query.Ordering;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.logging.LoggerService;
import jakarta.persistence.*;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.OptimisticLockException;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
@ -31,8 +30,11 @@ import java.util.function.Function;
public class InboxRepositoryImpl implements InboxRepository {
protected final ApplicationContext applicationContext;
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(InboxRepositoryImpl.class));
private final JsonHandlingService jsonHandlingService;
private final InboxProperties inboxProperties;
public InboxRepositoryImpl(
@ -44,7 +46,6 @@ public class InboxRepositoryImpl implements InboxRepository {
this.inboxProperties = inboxProperties;
}
@Override
public CandidateInfo candidate(Instant lastCandidateCreationTimestamp, MessageOptions options) {
EntityTransaction transaction = null;
@ -84,14 +85,17 @@ public class InboxRepositoryImpl implements InboxRepository {
} catch (OptimisticLockException ex) {
// we get this if/when someone else already modified the notifications. We want to essentially ignore this, and keep working
logger.debug("Concurrency exception getting queue inbox. Skipping: {} ", ex.getMessage());
if (transaction != null) transaction.rollback();
if (transaction != null)
transaction.rollback();
candidate = null;
} catch (Exception ex) {
logger.error("Problem getting list of queue inbox. Skipping: {}", ex.getMessage(), ex);
if (transaction != null) transaction.rollback();
if (transaction != null)
transaction.rollback();
candidate = null;
} finally {
if (entityManager != null) entityManager.close();
if (entityManager != null)
entityManager.close();
}
} catch (Exception ex) {
logger.error("Problem getting list of queue inbox. Skipping: {}", ex.getMessage(), ex);
@ -133,10 +137,12 @@ public class InboxRepositoryImpl implements InboxRepository {
transaction.commit();
} catch (Exception ex) {
this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex);
if (transaction != null) transaction.rollback();
if (transaction != null)
transaction.rollback();
success = false;
} finally {
if (entityManager != null) entityManager.close();
if (entityManager != null)
entityManager.close();
}
} catch (Exception ex) {
this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex);
@ -178,10 +184,12 @@ public class InboxRepositoryImpl implements InboxRepository {
transaction.commit();
} catch (Exception ex) {
this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex);
if (transaction != null) transaction.rollback();
if (transaction != null)
transaction.rollback();
success = false;
} finally {
if (entityManager != null) entityManager.close();
if (entityManager != null)
entityManager.close();
}
} catch (Exception ex) {
this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex);
@ -211,10 +219,12 @@ public class InboxRepositoryImpl implements InboxRepository {
transaction.commit();
} catch (Exception ex) {
this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex);
if (transaction != null) transaction.rollback();
if (transaction != null)
transaction.rollback();
success = false;
} finally {
if (entityManager != null) entityManager.close();
if (entityManager != null)
entityManager.close();
}
} catch (Exception ex) {
this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex);
@ -241,7 +251,6 @@ public class InboxRepositoryImpl implements InboxRepository {
return queueMessage;
}
@Override
public Boolean emit(CandidateInfo candidateInfo) {
EntityTransaction transaction = null;
@ -294,10 +303,12 @@ public class InboxRepositoryImpl implements InboxRepository {
transaction.commit();
} catch (Exception ex) {
this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex);
if (transaction != null) transaction.rollback();
if (transaction != null)
transaction.rollback();
success = false;
} finally {
if (entityManager != null) entityManager.close();
if (entityManager != null)
entityManager.close();
}
} catch (Exception ex) {
this.logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex);
@ -306,12 +317,10 @@ public class InboxRepositoryImpl implements InboxRepository {
}
private EventProcessingStatus processMessage(String routingKey, String messageId, String appId, String message) {
IntegrationEventHandler handler;
if (this.RoutingKeyMatched(routingKey, this.inboxProperties.getUserRemovalTopic())) handler = this.applicationContext.getBean(UserRemovalIntegrationEventHandler.class);
else if (this.RoutingKeyMatched(routingKey, this.inboxProperties.getUserTouchedTopic())) handler = this.applicationContext.getBean(UserTouchedIntegrationEventHandler.class);
else handler = null;
IntegrationEventHandler handler = null;
if (handler == null) return EventProcessingStatus.Discard;
if (handler == null)
return EventProcessingStatus.Discard;
IntegrationEventProperties properties = new IntegrationEventProperties();
properties.setAppId(appId);
@ -330,7 +339,8 @@ public class InboxRepositoryImpl implements InboxRepository {
}
private Boolean RoutingKeyMatched(String routingKey, List<String> topics) {
if (topics == null || topics.size() == 0) return false;
if (topics == null || topics.size() == 0)
return false;
return topics.stream().anyMatch(x -> x.equals(routingKey));
}
}

View File

@ -1,5 +1,7 @@
package eu.eudat.integrationevent.inbox;
public interface IntegrationEventHandler {
EventProcessingStatus handle(IntegrationEventProperties properties, String message);
}

View File

@ -1,7 +1,9 @@
package eu.eudat.integrationevent.inbox;
public class IntegrationEventProperties {
private String messageId;
private String appId;
public String getMessageId() {
@ -19,4 +21,5 @@ public class IntegrationEventProperties {
public void setAppId(String appId) {
this.appId = appId;
}
}

View File

@ -1,26 +0,0 @@
package eu.eudat.integrationevent.inbox.userremoval;
import eu.eudat.integrationevent.inbox.ConsistencyHandler;
import eu.eudat.query.UserQuery;
import gr.cite.tools.data.query.QueryFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("inboxuserremovalconsistencyhandler")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserRemovalConsistencyHandler implements ConsistencyHandler<UserRemovalConsistencyPredicates> {
private final QueryFactory queryFactory;
public UserRemovalConsistencyHandler(QueryFactory queryFactory) {
this.queryFactory = queryFactory;
}
@Override
public Boolean isConsistent(UserRemovalConsistencyPredicates consistencyPredicates) {
long count = this.queryFactory.query(UserQuery.class).ids(consistencyPredicates.getUserId()).count();
if (count == 0) return false;
return true;
}
}

View File

@ -1,21 +0,0 @@
package eu.eudat.integrationevent.inbox.userremoval;
import eu.eudat.integrationevent.inbox.ConsistencyPredicates;
import java.util.UUID;
public class UserRemovalConsistencyPredicates implements ConsistencyPredicates {
public UserRemovalConsistencyPredicates(UUID userId) {
this.userId = userId;
}
private UUID userId;
public UUID getUserId() {
return userId;
}
public void setUserId(UUID userId) {
this.userId = userId;
}
}

View File

@ -1,26 +0,0 @@
package eu.eudat.integrationevent.inbox.userremoval;
import eu.eudat.integrationevent.TrackedEvent;
import java.util.UUID;
public class UserRemovalIntegrationEvent extends TrackedEvent {
private UUID userId;
private UUID tenant;
public UUID getUserId() {
return userId;
}
public void setUserId(UUID userId) {
this.userId = userId;
}
public UUID getTenant() {
return tenant;
}
public void setTenant(UUID tenant) {
this.tenant = tenant;
}
}

View File

@ -1,6 +0,0 @@
package eu.eudat.integrationevent.inbox.userremoval;
import eu.eudat.integrationevent.inbox.IntegrationEventHandler;
public interface UserRemovalIntegrationEventHandler extends IntegrationEventHandler {
}

View File

@ -1,125 +0,0 @@
package eu.eudat.integrationevent.inbox.userremoval;
import eu.eudat.audit.AuditableAction;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.fake.FakeRequestScope;
import eu.eudat.data.TenantEntity;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import eu.eudat.integrationevent.inbox.EventProcessingStatus;
import eu.eudat.integrationevent.inbox.InboxPrincipal;
import eu.eudat.integrationevent.inbox.IntegrationEventProperties;
import eu.eudat.query.TenantQuery;
import eu.eudat.service.user.UserService;
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyValidationException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService;
import jakarta.persistence.*;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.AbstractMap;
import java.util.Map;
@Component("inboxuserremovalintegrationeventhandler")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegrationEventHandler {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserRemovalIntegrationEventHandlerImpl.class));
private final JsonHandlingService jsonHandlingService;
protected final ApplicationContext applicationContext;
private final ErrorThesaurusProperties errors;
private final MessageSource messageSource;
public UserRemovalIntegrationEventHandlerImpl(
JsonHandlingService jsonHandlingService,
ApplicationContext applicationContext,
ErrorThesaurusProperties errors,
MessageSource messageSource
) {
this.jsonHandlingService = jsonHandlingService;
this.applicationContext = applicationContext;
this.errors = errors;
this.messageSource = messageSource;
}
@Override
public EventProcessingStatus handle(IntegrationEventProperties properties, String message) {
UserRemovalIntegrationEvent event = this.jsonHandlingService.fromJsonSafe(UserRemovalIntegrationEvent.class, message);
if (event == null) return EventProcessingStatus.Error;
if (event.getUserId() == null) throw new MyValidationException(this.errors.getModelValidation().getCode(), "userId", messageSource.getMessage("Validation_Required", new Object[]{"userId"}, LocaleContextHolder.getLocale()));
EntityManager entityManager = null;
EntityTransaction transaction = null;
try (FakeRequestScope fakeRequestScope = new FakeRequestScope()) {
try {
QueryFactory queryFactory = this.applicationContext.getBean(QueryFactory.class);
// TenantScope scope = this.applicationContext.getBean(TenantScope.class);
// if (scope.isMultitenant() && event.getTenant() != null) {
// TenantEntity tenant = queryFactory.query(TenantQuery.class).ids(event.getTenant()).firstAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
// if (tenant == null) {
// logger.error("missing tenant from event message");
// return EventProcessingStatus.Error;
// }
// scope.setTenant(event.getTenant(), tenant.getCode());
// } else if (scope.isMultitenant()) {
// logger.error("missing tenant from event message");
// return EventProcessingStatus.Error;
// }
CurrentPrincipalResolver currentPrincipalResolver = this.applicationContext.getBean(CurrentPrincipalResolver.class);
currentPrincipalResolver.push(InboxPrincipal.build(properties));
UserRemovalConsistencyHandler userRemovalConsistencyHandler = this.applicationContext.getBean(UserRemovalConsistencyHandler.class);
if (!(userRemovalConsistencyHandler.isConsistent(new UserRemovalConsistencyPredicates(event.getUserId())))) return EventProcessingStatus.Postponed;
EntityManagerFactory entityManagerFactory = this.applicationContext.getBean(EntityManagerFactory.class);
entityManager = entityManagerFactory.createEntityManager();
transaction = entityManager.getTransaction();
transaction.begin();
try {
UserService userService = this.applicationContext.getBean(UserService.class);
userService.deleteAndSave(event.getUserId());
AuditService auditService = this.applicationContext.getBean(AuditService.class);
auditService.track(AuditableAction.User_Delete, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", event.getUserId())
));
//auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
throw e;
} finally {
currentPrincipalResolver.pop();
}
transaction.commit();
} catch (OptimisticLockException ex) {
// we get this if/when someone else already modified the notifications. We want to essentially ignore this, and keep working
this.logger.debug("Concurrency exception getting queue outbox. Skipping: {} ", ex.getMessage());
if (transaction != null) transaction.rollback();
} catch (Exception ex) {
this.logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
if (transaction != null) transaction.rollback();
} finally {
if (entityManager != null) entityManager.close();
}
} catch (Exception ex) {
this.logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
}
return null;
}
}

View File

@ -1,45 +0,0 @@
package eu.eudat.integrationevent.inbox.usertouched;
import eu.eudat.integrationevent.TrackedEvent;
import java.util.UUID;
public class UserTouchedIntegrationEvent extends TrackedEvent {
private UUID id;
private UUID tenant;
private String firstName;
private String lastName;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public UUID getTenant() {
return tenant;
}
public void setTenant(UUID tenant) {
this.tenant = tenant;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View File

@ -1,6 +0,0 @@
package eu.eudat.integrationevent.inbox.usertouched;
import eu.eudat.integrationevent.inbox.IntegrationEventHandler;
public interface UserTouchedIntegrationEventHandler extends IntegrationEventHandler {
}

View File

@ -1,114 +0,0 @@
package eu.eudat.integrationevent.inbox.usertouched;
import eu.eudat.audit.AuditableAction;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.fake.FakeRequestScope;
import eu.eudat.integrationevent.inbox.EventProcessingStatus;
import eu.eudat.integrationevent.inbox.InboxPrincipal;
import eu.eudat.integrationevent.inbox.IntegrationEventProperties;
import eu.eudat.service.user.UserService;
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.logging.LoggerService;
import jakarta.persistence.*;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.AbstractMap;
import java.util.Map;
@Component("inboxusertouchedintegrationeventhandler")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegrationEventHandler {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserTouchedIntegrationEventHandlerImpl.class));
private final JsonHandlingService jsonHandlingService;
protected final ApplicationContext applicationContext;
public UserTouchedIntegrationEventHandlerImpl(
JsonHandlingService jsonHandlingService,
ApplicationContext applicationContext
) {
this.jsonHandlingService = jsonHandlingService;
this.applicationContext = applicationContext;
}
@Override
public EventProcessingStatus handle(IntegrationEventProperties properties, String message) {
UserTouchedIntegrationEvent event = this.jsonHandlingService.fromJsonSafe(UserTouchedIntegrationEvent.class, message);
if (event == null) return EventProcessingStatus.Error;
// UserTouchedIntegrationEventPersist model = new UserTouchedIntegrationEventPersist();
// model.setId(event.getId());
// model.setFirstName(event.getFirstName());
// model.setLastName(event.getLastName());
EntityManager entityManager = null;
EntityTransaction transaction = null;
try (FakeRequestScope fakeRequestScope = new FakeRequestScope()) {
try {
QueryFactory queryFactory = this.applicationContext.getBean(QueryFactory.class);
// TenantScope scope = this.applicationContext.getBean(TenantScope.class);
// if (scope.isMultitenant() && event.getTenant() != null) {
// TenantEntity tenant = queryFactory.query(TenantQuery.class).ids(event.getTenant()).firstAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
// if (tenant == null) {
// logger.error("missing tenant from event message");
// return EventProcessingStatus.Error;
// }
// scope.setTenant(event.getTenant(), tenant.getCode());
// } else if (scope.isMultitenant()) {
// logger.error("missing tenant from event message");
// return EventProcessingStatus.Error;
// }
//
// ValidationService validator = this.applicationContext.getBean(ValidationService.class);
// validator.validateForce(model);
CurrentPrincipalResolver currentPrincipalResolver = this.applicationContext.getBean(CurrentPrincipalResolver.class);
currentPrincipalResolver.push(InboxPrincipal.build(properties));
EntityManagerFactory entityManagerFactory = this.applicationContext.getBean(EntityManagerFactory.class);
entityManager = entityManagerFactory.createEntityManager();
transaction = entityManager.getTransaction();
transaction.begin();
try {
UserService userService = this.applicationContext.getBean(UserService.class);
// userService.persist(model, null);
AuditService auditService = this.applicationContext.getBean(AuditService.class);
auditService.track(AuditableAction.User_Persist, Map.ofEntries(
// new AbstractMap.SimpleEntry<String, Object>("model", model)
));
transaction.commit();
} catch (Exception e) {
transaction.rollback();
throw e;
} finally {
currentPrincipalResolver.pop();
}
transaction.commit();
} catch (OptimisticLockException ex) {
// we get this if/when someone else already modified the notifications. We want to essentially ignore this, and keep working
this.logger.debug("Concurrency exception getting queue outbox. Skipping: {} ", ex.getMessage());
if (transaction != null) transaction.rollback();
} catch (Exception ex) {
this.logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
if (transaction != null) transaction.rollback();
} finally {
if (entityManager != null) entityManager.close();
}
} catch (Exception ex) {
this.logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
}
return null;
}
}

View File

@ -1,9 +1,8 @@
package eu.eudat.integrationevent.outbox.usertouched;
import eu.eudat.commons.enums.notification.TentativeUserProfile;
import eu.eudat.commons.enums.ContactInfoType;
import eu.eudat.commons.enums.notification.UserType;
import eu.eudat.integrationevent.TrackedEvent;
import eu.eudat.model.UserContactInfo;
import java.util.List;
import java.util.UUID;
@ -82,22 +81,12 @@ public class UserTouchedIntegrationEvent extends TrackedEvent {
public static class UserProfile {
private TentativeUserProfile isTentative;
private String timezone;
private String culture;
private String language;
public TentativeUserProfile getIsTentative() {
return isTentative;
}
public void setIsTentative(TentativeUserProfile isTentative) {
this.isTentative = isTentative;
}
public String getTimezone() {
return timezone;
}
@ -123,4 +112,27 @@ public class UserTouchedIntegrationEvent extends TrackedEvent {
}
}
public static class UserContactInfo {
private ContactInfoType type;
private String value;
public ContactInfoType getType() {
return type;
}
public void setType(ContactInfoType type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@ -1,10 +1,10 @@
package eu.eudat.interceptors;
import eu.eudat.authorization.ClaimNames;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.enums.ContactInfoType;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.enums.notification.UserType;
import eu.eudat.commons.lock.LockByKeyManager;
import eu.eudat.commons.scope.user.UserScope;
import eu.eudat.commons.types.user.AdditionalInfoEntity;
@ -14,6 +14,9 @@ import eu.eudat.data.UserContactInfoEntity;
import eu.eudat.data.UserCredentialEntity;
import eu.eudat.data.UserEntity;
import eu.eudat.data.UserRoleEntity;
import eu.eudat.integrationevent.outbox.OutboxIntegrationEvent;
import eu.eudat.integrationevent.outbox.OutboxService;
import eu.eudat.integrationevent.outbox.usertouched.UserTouchedIntegrationEvent;
import eu.eudat.model.UserContactInfo;
import eu.eudat.model.UserCredential;
import eu.eudat.model.UserRole;
@ -29,6 +32,7 @@ import gr.cite.tools.logging.LoggerService;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.apache.commons.validator.routines.EmailValidator;
import org.jetbrains.annotations.NotNull;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
@ -49,16 +53,29 @@ import java.util.concurrent.TimeUnit;
@Component
public class UserInterceptor implements WebRequestInterceptor {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserInterceptor.class));
private final UserScope userScope;
private final ClaimExtractor claimExtractor;
private final CurrentPrincipalResolver currentPrincipalResolver;
private final PlatformTransactionManager transactionManager;
private final UserInterceptorCacheService userInterceptorCacheService;
private final JsonHandlingService jsonHandlingService;
private final QueryFactory queryFactory;
private final LockByKeyManager lockByKeyManager;
private final LocaleProperties localeProperties;
private final OutboxService outboxService;
@PersistenceContext
public EntityManager entityManager;
@ -72,7 +89,7 @@ public class UserInterceptor implements WebRequestInterceptor {
JsonHandlingService jsonHandlingService,
QueryFactory queryFactory,
LockByKeyManager lockByKeyManager,
LocaleProperties localeProperties) {
LocaleProperties localeProperties, OutboxService outboxService) {
this.userScope = userScope;
this.currentPrincipalResolver = currentPrincipalResolver;
this.claimExtractor = claimExtractor;
@ -82,14 +99,16 @@ public class UserInterceptor implements WebRequestInterceptor {
this.queryFactory = queryFactory;
this.lockByKeyManager = lockByKeyManager;
this.localeProperties = localeProperties;
this.outboxService = outboxService;
}
@Override
public void preHandle(WebRequest request) throws InterruptedException {
public void preHandle(@NotNull WebRequest request) throws InterruptedException {
UUID userId = null;
if (this.currentPrincipalResolver.currentPrincipal().isAuthenticated()) {
String subjectId = this.claimExtractor.subjectString(this.currentPrincipalResolver.currentPrincipal());
if (subjectId == null || subjectId.isBlank()) throw new MyForbiddenException("Empty subjects not allowed");
if (subjectId == null || subjectId.isBlank())
throw new MyForbiddenException("Empty subjects not allowed");
UserInterceptorCacheService.UserInterceptorCacheValue cacheValue = this.userInterceptorCacheService.lookup(this.userInterceptorCacheService.buildKey(subjectId));
if (cacheValue != null && emailExistsToPrincipal(cacheValue.getProviderEmail()) && userRolesSynced(cacheValue.getRoles()) && providerExistsToPrincipal(cacheValue.getExternalProviderNames())) {
@ -123,22 +142,26 @@ public class UserInterceptor implements WebRequestInterceptor {
this.entityManager.flush();
transactionManager.commit(status);
} catch (Exception ex) {
if (status != null) transactionManager.rollback(status);
if (status != null)
transactionManager.rollback(status);
throw ex;
}
cacheValue = new UserInterceptorCacheService.UserInterceptorCacheValue(subjectId, userId);
cacheValue.setRoles(this.getRolesFromClaims());
if (email != null && !email.isBlank()) cacheValue.setProviderEmail(email);
if (email != null && !email.isBlank())
cacheValue.setProviderEmail(email);
UserCredentialEntity userCredential = this.queryFactory.query(UserCredentialQuery.class).externalIds(subjectId).firstAs(new BaseFieldSet().ensure(UserCredential._data));
if (userCredential != null && userCredential.getData() != null) {
UserCredentialDataEntity userCredentialDataEntity = this.jsonHandlingService.fromJsonSafe(UserCredentialDataEntity.class, userCredential.getData());
if (userCredentialDataEntity != null) cacheValue.setExternalProviderNames(userCredentialDataEntity.getExternalProviderNames());
if (userCredentialDataEntity != null)
cacheValue.setExternalProviderNames(userCredentialDataEntity.getExternalProviderNames());
}
this.userInterceptorCacheService.put(cacheValue);
} finally {
if (usedResource) this.lockByKeyManager.unlock(subjectId);
if (usedResource)
this.lockByKeyManager.unlock(subjectId);
}
}
@ -147,6 +170,8 @@ public class UserInterceptor implements WebRequestInterceptor {
}
private void syncUserWithClaims(UUID userId, String subjectId) {
UserTouchedIntegrationEvent event = new UserTouchedIntegrationEvent();
List<String> existingUserEmails = this.collectUserEmails(userId);
if (!this.containsPrincipalEmail(existingUserEmails)) {
String email = this.getEmailFromClaims();
@ -158,6 +183,12 @@ public class UserInterceptor implements WebRequestInterceptor {
UserContactInfoEntity contactInfo = this.buildEmailContact(userId, email);
contactInfo.setOrdinal(emailContactsCount.intValue());
this.entityManager.persist(contactInfo);
this.initUserTouchedEvent(event, userId);
UserTouchedIntegrationEvent.UserContactInfo eventContactInfo = new UserTouchedIntegrationEvent.UserContactInfo();
eventContactInfo.setType(ContactInfoType.Email);
eventContactInfo.setValue(email);
event.getUserContactInfo().add(eventContactInfo);
}
}
@ -169,12 +200,13 @@ public class UserInterceptor implements WebRequestInterceptor {
UserCredentialEntity userCredential = this.queryFactory.query(UserCredentialQuery.class).externalIds(subjectId).first();
if (userCredential == null) {
throw new MyForbiddenException("UserCredential not found");
}
else {
} else {
boolean updatedUserCredential = false;
UserCredentialDataEntity userCredentialDataEntity = this.jsonHandlingService.fromJsonSafe(UserCredentialDataEntity.class, userCredential.getData());
if (userCredentialDataEntity == null) userCredentialDataEntity = new UserCredentialDataEntity();
if (userCredentialDataEntity.getExternalProviderNames() == null) userCredentialDataEntity.setExternalProviderNames(new ArrayList<>());
if (userCredentialDataEntity == null)
userCredentialDataEntity = new UserCredentialDataEntity();
if (userCredentialDataEntity.getExternalProviderNames() == null)
userCredentialDataEntity.setExternalProviderNames(new ArrayList<>());
String email = this.getEmailFromClaims();
String provider = this.getProviderFromClaims();
@ -192,6 +224,12 @@ public class UserInterceptor implements WebRequestInterceptor {
this.entityManager.persist(userCredential);
}
}
OutboxIntegrationEvent message = new OutboxIntegrationEvent();
message.setMessageId(UUID.randomUUID());
message.setType(OutboxIntegrationEvent.USER_TOUCH);
message.setEvent(event);
this.outboxService.publish(message);
}
private UUID findExistingUserFromDb(String subjectId) {
@ -217,7 +255,8 @@ public class UserInterceptor implements WebRequestInterceptor {
private List<String> getRolesFromClaims() {
List<String> claimsRoles = claimExtractor.roles(currentPrincipalResolver.currentPrincipal());
if (claimsRoles == null) claimsRoles = new ArrayList<>();
if (claimsRoles == null)
claimsRoles = new ArrayList<>();
claimsRoles = claimsRoles.stream().filter(x -> x != null && !x.isBlank()).distinct().toList();
return claimsRoles;
}
@ -255,6 +294,7 @@ public class UserInterceptor implements WebRequestInterceptor {
return email == null || email.isBlank() ||
(existingUserEmails != null && existingUserEmails.stream().anyMatch(email::equals));
}
private boolean emailExistsToPrincipal(String existingUserEmail) {
String email = this.getEmailFromClaims();
return email == null || email.isBlank() || email.equalsIgnoreCase(existingUserEmail);
@ -266,28 +306,32 @@ public class UserInterceptor implements WebRequestInterceptor {
(principalCredentialProviders != null && principalCredentialProviders.stream().anyMatch(provider::equalsIgnoreCase));
}
private boolean userRolesSynced(List<String> existingUserRoles) {
List<String> claimsRoles = this.getRolesFromClaims();
if (existingUserRoles == null) existingUserRoles = new ArrayList<>();
if (existingUserRoles == null)
existingUserRoles = new ArrayList<>();
existingUserRoles = existingUserRoles.stream().filter(x -> x != null && !x.isBlank()).distinct().toList();
if (claimsRoles.size() != existingUserRoles.size()) return false;
if (claimsRoles.size() != existingUserRoles.size())
return false;
for (String claim : claimsRoles) {
if (existingUserRoles.stream().noneMatch(claim::equalsIgnoreCase)) return false;
if (existingUserRoles.stream().noneMatch(claim::equalsIgnoreCase))
return false;
}
return true;
}
private String getEmailFromClaims() {
String email = this.claimExtractor.email(this.currentPrincipalResolver.currentPrincipal());
if (email == null || email.isBlank() || !EmailValidator.getInstance().isValid(email)) return null;
if (email == null || email.isBlank() || !EmailValidator.getInstance().isValid(email))
return null;
return email.trim();
}
private String getProviderFromClaims() {
String provider = this.claimExtractor.asString(this.currentPrincipalResolver.currentPrincipal(), ClaimNames.ExternalProviderName);
if (provider == null || provider.isBlank()) return null;
if (provider == null || provider.isBlank())
return null;
return provider.trim();
}
@ -297,8 +341,10 @@ public class UserInterceptor implements WebRequestInterceptor {
String email = this.getEmailFromClaims();
String provider = this.getProviderFromClaims();
if (email != null && !email.isBlank()) userCredentialDataEntity.setEmail(email);
if (provider != null && !provider.isBlank()) userCredentialDataEntity.setExternalProviderNames(List.of(provider));
if (email != null && !email.isBlank())
userCredentialDataEntity.setEmail(email);
if (provider != null && !provider.isBlank())
userCredentialDataEntity.setExternalProviderNames(List.of(provider));
data.setData(this.jsonHandlingService.toJsonSafe(userCredentialDataEntity));
data.setId(UUID.randomUUID());
@ -328,9 +374,9 @@ public class UserInterceptor implements WebRequestInterceptor {
return data;
}
private UserEntity addNewUser(String subjectId, String email) {
UserTouchedIntegrationEvent event = new UserTouchedIntegrationEvent();
List<String> roles = this.getRolesFromClaims();
String name = this.claimExtractor.name(this.currentPrincipalResolver.currentPrincipal());
@ -347,12 +393,28 @@ public class UserInterceptor implements WebRequestInterceptor {
user.setAdditionalInfo(this.jsonHandlingService.toJsonSafe(additionalInfoEntity));
this.entityManager.persist(user);
this.initUserTouchedEvent(event, user.getId());
event.setType(UserType.Person);
event.setFirstName(user.getName().split(" ")[0]);
event.setLastName(user.getName().split(" ")[1]);
UserTouchedIntegrationEvent.UserProfile eventUserProfile = new UserTouchedIntegrationEvent.UserProfile();
eventUserProfile.setCulture(additionalInfoEntity.getCulture());
eventUserProfile.setTimezone(additionalInfoEntity.getTimezone());
eventUserProfile.setLanguage(additionalInfoEntity.getLanguage());
event.setProfile(eventUserProfile);
UserCredentialEntity credential = this.buildCredential(user.getId(), subjectId);
this.entityManager.persist(credential);
if (email != null && !email.isBlank()) {
UserContactInfoEntity contactInfo = this.buildEmailContact(user.getId(), email);
this.entityManager.persist(contactInfo);
UserTouchedIntegrationEvent.UserContactInfo userContactInfo = new UserTouchedIntegrationEvent.UserContactInfo();
userContactInfo.setType(ContactInfoType.Email);
userContactInfo.setValue(email);
event.getUserContactInfo().add(userContactInfo);
}
if (roles != null) {
for (String role : roles) {
@ -361,9 +423,38 @@ public class UserInterceptor implements WebRequestInterceptor {
}
}
OutboxIntegrationEvent message = new OutboxIntegrationEvent();
message.setMessageId(UUID.randomUUID());
message.setType(OutboxIntegrationEvent.USER_TOUCH);
message.setEvent(event);
this.outboxService.publish(message);
return user;
}
private void initUserTouchedEvent(UserTouchedIntegrationEvent event, UUID userId) {
event.setId(userId);
UserEntity userEntity = this.entityManager.find(UserEntity.class, userId);
if (userEntity != null) {
event.setFirstName(userEntity.getName().split(" ")[0]);
event.setLastName(userEntity.getName().split(" ")[1]);
event.setType(UserType.Person);
}
List<UserTouchedIntegrationEvent.UserContactInfo> userContactInfoList = new ArrayList<>();
if (userEntity != null) {
List<UserContactInfoEntity> contactInfoEntities = this.queryFactory.query(UserContactInfoQuery.class).userIds(userId).collect();
for (UserContactInfoEntity entity : contactInfoEntities) {
UserTouchedIntegrationEvent.UserContactInfo userContactInfo = new UserTouchedIntegrationEvent.UserContactInfo();
userContactInfo.setType(entity.getType());
userContactInfo.setValue(entity.getValue());
userContactInfoList.add(userContactInfo);
}
}
event.setUserContactInfo(userContactInfoList);
}
@Override
public void postHandle(@NonNull WebRequest request, ModelMap model) {
this.userScope.setUserId(null);

View File

@ -48,8 +48,6 @@ queue:
enable: true
options:
exchange: null
user-removal-topic: [ "user.remove" ]
user-touched-topic: [ "user.touch" ]
rabbitmq:
enable: true
interval-seconds: 30