use of org.graylog2.plugin.streams.StreamRuleType in project graylog2-server by Graylog2.
the class StreamRouterEngine method match.
/**
* Returns a list of matching streams for the given message.
*
* @param message the message
* @return the list of matching streams
*/
public List<Stream> match(Message message) {
final Set<Stream> result = Sets.newHashSet();
final Set<Stream> blackList = Sets.newHashSet();
for (final Rule rule : rulesList) {
if (blackList.contains(rule.getStream())) {
continue;
}
final StreamRule streamRule = rule.getStreamRule();
final StreamRuleType streamRuleType = streamRule.getType();
final Stream.MatchingType matchingType = rule.getMatchingType();
if (!ruleTypesNotNeedingFieldPresence.contains(streamRuleType) && !message.hasField(streamRule.getField())) {
if (matchingType == Stream.MatchingType.AND) {
result.remove(rule.getStream());
// blacklist stream because it can't match anymore
blackList.add(rule.getStream());
}
continue;
}
final Stream stream;
if (streamRuleType != StreamRuleType.REGEX) {
stream = rule.match(message);
} else {
stream = rule.matchWithTimeOut(message, streamProcessingTimeout, TimeUnit.MILLISECONDS);
}
if (stream == null) {
if (matchingType == Stream.MatchingType.AND) {
result.remove(rule.getStream());
// blacklist stream because it can't match anymore
blackList.add(rule.getStream());
}
} else {
result.add(stream);
if (matchingType == Stream.MatchingType.OR) {
// blacklist stream because it is already matched
blackList.add(rule.getStream());
}
}
}
final Stream defaultStream = defaultStreamProvider.get();
boolean alreadyRemovedDefaultStream = false;
for (Stream stream : result) {
streamMetrics.markIncomingMeter(stream.getId());
if (stream.getRemoveMatchesFromDefaultStream()) {
if (alreadyRemovedDefaultStream || message.removeStream(defaultStream)) {
alreadyRemovedDefaultStream = true;
if (LOG.isTraceEnabled()) {
LOG.trace("Successfully removed default stream <{}> from message <{}>", defaultStream.getId(), message.getId());
}
} else {
if (LOG.isWarnEnabled()) {
LOG.warn("Couldn't remove default stream <{}> from message <{}>", defaultStream.getId(), message.getId());
}
}
}
}
// or someone removed it, in which case we don't mark it.
if (!alreadyRemovedDefaultStream) {
streamMetrics.markIncomingMeter(defaultStream.getId());
}
return ImmutableList.copyOf(result);
}
use of org.graylog2.plugin.streams.StreamRuleType in project graylog2-server by Graylog2.
the class StreamRuleResource method update.
@PUT
@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)
@AuditEvent(type = AuditEventTypes.STREAM_RULE_UPDATE)
public SingleStreamRuleSummaryResponse update(@ApiParam(name = "streamid", value = "The stream id this rule belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "streamRuleId", value = "The stream rule id we are updating", required = true) @PathParam("streamRuleId") String streamRuleId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateStreamRuleRequest cr) throws NotFoundException, ValidationException {
checkPermission(RestPermissions.STREAMS_EDIT, streamid);
checkNotDefaultStream(streamid, "Cannot update stream rules on default stream.");
final StreamRule streamRule;
streamRule = streamRuleService.load(streamRuleId);
if (!streamRule.getStreamId().equals(streamid)) {
throw new NotFoundException("Couldn't update stream rule " + streamRuleId + "in stream " + streamid);
}
final StreamRuleType streamRuleType = StreamRuleType.fromInteger(cr.type());
if (null == streamRuleType) {
throw new BadRequestException("Unknown stream rule type " + cr.type());
}
streamRule.setField(cr.field());
streamRule.setType(streamRuleType);
streamRule.setInverted(cr.inverted());
streamRule.setValue(cr.value());
streamRule.setDescription(cr.description());
streamRuleService.save(streamRule);
clusterEventBus.post(StreamsChangedEvent.create(streamid));
return SingleStreamRuleSummaryResponse.create(streamRule.getId());
}
use of org.graylog2.plugin.streams.StreamRuleType in project graylog2-server by Graylog2.
the class StreamRouterEngineTest method getStreamRuleMock.
private StreamRule getStreamRuleMock(String id, StreamRuleType type, String field, String value) {
final StreamRule result = mock(StreamRule.class);
when(result.getId()).thenReturn(id);
when(result.getType()).thenReturn(type);
when(result.getField()).thenReturn(field);
when(result.getValue()).thenReturn(value);
return result;
}
Aggregations