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);
}
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();
}
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);
}
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;
}
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();
}
}
Aggregations