use of org.graylog2.lookup.dto.CacheDto in project graylog2-server by Graylog2.
the class LookupCacheFacade method findExisting.
private Optional<NativeEntity<CacheDto>> findExisting(EntityV1 entity, Map<String, ValueReference> parameters) {
final LookupCacheEntity cacheEntity = objectMapper.convertValue(entity.data(), LookupCacheEntity.class);
final String name = cacheEntity.name().asString(parameters);
final Optional<CacheDto> existingCache = cacheService.get(name);
return existingCache.map(cache -> NativeEntity.create(entity.id(), cache.id(), TYPE_V1, cache.title(), cache));
}
use of org.graylog2.lookup.dto.CacheDto in project graylog2-server by Graylog2.
the class LookupCacheFacade method decode.
private NativeEntity<CacheDto> decode(EntityV1 entity, Map<String, ValueReference> parameters) {
final LookupCacheEntity lookupCacheEntity = objectMapper.convertValue(entity.data(), LookupCacheEntity.class);
final LookupCacheConfiguration configuration = objectMapper.convertValue(toValueMap(lookupCacheEntity.configuration(), parameters), LookupCacheConfiguration.class);
final CacheDto cacheDto = CacheDto.builder().name(lookupCacheEntity.name().asString(parameters)).title(lookupCacheEntity.title().asString(parameters)).description(lookupCacheEntity.description().asString(parameters)).config(configuration).build();
final CacheDto savedCacheDto = cacheService.save(cacheDto);
return NativeEntity.create(entity.id(), savedCacheDto.id(), TYPE_V1, savedCacheDto.title(), savedCacheDto);
}
use of org.graylog2.lookup.dto.CacheDto in project graylog2-server by Graylog2.
the class LookupTableFacade method decode.
private NativeEntity<LookupTableDto> decode(EntityV1 entity, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities) {
final LookupTableEntity lookupTableEntity = objectMapper.convertValue(entity.data(), LookupTableEntity.class);
final String referencedDataAdapterName = lookupTableEntity.dataAdapterName().asString(parameters);
final EntityDescriptor dataAdapterDescriptor = adapterDescriptor(referencedDataAdapterName);
final Object dataAdapter = nativeEntities.get(dataAdapterDescriptor);
final String dataAdapterId;
if (dataAdapter instanceof DataAdapterDto) {
dataAdapterId = ((DataAdapterDto) dataAdapter).id();
} else {
throw new MissingNativeEntityException(dataAdapterDescriptor);
}
final String referencedCacheName = lookupTableEntity.cacheName().asString(parameters);
final EntityDescriptor cacheDescriptor = cacheDescriptor(referencedCacheName);
final Object cache = nativeEntities.get(cacheDescriptor);
final String cacheId;
if (cache instanceof CacheDto) {
cacheId = ((CacheDto) cache).id();
} else {
throw new MissingNativeEntityException(cacheDescriptor);
}
final LookupTableDto lookupTableDto = LookupTableDto.builder().name(lookupTableEntity.name().asString(parameters)).title(lookupTableEntity.title().asString(parameters)).description(lookupTableEntity.description().asString(parameters)).dataAdapterId(dataAdapterId).cacheId(cacheId).defaultSingleValue(lookupTableEntity.defaultSingleValue().asString(parameters)).defaultSingleValueType(lookupTableEntity.defaultSingleValueType().asEnum(parameters, LookupDefaultSingleValue.Type.class)).defaultMultiValue(lookupTableEntity.defaultMultiValue().asString(parameters)).defaultMultiValueType(lookupTableEntity.defaultMultiValueType().asEnum(parameters, LookupDefaultMultiValue.Type.class)).build();
final LookupTableDto savedLookupTableDto = lookupTableService.save(lookupTableDto);
return NativeEntity.create(entity.id(), savedLookupTableDto.id(), TYPE_V1, lookupTableDto.title(), savedLookupTableDto);
}
use of org.graylog2.lookup.dto.CacheDto 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.lookup.dto.CacheDto in project graylog2-server by Graylog2.
the class LookupTableResource method caches.
@GET
@Path("caches")
@ApiOperation(value = "List available caches")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public CachesPage caches(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "title,description,name,id") @DefaultValue(CacheDto.FIELD_TITLE) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("desc") @QueryParam("order") String order, @ApiParam(name = "query") @QueryParam("query") String query) {
if (!CACHE_ALLOWABLE_SORT_FIELDS.contains(sort.toLowerCase(Locale.ENGLISH))) {
sort = CacheDto.FIELD_TITLE;
}
DBSort.SortBuilder sortBuilder;
if ("desc".equalsIgnoreCase(order)) {
sortBuilder = DBSort.desc(sort);
} else {
sortBuilder = DBSort.asc(sort);
}
try {
final SearchQuery searchQuery = cacheSearchQueryParser.parse(query);
final DBQuery.Query dbQuery = searchQuery.toDBQuery();
PaginatedList<CacheDto> paginated = dbCacheService.findPaginated(dbQuery, sortBuilder, page, perPage);
return new CachesPage(query, paginated.pagination(), paginated.stream().map(CacheApi::fromDto).collect(Collectors.toList()));
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage(), e);
}
}
Aggregations