Search in sources :

Example 21 with DataAdapterDto

use of org.graylog2.lookup.dto.DataAdapterDto in project graylog2-server by Graylog2.

the class LookupTableResource method tables.

@GET
@Path("tables")
@ApiOperation(value = "List configured lookup tables")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public LookupTablePage tables(@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(LookupTableDto.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, @ApiParam(name = "resolve") @QueryParam("resolve") @DefaultValue("false") boolean resolveObjects) {
    if (!LUT_ALLOWABLE_SORT_FIELDS.contains(sort.toLowerCase(Locale.ENGLISH))) {
        sort = LookupTableDto.FIELD_TITLE;
    }
    DBSort.SortBuilder sortBuilder;
    if ("desc".equalsIgnoreCase(order)) {
        sortBuilder = DBSort.desc(sort);
    } else {
        sortBuilder = DBSort.asc(sort);
    }
    try {
        final SearchQuery searchQuery = lutSearchQueryParser.parse(query);
        final DBQuery.Query dbQuery = searchQuery.toDBQuery();
        PaginatedList<LookupTableDto> paginated = dbTableService.findPaginated(dbQuery, sortBuilder, page, perPage);
        ImmutableSet.Builder<CacheApi> caches = ImmutableSet.builder();
        ImmutableSet.Builder<DataAdapterApi> dataAdapters = ImmutableSet.builder();
        if (resolveObjects) {
            ImmutableSet.Builder<String> cacheIds = ImmutableSet.builder();
            ImmutableSet.Builder<String> dataAdapterIds = ImmutableSet.builder();
            paginated.forEach(dto -> {
                cacheIds.add(dto.cacheId());
                dataAdapterIds.add(dto.dataAdapterId());
            });
            dbCacheService.findByIds(cacheIds.build()).forEach(cacheDto -> caches.add(CacheApi.fromDto(cacheDto)));
            dbDataAdapterService.findByIds(dataAdapterIds.build()).forEach(dataAdapterDto -> dataAdapters.add(DataAdapterApi.fromDto(dataAdapterDto)));
        }
        return new LookupTablePage(query, paginated.pagination(), paginated.stream().map(LookupTableApi::fromDto).collect(Collectors.toList()), caches.build(), dataAdapters.build());
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) LookupTableApi(org.graylog2.rest.models.system.lookup.LookupTableApi) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) DataAdapterApi(org.graylog2.rest.models.system.lookup.DataAdapterApi) DBSort(org.mongojack.DBSort) ImmutableSet(com.google.common.collect.ImmutableSet) DBQuery(org.mongojack.DBQuery) BadRequestException(javax.ws.rs.BadRequestException) CacheApi(org.graylog2.rest.models.system.lookup.CacheApi) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 22 with DataAdapterDto

use of org.graylog2.lookup.dto.DataAdapterDto in project graylog2-server by Graylog2.

the class LookupTableResource method validateAdapter.

@POST
@Path("adapters/validate")
@NoAuditEvent("Validation only")
@ApiOperation(value = "Validate the data adapter config")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public ValidationResult validateAdapter(@Valid @ApiParam DataAdapterApi toValidate) {
    final ValidationResult validation = new ValidationResult();
    final Optional<DataAdapterDto> dtoOptional = dbDataAdapterService.get(toValidate.name());
    if (dtoOptional.isPresent()) {
        // an adapter exist with the given name, check that the IDs are the same, this might be an update
        final DataAdapterDto adapterDto = dtoOptional.get();
        // noinspection ConstantConditions
        if (!adapterDto.id().equals(toValidate.id())) {
            // an adapter exists with a different id, so the name is already in use, fail validation
            validation.addError("name", "The data adapter name is already in use.");
        }
    }
    final Optional<Multimap<String, String>> configValidations = toValidate.config().validate(lookupDataAdapterValidationContext);
    configValidations.ifPresent(validation::addAll);
    return validation;
}
Also used : Multimap(com.google.common.collect.Multimap) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Aggregations

DataAdapterDto (org.graylog2.lookup.dto.DataAdapterDto)17 LookupDataAdapterEntity (org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity)8 Test (org.junit.Test)7 ApiOperation (io.swagger.annotations.ApiOperation)6 Path (javax.ws.rs.Path)6 Entity (org.graylog2.contentpacks.model.entities.Entity)5 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)5 BadRequestException (javax.ws.rs.BadRequestException)4 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)4 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)4 LookupTableDto (org.graylog2.lookup.dto.LookupTableDto)4 AuditEvent (org.graylog2.audit.jersey.AuditEvent)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 FallbackAdapterConfig (org.graylog2.plugin.lookup.FallbackAdapterConfig)3 LookupDataAdapter (org.graylog2.plugin.lookup.LookupDataAdapter)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)2