package org.paules.geckoboard.api;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.paules.geckoboard.api.gson.GsonFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

public class Geckoboard {
    private final Logger        logger               = LoggerFactory.getLogger( getClass() );

    private static final int    RECOMMENDED_TIME_OUT = 70000;

    private static final String CONTENT_TYPE         = "Content-Type";

    private static final String CONTENT_TYPE_VALUE   = "application/json";

    private static void bestEffortToReleaseHttpWithoutExceptions( final OutputStream httpOutputStream, final InputStream httpInputStream, final HttpURLConnection connection ) {
        IOUtils.closeQuietly( httpOutputStream );
        IOUtils.closeQuietly( httpInputStream );
        if ( connection != null ) {
            connection.disconnect();
        }
    }

    private final String apiKey;

    public Geckoboard( String apiKey ) {
        this.apiKey = apiKey;
    }

    private HttpURLConnection createAndOpenConnection( String widgetKey ) throws IOException {
        final URL url = new URL( "https://push.geckoboard.com/v1/send/" + widgetKey );
        final HttpURLConnection connection = ( HttpURLConnection ) url.openConnection();
        connection.addRequestProperty( CONTENT_TYPE, CONTENT_TYPE_VALUE );
        connection.setDoOutput( true );
        connection.setRequestMethod( "POST" );
        connection.setConnectTimeout( RECOMMENDED_TIME_OUT );
        connection.setReadTimeout( RECOMMENDED_TIME_OUT );
        return connection;
    }

    public void push( Push push ) throws IOException {
        WidgetWrapper wrapper = new WidgetWrapper( push, apiKey );
        Gson gson = GsonFactory.getGson();
        String json = gson.toJson( wrapper );
        HttpURLConnection connection = null;
        OutputStream httpOutputStream = null;
        InputStream httpInputStream = null;
        try {
            connection = createAndOpenConnection( push.getWidgetKey() );
            httpOutputStream = connection.getOutputStream();
            logger.info( "Sending: " + json );
            IOUtils.write( json.getBytes(), httpOutputStream );
            if ( connection.getResponseCode() >= 400 ) {
                logger.error( new String( IOUtils.toByteArray( connection.getErrorStream() ) ) );
                return;
            }
            httpInputStream = connection.getInputStream();
            final byte[] responseXml = IOUtils.toByteArray( httpInputStream );
            logger.info( new String( responseXml ) );
        }
        finally {
            bestEffortToReleaseHttpWithoutExceptions( httpOutputStream, httpInputStream, connection );
        }
    }
}

@@ -169,16 +169,49 @@
 net.URL;
+%0Aimport java.nio.charset.Charset;
 %0A%0Aimport
@@ -435,24 +435,25 @@
 gger        
+ 
 logger      
@@ -535,16 +535,17 @@
  int    
+ 
 RECOMMEN
@@ -591,32 +591,33 @@
 ic final String 
+ 
 CONTENT_TYPE    
@@ -672,16 +672,17 @@
  String 
+ 
 CONTENT_
@@ -713,24 +713,109 @@
 ion/json%22;%0A%0A
+    private static final Charset DEFAULT_CHARSET      = Charset.forName( %22UTF-8%22 );%0A%0A
     private 
@@ -2450,16 +2450,31 @@
 etBytes(
+DEFAULT_CHARSET
 ), httpO
@@ -2637,16 +2637,33 @@
 ream() )
+, DEFAULT_CHARSET
  ) );%0A  
@@ -2884,16 +2884,33 @@
 ponseXml
+, DEFAULT_CHARSET
  ) );%0A  

Set default encoding