<commit_before>/**
 * Licensed to <NAME> at Cloudspace under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information 
 * regarding copyright ownership. <NAME> licenses 
 * this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package multiplex;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.cloudera.flume.conf.Context;
import com.cloudera.flume.conf.SinkFactory.SinkDecoBuilder;
import com.cloudera.flume.core.Event;
import com.cloudera.flume.core.EventImpl;
import com.cloudera.flume.core.EventSink;
import com.cloudera.flume.core.EventSinkDecorator;
import com.cloudera.util.Pair;
import com.google.common.base.Preconditions;

public class JsonMultiplexDecorator<S extends EventSink> extends EventSinkDecorator<S> {
  private final String serverName;
  private final String logType;

  public JsonMultiplexDecorator(S s, String serverName, String logType) {
    super(s);

    this.serverName = serverName;
    this.logType = logType;
  }

  @Override
  public void append(Event e) throws IOException {
    String body = "{ \"server\": \"" + this.serverName + "\"," +
      "\"log_type\": \"" + this.logType + "\", " +
      "\"body\": \"" + new String(e.getBody()) + "\" }";

    EventImpl e2 = new EventImpl(body.getBytes(),
        e.getTimestamp(), e.getPriority(), e.getNanos(), e.getHost(),
        e.getAttrs());

    super.append(e2);
  }

  public static SinkDecoBuilder builder() {
    return new SinkDecoBuilder() {
      @Override
      public EventSinkDecorator<EventSink> build(Context context,
          String... argv) {
        Preconditions.checkArgument(argv.length == 2,
            "usage: multiplexDecorator(serverName, logType)");

        return new JsonMultiplexDecorator<EventSink>(null, argv[0], argv[1]);
      }
    };
  }

  public static List<Pair<String, SinkDecoBuilder>> getDecoratorBuilders() {<commit_msg>Escaping quotes in the event body<commit_after>/**
 * Licensed to <NAME> at Cloudspace under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information 
 * regarding copyright ownership. <NAME> licenses 
 * this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package multiplex;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.cloudera.flume.conf.Context;
import com.cloudera.flume.conf.SinkFactory.SinkDecoBuilder;
import com.cloudera.flume.core.Event;
import com.cloudera.flume.core.EventImpl;
import com.cloudera.flume.core.EventSink;
import com.cloudera.flume.core.EventSinkDecorator;
import com.cloudera.util.Pair;
import com.google.common.base.Preconditions;

public class JsonMultiplexDecorator<S extends EventSink> extends EventSinkDecorator<S> {
  private final String serverName;
  private final String logType;

  public JsonMultiplexDecorator(S s, String serverName, String logType) {
    super(s);

    this.serverName = serverName;
    this.logType = logType;
  }

  @Override
  public void append(Event e) throws IOException {
	String body = e.getBody().replaceAll("\"", "\\\"");
    String json = "{ \"server\": \"" + this.serverName + "\"," +
      "\"log_type\": \"" + this.logType + "\", " +
      "\"body\": \"" + new String(e.getBody()) + "\" }";

    EventImpl e2 = new EventImpl(body.getBytes(),
        e.getTimestamp(), e.getPriority(), e.getNanos(), e.getHost(),
        e.getAttrs());

    super.append(e2);
  }

  public static SinkDecoBuilder builder() {
    return new SinkDecoBuilder() {
      @Override
      public EventSinkDecorator<EventSink> build(Context context,
          String... argv) {
        Preconditions.checkArgument(argv.length == 2,
            "usage: multiplexDecorator(serverName, logType)");

        return new JsonMultiplexDecorator<EventSink>(null, argv[0], argv[1]);
      }
    };
  }

  public static List<Pair<String, SinkDecoBuilder>> getDecoratorBuilders() {
{'fraction_non_alphanumeric': 0.11315106765116743, 'fraction_numerical': 0.003592097385751347, 'mean_word_length': 3.7552182163187857, 'pattern_counts': {'":': 6, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '362670', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1482, 'n_words': 440}