// Path: layout/src/main/java/com/vlkan/log4j2/logstash/layout/util/JsonGenerators.java
// public enum JsonGenerators {;
// 
//     private static final class DoubleWriterContext {
// 
//         private static final int MAX_BUFFER_LENGTH =
//                 String.format("%d.%d", Long.MAX_VALUE, Integer.MAX_VALUE).length()
//                         + 1;
// 
//         private final StringBuilder builder = new StringBuilder();
// 
//         private char[] buffer = new char[MAX_BUFFER_LENGTH];
// 
//     }
// 
//     private static final ThreadLocal<DoubleWriterContext> DOUBLE_WRITER_CONTEXT_REF =
//             Constants.ENABLE_THREADLOCALS
//                     ? ThreadLocal.withInitial(DoubleWriterContext::new)
//                     : null;
// 
//     public static void writeDouble(JsonGenerator jsonGenerator, long integralPart, int fractionalPart) throws IOException {
//         if (fractionalPart < 0) {
//             throw new IllegalArgumentException("negative fraction");
//         } else if (fractionalPart == 0) {
//             jsonGenerator.writeNumber(integralPart);
//         } else if (Constants.ENABLE_THREADLOCALS) {
//             DoubleWriterContext context = DOUBLE_WRITER_CONTEXT_REF.get();
//             context.builder.setLength(0);
//             context.builder.append(integralPart);
//             context.builder.append('.');
//             context.builder.append(fractionalPart);
//             int length = context.builder.length();
//             context.builder.getChars(0, length, context.buffer, 0);
//             // jackson-core version >=2.10.2 is required for the following line
//             // to avoid FasterXML/jackson-core#588 bug.
//             jsonGenerator.writeRawValue(context.buffer, 0, length);
//         } else {
//             String formattedNumber = "" + integralPart + '.' + fractionalPart;
//             jsonGenerator.writeNumber(formattedNumber);
//         }
//     }
// 
//     /**
//      * Writes given object, preferably using GC-free writers.
//      */
//     public static void writeObject(JsonGenerator jsonGenerator, Object object) throws IOException {
// 
//         if (object == null) {
//             jsonGenerator.writeNull();
//             return;
//         }
// 
//         if (object instanceof String) {
//             jsonGenerator.writeString((String) object);
//             return;
//         }
// 
//         if (object instanceof Short) {
//             jsonGenerator.writeNumber((Short) object);
//             return;
//         }
// 
//         if (object instanceof Integer) {
//             jsonGenerator.writeNumber((Integer) object);
//             return;
//         }
// 
//         if (object instanceof Long) {
//             jsonGenerator.writeNumber((Long) object);
//             return;
//         }
// 
//         if (object instanceof BigDecimal) {
//             jsonGenerator.writeNumber((BigDecimal) object);
//             return;
//         }
// 
//         if (object instanceof Float) {
//             jsonGenerator.writeNumber((Float) object);          // Not GC-free!
//             return;
//         }
// 
//         if (object instanceof Double) {
//             jsonGenerator.writeNumber((Double) object);         // Not GC-free!
//             return;
//         }
// 
//         if (object instanceof byte[]) {
//             jsonGenerator.writeBinary((byte[]) object);
//             return;
//         }
// 
//         jsonGenerator.writeObject(object);
// 
//     }
// 
// }


import com.fasterxml.jackson.core.JsonGenerator;
import com.vlkan.log4j2.logstash.layout.util.JsonGenerators;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.util.IndexedStringMap;
import org.apache.logging.log4j.util.ReadOnlyStringMap;
import java.io.IOException;
import java.util.regex.Pattern;
/*
 * Copyright 2017-2020 Volkan Yazıcı
 *
 * 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 permits and
 * limitations under the License.
 */

package com.vlkan.log4j2.logstash.layout.resolver;



/**
 * Add Mapped Diagnostic Context (MDC).
 */
class ContextDataResolver implements EventResolver {

    private final EventResolverContext context;

    private final String key;

    ContextDataResolver(EventResolverContext context, String key) {
        this.context = context;
        this.key = key;
    }

    static String getName() {
        return "mdc";
    }

