Search in sources :

Example 16 with SingletonResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project cxf by apache.

the class RxJava2ObservableServer method run.

protected void run() {
    Bus bus = BusFactory.getDefaultBus();
    // Make sure default JSONProvider is not loaded
    bus.setProperty("skip.default.json.provider.registration", true);
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setInvoker(new ReactiveIOInvoker());
    sf.setProvider(new JacksonJsonProvider());
    sf.getOutInterceptors().add(new LoggingOutInterceptor());
    sf.setResourceClasses(RxJava2ObservableService.class);
    sf.setResourceProvider(RxJava2ObservableService.class, new SingletonResourceProvider(new RxJava2ObservableService(), true));
    sf.setAddress("http://localhost:" + PORT + "/");
    server = sf.create();
}
Also used : Bus(org.apache.cxf.Bus) ReactiveIOInvoker(org.apache.cxf.jaxrs.rx2.server.ReactiveIOInvoker) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Example 17 with SingletonResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project cxf by apache.

the class RxJavaObservableServer method run.

protected void run() {
    Bus bus = BusFactory.getDefaultBus();
    // Make sure default JSONProvider is not loaded
    bus.setProperty("skip.default.json.provider.registration", true);
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setProvider(new JacksonJsonProvider());
    new ObservableCustomizer().customize(sf);
    sf.getOutInterceptors().add(new LoggingOutInterceptor());
    sf.setResourceClasses(RxJavaObservableService.class);
    sf.setResourceProvider(RxJavaObservableService.class, new SingletonResourceProvider(new RxJavaObservableService(), true));
    sf.setAddress("http://localhost:" + PORT + "/");
    server = sf.create();
}
Also used : ObservableCustomizer(org.apache.cxf.jaxrs.rx.server.ObservableCustomizer) Bus(org.apache.cxf.Bus) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Example 18 with SingletonResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project tutorials by eugenp.

the class RestfulServer method main.

public static void main(String[] args) throws Exception {
    JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
    factoryBean.setResourceClasses(CourseRepository.class);
    factoryBean.setResourceProvider(new SingletonResourceProvider(new CourseRepository()));
    factoryBean.setAddress("http://localhost:8080/");
    Server server = factoryBean.create();
    System.out.println("Server ready...");
    Thread.sleep(60 * 1000);
    System.out.println("Server exiting");
    server.destroy();
    System.exit(0);
}
Also used : Server(org.apache.cxf.endpoint.Server) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Example 19 with SingletonResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project opencast by opencast.

the class RestPublisher method createEndpoint.

/**
 * Creates a REST endpoint for the JAX-RS annotated service.
 *
 * @param ref
 *          the osgi service reference
 * @param service
 *          The service itself
 */
