Search in sources :

Example 51 with Application

use of javax.ws.rs.core.Application in project cxf by apache.

the class AbstractSpringComponentScanServer method createJaxRsServer.

@Override
protected Server createJaxRsServer() {
    JAXRSServerFactoryBean factoryBean = null;
    String[] beanNames = applicationContext.getBeanNamesForAnnotation(ApplicationPath.class);
    if (beanNames.length > 0) {
        Set<String> componentScanPackagesSet = parseSetProperty(componentScanPackages);
        Set<String> componentScanBeansSet = parseSetProperty(componentScanBeans);
        for (String beanName : beanNames) {
            if (isComponentMatched(beanName, componentScanPackagesSet, componentScanBeansSet)) {
                Application app = applicationContext.getBean(beanName, Application.class);
                factoryBean = createFactoryBeanFromApplication(app);
                for (String cxfBeanName : applicationContext.getBeanNamesForAnnotation(org.apache.cxf.annotations.Provider.class)) {
                    if (isComponentMatched(cxfBeanName, componentScanPackagesSet, componentScanBeansSet)) {
                        addCxfProvider(getProviderBean(cxfBeanName));
                    }
                }
                break;
            }
        }
    }
    if (!StringUtils.isEmpty(classesScanPackages)) {
        try {
            final Map<Class<? extends Annotation>, Collection<Class<?>>> appClasses = ClasspathScanner.findClasses(classesScanPackages, ApplicationPath.class);
            List<Application> apps = CastUtils.cast(JAXRSServerFactoryBeanDefinitionParser.createBeansFromDiscoveredClasses(super.applicationContext, appClasses.get(ApplicationPath.class), null));
            if (!apps.isEmpty()) {
                factoryBean = createFactoryBeanFromApplication(apps.get(0));
                final Map<Class<? extends Annotation>, Collection<Class<?>>> cxfClasses = ClasspathScanner.findClasses(classesScanPackages, org.apache.cxf.annotations.Provider.class);
                addCxfProvidersFromClasses(cxfClasses.get(org.apache.cxf.annotations.Provider.class));
            }
        } catch (Exception ex) {
            throw new ServiceConstructionException(ex);
        }
    }
    if (factoryBean != null) {
        setFactoryCxfProviders(factoryBean);
        return factoryBean.create();
    }
    return super.createJaxRsServer();
}
Also used : JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Annotation(java.lang.annotation.Annotation) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) Provider(javax.ws.rs.ext.Provider) Collection(java.util.Collection) Application(javax.ws.rs.core.Application)

Example 52 with Application

use of javax.ws.rs.core.Application in project cxf by apache.

the class CXFNonSpringJaxrsServlet method createSingletonInstance.

protected Object createSingletonInstance(Class<?> cls, Map<String, List<String>> props, ServletConfig sc) throws ServletException {
    Constructor<?> c = ResourceUtils.findResourceConstructor(cls, false);
    if (c == null) {
        throw new ServletException("No valid constructor found for " + cls.getName());
    }
    boolean isApplication = Application.class.isAssignableFrom(c.getDeclaringClass());
    try {
        final ProviderInfo<? extends Object> provider;
        if (c.getParameterTypes().length == 0) {
            if (isApplication) {
                provider = new ApplicationInfo((Application) c.newInstance(), getBus());
            } else {
                provider = new ProviderInfo<>(c.newInstance(), getBus(), false, true);
            }
        } else {
            Map<Class<?>, Object> values = new HashMap<>();
            values.put(ServletContext.class, sc.getServletContext());
            values.put(ServletConfig.class, sc);
            provider = ProviderFactory.createProviderFromConstructor(c, values, getBus(), isApplication, true);
        }
        Object instance = provider.getProvider();
        injectProperties(instance, props);
        configureSingleton(instance);
        return isApplication ? provider : instance;
    } catch (InstantiationException ex) {
        ex.printStackTrace();
        throw new ServletException("Resource class " + cls.getName() + " can not be instantiated");
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
        throw new ServletException("Resource class " + cls.getName() + " can not be instantiated due to IllegalAccessException");
    } catch (InvocationTargetException ex) {
        ex.printStackTrace();
        throw new ServletException("Resource class " + cls.getName() + " can not be instantiated due to InvocationTargetException");
    }
}
Also used : HashMap(java.util.HashMap) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) Application(javax.ws.rs.core.Application)

Example 53 with Application

use of javax.ws.rs.core.Application in project cxf by apache.

the class CXFNonSpringJaxrsServlet method createApplicationInfo.

