Search in sources :

Example 31 with AnnotationAttachmentNode

use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.

the class SwaggerServiceMapper method createDevelopersModel.

/**
 * Creates vendor extension for developers.
 * @param annotationAttributeValue The annotation attribute value for developer vendor extension.
 * @param info The info definition.
 */
private void createDevelopersModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Info info) {
    if (null != annotationAttributeValue) {
        List<Developer> developers = new LinkedList<>();
        for (AnnotationAttachmentAttributeValueNode value : annotationAttributeValue.getValueArray()) {
            AnnotationAttachmentNode developerAnnotation = (AnnotationAttachmentNode) value.getValue();
            Map<String, AnnotationAttachmentAttributeValueNode> developerAttributes = this.listToMap(developerAnnotation);
            Developer developer = new Developer();
            if (developerAttributes.containsKey("name")) {
                developer.setName(this.getStringLiteralValue(developerAttributes.get("name")));
            }
            if (developerAttributes.containsKey("email")) {
                developer.setEmail(this.getStringLiteralValue(developerAttributes.get("email")));
            }
            developers.add(developer);
        }
        info.setVendorExtension("x-developers", Lists.reverse(developers));
    }
}
Also used : AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) Developer(org.ballerinalang.ballerina.swagger.convertor.service.model.Developer) LinkedList(java.util.LinkedList) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 32 with AnnotationAttachmentNode

use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.

the class SwaggerServiceMapper method createTagModel.

/**
 * Creates tag swagger definition.
 * @param annotationAttributeValue The ballerina annotation attribute value for tag.
 * @param swagger The swagger definition which the tags needs to be build on.
 */
private void createTagModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Swagger swagger) {
    if (null != annotationAttributeValue) {
        List<Tag> tags = new LinkedList<>();
        for (AnnotationAttachmentAttributeValueNode value : annotationAttributeValue.getValueArray()) {
            AnnotationAttachmentNode tagAnnotation = (AnnotationAttachmentNode) value.getValue();
            Map<String, AnnotationAttachmentAttributeValueNode> tagAttributes = this.listToMap(tagAnnotation);
            Tag tag = new Tag();
            if (tagAttributes.containsKey("name")) {
                tag.setName(this.getStringLiteralValue(tagAttributes.get("name")));
            }
            if (tagAttributes.containsKey("description")) {
                tag.setDescription(this.getStringLiteralValue(tagAttributes.get("description")));
            }
            tags.add(tag);
        }
        swagger.setTags(Lists.reverse(tags));
    }
}
Also used : AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) Tag(io.swagger.models.Tag) LinkedList(java.util.LinkedList) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 33 with AnnotationAttachmentNode

use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.

the class SwaggerServiceMapper method parseConfigAnnotationAttachment.

/**
 * Parses the ballerina.net.http@config annotation and builds swagger definition. Also create the consumes and
 * produces annotations.
 * @param service The ballerina service which has the annotation.
 * @param swagger The swagger to build up.
 */
