/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.trainwatch.util.config.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import uk.trainwatch.util.MapBuilder;
import uk.trainwatch.util.config.Configuration;

/**
 * Configuration from a remote http server. Authentication is done
 *
 * @author peter
 */
public class HttpConfiguration
        extends ConfigurationWrapper
{

    private static final Logger LOG = Logger.getLogger( "HttpConfiguration" );

    private Configuration configuration;
    private final URI uri;

    public HttpConfiguration( URI uri )
    {
        this.uri = uri;
    }

    @Override
    protected Configuration getConfiguration()
    {
        if( configuration == null )
        {
            // Don't write uri as is as we may expose security details
            LOG.log( Level.INFO, () -> "Retrieving config " + uri.getScheme() + "://" + uri.getHost() + uri.getPath() );

            try( CloseableHttpClient client = HttpClients.createDefault() )
            {
                HttpGet get = new HttpGet( uri );
                get.setHeader( "User-Agent", "Area51 Configuration/1.0" );
                HttpResponse response = client.execute( get );
                switch( response.getStatusLine().getStatusCode() )
                {
                    case 200:
                    case 304:
                        try( InputStream is = response.getEntity().getContent() )
                        {
                            try( JsonReader r = Json.createReader( new InputStreamReader( is ) ) )
                            {
                                JsonObject jo = r.readObject();
                                LOG.log( Level.INFO, () -> jo.toString() );

                                configuration = new MapConfiguration( MapBuilder.fromJsonObject( jo ).build() );
                            }
                        }
                        break;
                        
                    default:
                        LOG.log( Level.INFO, () -> "Error " + uri.getScheme() + "://" + uri.getHost() + uri.getPath()
                                 + " " + response.getStatusLine().getStatusCode() );
                        configuration = EmptyConfiguration.INSTANCE;
                }
            } catch( IOException ex )
            {
                // Don't write uri as is as we may expose security details
                LOG.log( Level.INFO, ex, () -> "Failed retrieve of config " + uri.getScheme() + "://" + uri.getHost() + uri.getPath() );

                throw new UncheckedIOException( ex );
            }
        }
        return configuration;
    }

}

@@ -2153,149 +2153,8 @@
   %7B%0A
-                                JsonObject jo = r.readObject();%0A                                LOG.log( Level.INFO, () -%3E jo.toString() );%0A%0A
     
@@ -2250,10 +2250,22 @@
 ct( 
-jo
+r.readObject()
  ).b
@@ -2446,36 +2446,39 @@
  LOG.log( Level.
-INFO
+WARNING
 , () -%3E %22Error %22
@@ -2746,16 +2746,16 @@
 on ex )%0A
+
         
@@ -2764,221 +2764,8 @@
   %7B%0A
-                // Don't write uri as is as we may expose security details%0A                LOG.log( Level.INFO, ex, () -%3E %22Failed retrieve of config %22 + uri.getScheme() + %22://%22 + uri.getHost() + uri.getPath() );%0A%0A
     

Remove unwanted logging