Java Code Examples for org.apache.cxf.jaxrs.JAXRSServiceFactoryBean#getClassResourceInfo()

The following examples show how to use org.apache.cxf.jaxrs.JAXRSServiceFactoryBean#getClassResourceInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultApplicationFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * Detects the application (if present) or creates the default application (in case the scan is disabled).
 */
public static ApplicationInfo createApplicationInfoOrDefault(final Server server, 
            final ServerProviderFactory factory, final JAXRSServiceFactoryBean sfb, final Bus bus, 
                final boolean scan) {
    
    ApplicationInfo appInfo = null;
    if (!scan) {
        appInfo = factory.getApplicationProvider();
        if (appInfo == null) {
            Set<Class<?>> serviceClasses = new HashSet<>();
            for (ClassResourceInfo cri : sfb.getClassResourceInfo()) {
                serviceClasses.add(cri.getServiceClass());
            }
            appInfo = createApplicationInfo(serviceClasses, bus);
            server.getEndpoint().put(Application.class.getName(), appInfo);
        }
    }
    
    return appInfo;
}
 
Example 2
Source File: AbstractSwaggerFeature.java    From cxf with Apache License 2.0 6 votes vote down vote up
void calculateDefaultResourcePackage(Server server) {
    if (!StringUtils.isEmpty(getResourcePackage())) {
        return;
    }
    JAXRSServiceFactoryBean serviceFactoryBean =
            (JAXRSServiceFactoryBean)server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
    List<ClassResourceInfo> resourceInfos = serviceFactoryBean.getClassResourceInfo();

    if (resourceInfos.size() == 1) {
        setResourcePackage(resourceInfos.get(0).getServiceClass().getPackage().getName());
    } else {
        List<Class<?>> serviceClasses = new ArrayList<>(resourceInfos.size());
        for (ClassResourceInfo cri : resourceInfos) {
            serviceClasses.add(cri.getServiceClass());
        }
        String sharedPackage = PackageUtils.getSharedPackageName(serviceClasses);
        if (!StringUtils.isEmpty(sharedPackage)) {
            setResourcePackage(sharedPackage);
        }
    }
}
 
Example 3
Source File: CxfJaxrsServiceRegistrator.java    From aries-jax-rs-whiteboard with Apache License 2.0 5 votes vote down vote up
public Iterable<Class<?>> getStaticResourceClasses() {
    if (_jaxRsServerFactoryBean == null) {
        return Collections.emptyList();
    }

    JAXRSServiceFactoryBean serviceFactory =
        _jaxRsServerFactoryBean.getServiceFactory();

    List<ClassResourceInfo> classResourceInfo =
        serviceFactory.getClassResourceInfo();

    ArrayList<Class<?>> classes = new ArrayList<>();

    for (ClassResourceInfo resourceInfo : classResourceInfo) {
        if (resourceInfo == null) {
            continue;
        }

        ResourceProvider resourceProvider =
            resourceInfo.getResourceProvider();

        if (resourceProvider == null ||
            !ServiceReferenceResourceProvider.class.isAssignableFrom(
                resourceProvider.getClass())) {

            classes.add(resourceInfo.getResourceClass());
        }
    }

    return classes;
}
 
