use of org.graylog2.search.SearchQuery in project graylog2-server by Graylog2.
the class AuthzRolesResource method getList.
@GET
@Timed
@ApiOperation(value = "Get a paginated list of all roles")
@RequiresPermissions(RestPermissions.ROLES_READ)
public PaginatedResponse<AuthzRoleDTO> getList(@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,description") @DefaultValue(AuthzRoleDTO.FIELD_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
SearchQuery searchQuery;
try {
searchQuery = searchQueryParser.parse(query);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
}
final PaginatedList<AuthzRoleDTO> result = authzRolesService.findPaginated(searchQuery, page, perPage, sort, order);
final Map<String, Set<Map<String, String>>> userRoleMap = userRoleContext(result);
return PaginatedResponse.create("roles", result, query, ImmutableMap.of("users", userRoleMap));
}
use of org.graylog2.search.SearchQuery in project graylog2-server by Graylog2.
the class StreamResource method getPage.
@GET
@Timed
@Path("/paginated")
@ApiOperation(value = "Get a paginated list of streams")
@Produces(MediaType.APPLICATION_JSON)
public StreamPageListResponse getPage(@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 = "title,description") @DefaultValue(StreamImpl.FIELD_TITLE) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
SearchQuery searchQuery;
try {
searchQuery = searchQueryParser.parse(query);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
}
final Predicate<StreamDTO> permissionFilter = streamDTO -> isPermitted(RestPermissions.STREAMS_READ, streamDTO.id());
final PaginatedList<StreamDTO> result = paginatedStreamService.findPaginated(searchQuery, permissionFilter, page, perPage, sort, order);
final List<String> streamIds = result.stream().map(streamDTO -> streamDTO.id()).collect(Collectors.toList());
final Map<String, List<StreamRule>> streamRuleMap = streamRuleService.loadForStreamIds(streamIds);
final List<StreamDTO> streams = result.stream().map(streamDTO -> {
List<StreamRule> rules = streamRuleMap.getOrDefault(streamDTO.id(), Collections.emptyList());
return streamDTO.toBuilder().rules(rules).build();
}).collect(Collectors.toList());
final long total = paginatedStreamService.count();
final PaginatedList<StreamDTO> streamDTOS = new PaginatedList<>(streams, result.pagination().total(), result.pagination().page(), result.pagination().perPage());
return StreamPageListResponse.create(query, streamDTOS.pagination(), total, sort, order, streams);
}
use of org.graylog2.search.SearchQuery in project graylog2-server by Graylog2.
the class GrokResource method getPage.
@GET
@Timed
@Path("/paginated")
@ApiOperation("Get existing grok patterns paged")
@Produces(MediaType.APPLICATION_JSON)
public PaginatedResponse<GrokPattern> getPage(@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 = "title,description,id") @DefaultValue(GrokPattern.FIELD_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
checkPermission(RestPermissions.INPUTS_READ);
SearchQuery searchQuery;
try {
searchQuery = searchQueryParser.parse(query);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
}
final PaginatedList<GrokPattern> result = paginatedGrokPatternService.findPaginated(searchQuery, page, perPage, sort, order);
return PaginatedResponse.create("patterns", result);
}
use of org.graylog2.search.SearchQuery in project graylog2-server by Graylog2.
the class UsersResource method getPage.
@GET
@Timed
@Path("/paginated")
@ApiOperation(value = "Get paginated list of users")
@RequiresPermissions(RestPermissions.USERS_LIST)
@Produces(MediaType.APPLICATION_JSON)
public PaginatedResponse<UserOverviewDTO> getPage(@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 = "title,description") @DefaultValue(UserOverviewDTO.FIELD_FULL_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
SearchQuery searchQuery;
final AllUserSessions sessions = AllUserSessions.create(sessionService);
try {
searchQuery = searchQueryParser.parse(query);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
}
final PaginatedList<UserOverviewDTO> result = paginatedUserService.findPaginated(searchQuery, page, perPage, sort, order);
final Set<String> allRoleIds = result.stream().flatMap(userDTO -> {
if (userDTO.roles() != null) {
return userDTO.roles().stream();
}
return Stream.empty();
}).collect(Collectors.toSet());
Map<String, String> roleNameMap;
try {
roleNameMap = getRoleNameMap(allRoleIds);
} catch (org.graylog2.database.NotFoundException e) {
throw new NotFoundException("Couldn't find roles: " + e.getMessage());
}
final UserOverviewDTO adminUser = getAdminUserDTO(sessions);
List<UserOverviewDTO> users = result.stream().map(userDTO -> {
UserOverviewDTO.Builder builder = userDTO.toBuilder().fillSession(sessions.forUser(userDTO));
if (userDTO.roles() != null) {
builder.roles(userDTO.roles().stream().map(roleNameMap::get).collect(Collectors.toSet()));
}
return builder.build();
}).collect(Collectors.toList());
final PaginatedList<UserOverviewDTO> userOverviewDTOS = new PaginatedList<>(users, result.pagination().total(), result.pagination().page(), result.pagination().perPage());
return PaginatedResponse.create("users", userOverviewDTOS, query, Collections.singletonMap("admin_user", adminUser));
}
use of org.graylog2.search.SearchQuery 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);
}
}
Aggregations