package org.apache.flume.interceptor;

/**
 * Created by koichi.yanagimoto on 2017/05/15.
 */

import java.util.*;
import java.time.LocalDateTime;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.log4j.Logger;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JsonBodyTimestampInterceptor implements Interceptor {
    private static final Logger LOG;
    static {
        LOG = Logger.getLogger(JsonBodyTimestampInterceptor.class);
    }

    private String timestampKey;
    protected Context context;

    public static final String TIMESTAMP_KEY_NAME = "properties.timestampKeyName";

    public JsonBodyTimestampInterceptor(Context context) {
        this.context = context;
        this.timestampKey = context.getString(TIMESTAMP_KEY_NAME);
    }

    @Override
    public void initialize() {}

    @Override
    public Event intercept(Event event) {
        Map<String, String> headers = event.getHeaders();

        String body = new String(event.getBody());
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = null;

        try {
            jsonObject = (JSONObject) jsonParser.parse(body);
        } catch (org.json.simple.parser.ParseException e) {
            LOG.info("Received this log message that is not formatted in json: "+body+"\n");
            return event;
        }

        String timestamp = jsonObject.get(this.timestampKey).toString();
        LocalDateTime parsedDateTime = LocalDateTime.parse(timestamp);

        headers.put("timestampYear", String.valueOf(parsedDateTime.getYear()));
        headers.put("timestampMonth", String.valueOf(parsedDateTime.getMonthValue()));
        headers.put("timestampDay", String.valueOf(parsedDateTime.getDayOfMonth()));
        headers.put("timestampHour", String.valueOf(parsedDateTime.getHour()));

        return event;
    }

    @Override
    public List<Event> intercept(List<Event> events) {
        for (Event event:events) {
            intercept(event);
        }
        return events;
    }

    @Override
    public void close() {}



    public static class Builder implements Interceptor.Builder {
        private Context ctx;
        @Override
        public Interceptor build() {
            return new JsonBodyTimestampInterceptor(ctx);
        }

        @Override
        public void configure(Context context) {
            this.ctx = context;
        }
    }
}
@@ -541,16 +541,51 @@
 ampKey;%0A
+    private String dateTimeFormat;%0A
     prot
@@ -607,16 +607,16 @@
 ontext;%0A
-
 %0A    pub
@@ -690,16 +690,95 @@
 eyName%22;
+%0A    public static final String DATE_TIME_FORMAT = %22properties.dateTimeFormat%22;
 %0A%0A    pu
@@ -854,32 +854,32 @@
 text = context;%0A
-
         this.tim
@@ -929,16 +929,83 @@
 _NAME);%0A
+        this.dateTimeFormat = context.getString(DATE_TIME_FORMAT);%0A
     %7D%0A%0A 
@@ -1640,24 +1640,140 @@
 toString();%0A
+        java.time.format.DateTimeFormatter dtf = java.time.format.DateTimeFormatter.ofPattern(this.dateTimeFormat);%0A
         Loca
@@ -1828,16 +1828,21 @@
 imestamp
+, dtf
 );%0A%0A    

set format from property.