private void parseConfigAnnotationAttachment(ServiceNode service, Swagger swagger) {
    Optional<? extends AnnotationAttachmentNode> httpConfigAnnotationAttachment = service.getAnnotationAttachments().stream().filter(a -> null != this.httpAlias && this.httpAlias.equals(a.getPackageAlias().getValue()) && "configuration".equals(a.getAnnotationName().getValue())).findFirst();
    if (httpConfigAnnotationAttachment.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> configAttributes = this.listToMap(httpConfigAnnotationAttachment.get());
        if (configAttributes.containsKey("basePath")) {
            swagger.setBasePath(this.getStringLiteralValue(configAttributes.get("basePath")));
        }
        if (configAttributes.containsKey("host") && configAttributes.containsKey("port")) {
            swagger.setHost(this.getStringLiteralValue(configAttributes.get("host")) + ":" + this.getStringLiteralValue(configAttributes.get("port")));
        }
    }
    Optional<? extends AnnotationAttachmentNode> consumesAnnotationAttachment = service.getAnnotationAttachments().stream().filter(a -> null != this.httpAlias && this.httpAlias.equals(a.getPackageAlias().getValue()) && "Consumes".equals(a.getAnnotationName().getValue())).findFirst();
    if (consumesAnnotationAttachment.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> consumesAttributes = this.listToMap(consumesAnnotationAttachment.get());
        List<String> consumes = new LinkedList<>();
        for (AnnotationAttachmentAttributeValueNode consumesValue : consumesAttributes.get("value").getValueArray()) {
            consumes.add(this.getStringLiteralValue(consumesValue));
        }
        swagger.setConsumes(consumes);
    }
    Optional<? extends AnnotationAttachmentNode> producesAnnotationAttachment = service.getAnnotationAttachments().stream().filter(a -> null != this.httpAlias && this.httpAlias.equals(a.getPackageAlias().getValue()) && "Produces".equals(a.getAnnotationName().getValue())).findFirst();
    if (producesAnnotationAttachment.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> consumesAttributes = this.listToMap(producesAnnotationAttachment.get());
        List<String> produces = new LinkedList<>();
        for (AnnotationAttachmentAttributeValueNode consumesValue : consumesAttributes.get("value").getValueArray()) {
            produces.add(this.getStringLiteralValue(consumesValue));
        }
        swagger.setProduces(produces);
    }
}
Also used : Scheme(io.swagger.models.Scheme) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) Tag(io.swagger.models.Tag) License(io.swagger.models.License) Json(io.swagger.util.Json) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) Lists(com.google.common.collect.Lists) Map(java.util.Map) Organization(org.ballerinalang.ballerina.swagger.convertor.service.model.Organization) LinkedList(java.util.LinkedList) Contact(io.swagger.models.Contact) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) Info(io.swagger.models.Info) Developer(org.ballerinalang.ballerina.swagger.convertor.service.model.Developer) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) ServiceNode(org.ballerinalang.model.tree.ServiceNode) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) List(java.util.List) AnnotationAttachmentAttributeNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeNode) ExternalDocs(io.swagger.models.ExternalDocs) LiteralNode(org.ballerinalang.model.tree.expressions.LiteralNode) Optional(java.util.Optional) In(io.swagger.models.auth.In) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) LinkedList(java.util.LinkedList)

Example 34 with AnnotationAttachmentNode

use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.

the class SwaggerServiceMapper method parseServiceConfigAnnotationAttachment.

/**
 * Parses the 'ServiceConfig' annotation attachment.
 * @param service The ballerina service which has that annotation attachment.
 * @param swagger The swagger definition to build up.
 */
private void parseServiceConfigAnnotationAttachment(ServiceNode service, Swagger swagger) {
    Optional<? extends AnnotationAttachmentNode> swaggerConfigAnnotation = service.getAnnotationAttachments().stream().filter(a -> null != swaggerAlias && this.swaggerAlias.equals(a.getPackageAlias().getValue()) && "ServiceConfig".equals(a.getAnnotationName().getValue())).findFirst();
    if (swaggerConfigAnnotation.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> serviceConfigAttributes = this.listToMap(swaggerConfigAnnotation.get());
        if (serviceConfigAttributes.containsKey("host")) {
            swagger.setHost(this.getStringLiteralValue(serviceConfigAttributes.get("host")));
        }
        if (serviceConfigAttributes.containsKey("schemes") && serviceConfigAttributes.get("schemes").getValueArray().size() > 0) {
            List<Scheme> schemes = new LinkedList<>();
            for (AnnotationAttachmentAttributeValueNode schemesNodes : serviceConfigAttributes.get("schemes").getValueArray()) {
                String schemeStringValue = this.getStringLiteralValue(schemesNodes);
                if (null != Scheme.forValue(schemeStringValue)) {
                    schemes.add(Scheme.forValue(schemeStringValue));
                }
            }
            if (schemes.size() > 0) {
                schemes = Lists.reverse(schemes);
                swagger.setSchemes(schemes);
            }
        }
        this.createSecurityDefinitionsModel(serviceConfigAttributes.get("authorizations"), swagger);
    }
}
Also used : Scheme(io.swagger.models.Scheme) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) Tag(io.swagger.models.Tag) License(io.swagger.models.License) Json(io.swagger.util.Json) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) Lists(com.google.common.collect.Lists) Map(java.util.Map) Organization(org.ballerinalang.ballerina.swagger.convertor.service.model.Organization) LinkedList(java.util.LinkedList) Contact(io.swagger.models.Contact) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) Info(io.swagger.models.Info) Developer(org.ballerinalang.ballerina.swagger.convertor.service.model.Developer) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) ServiceNode(org.ballerinalang.model.tree.ServiceNode) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) List(java.util.List) AnnotationAttachmentAttributeNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeNode) ExternalDocs(io.swagger.models.ExternalDocs) LiteralNode(org.ballerinalang.model.tree.expressions.LiteralNode) Optional(java.util.Optional) In(io.swagger.models.auth.In) Scheme(io.swagger.models.Scheme) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) LinkedList(java.util.LinkedList)

