package uk.me.steev.java.heating.io.http;

import java.io.IOException;

  import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.jetty.server.AbstractNCSARequestLog;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Slf4jLog;

import uk.me.steev.java.heating.controller.Heating;
import uk.me.steev.java.heating.io.http.get.AllDetailsServlet;
import uk.me.steev.java.heating.io.http.get.CurrentTempServlet;
import uk.me.steev.java.heating.io.http.get.DesiredTempServlet;
import uk.me.steev.java.heating.io.http.get.ExternalWeatherServlet;
import uk.me.steev.java.heating.io.http.get.ProportionServlet;
import uk.me.steev.java.heating.io.http.get.StatusServlet;
import uk.me.steev.java.heating.io.http.set.SetGoneOutUntilServlet;
import uk.me.steev.java.heating.io.http.set.SetOverrideServlet;

public class HttpAdapter {
  static final Logger logger = LogManager.getLogger(HttpAdapter.class.getName());
  protected static HttpAdapter SINGLETON = null;
  protected static Heating heating = null;
  
  private HttpAdapter(Heating heating) {
    try {
      HttpAdapter.heating = heating;
      org.eclipse.jetty.util.log.Log.setLog(new Slf4jLog());

      Server server = new Server(8080);
      
      ServletHandler handler = new ServletHandler();
      server.setHandler(handler);

      handler.addServletWithMapping(new ServletHolder(new CurrentTempServlet(heating)), "/get/current_temp/*");
      handler.addServletWithMapping(new ServletHolder(new DesiredTempServlet(heating)), "/get/desired_temp");
      handler.addServletWithMapping(new ServletHolder(new ProportionServlet(heating)), "/get/proportion");
      handler.addServletWithMapping(new ServletHolder(new ExternalWeatherServlet(heating)), "/get/weather/*");
      handler.addServletWithMapping(new ServletHolder(new StatusServlet(heating)), "/get/status/*");
      handler.addServletWithMapping(new ServletHolder(new AllDetailsServlet(heating)), "/get/all_details/*");
      handler.addServletWithMapping(new ServletHolder(new SetOverrideServlet(heating)), "/set/override/*");
      handler.addServletWithMapping(new ServletHolder(new SetGoneOutUntilServlet(heating)), "/set/gone_out_until/*");
      handler.addServletWithMapping(new ServletHolder(new RefreshServlet(heating)), "/refresh/*");

      server.setRequestLog(new AccessLogHandler());
      server.start();

    } catch (Exception e) {
      logger.error(e);
    }
  }

  public static HttpAdapter getHttpAdapter(Heating heating) {
    if (null == HttpAdapter.SINGLETON)
      HttpAdapter.SINGLETON = new HttpAdapter(heating);
    
    return HttpAdapter.SINGLETON;
  }

  public class AccessLogHandler extends AbstractNCSARequestLog {
    private Logger logger = LogManager.getLogger(this.getClass().getName());
    {
      this.setPreferProxiedForAddress(true);
    }

    @Override
    protected boolean isEnabled() {
      return true;
    }

    @Override
    public void write(String requestEntry) throws IOException {
      logger.info(requestEntry);
    }
  }
}

@@ -65,18 +65,16 @@
 ption;%0A%0A
-  
 import o
@@ -181,28 +181,22 @@
 .server.
-AbstractNCSA
+Custom
 RequestL
@@ -235,24 +235,79 @@
 ver.Server;%0A
+import org.eclipse.jetty.server.Slf4jRequestLogWriter;%0A
 import org.e
@@ -1031,16 +1031,134 @@
 ter %7B%0A  
+private static final String format = %22%25%7Bclient%7Da %25r: %25O bytes sent in %25%7Bms%7DTms handled by %25R; status %25s%25X%22;%0A  private 
 static f
@@ -1321,18 +1321,16 @@
 = null;%0A
-  
 %0A  priva
@@ -1512,22 +1512,16 @@
 (8080);%0A
-      
 %0A      S
@@ -2613,25 +2613,60 @@
 new 
-AccessLogHandler(
+CustomRequestLog(new Slf4jRequestLogWriter(), format
 ));%0A
@@ -2907,20 +2907,16 @@
 ating);%0A
-    
 %0A    ret
@@ -2949,405 +2949,7 @@
 %0A  %7D
-%0A%0A  public class AccessLogHandler extends AbstractNCSARequestLog %7B%0A    private Logger logger = LogManager.getLogger(this.getClass().getName());%0A    %7B%0A      this.setPreferProxiedForAddress(true);%0A    %7D%0A%0A    @Override%0A    protected boolean isEnabled() %7B%0A      return true;%0A    %7D%0A%0A    @Override%0A    public void write(String requestEntry) throws IOException %7B%0A      logger.info(requestEntry);%0A    %7D%0A  %7D
 %0A%7D%0A

Remove deprecated NCSA log format class