    @Override
    public void resolve(LogEvent logEvent, JsonGenerator jsonGenerator) throws IOException {

        // Retrieve context data.
        ReadOnlyStringMap contextData = logEvent.getContextData();
        if (contextData == null || contextData.isEmpty()) {
            jsonGenerator.writeNull();
            return;
        }

        // Check if key matches.
        if (key != null) {
            Object value = contextData.getValue(key);
            boolean valueExcluded = isValueExcluded(context, value);
            if (valueExcluded) {
                jsonGenerator.writeNull();
            } else {
// Path: layout/src/main/java/com/vlkan/log4j2/logstash/layout/util/JsonGenerators.java
// public enum JsonGenerators {;
// 
//     private static final class DoubleWriterContext {
// 
//         private static final int MAX_BUFFER_LENGTH =
//                 String.format("%d.%d", Long.MAX_VALUE, Integer.MAX_VALUE).length()
//                         + 1;
// 
//         private final StringBuilder builder = new StringBuilder();
// 
//         private char[] buffer = new char[MAX_BUFFER_LENGTH];
// 
//     }
// 
//     private static final ThreadLocal<DoubleWriterContext> DOUBLE_WRITER_CONTEXT_REF =
//             Constants.ENABLE_THREADLOCALS
//                     ? ThreadLocal.withInitial(DoubleWriterContext::new)
//                     : null;
// 
//     public static void writeDouble(JsonGenerator jsonGenerator, long integralPart, int fractionalPart) throws IOException {
//         if (fractionalPart < 0) {
//             throw new IllegalArgumentException("negative fraction");
//         } else if (fractionalPart == 0) {
//             jsonGenerator.writeNumber(integralPart);
//         } else if (Constants.ENABLE_THREADLOCALS) {
//             DoubleWriterContext context = DOUBLE_WRITER_CONTEXT_REF.get();
//             context.builder.setLength(0);
//             context.builder.append(integralPart);
//             context.builder.append('.');
//             context.builder.append(fractionalPart);
//             int length = context.builder.length();
//             context.builder.getChars(0, length, context.buffer, 0);
//             // jackson-core version >=2.10.2 is required for the following line
//             // to avoid FasterXML/jackson-core#588 bug.
//             jsonGenerator.writeRawValue(context.buffer, 0, length);
//         } else {
//             String formattedNumber = "" + integralPart + '.' + fractionalPart;
//             jsonGenerator.writeNumber(formattedNumber);
//         }
//     }
// 
//     /**
//      * Writes given object, preferably using GC-free writers.
//      */
//     public static void writeObject(JsonGenerator jsonGenerator, Object object) throws IOException {
// 
//         if (object == null) {
//             jsonGenerator.writeNull();
//             return;
//         }
// 
//         if (object instanceof String) {
//             jsonGenerator.writeString((String) object);
//             return;
//         }
// 
//         if (object instanceof Short) {
//             jsonGenerator.writeNumber((Short) object);
//             return;
//         }
// 
//         if (object instanceof Integer) {
//             jsonGenerator.writeNumber((Integer) object);
//             return;
//         }
// 
//         if (object instanceof Long) {
//             jsonGenerator.writeNumber((Long) object);
//             return;
//         }
// 
//         if (object instanceof BigDecimal) {
//             jsonGenerator.writeNumber((BigDecimal) object);
//             return;
//         }
// 
//         if (object instanceof Float) {
//             jsonGenerator.writeNumber((Float) object);          // Not GC-free!
//             return;
//         }
// 
//         if (object instanceof Double) {
//             jsonGenerator.writeNumber((Double) object);         // Not GC-free!
//             return;
//         }
// 
//         if (object instanceof byte[]) {
//             jsonGenerator.writeBinary((byte[]) object);
//             return;
//         }
// 
//         jsonGenerator.writeObject(object);
// 
//     }
// 
// }


// Path: layout/src/main/java/com/vlkan/log4j2/logstash/layout/resolver/ContextDataResolver.java
import com.fasterxml.jackson.core.JsonGenerator;
import com.vlkan.log4j2.logstash.layout.util.JsonGenerators;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.util.IndexedStringMap;
import org.apache.logging.log4j.util.ReadOnlyStringMap;
import java.io.IOException;
import java.util.regex.Pattern;

/*
 * Copyright 2017-2020 Volkan Yazıcı
 *
 * 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 permits and
 * limitations under the License.
 */

package com.vlkan.log4j2.logstash.layout.resolver;



/**
 * Add Mapped Diagnostic Context (MDC).
 */
class ContextDataResolver implements EventResolver {

    private final EventResolverContext context;

    private final String key;

    ContextDataResolver(EventResolverContext context, String key) {
        this.context = context;
        this.key = key;
    }

    static String getName() {
        return "mdc";
    }

    @Override
    public void resolve(LogEvent logEvent, JsonGenerator jsonGenerator) throws IOException {

        // Retrieve context data.
        ReadOnlyStringMap contextData = logEvent.getContextData();
        if (contextData == null || contextData.isEmpty()) {
            jsonGenerator.writeNull();
            return;
        }

        // Check if key matches.
        if (key != null) {
            Object value = contextData.getValue(key);
            boolean valueExcluded = isValueExcluded(context, value);
            if (valueExcluded) {
                jsonGenerator.writeNull();
            } else {
                JsonGenerators.writeObject(jsonGenerator, value);