Search in sources :

Example 1 with SseContextProvider

use of org.apache.cxf.jaxrs.sse.SseContextProvider in project tomee by apache.

the class CxfRsHttpListener method deployApplication.

@Override
public void deployApplication(final Application application, final String prefix, final String webContext, final Collection<Object> additionalProviders, final Map<String, EJBRestServiceInfo> restEjbs, final ClassLoader classLoader, final Collection<Injection> injections, final Context context, final WebBeansContext owbCtx, final ServiceConfiguration serviceConfiguration) {
    final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(CxfUtil.initBusLoader());
    try {
        final ApplicationData applicationData = getApplicationData(application, prefix, additionalProviders);
        logApplication(applicationData);
        if (applicationData.getResources().size() == 0) {
            throw new NoResourcesFoundException(applicationData);
        }
        final JAXRSServerFactoryBean factory = newFactory(prefix, createServiceJmxName(classLoader), createEndpointName(application));
        configureFactory(additionalProviders, serviceConfiguration, factory, owbCtx, application);
        factory.setApplication(application);
        final List<Class<?>> classes = new ArrayList<>();
        for (final Class<?> clazz : application.getClasses()) {
            if (!additionalProviders.contains(clazz) && !clazz.isInterface()) {
                classes.add(clazz);
                final EJBRestServiceInfo restServiceInfo = getEjbRestServiceInfo(restEjbs, clazz);
                if (restServiceInfo != null) {
                    final Object proxy = ProxyEJB.subclassProxy(restServiceInfo.context);
                    factory.setResourceProvider(clazz, new NoopResourceProvider(restServiceInfo.context.getBeanClass(), proxy));
                } else {
                    if (owbCtx != null) {
                        final BeanManagerImpl bm = owbCtx.getBeanManagerImpl();
                        Bean<?> bean = null;
                        if (bm != null && bm.isInUse()) {
                            try {
                                final Set<Bean<?>> beans = bm.getBeans(clazz);
                                bean = bm.resolve(beans);
                            } catch (final InjectionException ie) {
                                final String msg = "Resource class " + clazz.getName() + " can not be instantiated";
                                LOGGER.warning(msg, ie);
                                throw new WebApplicationException(Response.serverError().entity(msg).build());
                            }
                            if (bean != null && isConsideredSingleton(bean.getScope())) {
                                final Object reference = bm.getReference(bean, bean.getBeanClass(), bm.createCreationalContext(bean));
                                factory.setResourceProvider(clazz, new CdiSingletonResourceProvider(classLoader, clazz, reference, injections, context, owbCtx));
                                continue;
                            }
                        }
                    }
                    factory.setResourceProvider(clazz, new OpenEJBPerRequestPojoResourceProvider(classLoader, clazz, injections, context, owbCtx));
                }
            }
        }
        for (final Object o : application.getSingletons()) {
            if (!additionalProviders.contains(o)) {
                final Class<?> clazz = realClass(o.getClass());
                classes.add(clazz);
                final EJBRestServiceInfo restServiceInfo = getEjbRestServiceInfo(restEjbs, clazz);
                if (restServiceInfo != null) {
                    final Object proxy = ProxyEJB.subclassProxy(restServiceInfo.context);
                    factory.setResourceProvider(clazz, new NoopResourceProvider(restServiceInfo.context.getBeanClass(), proxy));
                } else {
                    if (owbCtx != null && owbCtx.getBeanManagerImpl().isInUse()) {
                        final CdiSingletonResourceProvider provider = new CdiSingletonResourceProvider(classLoader, clazz, o, injections, context, owbCtx);
                        singletons.add(provider);
                        factory.setResourceProvider(clazz, provider);
                    } else {
                        factory.setResourceProvider(clazz, new SingletonResourceProvider(o));
                    }
                }
            }
        }
        factory.setResourceClasses(classes);
        factory.setInvoker(new AutoJAXRSInvoker(restEjbs));
        injectApplication(application, factory);
        /*
             * During setApplication CXF will inspect the binding annotations
             * on the Application subclass and apply them to every Resource class
             * definition.  This is how global bindings are supported.  Thus, if
             * setApplication is called before we've called setResourceClasses()
             * binding annotations on the Application subclass will not work.
             *
             * Global binding annotations are tested in:
             * com/sun/ts/tests/jaxrs/spec/filter/globalbinding/JAXRSClient#globalBoundResourceTest_from_standalone
             */
        factory.setApplication(application);
        this.context = webContext;
        if (!webContext.startsWith("/")) {
            this.context = "/" + webContext;
        }
        // /webcontext/servlet for event firing
        final Level level = SERVER_IMPL_LOGGER.getLevel();
        try {
            SERVER_IMPL_LOGGER.setLevel(Level.OFF);
        } catch (final UnsupportedOperationException e) {
        // ignore
        }
        try {
            factory.setProvider(new SseContextProvider());
            factory.setProvider(new TomEESseEventSinkContextProvider());
            server = factory.create();
            fixProviders(serviceConfiguration);
            fireServerCreated(oldLoader);
            final ServerProviderFactory spf = ServerProviderFactory.class.cast(server.getEndpoint().get(ServerProviderFactory.class.getName()));
            LOGGER.info("Using readers:");
            for (final Object provider : List.class.cast(Reflections.get(spf, "messageReaders"))) {
                LOGGER.info("     " + ProviderInfo.class.cast(provider).getProvider());
            }
            LOGGER.info("Using writers:");
            for (final Object provider : List.class.cast(Reflections.get(spf, "messageWriters"))) {
                LOGGER.info("     " + ProviderInfo.class.cast(provider).getProvider());
            }
            LOGGER.info("Using exception mappers:");
            for (final Object provider : List.class.cast(Reflections.get(spf, "exceptionMappers"))) {
                LOGGER.info("     " + ProviderInfo.class.cast(provider).getProvider());
            }
        } finally {
            try {
                SERVER_IMPL_LOGGER.setLevel(level);
            } catch (final UnsupportedOperationException e) {
            // ignore
            }
        }
        final int servletIdx = 1 + this.context.substring(1).indexOf('/');
        if (servletIdx > 0) {
            this.servlet = this.context.substring(servletIdx);
            this.context = this.context.substring(0, servletIdx);
        }
        destination = (HttpDestination) server.getDestination();
        final String base;
        if (prefix.endsWith("/")) {
            base = prefix.substring(0, prefix.length() - 1);
        } else if (prefix.endsWith(wildcard)) {
            base = prefix.substring(0, prefix.length() - wildcard.length());
        } else {
            base = prefix;
        }
        // stack info to log to get nice logs
        logEndpoints(application, prefix, restEjbs, factory, base);
    } finally {
        if (oldLoader != null) {
            CxfUtil.clearBusLoader(oldLoader);
        }
    }
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) WebApplicationException(javax.ws.rs.WebApplicationException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) EJBRestServiceInfo(org.apache.openejb.server.rest.EJBRestServiceInfo) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) MBean(org.apache.openejb.api.jmx.MBean) Bean(javax.enterprise.inject.spi.Bean) InjectionException(javax.enterprise.inject.InjectionException) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SseContextProvider(org.apache.cxf.jaxrs.sse.SseContextProvider) TomEESseEventSinkContextProvider(org.apache.openejb.server.cxf.rs.sse.TomEESseEventSinkContextProvider) ManagedEndpoint(org.apache.cxf.endpoint.ManagedEndpoint) Endpoint(org.apache.cxf.endpoint.Endpoint) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) Level(java.util.logging.Level)

