Search in sources :

Example 6 with InjectionManager

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

the class WadlGeneratorLoaderTest method testLoadStream.

@Test
public void testLoadStream() throws Exception {
    final InjectionManager injectionManager = InjectionManagerFactory.createInjectionManager();
    final Properties props = new Properties();
    final String path = getClass().getPackage().getName().replaceAll("\\.", "/") + "/testfile.xml";
    props.put("testStream", path);
    final WadlGeneratorDescription description = new WadlGeneratorDescription(MyWadlGenerator2.class, props);
    final WadlGenerator wadlGenerator = WadlGeneratorLoader.loadWadlGeneratorDescriptions(injectionManager, description);
    Assert.assertEquals(MyWadlGenerator2.class, wadlGenerator.getClass());
    final URL resource = getClass().getResource("testfile.xml");
    Assert.assertEquals(new File(resource.toURI()).length(), ((MyWadlGenerator2) wadlGenerator).getTestStreamContent().length());
}
Also used : WadlGenerator(org.glassfish.jersey.server.wadl.WadlGenerator) Properties(java.util.Properties) File(java.io.File) URL(java.net.URL) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 7 with InjectionManager

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

the class ValidatorTest method createInjectionManager.

private InjectionManager createInjectionManager() {
    InjectionManager injectionManager = InjectionManagerFactory.createInjectionManager();
    injectionManager.register(new AbstractBinder() {

        @Override
        protected void configure() {
            bind(new CommonConfig(RuntimeType.SERVER, ComponentBag.EXCLUDE_EMPTY)).to(Configuration.class);
        }
    });
    return injectionManager;
}
Also used : Configuration(javax.ws.rs.core.Configuration) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) CommonConfig(org.glassfish.jersey.model.internal.CommonConfig) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager)

Example 8 with InjectionManager

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

the class ResourceMethodDispatcherFactoryTest method setupApplication.

@Before
public void setupApplication() {
    InjectionManager locator = InjectionManagerFactory.createInjectionManager();
    rmdf = locator.getInstance(ResourceMethodDispatcherFactory.class);
    rmihf = locator.getInstance(ResourceMethodInvocationHandlerFactory.class);
}
Also used : InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Before(org.junit.Before)

Example 9 with InjectionManager

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

the class JettyContainerTest method testParentServiceLocator.

/**
     * Test that defined ServiceLocator becomes a parent of the newly created service locator.
     */
@Test
public void testParentServiceLocator() {
    final ServiceLocator locator = new ServiceLocatorImpl("MyServiceLocator", null);
    final Server server = JettyHttpContainerFactory.createServer(URI.create("http://localhost:9876"), new ResourceConfig(Resource.class), false, locator);
    JettyHttpContainer container = (JettyHttpContainer) server.getHandler();
    InjectionManager injectionManager = container.getApplicationHandler().getInjectionManager();
    HK2InjectionManager hk2InjectionManager = (HK2InjectionManager) injectionManager;
    ServiceLocator serviceLocator = hk2InjectionManager.getServiceLocator();
    assertTrue("Application injection manager was expected to have defined parent locator", serviceLocator.getParent() == locator);
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Server(org.eclipse.jetty.server.Server) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) JettyHttpContainer(org.glassfish.jersey.jetty.JettyHttpContainer) ServiceLocatorImpl(org.jvnet.hk2.internal.ServiceLocatorImpl) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 10 with InjectionManager

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

the class ParameterValueHelper method createValueProviders.

/**
     * Create list of parameter value providers for the given {@link Parameterized
     * parameterized} resource model component.
     *
     * @param injectionManager injection manager.
     * @param parameterized parameterized resource model component.
     * @return list of parameter value providers for the parameterized component.
     */
public static List<ParamValueFactoryWithSource<?>> createValueProviders(InjectionManager injectionManager, Parameterized parameterized) {
    if ((null == parameterized.getParameters()) || (0 == parameterized.getParameters().size())) {
        return Collections.emptyList();
    }
    List<ValueSupplierProvider> valueSupplierProviders = Providers.getProviders(injectionManager, ValueSupplierProvider.class).stream().sorted((o1, o2) -> o2.getPriority().getWeight() - o1.getPriority().getWeight()).collect(Collectors.toList());
    boolean entityParamFound = false;
    final List<ParamValueFactoryWithSource<?>> providers = new ArrayList<>(parameterized.getParameters().size());
    for (final Parameter parameter : parameterized.getParameters()) {
        final Parameter.Source parameterSource = parameter.getSource();
        entityParamFound = entityParamFound || Parameter.Source.ENTITY == parameterSource;
        final Supplier<?> valueSupplier = getParamValueSupplier(valueSupplierProviders, parameter);
        if (valueSupplier != null) {
            providers.add(wrapParamValueSupplier(valueSupplier, parameterSource));
        } else {
            providers.add(null);
        }
    }
    if (!entityParamFound && Collections.frequency(providers, null) == 1) {
        // Try to find entity if there is one unresolved parameter and the annotations are unknown
        final int entityParamIndex = providers.lastIndexOf(null);
        final Parameter parameter = parameterized.getParameters().get(entityParamIndex);
        if (Parameter.Source.UNKNOWN == parameter.getSource() && !parameter.isQualified()) {
            final Parameter overriddenParameter = Parameter.overrideSource(parameter, Parameter.Source.ENTITY);
            final Supplier<?> valueSupplier = getParamValueSupplier(valueSupplierProviders, overriddenParameter);
            if (valueSupplier != null) {
                providers.set(entityParamIndex, wrapParamValueSupplier(valueSupplier, overriddenParameter.getSource()));
            } else {
                providers.set(entityParamIndex, null);
            }
        }
    }
    return providers;
}
Also used : Iterator(java.util.Iterator) Collection(java.util.Collection) MappableException(org.glassfish.jersey.server.internal.process.MappableException) Providers(org.glassfish.jersey.internal.inject.Providers) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) ArrayList(java.util.ArrayList) List(java.util.List) MessageBodyProviderNotFoundException(org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException) Parameterized(org.glassfish.jersey.server.model.Parameterized) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) Parameter(org.glassfish.jersey.server.model.Parameter) Collections(java.util.Collections) NotSupportedException(javax.ws.rs.NotSupportedException) ArrayList(java.util.ArrayList) Parameter(org.glassfish.jersey.server.model.Parameter)

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