Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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 .
*/
package org.graylog2.inputs.extractors;
import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.auto.value.AutoValue;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.Maps;
import org.graylog.autovalue.WithBeanGetter;
import org.graylog2.ConfigurationException;
import org.graylog2.plugin.inputs.Converter;
import org.graylog2.plugin.inputs.Extractor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Strings.isNullOrEmpty;
public class JsonExtractor extends Extractor {
private static final Logger LOG = LoggerFactory.getLogger(JsonExtractor.class);
private static final String CK_FLATTEN = "flatten";
private static final String CK_LIST_SEPARATOR = "list_separator";
private static final String CK_KEY_SEPARATOR = "key_separator";
private static final String CK_KV_SEPARATOR = "kv_separator";
private static final String CK_REPLACE_KEY_WHITESPACE = "replace_key_whitespace";
private static final String CK_KEY_WHITESPACE_REPLACEMENT = "key_whitespace_replacement";
private static final String CK_KEY_PREFIX = "key_prefix";
private static final Pattern WHITE_SPACE_PATTERN = Pattern.compile("\\s");
private static final RemoveNullPredicate REMOVE_NULL_PREDICATE = new RemoveNullPredicate();
private final ObjectMapper mapper = new ObjectMapper();
private final boolean flatten;
private final String listSeparator;
private final String keySeparator;
private final String kvSeparator;
private final boolean replaceKeyWhitespace;
private final String keyWhitespaceReplacement;
private final String keyPrefix;
public JsonExtractor(final MetricRegistry metricRegistry,
final String id,
final String title,
final long order,
final CursorStrategy cursorStrategy,
final String sourceField,
final String targetField,
final Map extractorConfig,
final String creatorUserId,
final List converters,
final ConditionType conditionType,
final String conditionValue) throws ReservedFieldException, ConfigurationException {
super(metricRegistry, id, title, order, Type.JSON, cursorStrategy, sourceField, targetField, extractorConfig, creatorUserId, converters, conditionType, conditionValue);
if (extractorConfig == null) {
throw new ConfigurationException("Missing extractor configuration");
}
this.flatten = firstNonNull((Boolean) extractorConfig.get(CK_FLATTEN), false);
this.listSeparator = firstNonNull((String) extractorConfig.get(CK_LIST_SEPARATOR), ", ");
this.keySeparator = firstNonNull((String) extractorConfig.get(CK_KEY_SEPARATOR), "_");
this.kvSeparator = firstNonNull((String) extractorConfig.get(CK_KV_SEPARATOR), "=");
this.replaceKeyWhitespace = firstNonNull((Boolean) extractorConfig.get(CK_REPLACE_KEY_WHITESPACE), false);
this.keyWhitespaceReplacement = firstNonNull((String) extractorConfig.get(CK_KEY_WHITESPACE_REPLACEMENT), "_");
this.keyPrefix = firstNonNull((String) extractorConfig.get(CK_KEY_PREFIX), "");
}
@Override
protected Result[] run(String value) {
final Map extractedJson = extractJson(value);
final List results = new ArrayList<>(extractedJson.size());
for (Map.Entry entry : extractedJson.entrySet()) {
results.add(new Result(entry.getValue(), entry.getKey(), -1, -1));
}
return results.toArray(new Result[results.size()]);
}
public Map extractJson(String value) {
if (isNullOrEmpty(value)) {
return Collections.emptyMap();
}
final Map json;
try {
json = mapper.readValue(value, new TypeReference