Search in sources :

Example 1 with Method

use of com.webcohesion.enunciate.api.resources.Method in project enunciate by stoicflame.

the class AnnotationBasedResourceGroupImpl method getPaths.

@Override
public List<PathSummary> getPaths() {
    HashMap<String, PathSummary> paths = new HashMap<String, PathSummary>();
    for (Resource resource : this.resources) {
        PathSummary pathSummary = paths.get(resource.getPath());
        if (pathSummary == null) {
            pathSummary = new PathSummaryImpl(resource.getPath(), new TreeSet<String>(), resource.getStyles());
            paths.put(resource.getPath(), pathSummary);
        }
        for (Method method : resource.getMethods()) {
            pathSummary.getMethods().add(method.getHttpMethod());
        }
    }
    ArrayList<PathSummary> pathSummaries = new ArrayList<PathSummary>(paths.values());
    Collections.sort(pathSummaries, new PathSummaryComparator(sortStrategy));
    return pathSummaries;
}
Also used : PathSummaryComparator(com.webcohesion.enunciate.util.PathSummaryComparator) PathSummary(com.webcohesion.enunciate.api.PathSummary) Resource(com.webcohesion.enunciate.api.resources.Resource) Method(com.webcohesion.enunciate.api.resources.Method)

Example 2 with Method

use of com.webcohesion.enunciate.api.resources.Method in project enunciate by stoicflame.

the class PathBasedResourceGroupImpl method getDescription.

@Override
public String getDescription() {
    // we'll return a description if all descriptions of all methods are the same, or if there's only one defining resource class.
    String description = null;
    Set<com.webcohesion.enunciate.modules.jaxrs.model.Resource> definingResourceClasses = new TreeSet<com.webcohesion.enunciate.modules.jaxrs.model.Resource>(new TypeElementComparator());
    int methodCount = 0;
    RESOURCES: for (Resource resource : this.resources) {
        for (Method method : resource.getMethods()) {
            methodCount++;
            if (description != null && method.getDescription() != null && !description.equals(method.getDescription())) {
                description = null;
                break RESOURCES;
            }
            description = method.getDescription();
            if (description != null && description.trim().isEmpty()) {
                description = null;
            }
        }
        if (resource instanceof ResourceImpl) {
            definingResourceClasses.add(((ResourceImpl) resource).resourceMethod.getParent());
        }
    }
    if ((methodCount > 1 || description == null) && definingResourceClasses.size() == 1) {
        // if there's only one class, it's javadoc is probably a better description than the method-level.
        description = definingResourceClasses.iterator().next().getDocValue();
    }
    return description;
}
Also used : TypeElementComparator(com.webcohesion.enunciate.javac.TypeElementComparator) Resource(com.webcohesion.enunciate.api.resources.Resource) Method(com.webcohesion.enunciate.api.resources.Method)

Example 3 with Method

use of com.webcohesion.enunciate.api.resources.Method in project enunciate by stoicflame.

the class ResourceClassResourceGroupImpl method getPaths.

@Override
public List<PathSummary> getPaths() {
    HashMap<String, PathSummary> summaries = new HashMap<String, PathSummary>();
    for (Resource resource : this.resources) {
        Set<String> methods = new TreeSet<String>();
        for (Method method : resource.getMethods()) {
            methods.add(method.getHttpMethod());
        }
        PathSummary summary = summaries.get(resource.getPath());
        if (summary == null) {
            summary = new PathSummaryImpl(resource.getPath(), methods, resource.getStyles());
            summaries.put(resource.getPath(), summary);
        } else {
            summary.getMethods().addAll(methods);
        }
    }
    ArrayList<PathSummary> pathSummaries = new ArrayList<PathSummary>(summaries.values());
    Collections.sort(pathSummaries, new PathSummaryComparator(controllerClass.getContext().getPathSortStrategy()));
    return pathSummaries;
}
Also used : PathSummaryComparator(com.webcohesion.enunciate.util.PathSummaryComparator) PathSummary(com.webcohesion.enunciate.api.PathSummary) Resource(com.webcohesion.enunciate.api.resources.Resource) Method(com.webcohesion.enunciate.api.resources.Method)

Example 4 with Method

use of com.webcohesion.enunciate.api.resources.Method in project enunciate by stoicflame.

