Implementing validators for Deposit request models (completed)

This commit is contained in:
Thomas Georgios Giannos 2024-01-04 13:31:14 +02:00
parent c056e04a89
commit 0e1a9c88b1
3 changed files with 118 additions and 23 deletions

View File

@ -1,18 +1,31 @@
package eu.eudat.model.persist.deposit;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.commons.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import eu.eudat.model.persist.ReferencePersist;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
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.Arrays;
import java.util.List;
public class DepositAuthenticateRequest {
@NotNull(message = "{validation.empty}")
@NotEmpty(message = "{validation.empty}")
private String repositoryId;
@NotNull(message = "{validation.empty}")
@NotEmpty(message = "{validation.empty}")
public static final String _repositoryId = "repositoryId";
private String code;
public static final String _code = "code";
public String getRepositoryId() {
return repositoryId;
}
@ -26,4 +39,36 @@ public class DepositAuthenticateRequest {
public void setCode(String code) {
this.code = code;
}
@Component(DepositAuthenticateRequestValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class DepositAuthenticateRequestValidator extends BaseValidator<DepositAuthenticateRequest> {
public static final String ValidatorName = "DepositAuthenticateRequestValidator";
private final MessageSource messageSource;
protected DepositAuthenticateRequestValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) {
super(conventionService, errors);
this.messageSource = messageSource;
}
@Override
protected Class<DepositAuthenticateRequest> modelClass() {
return DepositAuthenticateRequest.class;
}
@Override
protected List<Specification> specifications(DepositAuthenticateRequest item) {
return Arrays.asList(
this.spec()
.must(() -> !this.isEmpty(item.getRepositoryId()))
.failOn(DepositAuthenticateRequest._repositoryId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DepositAuthenticateRequest._repositoryId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getCode()))
.failOn(DepositAuthenticateRequest._code).failWith(messageSource.getMessage("Validation_Required", new Object[]{DepositAuthenticateRequest._code}, LocaleContextHolder.getLocale()))
);
}
}
}

View File

@ -1,26 +1,38 @@
package eu.eudat.model.persist.deposit;
import eu.eudat.commons.validation.old.ValidId;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.commons.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import gr.cite.tools.fieldset.BaseFieldSet;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
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.Arrays;
import java.util.List;
import java.util.UUID;
public class DepositRequest {
@NotNull(message = "{validation.empty}")
@NotEmpty(message = "{validation.empty}")
private String repositoryId;
@NotNull(message = "{validation.empty}")
@ValidId(message = "{validation.invalidid}")
public static final String _repositoryId = "repositoryId";
private UUID dmpId;
public static final String _dmpId = "dmpId";
private String accessToken;
private BaseFieldSet project;
public String getRepositoryId() {
return repositoryId;
}
public void setRepositoryId(String repositoryId) {
this.repositoryId = repositoryId;
}
@ -28,6 +40,7 @@ public class DepositRequest {
public UUID getDmpId() {
return dmpId;
}
public void setDmpId(UUID dmpId) {
this.dmpId = dmpId;
}
@ -35,6 +48,7 @@ public class DepositRequest {
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
@ -46,4 +60,36 @@ public class DepositRequest {
public void setProject(BaseFieldSet project) {
this.project = project;
}
@Component(DepositRequestValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class DepositRequestValidator extends BaseValidator<DepositRequest> {
public static final String ValidatorName = "DepositRequestValidator";
private final MessageSource messageSource;
protected DepositRequestValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) {
super(conventionService, errors);
this.messageSource = messageSource;
}
@Override
protected Class<DepositRequest> modelClass() {
return DepositRequest.class;
}
@Override
protected List<Specification> specifications(DepositRequest item) {
return Arrays.asList(
this.spec()
.must(() -> !this.isEmpty(item.getRepositoryId()))
.failOn(DepositRequest._repositoryId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DepositRequest._repositoryId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isValidGuid(item.getDmpId()))
.failOn(DepositRequest._dmpId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DepositRequest._dmpId}, LocaleContextHolder.getLocale()))
);
}
}
}

View File

@ -1,7 +1,7 @@
package eu.eudat.controllers.v2;
import eu.eudat.audit.AuditableAction;
import eu.eudat.authorization.Permission;
import eu.eudat.commons.validation.ValidationFilterAnnotation;
import eu.eudat.controllers.BaseController;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.model.EntityDoi;
@ -17,7 +17,6 @@ import gr.cite.tools.data.censor.CensorFactory;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import gr.cite.tools.validation.MyValidate;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
@ -31,21 +30,24 @@ import java.util.Map;
@CrossOrigin
@RequestMapping(value = {"/api/deposit/"})
public class DepositController extends BaseController {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DepositController.class));
private final AuthorizationService authorizationService;
private final DepositService depositService;
private final CensorFactory censorFactory;
private final AuditService auditService;
@Autowired
public DepositController(ApiContext apiContext, AuthorizationService authorizationService, DepositService depositService, CensorFactory censorFactory, AuditService auditService){
public DepositController(ApiContext apiContext, AuthorizationService authorizationService, DepositService depositService, CensorFactory censorFactory, AuditService auditService) {
super(apiContext);
this.authorizationService = authorizationService;
this.depositService = depositService;
this.censorFactory = censorFactory;
this.auditService = auditService;
this.censorFactory = censorFactory;
this.auditService = auditService;
}
@GetMapping("/repositories/available")
@ -58,25 +60,27 @@ public class DepositController extends BaseController {
this.auditService.track(AuditableAction.Deposit_GetAvailableRepositories, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
return model;
}
@PostMapping("/get-access-token")
public String getAccessToken(@MyValidate @RequestBody DepositAuthenticateRequest model) throws Exception {
@ValidationFilterAnnotation(validator = DepositAuthenticateRequest.DepositAuthenticateRequestValidator.ValidatorName, argumentName = "model")
public String getAccessToken(@RequestBody DepositAuthenticateRequest model) {
logger.debug(new MapLogEntry("get access token" + DepositAuthenticateRequest.class.getSimpleName()).And("model", model));
String accessToken = this.depositService.authenticate(model);
this.auditService.track(AuditableAction.Deposit_GetAccessToken, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model)
));
return accessToken;
}
@PostMapping("/deposit")
@Transactional
public EntityDoi deposit(@MyValidate @RequestBody DepositRequest model) throws Exception {
@ValidationFilterAnnotation(validator = DepositRequest.DepositRequestValidator.ValidatorName, argumentName = "model")
public EntityDoi deposit(@RequestBody DepositRequest model) throws Exception {
logger.debug(new MapLogEntry("persisting" + DepositRequest.class.getSimpleName()).And("model", model).And("fieldSet", model.getProject()));
this.censorFactory.censor(EntityDoiCensor.class).censor(model.getProject(), null);
@ -84,7 +88,7 @@ public class DepositController extends BaseController {
this.auditService.track(AuditableAction.Deposit_Deposit, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model)
));
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
return persisted;
}
@ -96,7 +100,7 @@ public class DepositController extends BaseController {
this.auditService.track(AuditableAction.Deposit_GetLogo, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("repositoryId", repositoryId)
));
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
return logo;
}
}