-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GrokPatternServiceImpl.java
106 lines (95 loc) · 3.84 KB
/
GrokPatternServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.grok;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import oi.thekraken.grok.api.Grok;
import oi.thekraken.grok.api.exception.GrokException;
import org.bson.types.ObjectId;
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider;
import org.graylog2.database.MongoConnection;
import org.graylog2.database.NotFoundException;
import org.graylog2.plugin.database.ValidationException;
import org.mongojack.DBCursor;
import org.mongojack.JacksonDBCollection;
import org.mongojack.WriteResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.util.Set;
import java.util.regex.PatternSyntaxException;
public class GrokPatternServiceImpl implements GrokPatternService {
public static final String GROK_PATTERNS = "grok_patterns";
private static final Logger log = LoggerFactory.getLogger(GrokPatternServiceImpl.class);
private final JacksonDBCollection<GrokPattern, ObjectId> dbCollection;
@Inject
protected GrokPatternServiceImpl(MongoConnection mongoConnection,
MongoJackObjectMapperProvider mapper) {
dbCollection = JacksonDBCollection.wrap(
mongoConnection.getDatabase().getCollection(GROK_PATTERNS),
GrokPattern.class,
ObjectId.class,
mapper.get());
}
@Override
public GrokPattern load(String patternId) throws NotFoundException {
final GrokPattern pattern = dbCollection.findOneById(new ObjectId(patternId));
if (pattern == null) {
throw new NotFoundException();
}
return pattern;
}
@Override
public Set<GrokPattern> loadAll() {
final DBCursor<GrokPattern> grokPatterns = dbCollection.find();
final Set<GrokPattern> patterns = Sets.newHashSet();
Iterables.addAll(patterns, grokPatterns);
return patterns;
}
@Override
public GrokPattern save(GrokPattern pattern) throws ValidationException {
if (!validate(pattern)) {
throw new ValidationException("Invalid pattern " + pattern);
}
final WriteResult<GrokPattern, ObjectId> result = dbCollection.save(pattern);
return result.getSavedObject();
}
@Override
public boolean validate(GrokPattern pattern) {
final boolean fieldsMissing = !(Strings.isNullOrEmpty(pattern.name) || Strings.isNullOrEmpty(pattern.pattern));
try {
final Grok grok = new Grok();
grok.addPattern(pattern.name, pattern.pattern);
grok.compile("%{" + pattern.name + "}");
} catch (GrokException ignored) {
// this only checks for null or empty again.
} catch (PatternSyntaxException e) {
log.warn("Invalid regular expression syntax for '" + pattern.name + "' with pattern " + pattern.pattern, e);
return false;
}
return fieldsMissing;
}
@Override
public int delete(String patternId) {
return dbCollection.removeById(new ObjectId(patternId)).getN();
}
@Override
public void deleteAll() {
dbCollection.drop();
}
}