Search in sources :

Example 6 with PaginatedResponse

use of org.graylog2.rest.models.PaginatedResponse in project graylog2-server by Graylog2.

the class ViewsResource method views.

@GET
@ApiOperation("Get a list of all views")
public PaginatedResponse<ViewDTO> 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<ViewDTO> result = dbService.searchPaginated(searchQuery, searchUser::canReadView, order, sortField, page, perPage);
        return PaginatedResponse.create("views", result, query);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) ViewDTO(org.graylog.plugins.views.search.views.ViewDTO) BadRequestException(javax.ws.rs.BadRequestException) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 7 with PaginatedResponse

use of org.graylog2.rest.models.PaginatedResponse in project graylog2-server by Graylog2.

the class PaginatedResponseTest method serializeWithQueryAndContext.

@Test
public void serializeWithQueryAndContext() throws Exception {
    final ImmutableList<String> values = ImmutableList.of("hello", "world");
    final ImmutableMap<String, Object> context = ImmutableMap.of("context1", "wow");
    final PaginatedList<String> paginatedList = new PaginatedList<>(values, values.size(), 1, 10);
    final PaginatedResponse<String> response = PaginatedResponse.create("foo", paginatedList, "query1", context);
    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");
    jsonPathAssert.jsonPathAsString("$.context.context1").isEqualTo("wow");
}
Also used : JsonPathAssert(com.revinate.assertj.json.JsonPathAssert) PaginatedList(org.graylog2.database.PaginatedList) DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Example 8 with PaginatedResponse

use of org.graylog2.rest.models.PaginatedResponse in project graylog2-server by Graylog2.

the class PaginatedResponseTest method serializeWithContext.

@Test
public void serializeWithContext() throws Exception {
    final ImmutableList<String> values = ImmutableList.of("hello", "world");
    final ImmutableMap<String, Object> context = ImmutableMap.of("context1", "wow");
    final PaginatedList<String> paginatedList = new PaginatedList<>(values, values.size(), 1, 10);
    final PaginatedResponse<String> response = PaginatedResponse.create("foo", paginatedList, context);
    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");
    jsonPathAssert.jsonPathAsString("$.context.context1").isEqualTo("wow");
}
Also used : JsonPathAssert(com.revinate.assertj.json.JsonPathAssert) PaginatedList(org.graylog2.database.PaginatedList) DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Example 9 with PaginatedResponse

use of org.graylog2.rest.models.PaginatedResponse 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);
    }
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) ViewSummaryDTO(org.graylog.plugins.views.search.views.ViewSummaryDTO) BadRequestException(javax.ws.rs.BadRequestException) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 10 with PaginatedResponse

use of org.graylog2.rest.models.PaginatedResponse in project graylog2-server by Graylog2.

the class DashboardsResource method views.

@GET
@ApiOperation("Get a list of all dashboards")
@Timed
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.DASHBOARD, searchQuery, searchUser::canReadView, order, sortField, page, perPage);
        return PaginatedResponse.create("views", result, query);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) ViewSummaryDTO(org.graylog.plugins.views.search.views.ViewSummaryDTO) BadRequestException(javax.ws.rs.BadRequestException) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)14 GET (javax.ws.rs.GET)14 SearchQuery (org.graylog2.search.SearchQuery)12 BadRequestException (javax.ws.rs.BadRequestException)10 Path (javax.ws.rs.Path)8 PaginatedList (org.graylog2.database.PaginatedList)8 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)7 Produces (javax.ws.rs.Produces)5 Timed (com.codahale.metrics.annotation.Timed)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 DocumentContext (com.jayway.jsonpath.DocumentContext)4 JsonPathAssert (com.revinate.assertj.json.JsonPathAssert)4 Api (io.swagger.annotations.Api)4 ApiParam (io.swagger.annotations.ApiParam)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 NotFoundException (javax.ws.rs.NotFoundException)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3