Search in sources :

Example 26 with InjectionManager

use of org.glassfish.jersey.internal.inject.InjectionManager in project jersey by jersey.

the class ProviderBinderTest method testServicesNotEmpty.

@Test
public void testServicesNotEmpty() {
    InjectionManager injectionManager = Injections.createInjectionManager(initBinders());
    Set<MessageBodyReader> providers = Providers.getProviders(injectionManager, MessageBodyReader.class);
    assertTrue(providers.size() > 0);
}
Also used : MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 27 with InjectionManager

use of org.glassfish.jersey.internal.inject.InjectionManager in project jersey by jersey.

the class ProviderBinderTest method testServicesMbw.

@Test
public void testServicesMbw() {
    InjectionManager injectionManager = Injections.createInjectionManager(initBinders());
    Set<MessageBodyWriter> providers = Providers.getProviders(injectionManager, MessageBodyWriter.class);
    assertTrue(providers.size() > 0);
}
Also used : MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 28 with InjectionManager

use of org.glassfish.jersey.internal.inject.InjectionManager in project jersey by jersey.

the class ParameterInjectionBinder method configure.

@Override
public void configure() {
    // Param converter providers
    // TODO: Replace by non-di version
    bind(new ParamConverters.AggregatedProvider()).to(ParamConverterProvider.class);
    Provider<ContainerRequest> requestProvider = Injections.getProvider(injectionManager, ContainerRequest.class);
    Provider<AsyncContext> asyncContextProvider = Injections.getProvider(injectionManager, AsyncContext.class);
    Function<Class<? extends Configuration>, Configuration> clientConfigProvider = clientConfigClass -> Injections.getOrCreate(injectionManager, clientConfigClass);
    // Param Converters must be initialized Lazy and created at the time of the call on extractor
    LazyValue<ParamConverterFactory> lazyParamConverterFactory = Values.lazy((Value<ParamConverterFactory>) () -> new ParamConverterFactory(Providers.getProviders(injectionManager, ParamConverterProvider.class), Providers.getCustomProviders(injectionManager, ParamConverterProvider.class)));
    LazyValue<Configuration> lazyConfiguration = Values.lazy((Value<Configuration>) () -> injectionManager.getInstance(Configuration.class));
    MultivaluedParameterExtractorFactory paramExtractor = new MultivaluedParameterExtractorFactory(lazyParamConverterFactory);
    bind(paramExtractor).to(MultivaluedParameterExtractorProvider.class);
    // Parameter injection value providers
    AsyncResponseValueSupplierProvider asyncSupplier = new AsyncResponseValueSupplierProvider(asyncContextProvider);
    bindValueSupplier(asyncSupplier);
    CookieParamValueSupplierProvider cookieSupplier = new CookieParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(cookieSupplier);
    EntityParamValueSupplierProvider entitySupplier = new EntityParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(entitySupplier);
    FormParamValueSupplierProvider formSupplier = new FormParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(formSupplier);
    HeaderParamValueSupplierProvider headerSupplier = new HeaderParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(headerSupplier);
    MatrixParamValueSupplierProvider matrixSupplier = new MatrixParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(matrixSupplier);
    PathParamValueSupplierProvider pathSupplier = new PathParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(pathSupplier);
    QueryParamValueSupplierProvider querySupplier = new QueryParamValueSupplierProvider(paramExtractor, requestProvider);
    bindValueSupplier(querySupplier);
    WebTargetValueSupplierProvider webTargetSupplier = new WebTargetValueSupplierProvider(requestProvider, lazyConfiguration, clientConfigProvider);
    bindValueSupplier(webTargetSupplier);
    BeanParamValueSupplierProvider beanSupplier = new BeanParamValueSupplierProvider(paramExtractor, requestProvider, injectionManager);
    bindValueSupplier(beanSupplier);
    // Register InjectionResolvers with param providers
    // TODO: RENAME INJECTION RESOLVER
    bind(new ParamInjectionResolver<>(cookieSupplier, CookieParam.class));
    bind(new ParamInjectionResolver<>(formSupplier, FormParam.class));
    bind(new ParamInjectionResolver<>(headerSupplier, HeaderParam.class));
    bind(new ParamInjectionResolver<>(matrixSupplier, MatrixParam.class));
    bind(new ParamInjectionResolver<>(querySupplier, QueryParam.class));
    bind(new ParamInjectionResolver<>(pathSupplier, PathParam.class));
    bind(new ParamInjectionResolver<>(asyncSupplier, Suspended.class));
    bind(new ParamInjectionResolver<>(webTargetSupplier, Uri.class));
    bind(new ParamInjectionResolver<>(beanSupplier, BeanParam.class));
    // Delegated value supplier for Context InjectionResolver which is implemented directly in DI provider
    ContextInjectionResolver contextInjectionResolver = injectionManager.getInstance(ContextInjectionResolver.class);
    bind(new DelegatedInjectionValueSupplierProvider(contextInjectionResolver, injectionManager::createForeignDescriptor)).to(ValueSupplierProvider.class);
}
Also used : FormParam(javax.ws.rs.FormParam) ContextInjectionResolver(org.glassfish.jersey.internal.inject.ContextInjectionResolver) PathParam(javax.ws.rs.PathParam) Provider(javax.inject.Provider) Configuration(javax.ws.rs.core.Configuration) AsyncContext(org.glassfish.jersey.server.internal.process.AsyncContext) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Providers(org.glassfish.jersey.internal.inject.Providers) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) Function(java.util.function.Function) Suspended(javax.ws.rs.container.Suspended) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Uri(org.glassfish.jersey.server.Uri) BeanParam(javax.ws.rs.BeanParam) QueryParam(javax.ws.rs.QueryParam) Value(org.glassfish.jersey.internal.util.collection.Value) MatrixParam(javax.ws.rs.MatrixParam) LazyValue(org.glassfish.jersey.internal.util.collection.LazyValue) ValueSupplierProvider(org.glassfish.jersey.server.spi.internal.ValueSupplierProvider) HeaderParam(javax.ws.rs.HeaderParam) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) CookieParam(javax.ws.rs.CookieParam) Injections(org.glassfish.jersey.internal.inject.Injections) Values(org.glassfish.jersey.internal.util.collection.Values) HeaderParam(javax.ws.rs.HeaderParam) MatrixParam(javax.ws.rs.MatrixParam) Configuration(javax.ws.rs.core.Configuration) AsyncContext(org.glassfish.jersey.server.internal.process.AsyncContext) Uri(org.glassfish.jersey.server.Uri) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) PathParam(javax.ws.rs.PathParam) Suspended(javax.ws.rs.container.Suspended) ContextInjectionResolver(org.glassfish.jersey.internal.inject.ContextInjectionResolver) BeanParam(javax.ws.rs.BeanParam) CookieParam(javax.ws.rs.CookieParam) QueryParam(javax.ws.rs.QueryParam) FormParam(javax.ws.rs.FormParam)

Example 29 with InjectionManager

use of org.glassfish.jersey.internal.inject.InjectionManager in project jersey by jersey.

the class RequestResponseWrapperProvider method getRequestScopedInitializerProvider.

@Override
public RequestScopedInitializerProvider getRequestScopedInitializerProvider() {
    return new RequestScopedInitializerProvider() {

        @Override
        public RequestScopedInitializer get(final RequestContextProvider context) {
            return new RequestScopedInitializer() {

                @Override
                public void initialize(InjectionManager injectionManager) {
                    ServiceLocator locator = ((HK2InjectionManager) injectionManager).getServiceLocator();
                    locator.<Ref<HttpServletRequest>>getService(REQUEST_TYPE).set(finalWrap(context.getHttpServletRequest()));
                    locator.<Ref<HttpServletResponse>>getService(RESPONSE_TYPE).set(finalWrap(context.getHttpServletResponse()));
                }
            };
        }
    };
}
Also used : RequestScopedInitializer(org.glassfish.jersey.server.spi.RequestScopedInitializer) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestScopedInitializerProvider(org.glassfish.jersey.servlet.internal.spi.RequestScopedInitializerProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) RequestContextProvider(org.glassfish.jersey.servlet.internal.spi.RequestContextProvider) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager)

Example 30 with InjectionManager

use of org.glassfish.jersey.internal.inject.InjectionManager in project jersey by jersey.

the class ApplicationHandler method bindProvidersAndResources.

