Search in sources :

Example 1 with ApplicationPath

use of javax.ws.rs.ApplicationPath in project jersey by jersey.

the class JerseyServletContainerInitializer method addServletWithExistingRegistration.

/**
     * Enhance existing servlet configuration.
     */
private static void addServletWithExistingRegistration(final ServletContext context, ServletRegistration registration, final Class<? extends Application> clazz, final Set<Class<?>> classes) throws ServletException {
    // create a new servlet container for a given app.
    final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, classes).addProperties(getInitParams(registration)).addProperties(Utils.getContextParams(context));
    if (registration.getClassName() != null) {
        // class name present - complete servlet registration from container point of view
        Utils.store(resourceConfig, context, registration.getName());
    } else {
        // no class name - no complete servlet registration from container point of view
        final ServletContainer servlet = new ServletContainer(resourceConfig);
        final ServletRegistration.Dynamic dynamicRegistration = context.addServlet(clazz.getName(), servlet);
        dynamicRegistration.setAsyncSupported(true);
        dynamicRegistration.setLoadOnStartup(1);
        registration = dynamicRegistration;
    }
    if (registration.getMappings().isEmpty()) {
        final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class);
        if (ap != null) {
            final String mapping = createMappingPath(ap);
            if (!mappingExists(context, mapping)) {
                registration.addMapping(mapping);
                LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping));
            } else {
                LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping));
            }
        } else {
            // Error
            LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_NO_MAPPING_OR_ANNOTATION(clazz.getName(), ApplicationPath.class.getSimpleName()));
        }
    } else {
        LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_APPLICATION(clazz.getName()));
    }
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ApplicationPath(javax.ws.rs.ApplicationPath)

Example 2 with ApplicationPath

use of javax.ws.rs.ApplicationPath in project jersey by jersey.

the class JerseyServletContainerInitializer method addServletWithApplication.

/**
     * Add new servlet according to {@link Application} subclass with {@link ApplicationPath} annotation or existing
     * {@code servlet-mapping}.
     */
