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

com.englishtown.vertx.guice.GuiceJerseyBinder Maven / Gradle / Ivy

Go to download

Allows creating JAX-RS jersey resources that will handle incoming http requests to vert.x

The newest version!
package com.englishtown.vertx.guice;

import com.englishtown.vertx.jersey.*;
import com.englishtown.vertx.jersey.impl.DefaultJerseyHandler;
import com.englishtown.vertx.jersey.impl.DefaultJerseyOptions;
import com.englishtown.vertx.jersey.impl.DefaultVertxContainer;
import com.englishtown.vertx.jersey.impl.WriteStreamBodyWriter;
import com.englishtown.vertx.jersey.inject.ContainerResponseWriterProvider;
import com.englishtown.vertx.jersey.inject.VertxPostResponseProcessor;
import com.englishtown.vertx.jersey.inject.VertxRequestProcessor;
import com.englishtown.vertx.jersey.inject.VertxResponseProcessor;
import com.englishtown.vertx.jersey.inject.impl.VertxResponseWriterProvider;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.util.Providers;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.ServiceLocatorFactory;
import org.glassfish.jersey.server.model.ModelProcessor;
import org.glassfish.jersey.server.monitoring.ApplicationEventListener;
import org.glassfish.jersey.server.spi.ContainerLifecycleListener;

import javax.inject.Singleton;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.WriterInterceptor;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Guice Jersey binder
 */
public class GuiceJerseyBinder extends AbstractModule {

    /**
     * Configures a {@link com.google.inject.Binder} via the exposed methods.
     */
    @Override
    protected void configure() {

        // Create HK2 service locator and bind jersey injections
        ServiceLocator locator = ServiceLocatorFactory.getInstance().create(null);
        bind(ServiceLocator.class).toInstance(locator);

        bind(VertxContainer.class).to(DefaultVertxContainer.class);
        bind(JerseyServer.class).to(GuiceJerseyServer.class);
        bind(JerseyHandler.class).to(DefaultJerseyHandler.class);
        bind(JerseyOptions.class).to(DefaultJerseyOptions.class);
        bind(JerseyServerOptions.class).to(DefaultJerseyOptions.class);
        bind(ContainerResponseWriterProvider.class).to(VertxResponseWriterProvider.class);
        bind(MessageBodyWriter.class).to(WriteStreamBodyWriter.class).in(Singleton.class);
        bind(ApplicationConfigurator.class).toProvider(Providers.of(null));

        Multibinder.newSetBinder(binder(), VertxRequestProcessor.class);
        Multibinder.newSetBinder(binder(), VertxResponseProcessor.class);
        Multibinder.newSetBinder(binder(), VertxPostResponseProcessor.class);

        Multibinder.newSetBinder(binder(), ContainerRequestFilter.class);
        Multibinder.newSetBinder(binder(), ContainerResponseFilter.class);
        Multibinder.newSetBinder(binder(), ReaderInterceptor.class);
        Multibinder.newSetBinder(binder(), WriterInterceptor.class);
        Multibinder.newSetBinder(binder(), ModelProcessor.class);
        Multibinder.newSetBinder(binder(), ContainerLifecycleListener.class);
        Multibinder.newSetBinder(binder(), ApplicationEventListener.class);
        Multibinder.newSetBinder(binder(), ExceptionMapper.class);

    }

    @Provides
    List provideVertxRequestProcessorList(Set processors) {
        return new ArrayList<>(processors);
    }

    @Provides
    List provideVertxResponseProcessorList(Set processors) {
        return new ArrayList<>(processors);
    }

    @Provides
    List provideVertxPostResponseProcessorList(Set processors) {
        return new ArrayList<>(processors);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy