use of org.graylog2.rest.resources.streams.responses.TestMatchResponse in project graylog2-server by Graylog2.
the class StreamResource method testMatch.
@POST
@Path("/{streamId}/testMatch")
@Timed
@ApiOperation(value = "Test matching of a stream against a supplied message")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid or missing Stream id.") })
@NoAuditEvent("only used for testing stream matches")
public TestMatchResponse testMatch(@ApiParam(name = "streamId", required = true) @PathParam("streamId") String streamId, @ApiParam(name = "JSON body", required = true) @NotNull Map<String, Map<String, Object>> serialisedMessage) throws NotFoundException {
checkPermission(RestPermissions.STREAMS_READ, streamId);
final Stream stream = streamService.load(streamId);
// This is such a hack...
final Map<String, Object> m = new HashMap<>(serialisedMessage.get("message"));
final String timeStamp = firstNonNull((String) m.get(Message.FIELD_TIMESTAMP), DateTime.now(DateTimeZone.UTC).toString(ISODateTimeFormat.dateTime()));
m.put(Message.FIELD_TIMESTAMP, Tools.dateTimeFromString(timeStamp));
final Message message = new Message(m);
final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("stream-" + streamId + "-test-match-%d").build());
final StreamRouterEngine streamRouterEngine = streamRouterEngineFactory.create(Lists.newArrayList(stream), executor);
final List<StreamRouterEngine.StreamTestMatch> streamTestMatches = streamRouterEngine.testMatch(message);
final StreamRouterEngine.StreamTestMatch streamTestMatch = streamTestMatches.get(0);
final Map<String, Boolean> rules = Maps.newHashMap();
for (Map.Entry<StreamRule, Boolean> match : streamTestMatch.getMatches().entrySet()) {
rules.put(match.getKey().getId(), match.getValue());
}
return TestMatchResponse.create(streamTestMatch.isMatched(), rules);
}
Aggregations