Search in sources :

Example 1 with RootResource

use of com.webcohesion.enunciate.modules.jaxrs.model.RootResource in project enunciate by stoicflame.

the class EnunciateJaxrsContext method add.

/**
 * Add a root resource to the model.
 *
 * @param rootResource The root resource to add to the model.
 */
public void add(RootResource rootResource) {
    if (rootResource.isInterface()) {
        // if the root resource is an interface, don't add it if its implementation has already been added (avoid duplication).
        for (RootResource resource : this.rootResources) {
            if (((DecoratedTypeMirror) (resource.asType())).isInstanceOf(rootResource)) {
                debug("%s was identified as a JAX-RS root resource, but will be ignored because root resource %s implements it.", rootResource.getQualifiedName(), resource.getQualifiedName());
                return;
            }
        }
    } else {
        // remove any interfaces of this root resource that have been identified as root resources (avoid duplication)
        DecoratedTypeMirror rootResourceType = (DecoratedTypeMirror) rootResource.asType();
        Iterator<RootResource> it = this.rootResources.iterator();
        while (it.hasNext()) {
            RootResource resource = it.next();
            if (resource.isInterface() && rootResourceType.isInstanceOf(resource)) {
                debug("%s was identified as a JAX-RS root resource, but will be ignored because root resource %s implements it.", resource.getQualifiedName(), rootResource.getQualifiedName());
                it.remove();
            }
        }
    }
    this.rootResources.add(rootResource);
    debug("Added %s as a JAX-RS root resource.", rootResource.getQualifiedName());
    if (getContext().getProcessingEnvironment().findSourcePosition(rootResource) == null) {
        OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.log(getContext());
        if (OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.getLogged() <= 3) {
            info("Unable to find source file for %s.", rootResource.getQualifiedName());
        } else {
            debug("Unable to find source file for %s.", rootResource.getQualifiedName());
        }
    }
}
Also used : RootResource(com.webcohesion.enunciate.modules.jaxrs.model.RootResource) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 2 with RootResource

use of com.webcohesion.enunciate.modules.jaxrs.model.RootResource in project enunciate by stoicflame.

the class EnunciateJaxrsContext method getResourceGroupsByPath.

public List<ResourceGroup> getResourceGroupsByPath(ApiRegistrationContext registrationContext) {
    Map<String, PathBasedResourceGroupImpl> resourcesByPath = new HashMap<String, PathBasedResourceGroupImpl>();
    FacetFilter facetFilter = registrationContext.getFacetFilter();
    for (RootResource rootResource : rootResources) {
        if (!facetFilter.accept(rootResource)) {
            continue;
        }
        for (ResourceMethod method : rootResource.getResourceMethods(true)) {
            if (facetFilter.accept(method)) {
                com.webcohesion.enunciate.metadata.rs.ServiceContextRoot context = method.getAnnotation(com.webcohesion.enunciate.metadata.rs.ServiceContextRoot.class);
                com.webcohesion.enunciate.modules.jaxrs.model.Resource resource = method.getParent();
                while (context == null && resource != null) {
                    context = resource.getAnnotation(com.webcohesion.enunciate.metadata.rs.ServiceContextRoot.class);
                    resource = resource.getParent();
                }
                String path = method.getFullpath();
                PathBasedResourceGroupImpl resourceGroup = resourcesByPath.get(path);
                if (resourceGroup == null) {
                    String contextPath = context != null ? JaxrsModule.sanitizeContextPath(context.value()) : this.relativeContextPath;
                    resourceGroup = new PathBasedResourceGroupImpl(contextPath, path, new ArrayList<Resource>());
                    resourcesByPath.put(path, resourceGroup);
                }
                resourceGroup.getResources().add(new ResourceImpl(method, resourceGroup, registrationContext));
            }
        }
    }
    ArrayList<ResourceGroup> resourceGroups = new ArrayList<ResourceGroup>(resourcesByPath.values());
    Collections.sort(resourceGroups, new ResourceGroupComparator(this.pathSortStrategy));
    return resourceGroups;
}
Also used : RootResource(com.webcohesion.enunciate.modules.jaxrs.model.RootResource) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) PathBasedResourceGroupImpl(com.webcohesion.enunciate.modules.jaxrs.api.impl.PathBasedResourceGroupImpl) ResourceImpl(com.webcohesion.enunciate.modules.jaxrs.api.impl.ResourceImpl) ResourceMethod(com.webcohesion.enunciate.modules.jaxrs.model.ResourceMethod) ResourceGroup(com.webcohesion.enunciate.api.resources.ResourceGroup)

