All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.factoryfx.jetty.JerseyServletFactory Maven / Gradle / Ivy

package io.github.factoryfx.jetty;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import io.github.factoryfx.factory.attribute.types.ObjectValueAttribute;
import io.github.factoryfx.factory.FactoryBase;
import io.github.factoryfx.factory.SimpleFactoryBase;
import io.github.factoryfx.factory.attribute.dependency.FactoryPolymorphicAttribute;
import io.github.factoryfx.factory.attribute.dependency.FactoryPolymorphicListAttribute;
import io.github.factoryfx.factory.attribute.dependency.FactoryAttribute;
import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;

import javax.servlet.*;
import java.util.*;

public class JerseyServletFactory> extends SimpleFactoryBase {


    public final FactoryAttribute> objectMapper = new FactoryAttribute>().nullable().en("objectMapper");

    public final FactoryPolymorphicAttribute restLogging = new FactoryPolymorphicAttribute().userReadOnly().labelText("REST logging");

    public final ObjectValueAttribute> additionalJaxrsComponents = new ObjectValueAttribute>().userReadOnly().labelText("additionalJaxrsComponents").nullable();


    public final FactoryPolymorphicListAttribute resources = new FactoryPolymorphicListAttribute().labelText("resources");

    @Override
    public Servlet createImpl() {
        ResourceConfig resourceConfig = new ResourceConfig();
        resourceConfig.property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);// without we have 2 JacksonJaxbJsonProvider and wrong mapper
//        resourceConfig.property(ServerProperties.BV_FEATURE_DISABLE, true);
//        resourceConfig.property(ServerProperties.RESOURCE_VALIDATION_DISABLE, true);
        getResourcesInstances().forEach(resourceConfig::register);

        JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
        provider.setMapper(objectMapper.instance());
        resourceConfig.register(provider);

        resourceConfig.register(restLogging.instance());

        resourceConfig.register(new AllExceptionMapper());

        if (additionalJaxrsComponents.get()!=null){
            additionalJaxrsComponents.get().forEach(r -> {
                if (r instanceof Class) {
                    resourceConfig.register((Class) r);
                } else {
                    resourceConfig.register(r);
                }
            });
        }

        return new ServletContainer(resourceConfig);
    }

    protected List getResourcesInstances(){
        return resources.instances();
    }


}