Search in sources :

Example 16 with ServerProviderFactory

use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.

the class InjectionUtils method createFromParameterHandler.

public static <T> T createFromParameterHandler(String value, Class<T> pClass, Type genericType, Annotation[] anns, Message message) {
    T result = null;
    if (message != null) {
        ServerProviderFactory pf = ServerProviderFactory.getInstance(message);
        ParamConverter<T> pm = pf.createParameterHandler(pClass, genericType, anns, message);
        if (pm != null) {
            result = pm.fromString(value);
        }
    }
    return result;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory)

Example 17 with ServerProviderFactory

use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project tomee by apache.

the class JAXRSServerFactoryBean method create.

/**
 * Creates the JAX-RS Server instance
 * @return the server
 */
public Server create() {
    ClassLoaderHolder origLoader = null;
    try {
        Bus bus = getBus();
        ClassLoader loader = bus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        serviceFactory.setBus(bus);
        checkResources(true);
        if (serviceFactory.getService() == null) {
            serviceFactory.create();
        }
        Endpoint ep = createEndpoint();
        getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server);
        server = new ServerImpl(getBus(), ep, getDestinationFactory(), getBindingFactory());
        Invoker invoker = serviceFactory.getInvoker();
        if (invoker == null) {
            ep.getService().setInvoker(createInvoker());
        } else {
            ep.getService().setInvoker(invoker);
        }
        ServerProviderFactory factory = setupFactory(ep);
        ep.put(Application.class.getName(), appProvider);
        factory.setRequestPreprocessor(new RequestPreprocessor(languageMappings, extensionMappings));
        ep.put(Bus.class.getName(), getBus());
        if (documentLocation != null) {
            ep.put(JAXRSUtils.DOC_LOCATION, documentLocation);
        }
        if (rc != null) {
            ep.put("org.apache.cxf.jaxrs.comparator", rc);
        }
        checkPrivateEndpoint(ep);
        applyBusFeatures(getBus());
        applyFeatures();
        updateClassResourceProviders(ep);
        injectContexts(factory, (ApplicationInfo) ep.get(Application.class.getName()));
        factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
        getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, null, null);
        if (start) {
            try {
                server.start();
            } catch (RuntimeException re) {
                if (!(re instanceof ServiceConstructionException && re.getMessage().startsWith("There is an endpoint already running on"))) {
                    // avoid destroying another server on the same endpoint url
                    // prevent resource leak if server really started by itself
                    server.destroy();
                }
                throw re;
            }
        }
    } catch (Exception e) {
        throw new ServiceConstructionException(e);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
    }
    return server;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Bus(org.apache.cxf.Bus) RequestPreprocessor(org.apache.cxf.jaxrs.impl.RequestPreprocessor) Endpoint(org.apache.cxf.endpoint.Endpoint) ServerImpl(org.apache.cxf.endpoint.ServerImpl) Invoker(org.apache.cxf.service.invoker.Invoker) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Application(javax.ws.rs.core.Application) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException)

Example 18 with ServerProviderFactory

use of org.apache.cxf.jaxrs.provider.ServerProviderFactory 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 19 with ServerProviderFactory

use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.

the class JAXRSServerFactoryBeanTest method testRegisterProviders.

@Test
public void testRegisterProviders() {
    JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
    bean.setAddress("http://localhost:8080/rest");
    bean.setStart(false);
    bean.setResourceClasses(BookStore.class);
    List<Object> providers = new ArrayList<>();
    Object provider1 = new CustomExceptionMapper();
    providers.add(provider1);
    Object provider2 = new RuntimeExceptionMapper();
    providers.add(provider2);
    bean.setProviders(providers);
    Server s = bean.create();
    ServerProviderFactory factory = (ServerProviderFactory) s.getEndpoint().get(ServerProviderFactory.class.getName());
    ExceptionMapper<Exception> mapper1 = factory.createExceptionMapper(Exception.class, new MessageImpl());
    assertNotNull(mapper1);
    ExceptionMapper<RuntimeException> mapper2 = factory.createExceptionMapper(RuntimeException.class, new MessageImpl());
    assertNotNull(mapper2);
    assertNotSame(mapper1, mapper2);
    assertSame(provider1, mapper1);
    assertSame(provider2, mapper2);
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Server(org.apache.cxf.endpoint.Server) ArrayList(java.util.ArrayList) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 20 with ServerProviderFactory

use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.

the class JAXRSServerFactoryBean method create.

/**
 * Creates the JAX-RS Server instance
 * @return the server
 */
public Server create() {
    ClassLoaderHolder origLoader = null;
    try {
        Bus bus = getBus();
        ClassLoader loader = bus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        serviceFactory.setBus(bus);
        checkResources(true);
        if (serviceFactory.getService() == null) {
            serviceFactory.create();
        }
        Endpoint ep = createEndpoint();
        getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server);
        server = new ServerImpl(getBus(), ep, getDestinationFactory(), getBindingFactory());
        Invoker invoker = serviceFactory.getInvoker();
        if (invoker == null) {
            ep.getService().setInvoker(createInvoker());
        } else {
            ep.getService().setInvoker(invoker);
        }
        ServerProviderFactory factory = setupFactory(ep);
        ep.put(Application.class.getName(), appProvider);
        factory.setRequestPreprocessor(new RequestPreprocessor(languageMappings, extensionMappings));
        ep.put(Bus.class.getName(), getBus());
        if (documentLocation != null) {
            ep.put(JAXRSUtils.DOC_LOCATION, documentLocation);
        }
        if (rc != null) {
            ep.put("org.apache.cxf.jaxrs.comparator", rc);
        }
        checkPrivateEndpoint(ep);
        applyBusFeatures(getBus());
        applyFeatures();
        updateClassResourceProviders(ep);
        injectContexts(factory, (ApplicationInfo) ep.get(Application.class.getName()));
        factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
        getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, null, null);
        if (start) {
            try {
                server.start();
            } catch (RuntimeException re) {
                if (!(re instanceof ServiceConstructionException && re.getMessage().startsWith("There is an endpoint already running on"))) {
                    // avoid destroying another server on the same endpoint url
                    // prevent resource leak if server really started by itself
                    server.destroy();
                }
                throw re;
            }
        }
    } catch (Exception e) {
        throw new ServiceConstructionException(e);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
    }
    return server;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Bus(org.apache.cxf.Bus) RequestPreprocessor(org.apache.cxf.jaxrs.impl.RequestPreprocessor) Endpoint(org.apache.cxf.endpoint.Endpoint) ServerImpl(org.apache.cxf.endpoint.ServerImpl) Invoker(org.apache.cxf.service.invoker.Invoker) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Application(javax.ws.rs.core.Application) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException)

Aggregations

ServerProviderFactory (org.apache.cxf.jaxrs.provider.ServerProviderFactory)23 ArrayList (java.util.ArrayList)5 Application (javax.ws.rs.core.Application)5 Message (org.apache.cxf.message.Message)5 List (java.util.List)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 Response (javax.ws.rs.core.Response)4 RequestPreprocessor (org.apache.cxf.jaxrs.impl.RequestPreprocessor)4 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)4 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)4 Level (java.util.logging.Level)3 MediaType (javax.ws.rs.core.MediaType)3 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)3 ClassLoaderHolder (org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)3 Endpoint (org.apache.cxf.endpoint.Endpoint)3 ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)3 MessageContentsList (org.apache.cxf.message.MessageContentsList)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 Properties (java.util.Properties)2