@GET @Timed @ApiOperation(value = "Get a list of all stream rules") @Produces(MediaType.APPLICATION_JSON) public String get( @ApiParam( title = "streamid", description = "The id of the stream whose stream rules we want.", required = true) @PathParam("streamid") String streamid) { List<Map<String, Object>> streamRules = Lists.newArrayList(); checkPermission(RestPermissions.STREAMS_READ, streamid); final Stream stream; try { stream = streamService.load(streamid); } catch (NotFoundException e) { throw new WebApplicationException(404); } try { for (StreamRule streamRule : streamRuleService.loadForStream(stream)) { streamRules.add(streamRule.asMap()); } } catch (org.graylog2.database.NotFoundException e) { throw new WebApplicationException(404); } Map<String, Object> result = Maps.newHashMap(); result.put("total", streamRules.size()); result.put("stream_rules", streamRules); return json(result); }
@Test public void testMissedMatch() { StreamRule rule = getSampleRule(); rule.setValue("^foo"); Message msg = getSampleMessage(); msg.addField("something", "zomg"); StreamRuleMatcher matcher = getMatcher(rule); assertFalse(matcher.match(msg, rule)); }
@Test public void testSuccessfulMatch() { StreamRule rule = getSampleRule(); rule.setValue("^foo"); Message msg = getSampleMessage(); msg.addField("something", "foobar"); StreamRuleMatcher matcher = getMatcher(rule); assertTrue(matcher.match(msg, rule)); }
protected String buildStreamRules(Stream stream) { StringBuilder sb = new StringBuilder(); for (StreamRule streamRule : stream.getStreamRules()) { sb.append("_").append(streamRule.getField()).append("_ "); sb.append(streamRule.getType()) .append(" _") .append(streamRule.getValue()) .append("_") .append("\n"); } return sb.toString(); }
@Test public void testSuccessfulComplexRegexMatch() { StreamRule rule = getSampleRule(); rule.setField("some_field"); rule.setValue("foo=^foo|bar\\d.+wat"); Message msg = getSampleMessage(); msg.addField("some_field", "bar1foowat"); StreamRuleMatcher matcher = getMatcher(rule); assertTrue(matcher.match(msg, rule)); }
@Override public boolean match(Message msg, StreamRule rule) { Double msgVal = getDouble(msg.getField(rule.getField())); if (msgVal == null) { return false; } Double ruleVal = getDouble(rule.getValue()); if (ruleVal == null) { return false; } return rule.getInverted() ^ (msgVal > ruleVal); }
@DELETE @Path("/{streamRuleId}") @Timed @ApiOperation(value = "Delete a stream rule") @ApiResponses( value = { @ApiResponse(code = 404, message = "Stream rule not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") }) public Response delete( @ApiParam( title = "streamid", description = "The stream id this new rule belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(title = "streamRuleId", required = true) @PathParam("streamRuleId") String streamRuleId) { if (streamRuleId == null || streamRuleId.isEmpty()) { LOG.error("Missing streamRuleId. Returning HTTP 400."); throw new WebApplicationException(400); } checkPermission(RestPermissions.STREAMS_EDIT, streamid); try { StreamRule streamRule = streamRuleService.load(loadObjectId(streamRuleId)); if (streamRule.getStreamId().equals(streamid)) { streamRuleService.destroy(streamRule); } else { throw new NotFoundException(); } } catch (org.graylog2.database.NotFoundException e) { throw new WebApplicationException(404); } return Response.status(Response.Status.fromStatusCode(204)).build(); }
@POST @Path("/{streamRuleId}") @Timed @ApiOperation(value = "Update a stream rule") @ApiResponses( value = { @ApiResponse(code = 404, message = "Stream or stream rule not found."), @ApiResponse(code = 400, message = "Invalid JSON Body.") }) @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response update( @ApiParam( title = "streamid", description = "The stream id this rule belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam( title = "streamRuleId", description = "The stream rule id we are updating", required = true) @PathParam("streamRuleId") String streamRuleId, @ApiParam(title = "JSON body", required = true) String body) { CreateRequest cr; checkPermission(RestPermissions.STREAMS_EDIT, streamid); try { cr = objectMapper.readValue(body, CreateRequest.class); } catch (IOException e) { LOG.error("Error while parsing JSON", e); throw new WebApplicationException(e, Response.Status.BAD_REQUEST); } StreamRule streamRule; try { streamRule = streamRuleService.load(loadObjectId(streamRuleId)); if (!streamRule.getStreamId().equals(streamid)) { throw new NotFoundException(); } } catch (org.graylog2.database.NotFoundException e) { throw new WebApplicationException(404); } streamRule.setField(cr.field); streamRule.setType(StreamRuleType.fromInteger(cr.type)); streamRule.setInverted(cr.inverted); streamRule.setValue(cr.value); String id; try { streamRuleService.save(streamRule); id = streamRule.getId(); } catch (ValidationException e) { LOG.error("Validation error.", e); throw new WebApplicationException(e, Response.Status.BAD_REQUEST); } Map<String, Object> result = Maps.newHashMap(); result.put("streamrule_id", id); return Response.status(Response.Status.OK).entity(json(result)).build(); }
protected StreamRule getSampleRule() { StreamRule rule = super.getSampleRule(); rule.setType(StreamRuleType.REGEX); return rule; }