use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.
the class MongoDbGrokPatternService method save.
@Override
public GrokPattern save(GrokPattern pattern) throws ValidationException {
try {
if (!validate(pattern)) {
throw new ValidationException("Invalid pattern " + pattern);
}
} catch (GrokException | PatternSyntaxException e) {
throw new ValidationException("Invalid pattern " + pattern + "\n" + e.getMessage());
}
if (loadByName(pattern.name()).isPresent()) {
throw new ValidationException("Grok pattern " + pattern.name() + " already exists");
}
final WriteResult<GrokPattern, ObjectId> result = dbCollection.save(pattern);
final GrokPattern savedGrokPattern = result.getSavedObject();
clusterBus.post(GrokPatternsUpdatedEvent.create(ImmutableSet.of(savedGrokPattern.name())));
return savedGrokPattern;
}
use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.
the class MongoDbGrokPatternService method delete.
@Override
public int delete(String patternId) {
final GrokPattern grokPattern;
try {
grokPattern = load(patternId);
} catch (NotFoundException e) {
log.debug("Couldn't find grok pattern with ID <{}> for deletion", patternId, e);
return 0;
}
final ObjectId id = new ObjectId(patternId);
final String name = grokPattern.name();
final int deletedPatterns = dbCollection.removeById(id).getN();
clusterBus.post(GrokPatternsDeletedEvent.create(ImmutableSet.of(name)));
return deletedPatterns;
}
use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.
the class InMemoryGrokPatternService method save.
@Override
public GrokPattern save(GrokPattern pattern) throws ValidationException {
try {
if (!validate(pattern)) {
throw new ValidationException("Pattern " + pattern.name() + " invalid.");
}
} catch (GrokException | PatternSyntaxException e) {
throw new ValidationException("Invalid pattern " + pattern + "\n" + e.getMessage());
}
GrokPattern toSave;
if (pattern.id() == null) {
toSave = pattern.toBuilder().id(createId()).build();
} else {
toSave = pattern;
}
store.put(toSave.id(), toSave);
clusterBus.post(GrokPatternsUpdatedEvent.create(ImmutableSet.of(toSave.name())));
return toSave;
}
use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.
the class GrokTesterResource method doTestGrok.
private GrokTesterResponse doTestGrok(String string, String pattern, boolean namedCapturesOnly) throws GrokException {
final Set<GrokPattern> grokPatterns = grokPatternService.loadAll();
final GrokCompiler grokCompiler = GrokCompiler.newInstance();
for (GrokPattern grokPattern : grokPatterns) {
grokCompiler.register(grokPattern.name(), grokPattern.pattern());
}
final Grok grok;
try {
grok = grokCompiler.compile(pattern, namedCapturesOnly);
} catch (Exception e) {
return GrokTesterResponse.createError(pattern, string, e.getMessage());
}
final Match match = grok.match(string);
final Map<String, Object> matches = match.captureFlattened();
final GrokTesterResponse response;
if (matches.isEmpty()) {
response = GrokTesterResponse.createSuccess(false, Collections.<GrokTesterResponse.Match>emptyList(), pattern, string);
} else {
final List<GrokTesterResponse.Match> responseMatches = Lists.newArrayList();
for (final Map.Entry<String, Object> entry : matches.entrySet()) {
final Object value = entry.getValue();
if (value != null) {
responseMatches.add(GrokTesterResponse.Match.create(entry.getKey(), value.toString()));
}
}
response = GrokTesterResponse.createSuccess(true, responseMatches, pattern, string);
}
return response;
}
use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.
the class GrokResource method createPattern.
@POST
@Timed
@ApiOperation(value = "Add a new named pattern", response = GrokPattern.class)
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_CREATE)
public Response createPattern(@ApiParam(name = "pattern", required = true) @Valid @NotNull GrokPattern pattern) throws ValidationException {
checkPermission(RestPermissions.INPUTS_CREATE);
// remove the ID from the pattern, this is only used to create new patterns
final GrokPattern newPattern = grokPatternService.save(pattern.toBuilder().id(null).build());
final URI patternUri = getUriBuilderToSelf().path(GrokResource.class, "listPattern").build(newPattern.id());
return Response.created(patternUri).entity(newPattern).build();
}
Aggregations