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.
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.logstash.logback.pattern;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.logstash.logback.composite.JsonReadingUtils;
import net.logstash.logback.util.StringUtils;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.pattern.PatternLayoutBase;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.core.filter.FilteringGeneratorDelegate;
import com.fasterxml.jackson.core.filter.TokenFilter;
import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Parser that takes a JSON pattern, resolves all the conversion specifiers and returns an instance
* of NodeWriter that, when its write() method is invoked, produces JSON defined by the parsed pattern.
*
* @param - type of the event (ILoggingEvent, IAccessEvent)
*
* @author Dmitry Andrianov
*/
public abstract class AbstractJsonPatternParser {
/**
* Pattern used to parse and detect {@link AbstractJsonPatternParser.Operation} in a string.
* An operation starts with a #, followed by a name and a pair of {} with possible arguments in between.
*/
public static final Pattern OPERATION_PATTERN = Pattern.compile("\\# (\\w+) (?: \\{ (.*) \\} )", Pattern.COMMENTS);
private final Context context;
private final JsonFactory jsonFactory;
private final Map> operations = new HashMap<>();
/**
* When true, fields whose values are considered empty
* will be omitted from JSON output.
*/
private boolean omitEmptyFields;
AbstractJsonPatternParser(final Context context, final JsonFactory jsonFactory) {
this.context = Objects.requireNonNull(context);
this.jsonFactory = Objects.requireNonNull(jsonFactory);
addOperation("asLong", new AsLongOperation());
addOperation("asDouble", new AsDoubleOperation());
addOperation("asBoolean", new AsBooleanOperation());
addOperation("asJson", new AsJsonOperation());
addOperation("tryJson", new TryJsonOperation());
addOperation("asNullIfEmpty", new AsNullIfEmptyOperation());
}
/**
* Register a new {@link Operation} and bind it to the given {@code name}.
*
* @param name the name of the operation
* @param operation the {@link Operation} instance
*/
protected void addOperation(String name, Operation> operation) {
this.operations.put(name, operation);
}
protected interface Operation extends Function {
}
protected static class AsLongOperation implements Operation {
@Override
public Long apply(String value) {
try {
return Long.parseLong(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Failed to convert '" + value + "' into a Long numeric value");
}
}
}
protected static class AsDoubleOperation implements Operation {
@Override
public Double apply(String value) {
try {
return Double.parseDouble(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Failed to convert '" + value + "' into a Double numeric value");
}
}
}
protected static class AsBooleanOperation implements Operation {
@Override
public Boolean apply(String value) {
if (StringUtils.isEmpty(value)) {
return null;
}
return Boolean.valueOf(
"true".equalsIgnoreCase(value)
|| "1".equals(value)
|| "yes".equalsIgnoreCase(value)
|| "y".equalsIgnoreCase(value));
}
}
protected class AsJsonOperation implements Operation {
@Override
public JsonNode apply(final String value) {
try {
return JsonReadingUtils.readFully(jsonFactory, value);
} catch (JsonParseException e) {
throw new IllegalArgumentException("Failed to convert '" + value + "' into a JSON object", e);
} catch (IOException e) {
throw new IllegalStateException("Unexpected IOException when reading JSON value (was '" + value + "')", e);
}
}
}
protected class TryJsonOperation implements Operation