All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.restclient.models.FieldMapper Maven / Gradle / Ivy

The newest version!
/**
 * 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.restclient.models;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringEscapeUtils;
import org.graylog2.restclient.lib.Tools;

import java.util.List;

public class FieldMapper {

    public enum Type {
        SYSLOG_LEVEL("level"),
        NEWLINE_CONVERTER("full_message");

        public final String field;

        Type(String field) {
            this.field = field;
        }
    }

    public FieldMapper() {}

    // For now only the standard set.
    public static List getAll() {
        List mappers = Lists.newArrayList();

        mappers.add(Type.SYSLOG_LEVEL);
        mappers.add(Type.NEWLINE_CONVERTER);
        return mappers;
    }

    // TODO this is temporary and needs to be cleaned up significantly in 0.20.1!
    public Object map(String key, Object value) {
        if (key.equals("level")) {
            return mapSyslogLevel(value);
        }
        if (key.equals("full_message")) {
            return convertNewlinesToBr(value);
        }
        return value;
    }

    private static String convertNewlinesToBr(Object fullMessage) {
        if (fullMessage == null) {
            return null;
        }

        String s = StringEscapeUtils.escapeHtml4(fullMessage.toString());
        s = s.replaceAll("\\n", "
"); return s; } private static String mapSyslogLevel(Object level) { if (level == null) { return null; } String human; if (level instanceof Number) { human = Tools.syslogLevelToHuman(((Number)level).intValue()); // casteria } else { return level.toString() + " [failed to map syslog level]"; } return human + " [" + level.toString() + "]"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy