use of io.krakens.grok.api.exception.GrokException 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 io.krakens.grok.api.exception.GrokException 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 io.krakens.grok.api.exception.GrokException 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 io.krakens.grok.api.exception.GrokException in project graylog2-server by Graylog2.
the class GrokResource method bulkUpdatePatterns.
@PUT
@Timed
@ApiOperation("Add a list of new patterns")
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_IMPORT_CREATE)
public Response bulkUpdatePatterns(@ApiParam(name = "patterns", required = true) @NotNull GrokPatternList patternList, // deprecated. used to drop all existing patterns before import
@Deprecated @QueryParam("replace") @DefaultValue("false") boolean deprecatedDropAllExisting, @ApiParam(name = "import-strategy", value = "Strategy to apply when importing.") @QueryParam("import-strategy") ImportStrategy importStrategy) throws ValidationException {
checkPermission(RestPermissions.INPUTS_CREATE);
try {
if (!grokPatternService.validateAll(patternList.patterns())) {
throw new ValidationException("Invalid pattern contained. Did not save any patterns.");
}
} catch (GrokException | IllegalArgumentException e) {
throw new ValidationException("Invalid pattern. Did not save any patterns\n" + e.getMessage());
}
ImportStrategy resolvedStrategy = importStrategy != null ? importStrategy : deprecatedDropAllExisting ? ImportStrategy.DROP_ALL_EXISTING : ImportStrategy.ABORT_ON_CONFLICT;
grokPatternService.saveAll(patternList.patterns(), resolvedStrategy);
return Response.accepted().build();
}
use of io.krakens.grok.api.exception.GrokException in project graylog2-server by Graylog2.
the class GrokResource method bulkUpdatePatternsFromTextFile.
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Timed
@ApiOperation("Add a list of new patterns")
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_IMPORT_CREATE)
public Response bulkUpdatePatternsFromTextFile(@ApiParam(name = "patterns", required = true) @NotNull InputStream patternsFile, // deprecated. used to drop all existing patterns before import
@Deprecated @QueryParam("replace") @DefaultValue("false") boolean deprecatedDropAllExisting, @ApiParam(name = "import-strategy", value = "Strategy to apply when importing.") @QueryParam("import-strategy") ImportStrategy importStrategy) throws ValidationException, IOException {
checkPermission(RestPermissions.INPUTS_CREATE);
final List<GrokPattern> grokPatterns = readGrokPatterns(patternsFile);
if (!grokPatterns.isEmpty()) {
try {
if (!grokPatternService.validateAll(grokPatterns)) {
throw new ValidationException("Invalid pattern contained. Did not save any patterns.");
}
} catch (GrokException | IllegalArgumentException e) {
throw new ValidationException("Invalid pattern. Did not save any patterns\n" + e.getMessage());
}
ImportStrategy resolvedStrategy = importStrategy != null ? importStrategy : deprecatedDropAllExisting ? ImportStrategy.DROP_ALL_EXISTING : ImportStrategy.ABORT_ON_CONFLICT;
grokPatternService.saveAll(grokPatterns, resolvedStrategy);
}
return Response.accepted().build();
}
Aggregations