package plugins.webbridge.api;

import logbook.api.APIListenerSpi;
import logbook.proxy.RequestMetaData;
import logbook.proxy.ResponseMetaData;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import plugins.webbridge.bean.WebBridgeConfig;

import javax.json.Json;
import javax.json.JsonObject;
import java.util.Date;

public class WebBridgeListener implements APIListenerSpi {
    private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

    private OkHttpClient client = new OkHttpClient();

    @Override
    public void accept(JsonObject jsonObject, RequestMetaData requestMetaData, ResponseMetaData responseMetaData) {
        WebBridgeConfig config = WebBridgeConfig.get();
        if (!config.isBridgeEnabled()) {
            return;
        }

        RequestBody body = RequestBody.create(Json.createObjectBuilder()
                .add("uri", requestMetaData.getRequestURI())
                .add("date", new Date().getTime())
                .add("body", jsonObject)
                .build().toString(), JSON);

        String url = "http://" + config.getBridgeHost() + ":" + config.getBridgePort() + "/pub";
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        try {
            this.client.newCall(request).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@@ -835,16 +835,114 @@
      %7D%0A%0A
+        String url = %22http://%22 + config.getBridgeHost() + %22:%22 + config.getBridgePort() + %22/pub%22;%0A%0A
         
@@ -998,32 +998,32 @@
 ObjectBuilder()%0A
-
                 
@@ -1093,11 +1093,11 @@
 dd(%22
-dat
+tim
 e%22, 
@@ -1208,105 +1208,8 @@
 );%0A%0A
-        String url = %22http://%22 + config.getBridgeHost() + %22:%22 + config.getBridgePort() + %22/pub%22;%0A
     

Rename the WebBridge payload property: date -> time