Search in sources :

Example 6 with ServiceNode

use of org.ballerinalang.model.tree.ServiceNode 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 7 with ServiceNode

use of org.ballerinalang.model.tree.ServiceNode 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)

Aggregations

ServiceNode (org.ballerinalang.model.tree.ServiceNode)7 Swagger (io.swagger.models.Swagger)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Lists (com.google.common.collect.Lists)3 Contact (io.swagger.models.Contact)3 ExternalDocs (io.swagger.models.ExternalDocs)3 Info (io.swagger.models.Info)3 License (io.swagger.models.License)3 Scheme (io.swagger.models.Scheme)3 Tag (io.swagger.models.Tag)3 ApiKeyAuthDefinition (io.swagger.models.auth.ApiKeyAuthDefinition)3 BasicAuthDefinition (io.swagger.models.auth.BasicAuthDefinition)3 In (io.swagger.models.auth.In)3 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)3 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)3 Json (io.swagger.util.Json)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3