protected ApplicationInfo createApplicationInfo(String appClassName, ServletConfig servletConfig) throws ServletException {
    Application customApp = createApplicationInstance(appClassName, servletConfig);
    if (customApp != null) {
        return new ApplicationInfo(customApp, getBus());
    }
    Map<String, List<String>> props = new HashMap<>();
    appClassName = getClassNameAndProperties(appClassName, props);
    Class<?> appClass = loadApplicationClass(appClassName);
    ApplicationInfo appInfo = (ApplicationInfo) createSingletonInstance(appClass, props, servletConfig);
    Map<String, Object> servletProps = new HashMap<>();
    ServletContext servletContext = servletConfig.getServletContext();
    for (Enumeration<String> names = servletContext.getInitParameterNames(); names.hasMoreElements(); ) {
        String name = names.nextElement();
        servletProps.put(name, servletContext.getInitParameter(name));
    }
    for (Enumeration<String> names = servletConfig.getInitParameterNames(); names.hasMoreElements(); ) {
        String name = names.nextElement();
        servletProps.put(name, servletConfig.getInitParameter(name));
    }
    appInfo.setOverridingProps(servletProps);
    return appInfo;
}
Also used : HashMap(java.util.HashMap) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) ServletContext(javax.servlet.ServletContext) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Application(javax.ws.rs.core.Application)

Example 54 with Application

use of javax.ws.rs.core.Application in project cxf by apache.

the class CXFNonSpringJaxrsServlet method createServerFromApplication.

protected void createServerFromApplication(String applicationNames, ServletConfig servletConfig) throws ServletException {
    boolean ignoreApplicationPath = isIgnoreApplicationPath(servletConfig);
    String[] classNames = applicationNames.split(getParameterSplitChar(servletConfig));
    if (classNames.length > 1 && ignoreApplicationPath) {
        throw new ServletException("\"" + IGNORE_APP_PATH_PARAM + "\" parameter must be set to false for multiple Applications be supported");
    }
    for (String cName : classNames) {
        ApplicationInfo providerApp = createApplicationInfo(cName, servletConfig);
        Application app = providerApp.getProvider();
        JAXRSServerFactoryBean bean = ResourceUtils.createApplication(app, ignoreApplicationPath, getStaticSubResolutionValue(servletConfig), isAppResourceLifecycleASingleton(app, servletConfig), getBus());
        String splitChar = getParameterSplitChar(servletConfig);
        setAllInterceptors(bean, servletConfig, splitChar);
        setInvoker(bean, servletConfig);
        setExtensions(bean, servletConfig);
        setDocLocation(bean, servletConfig);
        setSchemasLocations(bean, servletConfig);
        List<?> providers = getProviders(servletConfig, splitChar);
        bean.setProviders(providers);
        List<? extends Feature> features = getFeatures(servletConfig, splitChar);
        bean.getFeatures().addAll(features);
        bean.setBus(getBus());
        bean.setApplicationInfo(providerApp);
        bean.create();
    }
}
Also used : ServletException(javax.servlet.ServletException) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) Application(javax.ws.rs.core.Application)

Example 55 with Application

use of javax.ws.rs.core.Application in project cxf by apache.

the class DefaultApplicationFactory method createApplicationOrDefault.

/**
 * Detects the application (if present) or creates the default application (in case the scan is disabled).
 */
public static Application createApplicationOrDefault(final Server server, final ServerProviderFactory factory, final JAXRSServiceFactoryBean sfb, final Bus bus, final Set<String> resourcePackages, final boolean scan) {
    ApplicationInfo appInfo = null;
    if (!scan) {
        appInfo = factory.getApplicationProvider();
        if (appInfo == null) {
            appInfo = createApplicationInfo(sfb, resourcePackages, bus);
            server.getEndpoint().put(Application.class.getName(), appInfo);
        }
    }
    return (appInfo == null) ? null : appInfo.getProvider();
}
Also used : ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) Application(javax.ws.rs.core.Application)

Aggregations

Application (javax.ws.rs.core.Application)63 HashSet (java.util.HashSet)14 HashMap (java.util.HashMap)12 ApplicationInfo (org.apache.cxf.jaxrs.model.ApplicationInfo)12 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 Set (java.util.Set)7 ServletException (javax.servlet.ServletException)7 Annotation (java.lang.annotation.Annotation)5 List (java.util.List)5 Map (java.util.Map)5 ApplicationPath (javax.ws.rs.ApplicationPath)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)4 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)3 Method (java.lang.reflect.Method)3 Type (java.lang.reflect.Type)3 Collection (java.util.Collection)3