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      private static final boolean THREAD_CONTEXT_MAP_AS_LIST = false;
37  
38      public JsonInputStreamLogEventBridge() {
39          this(1024, Charset.defaultCharset());
40      }
41  
42      public JsonInputStreamLogEventBridge(final int bufferSize, final Charset charset) {
43          super(new Log4jJsonObjectMapper(THREAD_CONTEXT_MAP_AS_LIST, true), bufferSize, charset,
44                  String.valueOf(EVENT_END_MARKER));
45      }
46  
47      @Override
48      protected int[] getEventIndices(final String text, final int beginIndex) {
49          // Scan the text for the end of the next JSON object.
50          final int start = text.indexOf(EVENT_START_MARKER, beginIndex);
51          if (start == END) {
52              return END_PAIR;
53          }
54          final char[] charArray = text.toCharArray();
55          int stack = 0;
56          boolean inStr = false;
57          boolean inEsc = false;
58          for (int i = start; i < charArray.length; i++) {
59              final char c = charArray[i];
60              if (inEsc) {
61              	// Skip this char and continue
62              	inEsc = false;
63              } else {
64                  switch (c) {
65                  case EVENT_START_MARKER:
66                      if (!inStr) {
67                          stack++;
68                      }
69                      break;
70                  case EVENT_END_MARKER:
71                      if (!inStr) {
72                          stack--;
73                      }
74                      break;
75                  case JSON_STR_DELIM:
76                      inStr = !inStr;
77                      break;
78                  case JSON_ESC:
79                      inEsc = true;
80                      break;
81                  }
82                  if (stack == 0) {
83                      return new int[] { start, i };
84                  }
85              }
86          }
87          return END_PAIR;
88      }
89  
90  }