001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.net.server;
018
019import java.io.InputStream;
020import java.nio.charset.Charset;
021
022import org.apache.logging.log4j.core.LogEvent;
023import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
024import org.apache.logging.log4j.util.Chars;
025
026/**
027 * Reads and logs JSON {@link LogEvent}s from an {@link InputStream}..
028 */
029public class JsonInputStreamLogEventBridge extends InputStreamLogEventBridge {
030
031    private static final int[] END_PAIR = new int[] { END, END };
032    private static final char EVENT_END_MARKER = '}';
033    private static final char EVENT_START_MARKER = '{';
034    private static final char JSON_ESC = '\\';
035    private static final char JSON_STR_DELIM = Chars.DQUOTE;
036
037    public JsonInputStreamLogEventBridge() {
038        this(1024, Charset.defaultCharset());
039    }
040
041    public JsonInputStreamLogEventBridge(final int bufferSize, final Charset charset) {
042        super(new Log4jJsonObjectMapper(), bufferSize, charset, String.valueOf(EVENT_END_MARKER));
043    }
044
045    @Override
046    protected int[] getEventIndices(final String text, final int beginIndex) {
047        // Scan the text for the end of the next JSON object.
048        final int start = text.indexOf(EVENT_START_MARKER, beginIndex);
049        if (start == END) {
050            return END_PAIR;
051        }
052        final char[] charArray = text.toCharArray();
053        int stack = 0;
054        boolean inStr = false;
055        boolean inEsc = false;
056        for (int i = start; i < charArray.length; i++) {
057            final char c = charArray[i];
058            if (!inEsc) {
059                inEsc = false;
060                switch (c) {
061                case EVENT_START_MARKER:
062                    if (!inStr) {
063                        stack++;
064                    }
065                    break;
066                case EVENT_END_MARKER:
067                    if (!inStr) {
068                        stack--;
069                    }
070                    break;
071                case JSON_STR_DELIM:
072                    inStr = !inStr;
073                    break;
074                case JSON_ESC:
075                    inEsc = true;
076                    break;
077                }
078                if (stack == 0) {
079                    return new int[] { start, i };
080                }
081            }
082        }
083        return END_PAIR;
084    }
085
086}