Example 4
Source File: CxfCdiAutoSetup.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
private Function<ServletDestination, String> getServletDestinationPath(ServletConfig sc, LogFacade log)
{
    return sd -> {
        final Endpoint endpoint = ChainInitiationObserver.class.cast(sd.getMessageObserver()).getEndpoint();
        final ApplicationInfo app = ApplicationInfo.class.cast(endpoint.get(Application.class.getName()));
        final JAXRSServiceFactoryBean sfb = JAXRSServiceFactoryBean.class.cast(endpoint.get(JAXRSServiceFactoryBean.class.getName()));
        final String base = sd.getEndpointInfo().getAddress();

        if (sfb != null) {
            final List<Logs.LogResourceEndpointInfo> resourcesToLog = new ArrayList<>();
            int classSize = 0;
            int addressSize = 0;

            final List<ClassResourceInfo> resources = sfb.getClassResourceInfo();
            for (final ClassResourceInfo info : resources) {
                if (info.getResourceClass() == null) { // possible?
                    continue;
                }

                final String address = Logs.singleSlash(base, info.getURITemplate().getValue());

                final String clazz = uproxyName(info.getResourceClass().getName());
                classSize = Math.max(classSize, clazz.length());
                addressSize = Math.max(addressSize, address.length());

                int methodSize = 7;
                int methodStrSize = 0;

                final List<Logs.LogOperationEndpointInfo> toLog = new ArrayList<>();

                final MethodDispatcher md = info.getMethodDispatcher();
                for (final OperationResourceInfo ori : md.getOperationResourceInfos()) {
                    final String httpMethod = ori.getHttpMethod();
                    final String currentAddress = Logs.singleSlash(address, ori.getURITemplate().getValue());
                    final String methodToStr = Logs.toSimpleString(ori.getMethodToInvoke());
                    toLog.add(new Logs.LogOperationEndpointInfo(httpMethod, currentAddress, methodToStr));

                    if (httpMethod != null) {
                        methodSize = Math.max(methodSize, httpMethod.length());
                    }
                    addressSize = Math.max(addressSize, currentAddress.length());
                    methodStrSize = Math.max(methodStrSize, methodToStr.length());
                }

                Collections.sort(toLog);

                resourcesToLog.add(new Logs.LogResourceEndpointInfo(address, clazz, toLog, methodSize, methodStrSize));
            }

            // effective logging
            log.info("REST Application: " + endpoint.getEndpointInfo().getAddress() + " -> "
                    + ofNullable(app).map(ApplicationInfo::getResourceClass).map(Class::getName).map(CxfCdiAutoSetup::uproxyName).orElse(""));

            Collections.sort(resourcesToLog);
            final int fClassSize = classSize;
            final int fAddressSize = addressSize;
            resourcesToLog.forEach(resource -> {
                log.info("     Service URI: "
                        + Logs.forceLength(resource.address, fAddressSize, true) + " -> "
                        + Logs.forceLength(resource.classname, fClassSize, true));

                resource.operations.forEach(info -> {
                    log.info("          "
                            + Logs.forceLength(info.http, resource.methodSize, false) + " "
                            + Logs.forceLength(info.address, fAddressSize, true) + " ->      "
                            + Logs.forceLength(info.method, resource.methodStrSize, true));
                });
                resource.operations.clear();
            });
            resourcesToLog.clear();

            // log @Providers
            if (Configuration.class.cast(sc.getServletContext().getAttribute("meecrowave.configuration")).isJaxrsLogProviders()) {
                final ServerProviderFactory spf = ServerProviderFactory.class.cast(endpoint.get(ServerProviderFactory.class.getName()));
                dump(log, spf, "MessageBodyReaders", "messageReaders");
                dump(log, spf, "MessageBodyWriters", "messageWriters");
            }
        } else {
            final EndpointInfo endpointInfo = endpoint.getEndpointInfo();
            if (endpointInfo.getName() != null) {
                log.info("@WebService > " + endpointInfo.getName().toString() + " -> " + base);
            }
        }

        return base;
    };
}
 
Example 5
Source File: Swagger2Feature.java    From cxf with Apache License 2.0 4 votes vote down vote up
@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());
    final ApplicationInfo appInfo = DefaultApplicationFactory.createApplicationInfoOrDefault(server,
            factory, sfb, bus, isScan());

    List<Object> swaggerResources = new LinkedList<>();

    if (customizer == null) {
        customizer = new Swagger2Customizer();
    }
    ApiListingResource apiListingResource = new Swagger2ApiListingResource(customizer);
    swaggerResources.add(apiListingResource);

    List<Object> providers = new ArrayList<>();
    providers.add(new SwaggerSerializers());

    if (isRunAsFilter()) {
        providers.add(new SwaggerContainerRequestFilter(appInfo == null ? null : appInfo.getProvider(),
                customizer));
    }

    final Properties swaggerProps = getSwaggerProperties(propertiesLocation, 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);
}