Search in sources :

Example 1 with ResourceMappingContext

use of org.glassfish.jersey.linking.mapping.ResourceMappingContext in project jersey by jersey.

the class InjectLinkFieldDescriptor method getLinkTemplate.

/**
     * TODO javadoc.
     */
public static String getLinkTemplate(ResourceMappingContext rmc, InjectLink link) {
    String template = null;
    if (!link.resource().equals(Class.class)) {
        ResourceMappingContext.Mapping map = rmc.getMapping(link.resource());
        if (map != null) {
            template = map.getTemplate().getTemplate().toString();
        } else {
            // extract template from specified class' @Path annotation
            Path path = link.resource().getAnnotation(Path.class);
            template = path == null ? "" : path.value();
        }
        // extract template from specified class' @Path annotation
        if (link.method().length() > 0) {
            // append value of method's @Path annotation
            MethodList methods = new MethodList(link.resource());
            methods = methods.withMetaAnnotation(HttpMethod.class);
            Iterator<AnnotatedMethod> iterator = methods.iterator();
            while (iterator.hasNext()) {
                AnnotatedMethod method = iterator.next();
                if (!method.getMethod().getName().equals(link.method())) {
                    continue;
                }
                StringBuilder builder = new StringBuilder();
                builder.append(template);
                Path methodPath = method.getAnnotation(Path.class);
                if (methodPath != null) {
                    String methodTemplate = methodPath.value();
                    if (!(template.endsWith("/") || methodTemplate.startsWith("/"))) {
                        builder.append("/");
                    }
                    builder.append(methodTemplate);
                }
                CharSequence querySubString = extractQueryParams(method);
                if (querySubString.length() > 0) {
                    builder.append("{?");
                    builder.append(querySubString);
                    builder.append("}");
                }
                template = builder.toString();
                break;
            }
        }
    } else {
        template = link.value();
    }
    return template;
}
Also used : Path(javax.ws.rs.Path) ResourceMappingContext(org.glassfish.jersey.linking.mapping.ResourceMappingContext) AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod) MethodList(org.glassfish.jersey.server.model.MethodList) HttpMethod(javax.ws.rs.HttpMethod)

Aggregations

HttpMethod (javax.ws.rs.HttpMethod)1 Path (javax.ws.rs.Path)1 ResourceMappingContext (org.glassfish.jersey.linking.mapping.ResourceMappingContext)1 AnnotatedMethod (org.glassfish.jersey.server.model.AnnotatedMethod)1 MethodList (org.glassfish.jersey.server.model.MethodList)1