private void bindProvidersAndResources(final Iterable<ComponentProvider> componentProviders, final ComponentBag componentBag, final Collection<Class<?>> resourceClasses, final Collection<Object> resourceInstances) {
    JerseyResourceContext resourceContext = injectionManager.getInstance(JerseyResourceContext.class);
    Set<Class<?>> registeredClasses = runtimeConfig.getRegisteredClasses();
    /*
         * Check the {@code component} whether it is correctly configured for client or server {@link RuntimeType runtime}.
         */
    java.util.function.Predicate<Class<?>> correctlyConfigured = componentClass -> Providers.checkProviderRuntime(componentClass, componentBag.getModel(componentClass), RuntimeType.SERVER, !registeredClasses.contains(componentClass), resourceClasses.contains(componentClass));
    /*
         * Check the {@code resource class} whether it is correctly configured for client or server {@link RuntimeType runtime}.
         */
    BiPredicate<Class<?>, ContractProvider> correctlyConfiguredResource = (resourceClass, model) -> Providers.checkProviderRuntime(resourceClass, model, RuntimeType.SERVER, !registeredClasses.contains(resourceClass), true);
    Set<Class<?>> componentClassses = componentBag.getClasses(ComponentBag.excludeMetaProviders(injectionManager)).stream().filter(correctlyConfigured).collect(Collectors.toSet());
    // Merge programmatic resource classes with component classes.
    Set<Class<?>> classes = Collections.newSetFromMap(new IdentityHashMap<>());
    classes.addAll(componentClassses);
    classes.addAll(resourceClasses);
    // Bind classes.
    for (final Class<?> componentClass : classes) {
        ContractProvider model = componentBag.getModel(componentClass);
        if (bindWithComponentProvider(componentClass, model, componentProviders)) {
            continue;
        }
        if (resourceClasses.contains(componentClass)) {
            if (!Resource.isAcceptable(componentClass)) {
                LOGGER.warning(LocalizationMessages.NON_INSTANTIABLE_COMPONENT(componentClass));
                continue;
            }
            if (model != null && !correctlyConfiguredResource.test(componentClass, model)) {
                model = null;
            }
            resourceContext.unsafeBindResource(componentClass, model, injectionManager);
        } else {
            ProviderBinder.bindProvider(componentClass, model, injectionManager);
        }
    }
    // Merge programmatic resource instances with other component instances.
    Set<Object> instances = componentBag.getInstances(ComponentBag.excludeMetaProviders(injectionManager)).stream().filter(instance -> correctlyConfigured.test(instance.getClass())).collect(Collectors.toSet());
    instances.addAll(resourceInstances);
    // Bind instances.
    for (Object component : instances) {
        ContractProvider model = componentBag.getModel(component.getClass());
        if (resourceInstances.contains(component)) {
            if (model != null && !correctlyConfiguredResource.test(component.getClass(), model)) {
                model = null;
            }
            resourceContext.unsafeBindResource(component, model, injectionManager);
        } else {
            ProviderBinder.bindProvider(component, model, injectionManager);
        }
    }
}
Also used : JerseyResourceContext(org.glassfish.jersey.server.internal.JerseyResourceContext) ComponentBag(org.glassfish.jersey.model.internal.ComponentBag) RankedComparator(org.glassfish.jersey.model.internal.RankedComparator) SecurityContext(javax.ws.rs.core.SecurityContext) Errors(org.glassfish.jersey.internal.Errors) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) ModelProcessor(org.glassfish.jersey.server.model.ModelProcessor) ComponentProvider(org.glassfish.jersey.server.spi.ComponentProvider) Application(javax.ws.rs.core.Application) JerseyResourceContext(org.glassfish.jersey.server.internal.JerseyResourceContext) ContainerLifecycleListener(org.glassfish.jersey.server.spi.ContainerLifecycleListener) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) RankedProvider(org.glassfish.jersey.model.internal.RankedProvider) JerseyRequestTimeoutHandler(org.glassfish.jersey.server.internal.JerseyRequestTimeoutHandler) ExecutorProviders(org.glassfish.jersey.process.internal.ExecutorProviders) Stages(org.glassfish.jersey.process.internal.Stages) Future(java.util.concurrent.Future) Ref(org.glassfish.jersey.internal.util.collection.Ref) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Map(java.util.Map) ProviderBinder(org.glassfish.jersey.internal.inject.ProviderBinder) RequestProcessingContext(org.glassfish.jersey.server.internal.process.RequestProcessingContext) ModelErrors(org.glassfish.jersey.server.model.internal.ModelErrors) ApplicationEvent(org.glassfish.jersey.server.monitoring.ApplicationEvent) Values(org.glassfish.jersey.internal.util.collection.Values) ContainerResponseWriter(org.glassfish.jersey.server.spi.ContainerResponseWriter) LocalizationMessages(org.glassfish.jersey.server.internal.LocalizationMessages) IdentityHashMap(java.util.IdentityHashMap) RuntimeType(javax.ws.rs.RuntimeType) Collection(java.util.Collection) Version(org.glassfish.jersey.internal.Version) MessageBodyWorkers(org.glassfish.jersey.message.MessageBodyWorkers) Container(org.glassfish.jersey.server.spi.Container) Set(java.util.Set) ContractProvider(org.glassfish.jersey.model.ContractProvider) ResourceModel(org.glassfish.jersey.server.model.ResourceModel) Providers(org.glassfish.jersey.internal.inject.Providers) Logger(java.util.logging.Logger) ModelValidationException(org.glassfish.jersey.server.model.ModelValidationException) Collectors(java.util.stream.Collectors) ChainableStage(org.glassfish.jersey.process.internal.ChainableStage) PreMatching(javax.ws.rs.container.PreMatching) GenericType(javax.ws.rs.core.GenericType) DynamicFeature(javax.ws.rs.container.DynamicFeature) List(java.util.List) Principal(java.security.Principal) HttpHeaders(javax.ws.rs.core.HttpHeaders) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) MonitoringContainerListener(org.glassfish.jersey.server.internal.monitoring.MonitoringContainerListener) Spliterator(java.util.Spliterator) Resource(org.glassfish.jersey.server.model.Resource) CommonProperties(org.glassfish.jersey.CommonProperties) Configuration(javax.ws.rs.core.Configuration) Order(org.glassfish.jersey.model.internal.RankedComparator.Order) CompletableFuture(java.util.concurrent.CompletableFuture) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Singleton(javax.inject.Singleton) Binder(org.glassfish.jersey.internal.inject.Binder) Function(java.util.function.Function) Supplier(java.util.function.Supplier) HttpMethod(javax.ws.rs.HttpMethod) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) ApplicationEventListener(org.glassfish.jersey.server.monitoring.ApplicationEventListener) BiPredicate(java.util.function.BiPredicate) ReferencesInitializer(org.glassfish.jersey.server.internal.process.ReferencesInitializer) Value(org.glassfish.jersey.internal.util.collection.Value) ApplicationEventImpl(org.glassfish.jersey.server.internal.monitoring.ApplicationEventImpl) ExternalRequestScope(org.glassfish.jersey.server.spi.ExternalRequestScope) Routing(org.glassfish.jersey.server.internal.routing.Routing) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LazyValue(org.glassfish.jersey.internal.util.collection.LazyValue) StreamSupport(java.util.stream.StreamSupport) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) Injections(org.glassfish.jersey.internal.inject.Injections) LinkedList(java.util.LinkedList) OutputStream(java.io.OutputStream) CompositeApplicationEventListener(org.glassfish.jersey.server.internal.monitoring.CompositeApplicationEventListener) Iterator(java.util.Iterator) ServiceConfigurationError(org.glassfish.jersey.internal.ServiceConfigurationError) ProcessingProviders(org.glassfish.jersey.server.internal.ProcessingProviders) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ReflectionHelper(org.glassfish.jersey.internal.util.ReflectionHelper) TimeUnit(java.util.concurrent.TimeUnit) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ComponentModelValidator(org.glassfish.jersey.server.model.ComponentModelValidator) NameBinding(javax.ws.rs.NameBinding) CompositeBinder(org.glassfish.jersey.internal.inject.CompositeBinder) Stage(org.glassfish.jersey.process.internal.Stage) ServiceFinder(org.glassfish.jersey.internal.ServiceFinder) NullOutputStream(org.glassfish.jersey.message.internal.NullOutputStream) Collections(java.util.Collections) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) ContractProvider(org.glassfish.jersey.model.ContractProvider)

Aggregations

InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)50 Test (org.junit.Test)41 WadlGenerator (org.glassfish.jersey.server.wadl.WadlGenerator)10 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)8 ProviderBinder (org.glassfish.jersey.internal.inject.ProviderBinder)7 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)5 File (java.io.File)4 URL (java.net.URL)4 Properties (java.util.Properties)4 WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)4 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)4 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)3 JerseyTest (org.glassfish.jersey.test.JerseyTest)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 IdentityHashMap (java.util.IdentityHashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Function (java.util.function.Function)2