Example 2 with SseContextProvider

use of org.apache.cxf.jaxrs.sse.SseContextProvider in project cxf by apache.

the class SseTransportCustomizationExtension method customize.

@Override
public void customize(final JAXRSServerFactoryBean bean) {
    bean.setTransportId(SseHttpTransportFactory.TRANSPORT_ID);
    bean.setProvider(new SseContextProvider());
    bean.setProvider(new SseAtmosphereEventSinkContextProvider());
    final Bus bus = bean.getBus();
    if (bus != null) {
        bus.setProperty(AbstractTransportFactory.PREFERRED_TRANSPORT_ID, SseHttpTransportFactory.TRANSPORT_ID);
    }
}
Also used : Bus(org.apache.cxf.Bus) SseAtmosphereEventSinkContextProvider(org.apache.cxf.jaxrs.sse.atmosphere.SseAtmosphereEventSinkContextProvider) SseContextProvider(org.apache.cxf.jaxrs.sse.SseContextProvider)

Aggregations

SseContextProvider (org.apache.cxf.jaxrs.sse.SseContextProvider)2 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Level (java.util.logging.Level)1 InjectionException (javax.enterprise.inject.InjectionException)1 Bean (javax.enterprise.inject.spi.Bean)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Bus (org.apache.cxf.Bus)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1 ManagedEndpoint (org.apache.cxf.endpoint.ManagedEndpoint)1 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)1 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)1 ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)1 ServerProviderFactory (org.apache.cxf.jaxrs.provider.ServerProviderFactory)1 SseAtmosphereEventSinkContextProvider (org.apache.cxf.jaxrs.sse.atmosphere.SseAtmosphereEventSinkContextProvider)1 MBean (org.apache.openejb.api.jmx.MBean)1 TomEESseEventSinkContextProvider (org.apache.openejb.server.cxf.rs.sse.TomEESseEventSinkContextProvider)1 EJBRestServiceInfo (org.apache.openejb.server.rest.EJBRestServiceInfo)1 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)1