Search in sources :

Example 1 with ApplicationInfo

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

the class Swagger2Feature method addSwaggerResource.

@Override
protected void addSwaggerResource(Server server, Bus bus) {
    JAXRSServiceFactoryBean sfb = (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
    ServerProviderFactory factory = (ServerProviderFactory) server.getEndpoint().get(ServerProviderFactory.class.getName());
    ApplicationInfo appInfo = null;
    if (!isScan()) {
        appInfo = factory.getApplicationProvider();
        if (appInfo == null) {
            Set<Class<?>> serviceClasses = new HashSet<>();
            for (ClassResourceInfo cri : sfb.getClassResourceInfo()) {
                serviceClasses.add(cri.getServiceClass());
            }
            appInfo = new ApplicationInfo(new DefaultApplication(serviceClasses), bus);
            server.getEndpoint().put(Application.class.getName(), appInfo);
        }
    }
    List<Object> swaggerResources = new LinkedList<>();
    if (customizer == null) {
        customizer = new Swagger2Customizer();
    }
    ApiListingResource apiListingResource = new Swagger2ApiListingResource(customizer);
    swaggerResources.add(apiListingResource);
    List<Object> providers = new ArrayList<>();
    if (isRunAsFilter()) {
        providers.add(new SwaggerContainerRequestFilter(appInfo == null ? null : appInfo.getProvider(), customizer));
    }
    final Properties swaggerProps = getSwaggerProperties(bus);
    final Registration swaggerUiRegistration = getSwaggerUi(bus, swaggerProps, isRunAsFilter());
    if (!isRunAsFilter()) {
        swaggerResources.addAll(swaggerUiRegistration.getResources());
    }
    providers.addAll(swaggerUiRegistration.getProviders());
    sfb.setResourceClassesFromBeans(swaggerResources);
    List<ClassResourceInfo> cris = sfb.getClassResourceInfo();
    if (!isRunAsFilter()) {
        for (ClassResourceInfo cri : cris) {
            if (ApiListingResource.class.isAssignableFrom(cri.getResourceClass())) {
                InjectionUtils.injectContextProxies(cri, apiListingResource);
            }
        }
    }
    customizer.setClassResourceInfos(cris);
    customizer.setDynamicBasePath(dynamicBasePath);
    BeanConfig beanConfig = appInfo == null ? new BeanConfig() : new ApplicationBeanConfig(appInfo.getProvider());
    initBeanConfig(beanConfig, swaggerProps);
    Swagger swagger = beanConfig.getSwagger();
    if (swagger != null && securityDefinitions != null) {
        swagger.setSecurityDefinitions(securityDefinitions);
    }
    customizer.setBeanConfig(beanConfig);
    providers.add(new ReaderConfigFilter());
    if (beanConfig.isUsePathBasedConfig()) {
        providers.add(new ServletConfigProvider());
    }
    factory.setUserProviders(providers);
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) ApiListingResource(io.swagger.jaxrs.listing.ApiListingResource) ArrayList(java.util.ArrayList) Properties(java.util.Properties) JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) Swagger(io.swagger.models.Swagger) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) BeanConfig(io.swagger.jaxrs.config.BeanConfig) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) LinkedList(java.util.LinkedList) Application(javax.ws.rs.core.Application)

Example 2 with ApplicationInfo

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

the class OpenApiFeature method getApplicationOrDefault.

/**
 * Detects the application (if present) or creates the default application (in case the scan is disabled).
 */
protected Application getApplicationOrDefault(final Server server, final ServerProviderFactory factory, final JAXRSServiceFactoryBean sfb, final Bus bus) {
    ApplicationInfo appInfo = null;
    if (!isScan()) {
        appInfo = factory.getApplicationProvider();
        if (appInfo == null) {
            appInfo = new ApplicationInfo(new DefaultApplication(sfb.getClassResourceInfo(), 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)

Example 3 with ApplicationInfo

use of org.apache.cxf.jaxrs.model.ApplicationInfo in project cxf 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]);
            }
        }
    }
    Object instance = null;
    try {
        instance = c.newInstance(cArgs);
    } catch (Throwable ex) {
        throw new RuntimeException("Resource or provider class " + c.getDeclaringClass().getName() + " can not be instantiated");
    }
    Map<Class<?>, ThreadLocalProxy<?>> proxies = new LinkedHashMap<Class<?>, ThreadLocalProxy<?>>();
    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 4 with ApplicationInfo

use of org.apache.cxf.jaxrs.model.ApplicationInfo 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 5 with ApplicationInfo

use of org.apache.cxf.jaxrs.model.ApplicationInfo 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 = StringUtils.split(applicationNames, 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);
        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)

Aggregations

ApplicationInfo (org.apache.cxf.jaxrs.model.ApplicationInfo)6 Application (javax.ws.rs.core.Application)5 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 ServletException (javax.servlet.ServletException)2 BeanConfig (io.swagger.jaxrs.config.BeanConfig)1 ApiListingResource (io.swagger.jaxrs.listing.ApiListingResource)1 Swagger (io.swagger.models.Swagger)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1 ServletContext (javax.servlet.ServletContext)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1