private static void addServletWithApplication(final ServletContext context, final Class<? extends Application> clazz, final Set<Class<?>> defaultClasses) throws ServletException {
    final ApplicationPath ap = clazz.getAnnotation(ApplicationPath.class);
    if (ap != null) {
        // App is annotated with ApplicationPath
        final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(clazz, defaultClasses).addProperties(Utils.getContextParams(context));
        final ServletContainer s = new ServletContainer(resourceConfig);
        final ServletRegistration.Dynamic dsr = context.addServlet(clazz.getName(), s);
        dsr.setAsyncSupported(true);
        dsr.setLoadOnStartup(1);
        final String mapping = createMappingPath(ap);
        if (!mappingExists(context, mapping)) {
            dsr.addMapping(mapping);
            LOGGER.log(Level.CONFIG, LocalizationMessages.JERSEY_APP_REGISTERED_MAPPING(clazz.getName(), mapping));
        } else {
            LOGGER.log(Level.WARNING, LocalizationMessages.JERSEY_APP_MAPPING_CONFLICT(clazz.getName(), mapping));
        }
    }
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ApplicationPath(javax.ws.rs.ApplicationPath)

Example 3 with ApplicationPath

use of javax.ws.rs.ApplicationPath in project typescript-generator by vojtechhabarta.

the class JaxrsApplicationParser method tryParse.

public Result tryParse(SourceType<?> sourceType) {
    if (!(sourceType.type instanceof Class<?>)) {
        return null;
    }
    final Class<?> cls = (Class<?>) sourceType.type;
    // application
    if (Application.class.isAssignableFrom(cls)) {
        final ApplicationPath applicationPathAnnotation = cls.getAnnotation(ApplicationPath.class);
        if (applicationPathAnnotation != null) {
            model.setApplicationPath(applicationPathAnnotation.value());
        }
        model.setApplicationName(cls.getSimpleName());
        final List<SourceType<Type>> discoveredTypes = JaxrsApplicationScanner.scanJaxrsApplication(cls, isClassNameExcluded);
        return new Result(discoveredTypes);
    }
    // resource
    final Path path = cls.getAnnotation(Path.class);
    if (path != null) {
        System.out.println("Parsing JAX-RS resource: " + cls.getName());
        final Result result = new Result();
        parseResource(result, new ResourceContext(cls, path.value()), cls);
        return result;
    }
    return null;
}
Also used : Path(javax.ws.rs.Path) ApplicationPath(javax.ws.rs.ApplicationPath) ApplicationPath(javax.ws.rs.ApplicationPath)

Example 4 with ApplicationPath

use of javax.ws.rs.ApplicationPath in project tomee by apache.

the class ResourceUtils method createApplication.

@SuppressWarnings("unchecked")
public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath, boolean staticSubresourceResolution, boolean useSingletonResourceProvider, Bus bus) {
    Set<Object> singletons = app.getSingletons();
    verifySingletons(singletons);
    List<Class<?>> resourceClasses = new ArrayList<>();
    List<Object> providers = new ArrayList<>();
    List<Feature> features = new ArrayList<>();
    Map<Class<?>, ResourceProvider> map = new HashMap<>();
    // or singleton provider classes
    for (Class<?> cls : app.getClasses()) {
        if (isValidApplicationClass(cls, singletons)) {
            if (isValidProvider(cls)) {
                providers.add(createProviderInstance(cls));
            } else if (Feature.class.isAssignableFrom(cls)) {
                features.add(createFeatureInstance((Class<? extends Feature>) cls));
            } else {
                resourceClasses.add(cls);
                if (useSingletonResourceProvider) {
                    map.put(cls, new SingletonResourceProvider(createProviderInstance(cls)));
                } else {
                    map.put(cls, new PerRequestResourceProvider(cls));
                }
            }
        }
    }
    // we can get either a provider or resource class here
    for (Object o : singletons) {
        if (isValidProvider(o.getClass())) {
            providers.add(o);
        } else if (o instanceof Feature) {
            features.add((Feature) o);
        } else {
            resourceClasses.add(o.getClass());
            map.put(o.getClass(), new SingletonResourceProvider(o));
        }
    }
    JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
    if (bus != null) {
        bean.setBus(bus);
    }
    String address = "/";
    if (!ignoreAppPath) {
        ApplicationPath appPath = locateApplicationPath(app.getClass());
        if (appPath != null) {
            address = appPath.value();
        }
    }
    if (!address.startsWith("/")) {
        address = "/" + address;
    }
    bean.setAddress(address);
    bean.setStaticSubresourceResolution(staticSubresourceResolution);
    bean.setResourceClasses(resourceClasses);
    bean.setProviders(providers);
    bean.getFeatures().addAll(features);
    for (Map.Entry<Class<?>, ResourceProvider> entry : map.entrySet()) {
        bean.setResourceProvider(entry.getKey(), entry.getValue());
    }
    Map<String, Object> appProps = app.getProperties();
    if (appProps != null) {
        bean.getProperties(true).putAll(appProps);
    }
    bean.setApplication(app);
    return bean;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ApplicationPath(javax.ws.rs.ApplicationPath) Feature(org.apache.cxf.feature.Feature) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ElementClass(org.apache.cxf.jaxrs.ext.xml.ElementClass) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 5 with ApplicationPath

use of javax.ws.rs.ApplicationPath in project tomee by apache.

the class RESTService method appPrefix.

private static String appPrefix(final WebAppInfo info, final Class<?> appClazz) {
    StringBuilder builder = null;
    // no annotation, try servlets
    for (final ServletInfo s : info.servlets) {
        if (s.mappings.isEmpty()) {
            continue;
        }
        String mapping = null;
        final String name = appClazz.getName();
        if (name.equals(s.servletClass) || name.equals(s.servletName) || "javax.ws.rs.core.Application ".equals(s.servletName)) {
            mapping = s.mappings.iterator().next();
        } else {
            for (final ParamValueInfo pvi : s.initParams) {
                if ("javax.ws.rs.Application".equals(pvi.name) || Application.class.getName().equals(pvi.name)) {
                    mapping = s.mappings.iterator().next();
                    break;
                }
            }
        }
        if (mapping != null) {
            if (mapping.endsWith("/*")) {
                mapping = mapping.substring(0, mapping.length() - 2);
            }
            if (mapping.startsWith("/")) {
                mapping = mapping.substring(1);
            }
            builder = new StringBuilder();
            builder.append(mapping);
            break;
        }
    }
    if (builder != null) {
        // https://issues.apache.org/jira/browse/CXF-5702
        return builder.toString();
    }
    // annotation
    final ApplicationPath path = appClazz.getAnnotation(ApplicationPath.class);
    if (path != null) {
        String appPath = path.value();
        /*
             * Percent encoded values are allowed in the value, an implementation will recognize
             * such values and will not double encode the '%' character.  As such we need to
             * decode the value now so that we hand it to CXF in raw, not url-safe, form.  CXF
             * will then encode it to make it url-safe.  If we give CXF the encoded value it will
             * still encode it and it will be encoded twice, which we do not want.
             *
             * Verified by
             * com.sun.ts.tests.jaxrs.servlet3.rs.applicationpath.JAXRSClient#applicationPathAnnotationEncodedTest_from_standalone
             */
        try {
            appPath = URLDecoder.decode(appPath, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new UncheckedIOException(e);
        }
        if (appPath.endsWith("*")) {
            appPath = appPath.substring(0, appPath.length() - 1);
        }
        builder = new StringBuilder();
        if (appPath.startsWith("/")) {
            builder.append(appPath.substring(1));
        } else {
            builder.append(appPath);
        }
    }
    if (builder == null) {
        return null;
    }
    return builder.toString();
}
Also used : ServletInfo(org.apache.openejb.assembler.classic.ServletInfo) ParamValueInfo(org.apache.openejb.assembler.classic.ParamValueInfo) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UncheckedIOException(java.io.UncheckedIOException) ApplicationPath(javax.ws.rs.ApplicationPath)

Aggregations

ApplicationPath (javax.ws.rs.ApplicationPath)13 HashMap (java.util.HashMap)6 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)3 Application (javax.ws.rs.core.Application)3 Parameter (io.swagger.v3.oas.models.parameters.Parameter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 List (java.util.List)2 Set (java.util.Set)2 ServletException (javax.servlet.ServletException)2 ServletRegistration (javax.servlet.ServletRegistration)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 Feature (org.apache.cxf.feature.Feature)2 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)2 ElementClass (org.apache.cxf.jaxrs.ext.xml.ElementClass)2 PerRequestResourceProvider (org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider)2 ResourceProvider (org.apache.cxf.jaxrs.lifecycle.ResourceProvider)2 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)2 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)2