Search in sources :

Example 41 with Application

use of javax.ws.rs.core.Application in project com-liferay-apio-architect by liferay.

the class ApioApplicationRegistrar method activate.

@Activate
public void activate(BundleContext bundleContext) {
    Application application = bundleContext.getService(_serviceReference);
    Dictionary<String, Object> properties = new Hashtable<>();
    String[] propertyKeys = _serviceReference.getPropertyKeys();
    for (String key : propertyKeys) {
        Object value = _serviceReference.getProperty(key);
        properties.put(key, value);
    }
    properties.put("osgi.jaxrs.application.base", "/");
    properties.put("osgi.jaxrs.name", ".default");
    _serviceRegistration = bundleContext.registerService(Application.class, application, properties);
    PersonModel.compute();
    BlogPostingModel.compute();
    BlogPostingCommentModel.compute();
}
Also used : Hashtable(java.util.Hashtable) Application(javax.ws.rs.core.Application) Activate(org.osgi.service.component.annotations.Activate)

Example 42 with Application

use of javax.ws.rs.core.Application in project Payara by payara.

the class OpenApiContext method generateResourceMapping.

/**
 * Generates a map listing the location each resource class is mapped to.
 */
private Map<String, Set<Type>> generateResourceMapping() {
    Set<Type> classList = new HashSet<>();
    Map<String, Set<Type>> mapping = new HashMap<>();
    for (Type type : allowedTypes) {
        if (type instanceof ClassModel) {
            ClassModel classModel = (ClassModel) type;
            if (classModel.getAnnotation(ApplicationPath.class.getName()) != null) {
                // Produce the mapping
                AnnotationModel annotation = classModel.getAnnotation(ApplicationPath.class.getName());
                String key = annotation.getValue("value", String.class);
                Set<Type> resourceClasses = new HashSet<>();
                mapping.put(key, resourceClasses);
                try {
                    Class<?> clazz = appClassLoader.loadClass(classModel.getName());
                    Application app = (Application) clazz.newInstance();
                    // Add all classes contained in the application
                    resourceClasses.addAll(app.getClasses().stream().map(Class::getName).filter(// Remove all Jersey providers
                    name -> !name.startsWith("org.glassfish.jersey")).map(allTypes::getBy).filter(Objects::nonNull).collect(toSet()));
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
                    LOGGER.log(WARNING, "Unable to initialise application class.", ex);
                }
            } else {
                classList.add(classModel);
            }
        }
    }
    // If there is one application and it's empty, add all classes
    if (mapping.keySet().size() == 1) {
        Set<Type> classes = mapping.values().iterator().next();
        if (classes.isEmpty()) {
            classes.addAll(classList);
        }
    }
    // If there is no application, add all classes to the context root.
    if (mapping.isEmpty()) {
        mapping.put("/", classList);
    }
    return mapping;
}
Also used : HashSet(java.util.HashSet) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ApplicationPath(javax.ws.rs.ApplicationPath) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType) Type(org.glassfish.hk2.classmodel.reflect.Type) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) Objects(java.util.Objects) Application(javax.ws.rs.core.Application) HashSet(java.util.HashSet)

Example 43 with Application

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

the class MPJWTInitializer method onStartup.

@Override
public void onStartup(final Set<Class<?>> classes, final ServletContext ctx) throws ServletException {
    if (classes == null || classes.isEmpty()) {
        // no class having @LoginConfig on it
        return;
    }
    for (Class<?> clazz : classes) {
        final LoginConfig loginConfig = clazz.getAnnotation(LoginConfig.class);
        if (loginConfig.authMethod() == null && !"MP-JWT".equals(loginConfig.authMethod())) {
            continue;
        }
        if (!Application.class.isAssignableFrom(clazz)) {
            continue;
        // do we really want Application?
        // See https://github.com/eclipse/microprofile-jwt-auth/issues/70 to clarify this point
        }
        final FilterRegistration.Dynamic mpJwtFilter = ctx.addFilter("mp-jwt-filter", MPJWTFilter.class);
        mpJwtFilter.setAsyncSupported(true);
        mpJwtFilter.addMappingForUrlPatterns(null, false, "/*");
        // no need to add it more than once
        break;
    }
}
Also used : LoginConfig(org.eclipse.microprofile.auth.LoginConfig) Application(javax.ws.rs.core.Application) FilterRegistration(javax.servlet.FilterRegistration)

Example 44 with Application

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

the class JAXRSServerFactoryBean method injectContexts.

protected void injectContexts(ServerProviderFactory factory, ApplicationInfo fallback) {
    // Sometimes the application provider (ApplicationInfo) is injected through
    // the endpoint, not JAXRSServerFactoryBean (like for example OpenApiFeature
    // or Swagger2Feature do). As such, without consulting the endpoint, the injection
    // may not work properly.
    final ApplicationInfo appInfoProvider = (appProvider == null) ? fallback : appProvider;
    final Application application = appInfoProvider == null ? null : appInfoProvider.getProvider();
    for (ClassResourceInfo cri : serviceFactory.getClassResourceInfo()) {
        if (cri.isSingleton()) {
            InjectionUtils.injectContextProxiesAndApplication(cri, cri.getResourceProvider().getInstance(null), application, factory);
        }
    }
    if (application != null) {
        InjectionUtils.injectContextProxiesAndApplication(appInfoProvider, application, null, null);
    }
}
Also used : ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Application(javax.ws.rs.core.Application)

Example 45 with Application

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

the class CXFNonSpringJaxrsServlet method createServerFromApplication.

protected void createServerFromApplication(String applicationNames, ServletConfig servletConfig) throws ServletException {
    boolean ignoreApplicationPath = isIgnoreApplicationPath(servletConfig);
    String[] classNames = applicationNames.split(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);
        List<?> providers = getProviders(servletConfig, splitChar);
        bean.setProviders(providers);
        List<? extends Feature> features = getFeatures(servletConfig, splitChar);
        bean.getFeatures().addAll(features);
        bean.setBus(getBus());
        bean.setApplicationInfo(providerApp);
        bean.create();
    }
}
Also used : ServletException(javax.servlet.ServletException) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) Application(javax.ws.rs.core.Application)

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