use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.
the class PaginatedResponseTest method serialize.
@Test
public void serialize() throws Exception {
final ImmutableList<String> values = ImmutableList.of("hello", "world");
final PaginatedList<String> paginatedList = new PaginatedList<>(values, values.size(), 1, 10);
final PaginatedResponse<String> response = PaginatedResponse.create("foo", paginatedList);
final DocumentContext ctx = JsonPath.parse(objectMapper.writeValueAsString(response));
final JsonPathAssert jsonPathAssert = JsonPathAssert.assertThat(ctx);
jsonPathAssert.jsonPathAsInteger("$.total").isEqualTo(2);
jsonPathAssert.jsonPathAsInteger("$.count").isEqualTo(2);
jsonPathAssert.jsonPathAsInteger("$.page").isEqualTo(1);
jsonPathAssert.jsonPathAsInteger("$.per_page").isEqualTo(10);
jsonPathAssert.jsonPathAsString("$.foo[0]").isEqualTo("hello");
jsonPathAssert.jsonPathAsString("$.foo[1]").isEqualTo("world");
assertThatThrownBy(() -> jsonPathAssert.jsonPathAsString("$.context")).isInstanceOf(PathNotFoundException.class);
}
use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.
the class PaginatedResponseTest method serializeWithQuery.
@Test
public void serializeWithQuery() throws Exception {
final ImmutableList<String> values = ImmutableList.of("hello", "world");
final PaginatedList<String> paginatedList = new PaginatedList<>(values, values.size(), 1, 10);
final PaginatedResponse<String> response = PaginatedResponse.create("foo", paginatedList, "query1");
final DocumentContext ctx = JsonPath.parse(objectMapper.writeValueAsString(response));
final JsonPathAssert jsonPathAssert = JsonPathAssert.assertThat(ctx);
jsonPathAssert.jsonPathAsString("$.query").isEqualTo("query1");
jsonPathAssert.jsonPathAsInteger("$.total").isEqualTo(2);
jsonPathAssert.jsonPathAsInteger("$.count").isEqualTo(2);
jsonPathAssert.jsonPathAsInteger("$.page").isEqualTo(1);
jsonPathAssert.jsonPathAsInteger("$.per_page").isEqualTo(10);
jsonPathAssert.jsonPathAsString("$.foo[0]").isEqualTo("hello");
jsonPathAssert.jsonPathAsString("$.foo[1]").isEqualTo("world");
assertThatThrownBy(() -> jsonPathAssert.jsonPathAsString("$.context")).isInstanceOf(PathNotFoundException.class);
}
use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.
the class CollectorResource method listSummary.
@GET
@Path("/summary")
@RequiresPermissions(SidecarRestPermissions.COLLECTORS_READ)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List a summary of all collectors")
public CollectorSummaryResponse listSummary(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "name,id,collector_id") @DefaultValue(Collector.FIELD_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
final SearchQuery searchQuery = searchQueryParser.parse(query);
final PaginatedList<Collector> collectors = this.collectorService.findPaginated(searchQuery, page, perPage, sort, order);
final long total = this.collectorService.count();
final List<CollectorSummary> summaries = collectors.stream().map(CollectorSummary::create).collect(Collectors.toList());
return CollectorSummaryResponse.create(query, collectors.pagination(), total, sort, order, summaries);
}
use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.
the class ConfigurationResource method listConfigurations.
@GET
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_READ)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List all configurations")
public ConfigurationListResponse listConfigurations(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "name,id,collector_id") @DefaultValue(Configuration.FIELD_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
final SearchQuery searchQuery = searchQueryParser.parse(query);
final PaginatedList<Configuration> configurations = this.configurationService.findPaginated(searchQuery, page, perPage, sort, order);
final long total = this.configurationService.count();
final List<ConfigurationSummary> result = configurations.stream().map(ConfigurationSummary::create).collect(Collectors.toList());
return ConfigurationListResponse.create(query, configurations.pagination(), total, sort, order, result);
}
use of org.graylog2.database.PaginatedList in project graylog2-server by Graylog2.
the class SavedSearchesResource method views.
@GET
@ApiOperation("Get a list of all searches")
public PaginatedResponse<ViewSummaryDTO> views(@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 = "id,title,created_at") @DefaultValue(ViewDTO.FIELD_TITLE) @QueryParam("sort") String sortField, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order, @ApiParam(name = "query") @QueryParam("query") String query, @Context SearchUser searchUser) {
if (!ViewDTO.SORT_FIELDS.contains(sortField.toLowerCase(ENGLISH))) {
sortField = ViewDTO.FIELD_TITLE;
}
try {
final SearchQuery searchQuery = searchQueryParser.parse(query);
final PaginatedList<ViewSummaryDTO> result = dbService.searchSummariesPaginatedByType(ViewDTO.Type.SEARCH, searchQuery, searchUser::canReadView, order, sortField, page, perPage);
return PaginatedResponse.create("views", result, query);
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage(), e);
}
}
Aggregations