package org.lantern.loggly;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;

import javax.net.ssl.HttpsURLConnection;

import org.codehaus.jackson.map.ObjectMapper;

public class Loggly {
    private static final String url = "https://logs-01.loggly.com/inputs/469973d5-6eaf-445a-be71-cf27141316a1/tag/http-client/";

    private final ObjectMapper mapper = new ObjectMapper();
    private final ConcurrentHashMap<String, LogglyMessage> messageCounts = new ConcurrentHashMap<String, LogglyMessage>();

    public void log(LogglyMessage msg) {
        String throwableOrigin = msg.getThrowableOrigin();
        if (throwableOrigin == null) {
            // Can't dedupe, report immediately
            reportToLoggly(msg);
        } else {
            LogglyMessage previouslySet = messageCounts.putIfAbsent(
                    msg.getThrowableOrigin(), msg);
            if (previouslySet == null) {
                reportToLoggly(msg);
            } else {
                previouslySet.incrementNsimilarSuppressed();
                // TODO: decide when to report batches of messages
                boolean report = false;
                if (report) {
                    // Report the most recent message with the full count of
                    // similar suppressed
                    msg.setnSimilarSuppressed(previouslySet
                            .getnSimilarSuppressed());
                    reportToLoggly(msg);
                    previouslySet.setnSimilarSuppressed(0);
                }
            }
        }
    }

    private void reportToLoggly(LogglyMessage msg) {
        try {
            HttpsURLConnection conn = (HttpsURLConnection) new URL(url)
                    .openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            OutputStream out = conn.getOutputStream();
            InputStream in = conn.getInputStream();
            try {
                mapper.writeValue(out, msg);
                BufferedInputStream bufferedIn = new BufferedInputStream(in);
                while (bufferedIn.read() != 0) {
                    // consume response
                    // TODO: check response code and stuff
                }
            } finally {
                try {
                    out.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace(System.err);
                }
                try {
                    in.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace(System.err);
                }
            }
        } catch (Exception e) {
            System.err.println("Unable to submit to Loggly");
            e.printStackTrace(System.err);
        }
    }
}

@@ -41,83 +41,19 @@
 .io.
-BufferedInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
+IOException
 ;%0Aim
@@ -389,16 +389,54 @@
 ent/%22;%0A%0A
+    private final boolean inTestMode;%0A
     priv
@@ -611,16 +611,101 @@
 ge%3E();%0A%0A
+    public Loggly(boolean inTestMode) %7B%0A        this.inTestMode = inTestMode;%0A    %7D%0A%0A
     publ
@@ -1909,24 +1909,133 @@
 nnection();%0A
+            if (inTestMode) %7B%0A                conn.setRequestProperty(%22X-LOGGLY-TAG%22, %22test%22);%0A            %7D%0A
             
@@ -2160,60 +2160,8 @@
 ();%0A
-            InputStream in = conn.getInputStream();%0A
     
@@ -2239,114 +2239,73 @@
     
-BufferedInputStream bufferedIn = new BufferedInputStream(in);%0A                while (bufferedIn.read() != 
+int code = conn.getResponseCode();%0A                if (code %3E= 30
 0) %7B
@@ -2332,24 +2332,28 @@
  // 
-consume response
+will be logged below
 %0A   
@@ -2373,46 +2373,82 @@
     
-// TODO: check response code and stuff
+throw new Exception(%22Got %22+code+%22 response:%5Cn%22+conn.getResponseMessage());
 %0A   
@@ -2660,177 +2660,8 @@
   %7D%0A
-                try %7B%0A                    in.close();%0A                %7D catch (IOException ioe) %7B%0A                    ioe.printStackTrace(System.err);%0A                %7D%0A
     

test reporting to loggly