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