org.graylog2.rest.resources.search.AbsoluteSearchResource Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.rest.resources.search;
import com.codahale.metrics.annotation.Timed;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.glassfish.jersey.server.ChunkedOutput;
import org.graylog.plugins.views.search.engine.SearchExecutor;
import org.graylog.plugins.views.search.permissions.SearchUser;
import org.graylog.plugins.views.search.searchtypes.Sort;
import org.graylog2.decorators.DecoratorProcessor;
import org.graylog2.indexer.results.ChunkedResult;
import org.graylog2.indexer.results.ResultChunk;
import org.graylog2.indexer.searches.Searches;
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.plugin.indexer.searches.timeranges.AbsoluteRange;
import org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException;
import org.graylog2.plugin.indexer.searches.timeranges.TimeRange;
import org.graylog2.rest.MoreMediaTypes;
import org.graylog2.rest.resources.search.responses.SearchResponse;
import org.graylog2.shared.security.RestPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.validation.constraints.NotEmpty;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import static org.graylog2.shared.rest.documentation.generator.Generator.CLOUD_VISIBLE;
@RequiresAuthentication
@Api(value = "Legacy/Search/Absolute", description = "Message search", tags = {CLOUD_VISIBLE})
@Path("/search/universal/absolute")
public class AbsoluteSearchResource extends SearchResource {
private static final Logger LOG = LoggerFactory.getLogger(AbsoluteSearchResource.class);
@Inject
public AbsoluteSearchResource(Searches searches,
SearchExecutor searchExecutor,
ClusterConfigService clusterConfigService,
DecoratorProcessor decoratorProcessor) {
super(searches, clusterConfigService, decoratorProcessor, searchExecutor);
}
@GET
@Timed
@ApiOperation(value = "Message search with absolute timerange.",
notes = "Search for messages using an absolute timerange, specified as from/to " +
"with format yyyy-MM-ddTHH:mm:ss.SSSZ (e.g. 2014-01-23T15:34:49.000Z) or yyyy-MM-dd HH:mm:ss.")
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid timerange parameters provided.")
})
public SearchResponse searchAbsolute(
@ApiParam(name = "query", value = "Query (Lucene syntax)", required = true)
@QueryParam("query") @NotEmpty String query,
@ApiParam(name = "from", value = "Timerange start. See description for date format", required = true)
@QueryParam("from") @NotEmpty String from,
@ApiParam(name = "to", value = "Timerange end. See description for date format", required = true)
@QueryParam("to") @NotEmpty String to,
@ApiParam(name = "limit", value = "Maximum number of messages to return.") @QueryParam("limit") int limit,
@ApiParam(name = "offset", value = "Offset") @QueryParam("offset") int offset,
@ApiParam(name = "filter", value = "Filter") @QueryParam("filter") String filter,
@ApiParam(name = "fields", value = "Comma separated list of fields to return") @QueryParam("fields") String fields,
@ApiParam(name = "sort", value = "Sorting (field:asc / field:desc)") @QueryParam("sort") String sort,
@ApiParam(name = "decorate", value = "Run decorators on search result") @QueryParam("decorate") @DefaultValue("true") boolean decorate,
@Context SearchUser searchUser) {
checkSearchPermission(filter, RestPermissions.SEARCHES_ABSOLUTE);
final Sort sorting = buildSortOrder(sort);
final List fieldList = parseOptionalFields(fields);
final TimeRange timeRange = buildAbsoluteTimeRange(from, to);
return search(query, limit, offset, filter, decorate, searchUser, fieldList, sorting, timeRange);
}
@GET
@Timed
@ApiOperation(value = "Message search with absolute timerange.",
notes = "Search for messages using an absolute timerange, specified as from/to " +
"with format yyyy-MM-ddTHH:mm:ss.SSSZ (e.g. 2014-01-23T15:34:49.000Z) or yyyy-MM-dd HH:mm:ss.")
@Produces(MoreMediaTypes.TEXT_CSV)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid timerange parameters provided.")
})
public ChunkedOutput searchAbsoluteChunked(
@ApiParam(name = "query", value = "Query (Lucene syntax)", required = true)
@QueryParam("query") @NotEmpty String query,
@ApiParam(name = "from", value = "Timerange start. See description for date format", required = true)
@QueryParam("from") @NotEmpty String from,
@ApiParam(name = "to", value = "Timerange end. See description for date format", required = true)
@QueryParam("to") @NotEmpty String to,
@ApiParam(name = "limit", value = "Maximum number of messages to return.", required = false) @QueryParam("limit") int limit,
@ApiParam(name = "offset", value = "Offset", required = false) @QueryParam("offset") int offset,
@ApiParam(name = "batch_size", value = "Batch size for the backend storage export request.", required = false) @QueryParam("batch_size") @DefaultValue(DEFAULT_SCROLL_BATCH_SIZE) int batchSize,
@ApiParam(name = "filter", value = "Filter", required = false) @QueryParam("filter") String filter,
@ApiParam(name = "fields", value = "Comma separated list of fields to return", required = true)
@QueryParam("fields") @NotEmpty String fields) {
checkSearchPermission(filter, RestPermissions.SEARCHES_ABSOLUTE);
final List fieldList = parseFields(fields);
final TimeRange timeRange = buildAbsoluteTimeRange(from, to);
final ChunkedResult scroll = searches
.scroll(query, timeRange, limit, offset, fieldList, filter, batchSize);
return buildChunkedOutput(scroll);
}
@GET
@Path("/export")
@Timed
@ApiOperation(value = "Export message search with absolute timerange.",
notes = "Search for messages using an absolute timerange, specified as from/to " +
"with format yyyy-MM-ddTHH:mm:ss.SSSZ (e.g. 2014-01-23T15:34:49.000Z) or yyyy-MM-dd HH:mm:ss.")
@Produces(MoreMediaTypes.TEXT_CSV)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid timerange parameters provided.")
})
public Response exportSearchAbsoluteChunked(
@ApiParam(name = "query", value = "Query (Lucene syntax)", required = true)
@QueryParam("query") @NotEmpty String query,
@ApiParam(name = "from", value = "Timerange start. See description for date format", required = true)
@QueryParam("from") @NotEmpty String from,
@ApiParam(name = "to", value = "Timerange end. See description for date format", required = true)
@QueryParam("to") @NotEmpty String to,
@ApiParam(name = "limit", value = "Maximum number of messages to return.", required = false) @QueryParam("limit") int limit,
@ApiParam(name = "offset", value = "Offset", required = false) @QueryParam("offset") int offset,
@ApiParam(name = "batch_size", value = "Batch size for the backend storage export request.", required = false) @QueryParam("batch_size") @DefaultValue(DEFAULT_SCROLL_BATCH_SIZE) int batchSize,
@ApiParam(name = "filter", value = "Filter", required = false) @QueryParam("filter") String filter,
@ApiParam(name = "fields", value = "Comma separated list of fields to return", required = true)
@QueryParam("fields") @NotEmpty String fields) {
checkSearchPermission(filter, RestPermissions.SEARCHES_ABSOLUTE);
final String filename = "graylog-search-result-absolute-" + from + "-" + to + ".csv";
return Response
.ok(searchAbsoluteChunked(query, from, to, limit, offset, batchSize, filter, fields))
.header("Content-Disposition", "attachment; filename=" + filename)
.build();
}
private TimeRange buildAbsoluteTimeRange(String from, String to) {
try {
return restrictTimeRange(AbsoluteRange.create(from, to));
} catch (InvalidRangeParametersException e) {
LOG.warn("Invalid timerange parameters provided. Returning HTTP 400.");
throw new BadRequestException("Invalid timerange parameters provided", e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy