Search in sources :

Example 21 with Application

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

the class CheckRestMethodArePublic method validate.

@Override
public void validate(final AppModule appModule) {
    // valid standalone classes
    final Collection<String> standAloneClasses = new ArrayList<>();
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    try {
        for (final EjbModule ejb : appModule.getEjbModules()) {
            Thread.currentThread().setContextClassLoader(ejb.getClassLoader());
            for (final EnterpriseBean bean : ejb.getEjbJar().getEnterpriseBeans()) {
                if (bean instanceof SessionBean && ((SessionBean) bean).isRestService()) {
                    standAloneClasses.add(bean.getEjbClass());
                    valid(ejb.getValidation(), ejb.getClassLoader(), bean.getEjbClass());
                }
            }
        }
        for (final WebModule web : appModule.getWebModules()) {
            Thread.currentThread().setContextClassLoader(web.getClassLoader());
            // build the list of classes to validate
            final Collection<String> classes = new ArrayList<>();
            classes.addAll(web.getRestClasses());
            classes.addAll(web.getEjbRestServices());
            for (final String app : web.getRestApplications()) {
                final Class<?> clazz;
                try {
                    clazz = web.getClassLoader().loadClass(app);
                } catch (final ClassNotFoundException e) {
                    // managed elsewhere, here we just check methods
                    continue;
                }
                final Application appInstance;
                try {
                    appInstance = (Application) clazz.newInstance();
                } catch (final Exception e) {
                    // managed elsewhere
                    continue;
                }
                try {
                    for (final Class<?> rsClass : appInstance.getClasses()) {
                        classes.add(rsClass.getName());
                    }
                /* don't do it or ensure you have cdi activated! + CXF will catch it later
                        for (final Object rsSingleton : appInstance.getSingletons()) {
                            classes.add(rsSingleton.getClass().getName());
                        }
                        */
                } catch (final RuntimeException npe) {
                    if (appInstance == null) {
                        throw npe;
                    }
                // if app relies on cdi it is null here
                }
            }
            // try to avoid to valid twice the same classes
            classes.removeIf(standAloneClasses::contains);
            // valid
            for (final String classname : classes) {
                valid(web.getValidation(), web.getClassLoader(), classname);
            }
            classes.clear();
        }
    } finally {
        Thread.currentThread().setContextClassLoader(loader);
    }
    standAloneClasses.clear();
}
Also used : EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) ArrayList(java.util.ArrayList) EjbModule(org.apache.openejb.config.EjbModule) WebModule(org.apache.openejb.config.WebModule) SessionBean(org.apache.openejb.jee.SessionBean) Application(javax.ws.rs.core.Application)

Example 22 with Application

use of javax.ws.rs.core.Application in project tomee 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 23 with Application

use of javax.ws.rs.core.Application in project tomee 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 24 with Application

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

the class CxfRsHttpListener method injectApplication.

/**
 * JAX-RS allows for the Application subclass to have @Context injectable fields, as is
 * the case for Resources and Providers.  CXF will do the injection on the Application
 * instance when ApplicationInfo is constructed passing in the Application instance.
 *
 * We don't actually need the ApplicationInfo, we just need the side effect of calling
 * the constructor, which is all the @Context injections will be done.  Afterwards, we
 * can throw the ApplicationInfo away.
 *
 * This is verified in test:
 * com/sun/ts/tests/jaxrs/spec/context/server/JAXRSClient#applicationInjectionTest_from_standalone
 */
public static void injectApplication(final Application application, final JAXRSServerFactoryBean factory) {
    if (application == null) {
        return;
    }
    /*
         * We may have wrapped the Application instance in an InternalApplication.  If so, unwrap
         * it and do the injection on that instance.
         */
    if (application instanceof InternalApplication) {
        final InternalApplication internalApplication = (InternalApplication) application;
        final Application original = internalApplication.getOriginal();
        injectApplication(original, factory);
        return;
    }
    final Bus bus = factory.getBus();
    new ApplicationInfo(application, bus);
}
Also used : Bus(org.apache.cxf.Bus) InternalApplication(org.apache.openejb.server.rest.InternalApplication) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) InternalApplication(org.apache.openejb.server.rest.InternalApplication) Application(javax.ws.rs.core.Application)

Example 25 with Application

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

the class CxfRsHttpListener method isCXFResource.

public boolean isCXFResource(final HttpServletRequest request) {
    try {
        Application application = findApplication();
        if (!applicationProvidesResources(application)) {
            JAXRSServiceImpl service = (JAXRSServiceImpl) server.getEndpoint().getService();
            if (service == null) {
                return false;
            }
            String pathToMatch = HttpUtils.getPathToMatch(request.getServletPath(), pattern, true);
            final List<ClassResourceInfo> resources = service.getClassResourceInfos();
            for (final ClassResourceInfo info : resources) {
                if (info.getResourceClass() == null || info.getURITemplate() == null) {
                    // possible?
                    continue;
                }
                final MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>();
                if (info.getURITemplate().match(pathToMatch, parameters)) {
                    return true;
                }
            }
        } else {
            return true;
        }
    } catch (final Exception e) {
        LOGGER.info("No JAX-RS service");
    }
    return false;
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) InternalApplication(org.apache.openejb.server.rest.InternalApplication) Application(javax.ws.rs.core.Application) ResponseConstraintViolationException(org.apache.cxf.validation.ResponseConstraintViolationException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) EndpointException(org.apache.cxf.endpoint.EndpointException) ServletException(javax.servlet.ServletException) BusException(org.apache.cxf.BusException) InjectionException(javax.enterprise.inject.InjectionException)

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