Search in sources :

Example 6 with ApplicationPath

use of javax.ws.rs.ApplicationPath in project japid42 by branaway.

the class JaxrsRouter method init.

public static void init(Application app, play.GlobalSettings g) {
    parentClassloader = app.classloader();
    classes = RouterUtils.classes(parentClassloader);
    global = g;
    ApplicationPath appPathAnno = global.getClass().getAnnotation(ApplicationPath.class);
    if (appPathAnno != null)
        appPath = appPathAnno.value();
    routerClasses = parseRouterClasses(classes);
    String string = app.configuration().getString(ASSET_SERVING);
    if (string != null)
        assetServing = string.split(",");
}
Also used : ApplicationPath(javax.ws.rs.ApplicationPath)

Example 7 with ApplicationPath

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

the class OpenApiCustomizer method customize.

public OpenAPIConfiguration customize(final OpenAPIConfiguration configuration) {
    if (configuration == null) {
        return configuration;
    }
    if (dynamicBasePath) {
        final MessageContext ctx = createMessageContext();
        // If the JAX-RS application with custom path is defined, it might be present twice, in the
        // request URI as well as in each resource operation URI. To properly represent server URL,
        // the application path should be removed from it.
        final String url = StringUtils.removeEnd(StringUtils.substringBeforeLast(ctx.getUriInfo().getRequestUri().toString(), "/"), applicationPath);
        final Collection<Server> servers = configuration.getOpenAPI().getServers();
        if (servers == null || servers.stream().noneMatch(s -> s.getUrl().equalsIgnoreCase(url))) {
            configuration.getOpenAPI().setServers(Collections.singletonList(new Server().url(url)));
        }
    }
    return configuration;
}
Also used : Reader(io.swagger.v3.jaxrs2.Reader) URL(java.net.URL) Parameter(io.swagger.v3.oas.models.parameters.Parameter) OpenAPIConfiguration(io.swagger.v3.oas.integration.api.OpenAPIConfiguration) HashMap(java.util.HashMap) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) JAXRSUtils(org.apache.cxf.jaxrs.utils.JAXRSUtils) Operation(io.swagger.v3.oas.models.Operation) StringUtils(org.apache.commons.lang3.StringUtils) JavaDocProvider(org.apache.cxf.jaxrs.model.doc.JavaDocProvider) ArrayList(java.util.ArrayList) Pair(org.apache.commons.lang3.tuple.Pair) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) ResourceUtils(org.apache.cxf.jaxrs.utils.ResourceUtils) Tag(io.swagger.v3.oas.models.tags.Tag) DocumentationProvider(org.apache.cxf.jaxrs.model.doc.DocumentationProvider) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Collection(java.util.Collection) ApplicationPath(javax.ws.rs.ApplicationPath) Objects(java.util.Objects) List(java.util.List) Server(io.swagger.v3.oas.models.servers.Server) Optional(java.util.Optional) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Collections(java.util.Collections) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) Server(io.swagger.v3.oas.models.servers.Server) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext)

Example 8 with ApplicationPath

use of javax.ws.rs.ApplicationPath in project cxf 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 9 with ApplicationPath

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

the class OpenApiCustomizer method setApplicationInfo.

public void setApplicationInfo(ApplicationInfo application) {
    if (application != null && application.getProvider() != null) {
        final Class<?> clazz = application.getProvider().getClass();
        final ApplicationPath path = ResourceUtils.locateApplicationPath(clazz);
        if (path != null) {
            applicationPath = path.value();
            if (!applicationPath.startsWith("/")) {
                applicationPath = "/" + applicationPath;
            }
            if (applicationPath.endsWith("/")) {
                applicationPath = applicationPath.substring(0, applicationPath.lastIndexOf('/'));
            }
        }
    }
}
Also used : ApplicationPath(javax.ws.rs.ApplicationPath)

Example 10 with ApplicationPath

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

the class JaxrsServletContainerInitializer method onStartup.

@Override
public void onStartup(final Set<Class<?>> classes, final ServletContext ctx) throws ServletException {
    Application app = null;
    String servletName = null;
    String servletMapping = null;
    final Class<?> appClass = findCandidate(classes);
    if (appClass != null) {
        // Custom servlets using non-standard mechanisms to create Application will not be detected
        if (isApplicationServletAvailable(ctx, appClass)) {
            return;
        }
        try {
            app = (Application) appClass.newInstance();
        } catch (Throwable t) {
            throw new ServletException(t);
        }
        // Servlet name is the application class name
        servletName = appClass.getName();
        ApplicationPath appPath = ResourceUtils.locateApplicationPath(appClass);
        // a servlet registration with an application implementation class name
        if (appPath != null) {
            servletMapping = appPath.value() + "/*";
        } else {
            servletMapping = getServletMapping(ctx, servletName);
        }
    }
    // resource and provider classes
    if (app == null || (app.getClasses().isEmpty() && app.getSingletons().isEmpty())) {
        // Custom servlets using non-standard mechanisms to create Application will not be detected
        if (isCxfServletAvailable(ctx)) {
            return;
        }
        final Map<Class<? extends Annotation>, Collection<Class<?>>> providersAndResources = groupByAnnotations(classes);
        if (!providersAndResources.get(Path.class).isEmpty() || !providersAndResources.get(Provider.class).isEmpty()) {
            if (app == null) {
                // Servlet name is a JAX-RS Application class name
                servletName = JAXRS_APPLICATION_SERVLET_NAME;
                // Servlet mapping is obtained from a servlet registration
                // with a JAX-RS Application class name
                servletMapping = getServletMapping(ctx, servletName);
            }
            final Map<String, Object> appProperties = app != null ? app.getProperties() : Collections.emptyMap();
            app = new Application() {

                @Override
                public Set<Class<?>> getClasses() {
                    Set<Class<?>> set = new HashSet<>();
                    set.addAll(providersAndResources.get(Path.class));
                    set.addAll(providersAndResources.get(Provider.class));
                    return set;
                }

                @Override
                public Map<String, Object> getProperties() {
                    return appProperties;
                }
            };
        }
    }
    if (app == null) {
        return;
    }
    CXFNonSpringJaxrsServlet cxfServlet = new CXFNonSpringJaxrsServlet(app);
    final Dynamic servlet = ctx.addServlet(servletName, cxfServlet);
    servlet.addMapping(servletMapping);
}
Also used : Path(javax.ws.rs.Path) ApplicationPath(javax.ws.rs.ApplicationPath) Dynamic(javax.servlet.ServletRegistration.Dynamic) Set(java.util.Set) HashSet(java.util.HashSet) ApplicationPath(javax.ws.rs.ApplicationPath) Annotation(java.lang.annotation.Annotation) Provider(javax.ws.rs.ext.Provider) CXFNonSpringJaxrsServlet(org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet) ServletException(javax.servlet.ServletException) Collection(java.util.Collection) Application(javax.ws.rs.core.Application) HashMap(java.util.HashMap) Map(java.util.Map)

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