package de.factoryfx.adminui.javafx.server;

import java.util.Collection;
import java.util.function.Function;
import java.util.logging.LogManager;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.ErrorHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;

public class AdminUiJavafxServer {

    private final ApplicationServerResource factoryManagerResource;
    private final Server server;

    public AdminUiJavafxServer(ApplicationServerResource factoryManagerResource,
                               Function<Server,Collection<ServerConnector>> connectorFactory, String contextPath) {
        this.factoryManagerResource = factoryManagerResource;
        server=new Server();
        Collection<ServerConnector> connectors = connectorFactory.apply(server);
        server.setConnectors(connectors.toArray(new ServerConnector[connectors.size()]));

        ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        contextHandler.addServlet( new ServletHolder(new ServletContainer(jerseySetup(factoryManagerResource))), contextPath);

        ErrorHandler errorHandler = new ErrorHandler();
        errorHandler.setShowStacks(true);
        contextHandler.setErrorHandler(errorHandler);

        HandlerCollection handlers = new HandlerCollection();
        GzipHandler gzipHandler = new GzipHandler();
        gzipHandler.setHandler(contextHandler);
        handlers.setHandlers(new Handler[]{gzipHandler, new DefaultHandler()});

        server.setHandler(handlers);
    }

    private ResourceConfig jerseySetup(Object resource) {
        ResourceConfig resourceConfig = new ResourceConfig();
        resourceConfig.register(resource);
        resourceConfig.register(new AllExceptionMapper());

        ObjectMapper mapper = new ObjectMapper();

        JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
        provider.setMapper(mapper);
        resourceConfig.register(provider);

        LoggingFeature loggingFilter = new LoggingFeature(java.util.logging.Logger.getLogger(LoggingFeature.class.getName()));
        resourceConfig.registerInstances(loggingFilter);
        LogManager.getLogManager().reset();
        return resourceConfig;
    }

    public void start() throws Error {
        try {
            server.start();
        } catch (RuntimeException | Error re) {
            throw re;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void stop() {
        try {
            server.stop();
        } catch (RuntimeException | Error re) {
            throw re;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -180,29 +180,381 @@
 son.
-databind.ObjectMapper
+annotation.JsonInclude;%0D%0Aimport com.fasterxml.jackson.databind.DeserializationFeature;%0D%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0D%0Aimport com.fasterxml.jackson.databind.SerializationFeature;%0D%0Aimport com.fasterxml.jackson.datatype.jdk7.Jdk7Module;%0D%0Aimport com.fasterxml.jackson.datatype.jdk8.Jdk8Module;%0D%0Aimport com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
 ;%0D%0Ai
@@ -2808,20 +2808,22 @@
 apper = 
-new 
+create
 ObjectMa
@@ -3218,24 +3218,24 @@
 ).reset();%0D%0A
-
         retu
@@ -3255,32 +3255,821 @@
 nfig;%0D%0A    %7D%0D%0A%0D%0A
+    private ObjectMapper createObjectMapper() %7B%0D%0A        ObjectMapper objectMapper = new ObjectMapper();%0D%0A        objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);%0D%0A        objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);%0D%0A        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);%0D%0A        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);%0D%0A        objectMapper.registerModule(new JavaTimeModule());%0D%0A        objectMapper.registerModule(new Jdk7Module());%0D%0A        objectMapper.registerModule(new Jdk8Module());%0D%0A        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);%0D%0A        objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);%0D%0A        return objectMapper;%0D%0A    %7D%0D%0A%0D%0A
     public void 

create object mapper