Search in sources :

Example 61 with Application

use of javax.ws.rs.core.Application in project wildfly by wildfly.

the class JaxrsScanningProcessor method scan.

protected void scan(final DeploymentUnit du, final ClassLoader classLoader, final ResteasyDeploymentData resteasyDeploymentData) throws DeploymentUnitProcessingException, ModuleLoadException {
    final CompositeIndex index = du.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
    if (!resteasyDeploymentData.shouldScan()) {
        return;
    }
    if (!resteasyDeploymentData.isDispatcherCreated()) {
        final Set<ClassInfo> applicationClasses = index.getAllKnownSubclasses(APPLICATION);
        try {
            for (ClassInfo c : applicationClasses) {
                if (Modifier.isAbstract(c.flags()))
                    continue;
                @SuppressWarnings("unchecked") Class<? extends Application> scanned = (Class<? extends Application>) classLoader.loadClass(c.name().toString());
                resteasyDeploymentData.getScannedApplicationClasses().add(scanned);
            }
        } catch (ClassNotFoundException e) {
            throw JaxrsLogger.JAXRS_LOGGER.cannotLoadApplicationClass(e);
        }
    }
    List<AnnotationInstance> resources = null;
    List<AnnotationInstance> providers = null;
    if (resteasyDeploymentData.isScanResources()) {
        resources = index.getAnnotations(JaxrsAnnotations.PATH.getDotName());
    }
    if (resteasyDeploymentData.isScanProviders()) {
        providers = index.getAnnotations(JaxrsAnnotations.PROVIDER.getDotName());
    }
    if ((resources == null || resources.isEmpty()) && (providers == null || providers.isEmpty()))
        return;
    final Set<ClassInfo> pathInterfaces = new HashSet<ClassInfo>();
    if (resources != null) {
        for (AnnotationInstance e : resources) {
            final ClassInfo info;
            if (e.target() instanceof ClassInfo) {
                info = (ClassInfo) e.target();
            } else if (e.target() instanceof MethodInfo) {
                // ignore
                continue;
            } else {
                JAXRS_LOGGER.classOrMethodAnnotationNotFound("@Path", e.target());
                continue;
            }
            if (info.name().toString().startsWith(ORG_APACHE_CXF)) {
                // see WFLY-9752
                continue;
            }
            if (info.annotations().containsKey(DECORATOR)) {
                // we can't pick up on programatically added decorators, but that is such an edge case it should not really matter
                continue;
            }
            if (!Modifier.isInterface(info.flags())) {
                resteasyDeploymentData.getScannedResourceClasses().add(info.name().toString());
            } else {
                pathInterfaces.add(info);
            }
        }
    }
    if (providers != null) {
        for (AnnotationInstance e : providers) {
            if (e.target() instanceof ClassInfo) {
                ClassInfo info = (ClassInfo) e.target();
                if (info.name().toString().startsWith(ORG_APACHE_CXF)) {
                    // see WFLY-9752
                    continue;
                }
                if (info.annotations().containsKey(DECORATOR)) {
                    // we can't pick up on programatically added decorators, but that is such an edge case it should not really matter
                    continue;
                }
                if (!Modifier.isInterface(info.flags())) {
                    resteasyDeploymentData.getScannedProviderClasses().add(info.name().toString());
                }
            } else {
                JAXRS_LOGGER.classAnnotationNotFound("@Provider", e.target());
            }
        }
    }
    // look for all implementations of interfaces annotated @Path
    for (final ClassInfo iface : pathInterfaces) {
        final Set<ClassInfo> implementors = index.getAllKnownImplementors(iface.name());
        for (final ClassInfo implementor : implementors) {
            if (implementor.name().toString().startsWith(ORG_APACHE_CXF)) {
                // see WFLY-9752
                continue;
            }
            if (implementor.annotations().containsKey(DECORATOR)) {
                // we can't pick up on programatically added decorators, but that is such an edge case it should not really matter
                continue;
            }
            resteasyDeploymentData.getScannedResourceClasses().add(implementor.name().toString());
        }
    }
}
Also used : CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) MethodInfo(org.jboss.jandex.MethodInfo) Application(javax.ws.rs.core.Application) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo) HashSet(java.util.HashSet)

Example 62 with Application

use of javax.ws.rs.core.Application in project swagger-core by swagger-api.

the class Reader method resolveApplicationPath.

protected String resolveApplicationPath() {
    if (application != null) {
        Class<?> applicationToScan = this.application.getClass();
        ApplicationPath applicationPath;
        // search up in the hierarchy until we find one with the annotation, this is needed because for example Weld proxies will not have the annotation and the right class will be the superClass
        while ((applicationPath = applicationToScan.getAnnotation(ApplicationPath.class)) == null && !applicationToScan.getSuperclass().equals(Application.class)) {
            applicationToScan = applicationToScan.getSuperclass();
        }
        if (applicationPath != null) {
            if (StringUtils.isNotBlank(applicationPath.value())) {
                return applicationPath.value();
            }
        }
        // look for inner application, e.g. ResourceConfig
        try {
            Application innerApp = application;
            Method m = application.getClass().getMethod("getApplication");
            while (m != null) {
                Application retrievedApp = (Application) m.invoke(innerApp);
                if (retrievedApp == null) {
                    break;
                }
                if (retrievedApp.getClass().equals(innerApp.getClass())) {
                    break;
                }
                innerApp = retrievedApp;
                applicationPath = innerApp.getClass().getAnnotation(ApplicationPath.class);
                if (applicationPath != null) {
                    if (StringUtils.isNotBlank(applicationPath.value())) {
                        return applicationPath.value();
                    }
                }
                m = innerApp.getClass().getMethod("getApplication");
            }
        } catch (NoSuchMethodException e) {
        // no inner application found
        } catch (Exception e) {
        // no inner application found
        }
    }
    return "";
}
Also used : Method(java.lang.reflect.Method) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) ApplicationPath(javax.ws.rs.ApplicationPath) Application(javax.ws.rs.core.Application)

Example 63 with Application

use of javax.ws.rs.core.Application in project swagger-core by swagger-api.

the class JaxrsApplicationAndAnnotationScannerTest method setUp.

@BeforeMethod
public void setUp() {
    scanner = new JaxrsApplicationAndAnnotationScanner();
    scanner.setApplication(new Application() {

        @Override
        public Set<Class<?>> getClasses() {
            return Collections.singleton(ResourceInApplication.class);
        }
    });
}
Also used : Set(java.util.Set) Application(javax.ws.rs.core.Application) BeforeMethod(org.testng.annotations.BeforeMethod)

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