Search in sources :

Example 1 with LookupTableApi

use of org.graylog2.rest.models.system.lookup.LookupTableApi 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;
}
Also used : LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) ValidationResult(org.graylog2.plugin.rest.ValidationResult) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) DuplicateKeyException(com.mongodb.DuplicateKeyException) 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)

Example 2 with LookupTableApi

use of org.graylog2.rest.models.system.lookup.LookupTableApi in project graylog2-server by Graylog2.

the class LookupTableResource method updateTable.

@PUT
@Path("tables/{idOrName}")
@AuditEvent(type = AuditEventTypes.LOOKUP_TABLE_UPDATE)
@ApiOperation(value = "Update the given lookup table")
public LookupTableApi updateTable(@ApiParam(name = "idOrName") @PathParam("idOrName") @NotEmpty String idOrName, @Valid @ApiParam LookupTableApi toUpdate) {
    checkLookupTableId(idOrName, toUpdate);
    checkPermission(RestPermissions.LOOKUP_TABLES_EDIT, toUpdate.id());
    LookupTableDto saved = dbTableService.save(toUpdate.toDto());
    return LookupTableApi.fromDto(saved);
}
Also used : LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 3 with LookupTableApi

use of org.graylog2.rest.models.system.lookup.LookupTableApi 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 4 with LookupTableApi

use of org.graylog2.rest.models.system.lookup.LookupTableApi in project graylog2-server by Graylog2.

the class LookupTableResource method get.

@GET
@Path("tables/{idOrName}")
@ApiOperation(value = "Retrieve the named lookup table")
public LookupTablePage get(@ApiParam(name = "idOrName") @PathParam("idOrName") @NotEmpty String idOrName, @ApiParam(name = "resolve") @QueryParam("resolve") @DefaultValue("false") boolean resolveObjects) {
    Optional<LookupTableDto> lookupTableDto = dbTableService.get(idOrName);
    if (!lookupTableDto.isPresent()) {
        throw new NotFoundException();
    }
    LookupTableDto tableDto = lookupTableDto.get();
    checkPermission(RestPermissions.LOOKUP_TABLES_READ, tableDto.id());
    Set<CacheApi> caches = Collections.emptySet();
    Set<DataAdapterApi> adapters = Collections.emptySet();
    if (resolveObjects) {
        caches = dbCacheService.findByIds(Collections.singleton(tableDto.cacheId())).stream().map(CacheApi::fromDto).collect(Collectors.toSet());
        adapters = dbDataAdapterService.findByIds(Collections.singleton(tableDto.dataAdapterId())).stream().map(DataAdapterApi::fromDto).collect(Collectors.toSet());
    }
    final PaginatedList<LookupTableApi> result = PaginatedList.singleton(LookupTableApi.fromDto(tableDto), 1, 1);
    return new LookupTablePage(null, result.pagination(), result, caches, adapters);
}
Also used : LookupTableApi(org.graylog2.rest.models.system.lookup.LookupTableApi) NotFoundException(javax.ws.rs.NotFoundException) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) CacheApi(org.graylog2.rest.models.system.lookup.CacheApi) DataAdapterApi(org.graylog2.rest.models.system.lookup.DataAdapterApi) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)4 Path (javax.ws.rs.Path)4 LookupTableDto (org.graylog2.lookup.dto.LookupTableDto)4 BadRequestException (javax.ws.rs.BadRequestException)2 GET (javax.ws.rs.GET)2 NotFoundException (javax.ws.rs.NotFoundException)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)2 CacheApi (org.graylog2.rest.models.system.lookup.CacheApi)2 DataAdapterApi (org.graylog2.rest.models.system.lookup.DataAdapterApi)2 LookupTableApi (org.graylog2.rest.models.system.lookup.LookupTableApi)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 DuplicateKeyException (com.mongodb.DuplicateKeyException)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 AuditEvent (org.graylog2.audit.jersey.AuditEvent)1 ValidationResult (org.graylog2.plugin.rest.ValidationResult)1 SearchQuery (org.graylog2.search.SearchQuery)1 DBQuery (org.mongojack.DBQuery)1 DBSort (org.mongojack.DBSort)1