Search in sources :

Example 16 with AnnAttributeValue

use of org.ballerinalang.util.codegen.AnnAttributeValue in project ballerina by ballerina-lang.

the class SwaggerServiceMapper method createSecurityDefinitionsModel.

/**
 * Creates the security definition models for swagger definition.
 * @param annotationAttributeValue The annotation attribute value for security definitions.
 * @param swagger The swagger definition.
 */
private void createSecurityDefinitionsModel(AnnAttributeValue annotationAttributeValue, Swagger swagger) {
    if (null != annotationAttributeValue) {
        Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = new HashMap<>();
        for (AnnAttributeValue authorizationValues : annotationAttributeValue.getAttributeValueArray()) {
            AnnAttachmentInfo authAnnotationAttachment = authorizationValues.getAnnotationAttachmentValue();
            Map<String, AnnAttributeValue> authAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(authAnnotationAttachment);
            if (null != authAnnAttributeValueMap.get("name") && null != authAnnAttributeValueMap.get("authType")) {
                String name = authAnnAttributeValueMap.get("name").getStringValue();
                String type = authAnnAttributeValueMap.get("authType").getStringValue();
                String description = "";
                if (null != authAnnAttributeValueMap.get("description")) {
                    description = authAnnAttributeValueMap.get("description").getStringValue();
                }
                if ("basic".equals(type)) {
                    BasicAuthDefinition basicAuthDefinition = new BasicAuthDefinition();
                    basicAuthDefinition.setDescription(description);
                    securitySchemeDefinitionMap.put(name, basicAuthDefinition);
                } else if ("apiKey".equals(type)) {
                    ApiKeyAuthDefinition apiKeyAuthDefinition = new ApiKeyAuthDefinition();
                    apiKeyAuthDefinition.setName(authAnnAttributeValueMap.get("apiName").getStringValue());
                    apiKeyAuthDefinition.setIn(In.forValue(authAnnAttributeValueMap.get("in").getStringValue()));
                    apiKeyAuthDefinition.setDescription(description);
                    securitySchemeDefinitionMap.put(name, apiKeyAuthDefinition);
                } else if ("oauth2".equals(type)) {
                    OAuth2Definition oAuth2Definition = new OAuth2Definition();
                    oAuth2Definition.setFlow(authAnnAttributeValueMap.get("flow").getStringValue());
                    oAuth2Definition.setAuthorizationUrl(authAnnAttributeValueMap.get("authorizationUrl").getStringValue());
                    oAuth2Definition.setTokenUrl(authAnnAttributeValueMap.get("tokenUrl").getStringValue());
                    this.createSecurityDefinitionScopesModel(authAnnAttributeValueMap.get("authorizationScopes"), oAuth2Definition);
                    oAuth2Definition.setDescription(description);
                    securitySchemeDefinitionMap.put(name, oAuth2Definition);
                }
            }
        }
        swagger.setSecurityDefinitions(securitySchemeDefinitionMap);
    }
}
Also used : AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) HashMap(java.util.HashMap) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition)

Example 17 with AnnAttributeValue

use of org.ballerinalang.util.codegen.AnnAttributeValue 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(ServiceInfo service, Swagger swagger) {
    AnnAttachmentInfo swaggerConfigAnnotation = service.getAnnotationAttachmentInfo(SwaggerConstants.SWAGGER_PACKAGE_PATH, "ServiceConfig");
    if (null != swaggerConfigAnnotation) {
        Map<String, AnnAttributeValue> swaggerConfigAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(swaggerConfigAnnotation);
        if (null != swaggerConfigAnnAttributeValueMap.get("host")) {
            swagger.setHost(swaggerConfigAnnAttributeValueMap.get("host").getStringValue());
        }
        if (null != swaggerConfigAnnAttributeValueMap.get("schemes")) {
            if (swaggerConfigAnnAttributeValueMap.get("schemes").getAttributeValueArray().length > 0) {
                List<Scheme> schemes = new LinkedList<>();
                for (AnnAttributeValue schemeValue : swaggerConfigAnnAttributeValueMap.get("schemes").getAttributeValueArray()) {
                    if (null != Scheme.forValue(schemeValue.getStringValue())) {
                        schemes.add(Scheme.forValue(schemeValue.getStringValue()));
                    }
                }
                if (schemes.size() > 0) {
                    swagger.setSchemes(schemes);
                }
            }
        }
        this.createSecurityDefinitionsModel(swaggerConfigAnnAttributeValueMap.get("authorizations"), swagger);
    }
}
Also used : AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) Scheme(io.swagger.models.Scheme) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) LinkedList(java.util.LinkedList)

Example 18 with AnnAttributeValue

use of org.ballerinalang.util.codegen.AnnAttributeValue 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(AnnAttributeValue annotationAttributeValue, Swagger swagger) {
    if (null != annotationAttributeValue && annotationAttributeValue.getAttributeValueArray().length > 0) {
        List<Tag> tags = new LinkedList<>();
        for (AnnAttributeValue tagAttributeValue : annotationAttributeValue.getAttributeValueArray()) {
            AnnAttachmentInfo tagAnnotationAttachment = tagAttributeValue.getAnnotationAttachmentValue();
            Tag tag = new Tag();
            for (AnnAttributeKeyValuePair annAttributeKeyValuePair : tagAnnotationAttachment.getAttributeKeyValuePairs()) {
                if ("name".equals(annAttributeKeyValuePair.getAttributeName())) {
                    tag.setName(annAttributeKeyValuePair.getAttributeValue().getStringValue());
                } else if ("description".equals(annAttributeKeyValuePair.getAttributeName())) {
                    tag.setDescription(annAttributeKeyValuePair.getAttributeValue().getStringValue());
                }
            }
            tags.add(tag);
        }
        swagger.setTags(tags);
    }
}
Also used : AnnAttributeKeyValuePair(org.ballerinalang.util.codegen.AnnAttributeKeyValuePair) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) Tag(io.swagger.models.Tag) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) LinkedList(java.util.LinkedList)

Aggregations

AnnAttributeValue (org.ballerinalang.util.codegen.AnnAttributeValue)18 AnnAttachmentInfo (org.ballerinalang.util.codegen.AnnAttachmentInfo)17 ParamAnnAttachmentInfo (org.ballerinalang.util.codegen.ParamAnnAttachmentInfo)10 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)4 Scheme (io.swagger.models.Scheme)2 BodyParameter (io.swagger.models.parameters.BodyParameter)2 PathParameter (io.swagger.models.parameters.PathParameter)2 QueryParameter (io.swagger.models.parameters.QueryParameter)2 AnnotationAttributeInfo (org.ballerinalang.util.codegen.attributes.AnnotationAttributeInfo)2 Test (org.testng.annotations.Test)2 ExternalDocs (io.swagger.models.ExternalDocs)1 Model (io.swagger.models.Model)1 ModelImpl (io.swagger.models.ModelImpl)1 RefModel (io.swagger.models.RefModel)1 Response (io.swagger.models.Response)1 Tag (io.swagger.models.Tag)1 ApiKeyAuthDefinition (io.swagger.models.auth.ApiKeyAuthDefinition)1 BasicAuthDefinition (io.swagger.models.auth.BasicAuthDefinition)1 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)1