View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.net.server;
18  
19  import java.io.InputStream;
20  import java.nio.charset.Charset;
21  
22  import org.apache.logging.log4j.core.LogEvent;
23  import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
24  import org.apache.logging.log4j.util.Chars;
25  
26  /**
27   * Reads and logs JSON {@link LogEvent}s from an {@link InputStream}..
28   */
29  public class JsonInputStreamLogEventBridge extends InputStreamLogEventBridge {
30  
31      private static final int[] END_PAIR = new int[] { END, END };
32      private static final char EVENT_END_MARKER = '}';
33      private static final char EVENT_START_MARKER = '{';
34      private static final char JSON_ESC = '\\';
35      private static final char JSON_STR_DELIM = Chars.DQUOTE;
36  
37      public JsonInputStreamLogEventBridge() {
38          this(1024, Charset.defaultCharset());
39      }
40  
41      public JsonInputStreamLogEventBridge(final int bufferSize, final Charset charset) {
42          super(new Log4jJsonObjectMapper(), bufferSize, charset, String.valueOf(EVENT_END_MARKER));
43      }
44  
45      @Override
46      protected int[] getEventIndices(final String text, final int beginIndex) {
47          // Scan the text for the end of the next JSON object.
48          final int start = text.indexOf(EVENT_START_MARKER, beginIndex);
49          if (start == END) {
50              return END_PAIR;
51          }
52          final char[] charArray = text.toCharArray();
53          int stack = 0;
54          boolean inStr = false;
55          boolean inEsc = false;
56          for (int i = start; i < charArray.length; i++) {
57              final char c = charArray[i];
58              if (!inEsc) {
59                  inEsc = false;
60                  switch (c) {
61                  case EVENT_START_MARKER:
62                      if (!inStr) {
63                          stack++;
64                      }
65                      break;
66                  case EVENT_END_MARKER:
67                      if (!inStr) {
68                          stack--;
69                      }
70                      break;
71                  case JSON_STR_DELIM:
72                      inStr = !inStr;
73                      break;
74                  case JSON_ESC:
75                      inEsc = true;
76                      break;
77                  }
78                  if (stack == 0) {
79                      return new int[] { start, i };
80                  }
81              }
82          }
83          return END_PAIR;
84      }
85  
86  }