the class ResponsesOfMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The responsesOf method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (unwrapped instanceof Method) {
        Method method = (Method) unwrapped;
        TreeSet<SwaggerResponse> responses = new TreeSet<>(Comparator.comparingInt(SwaggerResponse::getCode));
        List<? extends Parameter> successHeaders = method.getResponseHeaders();
        Entity responseEntity = method.getResponseEntity();
        DataTypeReference successDataType = FindBestDataTypeMethod.findBestDataType(responseEntity);
        boolean successResponseFound = false;
        if (method.getResponseCodes() != null) {
            for (StatusCode code : method.getResponseCodes()) {
                boolean successResponse = code.getCode() >= 200 && code.getCode() < 300;
                DataTypeReference dataType = FindBestDataTypeMethod.findBestDataType(code.getMediaTypes());
                dataType = dataType == null && successResponse ? successDataType : dataType;
                List<? extends Parameter> headers = successResponse ? successHeaders : Collections.<Parameter>emptyList();
                responses.add(new SwaggerResponse(code.getCode(), dataType, headers, code.getCondition()));
                successResponseFound |= successResponse;
            }
        }
        if (!successResponseFound) {
            int code = DEFAULT_201_METHODS.contains(method.getHttpMethod().toUpperCase()) ? 201 : DEFAULT_204_METHODS.contains(method.getHttpMethod().toUpperCase()) ? 204 : 200;
            String description = responseEntity != null ? responseEntity.getDescription() : "Success";
            responses.add(new SwaggerResponse(code, successDataType, successHeaders, description));
        }
        return responses;
    }
    throw new TemplateModelException("No responses for: " + unwrapped);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Entity(com.webcohesion.enunciate.api.resources.Entity) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) TemplateModel(freemarker.template.TemplateModel) Method(com.webcohesion.enunciate.api.resources.Method) StatusCode(com.webcohesion.enunciate.api.resources.StatusCode)

Example 5 with Method

use of com.webcohesion.enunciate.api.resources.Method in project enunciate by stoicflame.

the class ValidParametersMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The responsesOf method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (unwrapped instanceof Method) {
        Method method = (Method) unwrapped;
        ArrayList<Parameter> params = new ArrayList<Parameter>();
        for (Parameter parameter : method.getParameters()) {
            String type = parameter.getTypeLabel().toLowerCase();
            if (type.contains("path")) {
                params.add(new SwaggerParameter(parameter, "path"));
            } else if (type.contains("form")) {
                params.add(new SwaggerParameter(parameter, "formData"));
            } else if (type.contains("query")) {
                params.add(new SwaggerParameter(parameter, "query"));
            } else if (type.contains("header")) {
                params.add(new SwaggerParameter(parameter, "header"));
            }
        }
        return params;
    }
    throw new TemplateModelException("No parameters for: " + unwrapped);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) ArrayList(java.util.ArrayList) Parameter(com.webcohesion.enunciate.api.resources.Parameter) TemplateModel(freemarker.template.TemplateModel) Method(com.webcohesion.enunciate.api.resources.Method)

Aggregations

Method (com.webcohesion.enunciate.api.resources.Method)17 Resource (com.webcohesion.enunciate.api.resources.Resource)11 PathSummary (com.webcohesion.enunciate.api.PathSummary)4 TypeElementComparator (com.webcohesion.enunciate.javac.TypeElementComparator)4 PathSummaryComparator (com.webcohesion.enunciate.util.PathSummaryComparator)4 TemplateModel (freemarker.template.TemplateModel)4 TemplateModelException (freemarker.template.TemplateModelException)4 EnunciateException (com.webcohesion.enunciate.EnunciateException)2 Parameter (com.webcohesion.enunciate.api.resources.Parameter)2 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)2 AccessorOverridesAnotherMethod (com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod)2 FindRootElementMethod (com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod)2 SpringController (com.webcohesion.enunciate.modules.spring_web.model.SpringController)2 IsFacetExcludedMethod (com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod)2 URL (java.net.URL)2 DataType (com.webcohesion.enunciate.api.datatype.DataType)1 DataTypeReference (com.webcohesion.enunciate.api.datatype.DataTypeReference)1 Property (com.webcohesion.enunciate.api.datatype.Property)1 Syntax (com.webcohesion.enunciate.api.datatype.Syntax)1 Value (com.webcohesion.enunciate.api.datatype.Value)1