Example 3 with RootResource

use of com.webcohesion.enunciate.modules.jaxrs.model.RootResource in project enunciate by stoicflame.

the class EnunciateJaxrsContext method getResourceGroupsByClass.

public List<ResourceGroup> getResourceGroupsByClass(ApiRegistrationContext registrationContext) {
    List<ResourceGroup> resourceGroups = new ArrayList<ResourceGroup>();
    Set<String> slugs = new TreeSet<String>();
    FacetFilter facetFilter = registrationContext.getFacetFilter();
    for (RootResource rootResource : rootResources) {
        if (!facetFilter.accept(rootResource)) {
            continue;
        }
        String slug = rootResource.getSimpleName().toString();
        if (slugs.contains(slug)) {
            slug = "";
            String[] qualifiedNameTokens = rootResource.getQualifiedName().toString().split("\\.");
            for (int i = qualifiedNameTokens.length - 1; i >= 0; i--) {
                slug = slug.isEmpty() ? qualifiedNameTokens[i] : slug + "_" + qualifiedNameTokens[i];
                if (!slugs.contains(slug)) {
                    break;
                }
            }
        }
        slugs.add(slug);
        com.webcohesion.enunciate.metadata.rs.ServiceContextRoot context = rootResource.getAnnotation(com.webcohesion.enunciate.metadata.rs.ServiceContextRoot.class);
        com.webcohesion.enunciate.modules.jaxrs.model.Resource resource = rootResource.getParent();
        while (context == null && resource != null) {
            context = resource.getAnnotation(com.webcohesion.enunciate.metadata.rs.ServiceContextRoot.class);
            resource = resource.getParent();
        }
        String contextPath = context != null ? JaxrsModule.sanitizeContextPath(context.value()) : this.relativeContextPath;
        ResourceGroup group = new ResourceClassResourceGroupImpl(rootResource, slug, contextPath, registrationContext);
        if (!group.getResources().isEmpty()) {
            resourceGroups.add(group);
        }
    }
    Collections.sort(resourceGroups, new ResourceGroupComparator(this.pathSortStrategy));
    return resourceGroups;
}
Also used : RootResource(com.webcohesion.enunciate.modules.jaxrs.model.RootResource) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) ResourceClassResourceGroupImpl(com.webcohesion.enunciate.modules.jaxrs.api.impl.ResourceClassResourceGroupImpl) ResourceGroup(com.webcohesion.enunciate.api.resources.ResourceGroup)

Example 4 with RootResource

use of com.webcohesion.enunciate.modules.jaxrs.model.RootResource in project enunciate by stoicflame.

the class EnunciateJaxrsContext method getResourceGroupsByAnnotation.