protected void createEndpoint(ServiceReference<?> ref, Object service) {
    String serviceType = (String) ref.getProperty(SERVICE_TYPE_PROPERTY);
    String servicePath = (String) ref.getProperty(SERVICE_PATH_PROPERTY);
    boolean servicePublishFlag = ref.getProperty(SERVICE_PUBLISH_PROPERTY) == null || Boolean.parseBoolean((String) ref.getProperty(SERVICE_PUBLISH_PROPERTY));
    boolean jobProducer = Boolean.parseBoolean((String) ref.getProperty(SERVICE_JOBPRODUCER_PROPERTY));
    ServiceRegistration<?> reg = servletRegistrationMap.get(servicePath);
    if (reg != null) {
        logger.debug("Rest endpoint {} is still registred, skip registering again", servicePath);
        return;
    }
    RestServlet cxf = new RestServlet();
    try {
        Dictionary<String, Object> props = new Hashtable<>();
        props.put(SharedHttpContext.ALIAS, servicePath);
        props.put(SharedHttpContext.SERVLET_NAME, service.toString());
        props.put(SharedHttpContext.CONTEXT_ID, RestConstants.HTTP_CONTEXT_ID);
        props.put(SharedHttpContext.SHARED, "true");
        props.put(SERVICE_TYPE_PROPERTY, serviceType);
        props.put(SERVICE_PATH_PROPERTY, servicePath);
        props.put(SERVICE_PUBLISH_PROPERTY, servicePublishFlag);
        props.put(SERVICE_JOBPRODUCER_PROPERTY, jobProducer);
        reg = componentContext.getBundleContext().registerService(Servlet.class.getName(), cxf, props);
    } catch (Exception e) {
        logger.info("Problem registering REST endpoint {} : {}", servicePath, e.getMessage());
        return;
    }
    servletRegistrationMap.put(servicePath, reg);
    // Wait for the servlet to be initialized as long as one minute. Since the servlet is published via the whiteboard,
    // this may happen asynchronously. However, after 30 seconds we expect the HTTP service and the whiteboard
    // implementation to be loaded and active.
    int count = 0;
    while (!cxf.isInitialized() && count < 300) {
        logger.debug("Waiting for the servlet at '{}' to be initialized", servicePath);
        try {
            Thread.sleep(100);
            count++;
        } catch (InterruptedException e) {
            logger.warn("Interrupt while waiting for RestServlet initialization");
            break;
        }
    }
    // Was initialization successful
    if (!cxf.isInitialized()) {
        logger.error("Whiteboard implemenation failed to pick up REST endpoint declaration {}", serviceType);
        return;
    }
    resourceProviders.put(ref, new SingletonResourceProvider(service));
    // Set up cxf
    Bus bus = cxf.getBus();
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(bus);
    sf.setProviders(providers);
    // Set the service class
    sf.setResourceProviders(new ArrayList<>(resourceProviders.values()));
    sf.setResourceComparator(new OsgiCxfEndpointComparator());
    sf.setAddress("/");
    BindingFactoryManager manager = sf.getBus().getExtension(BindingFactoryManager.class);
    JAXRSBindingFactory factory = new JAXRSBindingFactory();
    factory.setBus(sf.getBus());
    manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, factory);
    if (server != null) {
        logger.debug("Destroying JAX-RS server");
        server.stop();
        server.destroy();
    }
    server = sf.create();
    logger.info("Registered REST endpoint at " + servicePath);
    if (service instanceof RestEndpoint) {
        ((RestEndpoint) service).endpointPublished();
    }
}
Also used : Bus(org.apache.cxf.Bus) JAXRSBindingFactory(org.apache.cxf.jaxrs.JAXRSBindingFactory) Hashtable(java.util.Hashtable) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) BindingFactoryManager(org.apache.cxf.binding.BindingFactoryManager) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ServletException(javax.servlet.ServletException) XMLStreamException(javax.xml.stream.XMLStreamException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Example 20 with SingletonResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project tomee by apache.

the class ResourceUtils method createApplication.

@SuppressWarnings("unchecked")
public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath, boolean staticSubresourceResolution, boolean useSingletonResourceProvider, Bus bus) {
    Set<Object> singletons = app.getSingletons();
    verifySingletons(singletons);
    List<Class<?>> resourceClasses = new ArrayList<>();
    List<Object> providers = new ArrayList<>();
    List<Feature> features = new ArrayList<>();
    Map<Class<?>, ResourceProvider> map = new HashMap<>();
    // or singleton provider classes
    for (Class<?> cls : app.getClasses()) {
        if (isValidApplicationClass(cls, singletons)) {
            if (isValidProvider(cls)) {
                providers.add(createProviderInstance(cls));
            } else if (Feature.class.isAssignableFrom(cls)) {
                features.add(createFeatureInstance((Class<? extends Feature>) cls));
            } else {
                resourceClasses.add(cls);
                if (useSingletonResourceProvider) {
                    map.put(cls, new SingletonResourceProvider(createProviderInstance(cls)));
                } else {
                    map.put(cls, new PerRequestResourceProvider(cls));
                }
            }
        }
    }
    // we can get either a provider or resource class here
    for (Object o : singletons) {
        if (isValidProvider(o.getClass())) {
            providers.add(o);
        } else if (o instanceof Feature) {
            features.add((Feature) o);
        } else {
            resourceClasses.add(o.getClass());
            map.put(o.getClass(), new SingletonResourceProvider(o));
        }
    }
    JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
    if (bus != null) {
        bean.setBus(bus);
    }
    String address = "/";
    if (!ignoreAppPath) {
        ApplicationPath appPath = locateApplicationPath(app.getClass());
        if (appPath != null) {
            address = appPath.value();
        }
    }
    if (!address.startsWith("/")) {
        address = "/" + address;
    }
    bean.setAddress(address);
    bean.setStaticSubresourceResolution(staticSubresourceResolution);
    bean.setResourceClasses(resourceClasses);
    bean.setProviders(providers);
    bean.getFeatures().addAll(features);
    for (Map.Entry<Class<?>, ResourceProvider> entry : map.entrySet()) {
        bean.setResourceProvider(entry.getKey(), entry.getValue());
    }
    Map<String, Object> appProps = app.getProperties();
    if (appProps != null) {
        bean.getProperties(true).putAll(appProps);
    }
    bean.setApplication(app);
    return bean;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ApplicationPath(javax.ws.rs.ApplicationPath) Feature(org.apache.cxf.feature.Feature) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ElementClass(org.apache.cxf.jaxrs.ext.xml.ElementClass) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Aggregations

SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)84 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)63 Bus (org.apache.cxf.Bus)17 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)15 ArrayList (java.util.ArrayList)14 LoggingOutInterceptor (org.apache.cxf.ext.logging.LoggingOutInterceptor)14 ResourceProvider (org.apache.cxf.jaxrs.lifecycle.ResourceProvider)9 HashMap (java.util.HashMap)7 Before (org.junit.Before)7 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)5 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)5 Map (java.util.Map)4 ReactiveIOCustomizer (org.apache.cxf.jaxrs.rx3.server.ReactiveIOCustomizer)4 DetectorResource (org.apache.tika.server.resource.DetectorResource)4 MetadataResource (org.apache.tika.server.resource.MetadataResource)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Feature (org.apache.cxf.feature.Feature)3 ReactiveIOCustomizer (org.apache.cxf.jaxrs.rx2.server.ReactiveIOCustomizer)3 Message (org.apache.cxf.message.Message)3