Search in sources :

Example 1 with MessagesRequest

use of org.graylog.plugins.views.search.export.MessagesRequest in project graylog2-server by Graylog2.

the class MessagesResourceTest method appliesDefaultStreamsToRequestIfOmitted.

@Test
void appliesDefaultStreamsToRequestIfOmitted() {
    MessagesRequest request = validRequest();
    final SearchUser searchUser = TestSearchUser.builder().allowStream("stream-1").allowStream("stream-2").build();
    ArgumentCaptor<MessagesRequest> captor = ArgumentCaptor.forClass(MessagesRequest.class);
    when(commandFactory.buildFromRequest(captor.capture())).thenReturn(ExportMessagesCommand.withDefaults());
    sut.retrieve(request, searchUser);
    MessagesRequest value = captor.getValue();
    assertThat(value.streams()).containsExactly("stream-1", "stream-2");
}
Also used : MessagesRequest(org.graylog.plugins.views.search.export.MessagesRequest) SearchUser(org.graylog.plugins.views.search.permissions.SearchUser) Test(org.junit.jupiter.api.Test)

Example 2 with MessagesRequest

use of org.graylog.plugins.views.search.export.MessagesRequest in project graylog2-server by Graylog2.

the class MessagesResourceTest method checksStreamPermissionsForPlainRequest.

@Test
void checksStreamPermissionsForPlainRequest() {
    final SearchUser searchUser = TestSearchUser.builder().denyStream("stream-1").build();
    MessagesRequest request = validRequest().toBuilder().streams(ImmutableSet.of("stream-1")).build();
    PermissionException exception = new PermissionException("The wurst is yet to come");
    doThrow(exception).when(executionGuard).checkUserIsPermittedToSeeStreams(eq(ImmutableSet.of("stream-1")), any());
    assertThatExceptionOfType(PermissionException.class).isThrownBy(() -> sut.retrieve(request, searchUser)).withMessageContaining(exception.getMessage());
}
Also used : PermissionException(org.graylog.plugins.views.search.errors.PermissionException) MessagesRequest(org.graylog.plugins.views.search.export.MessagesRequest) SearchUser(org.graylog.plugins.views.search.permissions.SearchUser) Test(org.junit.jupiter.api.Test)

Example 3 with MessagesRequest

use of org.graylog.plugins.views.search.export.MessagesRequest in project graylog2-server by Graylog2.

the class MessagesResource method retrieve.

@ApiOperation(value = "Export messages as CSV", notes = "Use this endpoint, if you want to configure export parameters freely instead of relying on an existing Search")
@POST
@Produces(MoreMediaTypes.TEXT_CSV)
@NoAuditEvent("Has custom audit events")
public ChunkedOutput<SimpleMessageChunk> retrieve(@ApiParam @Valid MessagesRequest rawrequest, @Context SearchUser searchUser) {
    final MessagesRequest request = fillInIfNecessary(rawrequest, searchUser);
    final ValidationRequest.Builder validationReq = ValidationRequest.builder();
    Optional.ofNullable(rawrequest.queryString()).ifPresent(validationReq::query);
    Optional.ofNullable(rawrequest.timeRange()).ifPresent(validationReq::timerange);
    Optional.ofNullable(rawrequest.streams()).ifPresent(validationReq::streams);
    final ValidationResponse validationResponse = queryValidationService.validate(validationReq.build());
    if (validationResponse.status().equals(ValidationStatus.ERROR)) {
        validationResponse.explanations().stream().findFirst().map(ValidationMessage::errorMessage).ifPresent(message -> {
            throw new BadRequestException("Request validation failed: " + message);
        });
    }
    executionGuard.checkUserIsPermittedToSeeStreams(request.streams(), searchUser::canReadStream);
    ExportMessagesCommand command = commandFactory.buildFromRequest(request);
    return asyncRunner.apply(chunkConsumer -> exporter().export(command, chunkConsumer));
}
Also used : ValidationResponse(org.graylog.plugins.views.search.validation.ValidationResponse) ValidationRequest(org.graylog.plugins.views.search.validation.ValidationRequest) MessagesRequest(org.graylog.plugins.views.search.export.MessagesRequest) BadRequestException(javax.ws.rs.BadRequestException) ExportMessagesCommand(org.graylog.plugins.views.search.export.ExportMessagesCommand) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Aggregations

MessagesRequest (org.graylog.plugins.views.search.export.MessagesRequest)3 SearchUser (org.graylog.plugins.views.search.permissions.SearchUser)2 Test (org.junit.jupiter.api.Test)2 ApiOperation (io.swagger.annotations.ApiOperation)1 BadRequestException (javax.ws.rs.BadRequestException)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 PermissionException (org.graylog.plugins.views.search.errors.PermissionException)1 ExportMessagesCommand (org.graylog.plugins.views.search.export.ExportMessagesCommand)1 ValidationRequest (org.graylog.plugins.views.search.validation.ValidationRequest)1 ValidationResponse (org.graylog.plugins.views.search.validation.ValidationResponse)1 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)1