use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.
the class EventDefinitionsResource method update.
@PUT
@Path("{definitionId}")
@ApiOperation("Update existing event definition")
@AuditEvent(type = EventsAuditEventTypes.EVENT_DEFINITION_UPDATE)
public Response update(@ApiParam(name = "definitionId") @PathParam("definitionId") @NotBlank String definitionId, @ApiParam("schedule") @QueryParam("schedule") @DefaultValue("true") boolean schedule, @ApiParam(name = "JSON Body") EventDefinitionDto dto) {
checkPermission(RestPermissions.EVENT_DEFINITIONS_EDIT, definitionId);
checkEventDefinitionPermissions(dto, "update");
dbService.get(definitionId).orElseThrow(() -> new NotFoundException("Event definition <" + definitionId + "> doesn't exist"));
final ValidationResult result = dto.validate();
if (!definitionId.equals(dto.id())) {
result.addError("id", "Event definition IDs don't match");
}
if (result.failed()) {
return Response.status(Response.Status.BAD_REQUEST).entity(result).build();
}
return Response.ok().entity(eventDefinitionHandler.update(dto, schedule)).build();
}
use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.
the class PersistedServiceImpl method validate.
@Override
public Map<String, List<ValidationResult>> validate(Map<String, Validator> validators, Map<String, Object> fields) {
if (validators == null || validators.isEmpty()) {
return Collections.emptyMap();
}
final Map<String, List<ValidationResult>> validationErrors = new HashMap<>();
for (Map.Entry<String, Validator> validation : validators.entrySet()) {
Validator v = validation.getValue();
String field = validation.getKey();
try {
ValidationResult validationResult = v.validate(fields.get(field));
if (validationResult instanceof ValidationResult.ValidationFailed) {
LOG.debug("Validation failure: [{}] on field [{}]", v.getClass().getCanonicalName(), field);
validationErrors.computeIfAbsent(field, k -> new ArrayList<>());
validationErrors.get(field).add(validationResult);
}
} catch (Exception e) {
final String error = "Error while trying to validate <" + field + ">, got exception: " + e;
LOG.debug(error);
validationErrors.computeIfAbsent(field, k -> new ArrayList<>());
validationErrors.get(field).add(new ValidationResult.ValidationFailed(error));
}
}
return validationErrors;
}
use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.
the class LookupTableResource method validateTable.
@POST
@Path("tables/validate")
@NoAuditEvent("Validation only")
@ApiOperation(value = "Validate the lookup table config")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public ValidationResult validateTable(@Valid @ApiParam LookupTableApi toValidate) {
final ValidationResult validation = new ValidationResult();
final Optional<LookupTableDto> dtoOptional = dbTableService.get(toValidate.name());
if (dtoOptional.isPresent()) {
// a table exist with the given name, check that the IDs are the same, this might be an update
final LookupTableDto tableDto = dtoOptional.get();
// noinspection ConstantConditions
if (!tableDto.id().equals(toValidate.id())) {
// a table exists with a different id, so the name is already in use, fail validation
validation.addError("name", "The lookup table name is already in use.");
}
}
try {
LookupDefaultSingleValue.create(toValidate.defaultSingleValue(), toValidate.defaultSingleValueType());
} catch (Exception e) {
validation.addError(LookupTableApi.FIELD_DEFAULT_SINGLE_VALUE, e.getMessage());
}
try {
LookupDefaultMultiValue.create(toValidate.defaultMultiValue(), toValidate.defaultMultiValueType());
} catch (Exception e) {
validation.addError(LookupTableApi.FIELD_DEFAULT_MULTI_VALUE, e.getMessage());
}
return validation;
}
use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.
the class LookupTableResource method validateCache.
@POST
@Path("caches/validate")
@NoAuditEvent("Validation only")
@ApiOperation(value = "Validate the cache config")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public ValidationResult validateCache(@Valid @ApiParam CacheApi toValidate) {
final ValidationResult validation = new ValidationResult();
final Optional<CacheDto> dtoOptional = dbCacheService.get(toValidate.name());
if (dtoOptional.isPresent()) {
// a cache exist with the given name, check that the IDs are the same, this might be an update
final CacheDto cacheDto = dtoOptional.get();
// noinspection ConstantConditions
if (!cacheDto.id().equals(toValidate.id())) {
// a ache exists with a different id, so the name is already in use, fail validation
validation.addError("name", "The cache name is already in use.");
}
}
final Optional<Multimap<String, String>> configValidations = toValidate.config().validate();
configValidations.ifPresent(validation::addAll);
return validation;
}
use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.
the class EntitySharesServiceTest method ignoreInvisibleOwners.
@DisplayName("The validation should ignore invisble owners")
@Test
void ignoreInvisibleOwners() {
final GRN entity = grnRegistry.newGRN(GRNTypes.STREAM, "54e3deadbeefdeadbeefaffe");
final EntityShareRequest shareRequest = EntityShareRequest.create(ImmutableMap.of());
final Set<GRN> allGrantees = dbGrantService.getAll().stream().map(GrantDTO::grantee).collect(Collectors.toSet());
lenient().when(granteeService.getAvailableGrantees(any())).thenReturn(allGrantees.stream().filter(g -> g.toString().equals("grn::::user:invisible")).map(g -> Grantee.createUser(g, g.entity())).collect(Collectors.toSet()));
final User user = createMockUser("hans");
final Subject subject = mock(Subject.class);
final EntityShareResponse entityShareResponse = entitySharesService.prepareShare(entity, shareRequest, user, subject);
assertThat(entityShareResponse.validationResult()).satisfies(validationResult -> {
assertThat(validationResult.failed()).isFalse();
});
}
Aggregations