Example 35 with AnnotationAttachmentNode

use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.

the class ResourceContextHolder method buildContext.

public static ResourceContextHolder buildContext(ResourceNode resource) throws CodeGeneratorException {
    ResourceContextHolder context = new ResourceContextHolder();
    context.name = resource.getName().getValue();
    context.parameters = new ArrayList<>();
    for (VariableNode node : resource.getParameters()) {
        ParameterContextHolder parameter = ParameterContextHolder.buildContext(node);
        if (parameter != null) {
            context.parameters.add(parameter);
        }
    }
    // Iterate through all resource level annotations and find out resource configuration information
    AnnotationAttachmentNode ann = GeneratorUtils.getAnnotationFromList(GeneratorConstants.RES_CONFIG_ANNOTATION, GeneratorConstants.HTTP_PKG_ALIAS, resource.getAnnotationAttachments());
    if (ann == null) {
        throw new CodeGeneratorException("Incomplete resource configuration found");
    }
    BLangRecordLiteral bLiteral = ((BLangRecordLiteral) ((BLangAnnotationAttachment) ann).getExpression());
    List<BLangRecordLiteral.BLangRecordKeyValue> list = bLiteral.getKeyValuePairs();
    Map<String, String[]> attrs = GeneratorUtils.getKeyValuePairAsMap(list);
    // We don't expect multiple http methods to be supported by single action
    // We only consider first content type for a single resource
    context.method = attrs.get(GeneratorConstants.ATTR_METHODS) != null ? attrs.get(GeneratorConstants.ATTR_METHODS)[0] : null;
    context.contentType = attrs.get(GeneratorConstants.ATTR_CONSUMES) != null ? attrs.get(GeneratorConstants.ATTR_CONSUMES)[0] : null;
    String path = attrs.get(GeneratorConstants.ATTR_PATH) != null ? attrs.get(GeneratorConstants.ATTR_PATH)[0] : null;
    context.path = context.getTemplatePath(path);
    return context;
}
Also used : VariableNode(org.ballerinalang.model.tree.VariableNode) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Aggregations

AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)35 AnnotationAttachmentAttributeValueNode (org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode)18 HashMap (java.util.HashMap)12 List (java.util.List)12 LinkedList (java.util.LinkedList)11 ExternalDocs (io.swagger.models.ExternalDocs)10 Map (java.util.Map)9 Collectors (java.util.stream.Collectors)9 Lists (com.google.common.collect.Lists)8 Scheme (io.swagger.models.Scheme)8 Swagger (io.swagger.models.Swagger)8 ArrayList (java.util.ArrayList)8 Optional (java.util.Optional)8 AnnotationAttachmentAttributeNode (org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeNode)7 LiteralNode (org.ballerinalang.model.tree.expressions.LiteralNode)7 ArrayProperty (io.swagger.models.properties.ArrayProperty)6 BooleanProperty (io.swagger.models.properties.BooleanProperty)6 IntegerProperty (io.swagger.models.properties.IntegerProperty)6 Property (io.swagger.models.properties.Property)6 StringProperty (io.swagger.models.properties.StringProperty)6