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  
25  /**
26   * Reads and logs JSON {@link LogEvent}s from an {@link InputStream}..
27   */
28  public class JsonInputStreamLogEventBridge extends InputStreamLogEventBridge {
29  
30      private static final int[] END_PAIR = new int[] { END, END };
31      private static final char EVENT_END_MARKER = '}';
32      private static final char EVENT_START_MARKER = '{';
33      private static final char JSON_ESC = '\\';
34      private static final char JSON_STR_DELIM = '"';
35  
36      public JsonInputStreamLogEventBridge() {
37          this(1024, Charset.defaultCharset());
38      }
39  
40      public JsonInputStreamLogEventBridge(final int bufferSize, final Charset charset) {
41          super(new Log4jJsonObjectMapper(), bufferSize, charset, String.valueOf(EVENT_END_MARKER));
42      }
43  
44      @Override
45      protected int[] getEventIndices(final String text, final int beginIndex) {
46          // Scan the text for the end of the next JSON object.
47          final int start = text.indexOf(EVENT_START_MARKER, beginIndex);
48          if (start == END) {
49              return END_PAIR;
50          }
51          final char[] charArray = text.toCharArray();
52          int stack = 0;
53          boolean inStr = false;
54          boolean inEsc = false;
55          for (int i = start; i < charArray.length; i++) {
56              final char c = charArray[i];
57              if (!inEsc) {
58                  inEsc = false;
59                  switch (c) {
60                  case EVENT_START_MARKER:
61                      if (!inStr) {
62                          stack++;
63                      }
64                      break;
65                  case EVENT_END_MARKER:
66                      if (!inStr) {
67                          stack--;
68                      }
69                      break;
70                  case JSON_STR_DELIM:
71                      inStr = !inStr;
72                      break;
73                  case JSON_ESC:
74                      inEsc = true;
75                      break;
76                  }
77                  if (stack == 0) {
78                      return new int[] { start, i };
79                  }
80              }
81          }
82          return END_PAIR;
83      }
84  
85  }