Java Code Examples for javax.ws.rs.ApplicationPath#value()

The following examples show how to use javax.ws.rs.ApplicationPath#value() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenApiCustomizer.java    From cxf with Apache License 2.0 6 votes vote down vote up
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('/'));
            }
        }
    }
}
 
Example 2
Source File: ApplicationPathFilter.java    From jrestless with Apache License 2.0 5 votes vote down vote up
private static String getApplicationPath(Application applicationConfig) {
	if (applicationConfig == null) {
		return null;
	}
	ApplicationPath ap = applicationConfig.getClass().getAnnotation(ApplicationPath.class);
	if (ap == null) {
		return null;
	}
	String applicationPath = ap.value();
	if (isBlank(applicationPath)) {
		return null;
	}
	while (applicationPath.startsWith("/")) {
		applicationPath = applicationPath.substring(1);
	}
	// support Servlet configs
	if (applicationPath.endsWith("/*")) {
		applicationPath = applicationPath.substring(0, applicationPath.length() - 2);
	}
	while (applicationPath.endsWith("/")) {
		applicationPath = applicationPath.substring(0, applicationPath.length() - 1);
	}
	if (isBlank(applicationPath)) {
		return null;
	}
	return applicationPath;
}
 
Example 3
Source File: JerseyContainerConfigurator.java    From hammock with Apache License 2.0 5 votes vote down vote up
@Produces
public ServletDescriptor jerseyServlet() {
    String urlPattern = restServerConfiguration.getRestServletMapping();
    if (!applicationInstance.isUnsatisfied() && !applicationInstance.isAmbiguous()) {
        ApplicationPath annotation = ClassUtils.getAnnotation(applicationInstance.get().getClass(), ApplicationPath.class);
        if(annotation != null) {
            String path = annotation.value();
            urlPattern = path.endsWith("/") ? path + "*" : path + "/*";
        }
    }
    return new ServletDescriptor(SERVLET_NAME, null, new String[] { urlPattern }, 1, null, true, ServletContainer.class);
}
 
Example 4
Source File: ResteasyServletContextAttributeProvider.java    From hammock with Apache License 2.0 5 votes vote down vote up
@Produces
public ServletDescriptor resteasyServlet() {
    String path = restServerConfiguration.getRestServerUri();
    if( !(applicationInstance.isUnsatisfied() || applicationInstance.isAmbiguous())) {
        ApplicationPath appPath = ClassUtils.getAnnotation(applicationInstance.get().getClass(), ApplicationPath.class);
        if(appPath != null) {
            path = appPath.value();
        }
    }
    String pattern = path.endsWith("/") ? path + "*" : path + "/*";
    WebInitParam param = new WebParam("resteasy.servlet.mapping.prefix", path);
    return new ServletDescriptor("ResteasyServlet",new String[]{pattern}, new String[]{pattern},
            1,new WebInitParam[]{param},true,HttpServlet30Dispatcher.class);
}
 
Example 5
Source File: RestClientBuilder.java    From microshed-testing with Apache License 2.0 4 votes vote down vote up
private static String locateApplicationPath(Class<?> clazz) {
    String resourcePackage = clazz.getPackage().getName();

    // If the rest client directly extends Application, look for ApplicationPath on it
    if (AnnotationSupport.isAnnotated(clazz, ApplicationPath.class))
        return AnnotationSupport.findAnnotation(clazz, ApplicationPath.class).get().value();

    // First check for a javax.ws.rs.core.Application in the same package as the resource
    List<Class<?>> appClasses = ReflectionSupport.findAllClassesInPackage(resourcePackage,
                                                                          c -> Application.class.isAssignableFrom(c) &&
                                                                               AnnotationSupport.isAnnotated(c, ApplicationPath.class),
                                                                          n -> true);
    if (appClasses.size() == 0) {
        LOG.debug("no classes implementing Application found in pkg: " + resourcePackage);
        // If not found, check under the 3rd package, so com.foo.bar.*
        // Classpath scanning can be expensive, so we jump straight to the 3rd package from root instead
        // of recursing up one package at a time and scanning the entire CP for each step
        String[] pkgs = resourcePackage.split("\\.");
        if (pkgs.length > 3) {
            String checkPkg = pkgs[0] + '.' + pkgs[1] + '.' + pkgs[2];
            LOG.debug("checking in pkg: " + checkPkg);
            appClasses = ReflectionSupport.findAllClassesInPackage(checkPkg,
                                                                   c -> Application.class.isAssignableFrom(c) &&
                                                                        AnnotationSupport.isAnnotated(c, ApplicationPath.class),
                                                                   n -> true);
        }
    }

    if (appClasses.size() == 0) {
        LOG.info("No classes implementing 'javax.ws.rs.core.Application' found on classpath to set as context root for " + clazz +
                 ". Defaulting context root to '/'");
        return "";
    }

    Class<?> selectedClass = appClasses.stream()
                    .sorted((c1, c2) -> c1.getName().compareTo(c2.getName()))
                    .findFirst()
                    .get();
    ApplicationPath appPath = AnnotationSupport.findAnnotation(selectedClass, ApplicationPath.class).get();
    if (appClasses.size() > 1) {
        LOG.warn("Found multiple classes implementing 'javax.ws.rs.core.Application' on classpath: " + appClasses +
                 ". Setting context root to the first class discovered (" + selectedClass.getCanonicalName() + ") with path: " +
                 appPath.value());
    }
    LOG.debug("Using ApplicationPath of '" + appPath.value() + "'");
    return appPath.value();
}
 
Example 6
Source File: ResourceUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
@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<>();

    // Note, app.getClasses() returns a list of per-request classes
    // 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;
}
 
Example 7
Source File: RESTService.java    From tomee with Apache License 2.0 4 votes vote down vote up
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();
        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();
}