Search in sources :

Example 11 with ProviderInfo

use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.

the class MicroProfileClientProxyImpl method mergeHeaders.

private void mergeHeaders(Class<ClientHeadersFactory> factoryCls, MultivaluedMap<String, String> existingHeaders) {
    try {
        ClientHeadersFactory factory;
        Message m = JAXRS_UTILS_GET_CURRENT_MESSAGE_METHOD == null ? null : (Message) JAXRS_UTILS_GET_CURRENT_MESSAGE_METHOD.invoke(null);
        if (m != null) {
            factory = CDIFacade.getInstanceFromCDI(factoryCls, m.getExchange().getBus()).map(this::mapInstance).orElse(factoryCls.newInstance());
            ProviderInfo<ClientHeadersFactory> pi = clientHeaderFactories.computeIfAbsent(factoryCls, k -> {
                return new ProviderInfo<ClientHeadersFactory>(factory, m.getExchange().getBus(), true);
            });
            InjectionUtils.injectContexts(factory, pi, m);
        } else {
            factory = CDIFacade.getInstanceFromCDI(factoryCls).map(this::mapInstance).orElse(factoryCls.newInstance());
        }
        MultivaluedMap<String, String> updatedHeaders = factory.update(getJaxrsHeaders(m), existingHeaders);
        existingHeaders.putAll(updatedHeaders);
    } catch (Throwable t) {
        // expected if not running in a JAX-RS server environment.
        if (LOG.isLoggable(Level.FINEST)) {
            LOG.log(Level.FINEST, "Caught exception getting JAX-RS incoming headers", t);
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ClientHeadersFactory(org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory)

Example 12 with ProviderInfo

use of org.apache.cxf.jaxrs.model.ProviderInfo in project tomee by apache.

the class ProviderFactory method createMessageBodyWriterInterceptor.

public <T> List<WriterInterceptor> createMessageBodyWriterInterceptor(Class<T> bodyType, Type parameterType, Annotation[] parameterAnnotations, MediaType mediaType, Message m, Set<String> names) {
    MessageBodyWriter<T> mw = createMessageBodyWriter(bodyType, parameterType, parameterAnnotations, mediaType, m);
    int size = writerInterceptors.size();
    if (mw != null || size > 0) {
        @SuppressWarnings({ "unchecked", "rawtypes" }) WriterInterceptor mbwWriter = new WriterInterceptorMBW((MessageBodyWriter) mw, m);
        final List<WriterInterceptor> interceptors;
        if (size > 0) {
            interceptors = new ArrayList<>(size + 1);
            List<ProviderInfo<WriterInterceptor>> writers = getBoundFilters(writerInterceptors, names);
            for (ProviderInfo<WriterInterceptor> p : writers) {
                injectContextValues(p, m);
                interceptors.add(p.getProvider());
            }
            interceptors.add(mbwWriter);
        } else {
            interceptors = Collections.singletonList(mbwWriter);
        }
        return interceptors;
    }
    return null;
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) FilterProviderInfo(org.apache.cxf.jaxrs.model.FilterProviderInfo) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 13 with ProviderInfo

use of org.apache.cxf.jaxrs.model.ProviderInfo in project tomee by apache.

the class ProviderFactory method createProviderFromConstructor.

public static ProviderInfo<? extends Object> createProviderFromConstructor(Constructor<?> c, Map<Class<?>, Object> values, Bus theBus, boolean checkContexts, boolean custom) {
    Map<Class<?>, Map<Class<?>, ThreadLocalProxy<?>>> proxiesMap = CastUtils.cast((Map<?, ?>) theBus.getProperty(AbstractResourceInfo.CONSTRUCTOR_PROXY_MAP));
    Map<Class<?>, ThreadLocalProxy<?>> existingProxies = null;
    if (proxiesMap != null) {
        existingProxies = proxiesMap.get(c.getDeclaringClass());
    }
    Class<?>[] paramTypes = c.getParameterTypes();
    Object[] cArgs = ResourceUtils.createConstructorArguments(c, null, false, values);
    if (existingProxies != null && existingProxies.size() <= paramTypes.length) {
        for (int i = 0; i < paramTypes.length; i++) {
            if (cArgs[i] instanceof ThreadLocalProxy) {
                cArgs[i] = existingProxies.get(paramTypes[i]);
            }
        }
    }
    final Object instance;
    try {
        instance = c.newInstance(cArgs);
    } catch (Throwable ex) {
        throw new RuntimeException("Resource or provider class " + c.getDeclaringClass().getName() + " can not be instantiated", ex);
    }
    Map<Class<?>, ThreadLocalProxy<?>> proxies = new LinkedHashMap<>();
    for (int i = 0; i < paramTypes.length; i++) {
        if (cArgs[i] instanceof ThreadLocalProxy) {
            @SuppressWarnings("unchecked") ThreadLocalProxy<Object> proxy = (ThreadLocalProxy<Object>) cArgs[i];
            proxies.put(paramTypes[i], proxy);
        }
    }
    boolean isApplication = Application.class.isAssignableFrom(c.getDeclaringClass());
    if (isApplication) {
        return new ApplicationInfo((Application) instance, proxies, theBus);
    }
    return new ProviderInfo<Object>(instance, proxies, theBus, checkContexts, custom);
}
Also used : ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) LinkedHashMap(java.util.LinkedHashMap) FilterProviderInfo(org.apache.cxf.jaxrs.model.FilterProviderInfo) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TreeMap(java.util.TreeMap)

Example 14 with ProviderInfo

use of org.apache.cxf.jaxrs.model.ProviderInfo in project tomee by apache.

the class CxfRsHttpListener method fixProviders.

/**
 * Fix providers set in ProviderFactory
 * - changes default Jonhzon by the TomEE specific one
 * - remove deactivated providers
 * @param serviceConfiguration
 */
private void fixProviders(final ServiceConfiguration serviceConfiguration) {
    final ServerProviderFactory spf = ServerProviderFactory.class.cast(server.getEndpoint().get(ServerProviderFactory.class.getName()));
    for (final String field : asList("messageWriters", "messageReaders")) {
        final List<ProviderInfo<?>> values = List.class.cast(Reflections.get(spf, field));
        boolean customJsonProvider = false;
        for (final ProviderInfo<?> o : values) {
            // using getName to not suppose any classloader setup
            final String name = o.getResourceClass().getName();
            if ("org.apache.johnzon.jaxrs.ConfigurableJohnzonProvider".equals(name) || "org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider".equals(name) || // contains in case of proxying
            name.contains("com.fasterxml.jackson.jaxrs.json")) {
                customJsonProvider = true;
                // cause we only handle json for now
                break;
            }
        }
        final Iterator<ProviderInfo<?>> it = values.iterator();
        while (it.hasNext()) {
            final String name = it.next().getResourceClass().getName();
            // remove JohnzonProvider default versions
            if (("org.apache.johnzon.jaxrs.JohnzonProvider".equals(name) || "org.apache.openejb.server.cxf.rs.CxfRSService$TomEEJohnzonProvider".equals(name)) && customJsonProvider) {
                it.remove();
            }
            // remove deactivated providers
            if (!isActive(name, serviceConfiguration)) {
                it.remove();
            }
        }
    }
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo)

Example 15 with ProviderInfo

use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.

the class ProviderFactoryTest method testCustomProviderSortingWithBus2.

@Test
public void testCustomProviderSortingWithBus2() {
    WildcardReader wc1 = new WildcardReader();
    WildcardReader2 wc2 = new WildcardReader2();
    Bus bus = BusFactory.newInstance().createBus();
    bus.setProperty(MessageBodyReader.class.getName(), wc2);
    ProviderFactory pf = ServerProviderFactory.createInstance(bus);
    pf.registerUserProvider(wc1);
    List<ProviderInfo<MessageBodyReader<?>>> readers = pf.getMessageReaders();
    assertEquals(11, readers.size());
    assertSame(wc1, readers.get(7).getProvider());
    assertSame(wc2, readers.get(8).getProvider());
}
Also used : Bus(org.apache.cxf.Bus) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) Test(org.junit.Test)

Aggregations

ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)26 Endpoint (org.apache.cxf.endpoint.Endpoint)9 FilterProviderInfo (org.apache.cxf.jaxrs.model.FilterProviderInfo)7 Response (javax.ws.rs.core.Response)6 Message (org.apache.cxf.message.Message)5 Test (org.junit.Test)5 IOException (java.io.IOException)4 Comparator (java.util.Comparator)4 Map (java.util.Map)4 WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)4 MessageImpl (org.apache.cxf.message.MessageImpl)4 List (java.util.List)3 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)3 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)3 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)3 ReaderInterceptor (javax.ws.rs.ext.ReaderInterceptor)3 Bus (org.apache.cxf.Bus)3 ContainerRequestContextImpl (org.apache.cxf.jaxrs.impl.ContainerRequestContextImpl)3 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)3 File (java.io.File)2