org.apache.cxf.jaxrs.ext.ContextProvider Java Examples

The following examples show how to use org.apache.cxf.jaxrs.ext.ContextProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProviderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> ContextProvider<T> createContextProvider(Type contextType,
                                                    Message m) {
    Class<?> contextCls = InjectionUtils.getActualType(contextType);
    if (contextCls == null) {
        return null;
    }
    for (ProviderInfo<ContextProvider<?>> cr : contextProviders) {
        Type[] types = cr.getProvider().getClass().getGenericInterfaces();
        for (Type t : types) {
            if (t instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType)t;
                Type[] args = pt.getActualTypeArguments();
                if (args.length > 0) {
                    Class<?> argCls = InjectionUtils.getActualType(args[0]);

                    if (argCls != null && argCls.isAssignableFrom(contextCls)) {
                        return (ContextProvider<T>)cr.getProvider();
                    }
                }
            }
        }
    }
    return null;
}
 
Example #2
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInjectCustomContext() throws Exception {
    final CustomerContext contextImpl = new CustomerContext() {

        public String get() {
            return "customerContext";
        }

    };
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    Customer customer = new Customer();
    sf.setServiceBeanObjects(customer);
    sf.setProvider(new ContextProvider<CustomerContext>() {
        public CustomerContext createContext(Message message) {
            return contextImpl;
        }
    });
    sf.setStart(false);
    Server s = sf.create();
    assertTrue(customer.getCustomerContext() instanceof ThreadLocalProxy<?>);
    invokeCustomerMethod(sf.getServiceFactory().getClassResourceInfo().get(0),
                         customer, s);
    CustomerContext context = customer.getCustomerContext();
    assertEquals("customerContext", context.get());
}
 
Example #3
Source File: JaxrsTest.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Test
public void testCxfExtension() {
    registerApplication(new TestApplication());

    registerExtension(
        ContextProvider.class,
        new BusContextProvider(),
        "Bus provider");

    registerAddon(
        new CxfExtensionTestAddon(), JAX_RS_APPLICATION_SELECT,
        String.format(
            "(%s=%s)", JAX_RS_APPLICATION_BASE, "/test-application"));

    @SuppressWarnings("serial")
    ServiceRegistration<?> extensionRegistration =
        bundleContext.registerService(
            new String[]{
                ExtensionA.class.getName(),
                ExtensionB.class.getName()
            },
            new TestCxfExtension(),
            new Hashtable<String, Object>() {{
                put("cxf.extension", Boolean.TRUE);
            }});

    try {
        WebTarget path = createDefaultTarget().
            path("/test-application").
            path("/extensions").
            path(ExtensionA.class.getName());

        String result = path.request().get(String.class);

        assertTrue(Boolean.parseBoolean(result));

        path = createDefaultTarget().
            path("/test-application").
            path("/extensions").
            path(ExtensionB.class.getName());

        result = path.request().get(String.class);

        assertTrue(Boolean.parseBoolean(result));

        extensionRegistration.unregister();

        path = createDefaultTarget().
            path("/test-application").
            path("/extensions").
            path(ExtensionA.class.getName());

        result = path.request().get(String.class);

        assertFalse(Boolean.parseBoolean(result));

        path = createDefaultTarget().
            path("/test-application").
            path("/extensions").
            path(ExtensionB.class.getName());

        result = path.request().get(String.class);

        assertFalse(Boolean.parseBoolean(result));
    }
    finally {
        try {
            extensionRegistration.unregister();
        }
        catch (Exception e) {
        }
    }

}
 
