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                // Skip this char and continue
060                inEsc = false;
061            } else { 
062                switch (c) {
063                case EVENT_START_MARKER:
064                    if (!inStr) {
065                        stack++;
066                    }
067                    break;
068                case EVENT_END_MARKER:
069                    if (!inStr) {
070                        stack--;
071                    }
072                    break;
073                case JSON_STR_DELIM:
074                    inStr = !inStr;
075                    break;
076                case JSON_ESC:
077                    inEsc = true;
078                    break;
079                }
080                if (stack == 0) {
081                    return new int[] { start, i };
082                }
083            }
084        }
085        return END_PAIR;
086    }
087
088}