public List<ResourceGroup> getResourceGroupsByAnnotation(ApiRegistrationContext registrationContext) {
    Map<String, AnnotationBasedResourceGroupImpl> resourcesByAnnotation = new HashMap<String, AnnotationBasedResourceGroupImpl>();
    FacetFilter facetFilter = registrationContext.getFacetFilter();
    for (RootResource rootResource : rootResources) {
        if (!facetFilter.accept(rootResource)) {
            continue;
        }
        for (ResourceMethod method : rootResource.getResourceMethods(true)) {
            if (facetFilter.accept(method)) {
                com.webcohesion.enunciate.metadata.rs.ResourceGroup annotation = AnnotationUtils.getResourceGroup(method);
                com.webcohesion.enunciate.modules.jaxrs.model.Resource resource = method.getParent();
                while (annotation == null && resource != null) {
                    annotation = AnnotationUtils.getResourceGroup(resource);
                    resource = resource.getParent();
                }
                com.webcohesion.enunciate.metadata.rs.ServiceContextRoot context = method.getAnnotation(com.webcohesion.enunciate.metadata.rs.ServiceContextRoot.class);
                resource = method.getParent();
                while (context == null && resource != null) {
                    context = resource.getAnnotation(com.webcohesion.enunciate.metadata.rs.ServiceContextRoot.class);
                    resource = resource.getParent();
                }
                String label = annotation == null ? "Other" : annotation.value();
                String description = annotation == null ? null : annotation.description();
                if ("##default".equals(description)) {
                    description = null;
                }
                AnnotationBasedResourceGroupImpl resourceGroup = resourcesByAnnotation.get(label);
                if (resourceGroup == null) {
                    String contextPath = context != null ? JaxrsModule.sanitizeContextPath(context.value()) : this.relativeContextPath;
                    resourceGroup = new AnnotationBasedResourceGroupImpl(contextPath, label, new SortedList<Resource>(new ResourceComparator(this.pathSortStrategy)), this.pathSortStrategy);
                    resourcesByAnnotation.put(label, resourceGroup);
                }
                resourceGroup.setDescriptionIfNull(description);
                resourceGroup.getResources().add(new ResourceImpl(method, resourceGroup, registrationContext));
            }
        }
    }
    ArrayList<ResourceGroup> resourceGroups = new ArrayList<ResourceGroup>(resourcesByAnnotation.values());
    Collections.sort(resourceGroups, new ResourceGroupComparator(this.pathSortStrategy));
    return resourceGroups;
}
Also used : RootResource(com.webcohesion.enunciate.modules.jaxrs.model.RootResource) AnnotationBasedResourceGroupImpl(com.webcohesion.enunciate.modules.jaxrs.api.impl.AnnotationBasedResourceGroupImpl) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) ResourceGroup(com.webcohesion.enunciate.api.resources.ResourceGroup) ResourceImpl(com.webcohesion.enunciate.modules.jaxrs.api.impl.ResourceImpl) ResourceMethod(com.webcohesion.enunciate.modules.jaxrs.model.ResourceMethod)

Example 5 with RootResource

use of com.webcohesion.enunciate.modules.jaxrs.model.RootResource in project enunciate by stoicflame.

the class JaxrsRootResourceClassListArtifact method exportTo.

@Override
public void exportTo(File fileOrDirectory, Enunciate enunciate) throws IOException {
    FileWriter out = new FileWriter(fileOrDirectory.isDirectory() ? new File(fileOrDirectory, getName()) : fileOrDirectory);
    Set<String> written = new HashSet<String>();
    for (RootResource resource : this.jaxrsContext.getRootResources()) {
        if (written.add(resource.getQualifiedName().toString())) {
            out.write(resource.getQualifiedName() + "\n");
        }
    }
    out.flush();
    out.close();
}
Also used : RootResource(com.webcohesion.enunciate.modules.jaxrs.model.RootResource) FileWriter(java.io.FileWriter) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

RootResource (com.webcohesion.enunciate.modules.jaxrs.model.RootResource)5 ResourceGroup (com.webcohesion.enunciate.api.resources.ResourceGroup)3 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)3 ResourceImpl (com.webcohesion.enunciate.modules.jaxrs.api.impl.ResourceImpl)2 ResourceMethod (com.webcohesion.enunciate.modules.jaxrs.model.ResourceMethod)2 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)1 AnnotationBasedResourceGroupImpl (com.webcohesion.enunciate.modules.jaxrs.api.impl.AnnotationBasedResourceGroupImpl)1 PathBasedResourceGroupImpl (com.webcohesion.enunciate.modules.jaxrs.api.impl.PathBasedResourceGroupImpl)1 ResourceClassResourceGroupImpl (com.webcohesion.enunciate.modules.jaxrs.api.impl.ResourceClassResourceGroupImpl)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 HashSet (java.util.HashSet)1