Example #4
Source File: JAXRSUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static <T> T createContextValue(Message m, Type genericType, Class<T> clazz) {

        Message contextMessage = getContextMessage(m);
        Object o = null;
        if (UriInfo.class.isAssignableFrom(clazz)) {
            o = createUriInfo(contextMessage);
        } else if (HttpHeaders.class.isAssignableFrom(clazz)
            || ProtocolHeaders.class.isAssignableFrom(clazz)) {
            o = createHttpHeaders(contextMessage, clazz);
        } else if (SecurityContext.class.isAssignableFrom(clazz)) {
            SecurityContext customContext = contextMessage.get(SecurityContext.class);
            o = customContext == null ? new SecurityContextImpl(contextMessage) : customContext;
        } else if (MessageContext.class.isAssignableFrom(clazz)) {
            o = new MessageContextImpl(m);
        } else if (ResourceInfo.class.isAssignableFrom(clazz)) {
            o = new ResourceInfoImpl(contextMessage);
        } else if (ResourceContext.class.isAssignableFrom(clazz)) {
            o = new ResourceContextImpl(contextMessage, contextMessage.getExchange().get(OperationResourceInfo.class));
        } else if (Request.class.isAssignableFrom(clazz)) {
            o = new RequestImpl(contextMessage);
        } else if (Providers.class.isAssignableFrom(clazz)) {
            o = new ProvidersImpl(contextMessage);
        } else if (ContextResolver.class.isAssignableFrom(clazz)) {
            o = createContextResolver(genericType, contextMessage);
        } else if (Configuration.class.isAssignableFrom(clazz)) {
            o = ProviderFactory.getInstance(contextMessage).getConfiguration(contextMessage);
        } else if (Application.class.isAssignableFrom(clazz)) {
            ProviderInfo<?> providerInfo =
                (ProviderInfo<?>)contextMessage.getExchange().getEndpoint().get(Application.class.getName());
            o = providerInfo == null ? null : providerInfo.getProvider();
        } else if (contextMessage != null) {
            ContextProvider<?> provider =
                ProviderFactory.getInstance(contextMessage).createContextProvider(clazz, contextMessage);
            if (provider != null) {
                o = provider.createContext(contextMessage);
            }
        }
        if (o == null && contextMessage != null && !MessageUtils.isRequestor(contextMessage)) {
            o = HttpUtils.createServletResourceValue(contextMessage, clazz);
        }
        return clazz.cast(o);
    }
 
Example #5
Source File: ProviderFactory.java    From cxf with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected void setCommonProviders(List<ProviderInfo<? extends Object>> theProviders) {
    List<ProviderInfo<ReaderInterceptor>> readInts =
        new LinkedList<>();
    List<ProviderInfo<WriterInterceptor>> writeInts =
        new LinkedList<>();
    for (ProviderInfo<? extends Object> provider : theProviders) {
        Class<?> providerCls = ClassHelper.getRealClass(bus, provider.getProvider());

        if (filterContractSupported(provider, providerCls, MessageBodyReader.class)) {
            addProviderToList(messageReaders, provider);
        }

        if (filterContractSupported(provider, providerCls, MessageBodyWriter.class)) {
            addProviderToList(messageWriters, provider);
        }

        if (filterContractSupported(provider, providerCls, ContextResolver.class)) {
            addProviderToList(contextResolvers, provider);
        }

        if (ContextProvider.class.isAssignableFrom(providerCls)) {
            addProviderToList(contextProviders, provider);
        }

        if (filterContractSupported(provider, providerCls, ReaderInterceptor.class)) {
            readInts.add((ProviderInfo<ReaderInterceptor>)provider);
        }

        if (filterContractSupported(provider, providerCls, WriterInterceptor.class)) {
            writeInts.add((ProviderInfo<WriterInterceptor>)provider);
        }

        if (filterContractSupported(provider, providerCls, ParamConverterProvider.class)) {
            paramConverters.add((ProviderInfo<ParamConverterProvider>)provider);
        }
    }
    sortReaders();
    sortWriters();
    sortContextResolvers();

    mapInterceptorFilters(readerInterceptors, readInts, ReaderInterceptor.class, true);
    mapInterceptorFilters(writerInterceptors, writeInts, WriterInterceptor.class, true);

    injectContextProxies(messageReaders, messageWriters, contextResolvers, paramConverters,
        readerInterceptors.values(), writerInterceptors.values());
    checkParamConverterContexts();
}