11/*
22 * Copyright OpenSearch Contributors
33 * SPDX-License-Identifier: Apache-2.0
4+ *
5+ * The OpenSearch Contributors require contributions made to
6+ * this file be licensed under the Apache-2.0 license or a
7+ * compatible open source license.
8+ *
49 */
510package org .opensearch .dataprepper .plugins .codec .newline ;
611
@@ -25,24 +30,28 @@ public class NewlineDelimitedOutputCodec implements OutputCodec {
2530 private static final String MESSAGE_FIELD = "message" ;
2631 @ SuppressWarnings ("unused" )
2732 private OutputCodecContext deprecatedSupportCodecContext ;
33+ private final boolean includeEmptyObjects ;
2834
2935 @ DataPrepperPluginConstructor
3036 public NewlineDelimitedOutputCodec (final NewlineDelimitedOutputConfig config ) {
3137 Objects .requireNonNull (config );
38+ this .includeEmptyObjects = config .isIncludeEmptyObjects ();
3239 }
3340
3441 private static class NewlineWriter implements Writer {
3542 private final OutputStream outputStream ;
3643 private final OutputCodecContext codecContext ;
44+ private final boolean includeEmptyObjects ;
3745
38- private NewlineWriter (final OutputStream outputStream , final OutputCodecContext codecContext ) {
46+ private NewlineWriter (final OutputStream outputStream , final OutputCodecContext codecContext , final boolean includeEmptyObjects ) {
3947 this .outputStream = outputStream ;
4048 this .codecContext = codecContext ;
49+ this .includeEmptyObjects = includeEmptyObjects ;
4150 }
4251
4352 @ Override
4453 public void writeEvent (final Event event ) throws IOException {
45- doWriteEvent (outputStream , event , codecContext );
54+ doWriteEvent (outputStream , event , codecContext , includeEmptyObjects );
4655 }
4756
4857 @ Override
@@ -56,7 +65,7 @@ public Writer createWriter(final OutputStream outputStream, final Event sampleEv
5665 Objects .requireNonNull (outputStream );
5766 Objects .requireNonNull (codecContext );
5867
59- return new NewlineWriter (outputStream , codecContext );
68+ return new NewlineWriter (outputStream , codecContext , includeEmptyObjects );
6069 }
6170
6271 @ Override
@@ -68,25 +77,7 @@ public void start(final OutputStream outputStream, Event event, final OutputCode
6877
6978 @ Override
7079 public void writeEvent (final Event event , final OutputStream outputStream ) throws IOException {
71- Objects .requireNonNull (event );
72-
73- // Extract the message field and write it as plain text
74- String message = null ;
75- if (event .containsKey (MESSAGE_FIELD )) {
76- Object messageObj = event .get (MESSAGE_FIELD , Object .class );
77- if (messageObj != null ) {
78- message = messageObj .toString ();
79- }
80- }
81-
82- // If message is null or empty, write empty string
83- if (message == null ) {
84- message = "" ;
85- }
86-
87- // Write the message as plain text followed by a newline
88- outputStream .write (message .getBytes ());
89- outputStream .write (System .lineSeparator ().getBytes ());
80+ doWriteEvent (outputStream , event , deprecatedSupportCodecContext , includeEmptyObjects );
9081 }
9182
9283 @ Override
@@ -99,7 +90,7 @@ public String getExtension() {
9990 return NEWLINE ;
10091 }
10192
102- private static void doWriteEvent (final OutputStream outputStream , final Event event , final OutputCodecContext codecContext ) throws IOException {
93+ private static void doWriteEvent (final OutputStream outputStream , final Event event , final OutputCodecContext codecContext , final boolean includeEmptyObjects ) throws IOException {
10394 Objects .requireNonNull (event );
10495
10596 // Extract the message field and write it as plain text
@@ -111,8 +102,11 @@ private static void doWriteEvent(final OutputStream outputStream, final Event ev
111102 }
112103 }
113104
114- // If message is null or empty, write empty string
115- if (message == null ) {
105+ // Default: do not write anything if message is null or empty
106+ if (message == null || message .isEmpty ()) {
107+ if (!includeEmptyObjects ) {
108+ return ;
109+ }
116110 message = "" ;
117111 }
118112
0 commit comments