Search in sources :

Example 26 with AnnAttachmentInfo

use of org.ballerinalang.util.codegen.AnnAttachmentInfo 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 27 with AnnAttachmentInfo

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

the class SwaggerServiceMapper method createExternalDocModel.

/**
 * Creates external docs swagger definition.
 * @param annotationAttributeValue The ballerina annotation attribute value for external docs.
 * @param swagger The swagger definition which the external docs needs to be build on.
 */
private void createExternalDocModel(AnnAttributeValue annotationAttributeValue, Swagger swagger) {
    if (null != annotationAttributeValue) {
        AnnAttachmentInfo externalDocAnnotationAttachment = annotationAttributeValue.getAnnotationAttachmentValue();
        ExternalDocs externalDoc = new ExternalDocs();
        for (AnnAttributeKeyValuePair annAttributeKeyValuePair : externalDocAnnotationAttachment.getAttributeKeyValuePairs()) {
            if ("description".equals(annAttributeKeyValuePair.getAttributeName())) {
                externalDoc.setDescription(annAttributeKeyValuePair.getAttributeValue().getStringValue());
            } else if ("url".equals(annAttributeKeyValuePair.getAttributeName())) {
                externalDoc.setUrl(annAttributeKeyValuePair.getAttributeValue().getStringValue());
            }
        }
        swagger.setExternalDocs(externalDoc);
    }
}
Also used : AnnAttributeKeyValuePair(org.ballerinalang.util.codegen.AnnAttributeKeyValuePair) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) ExternalDocs(io.swagger.models.ExternalDocs)

Example 28 with AnnAttachmentInfo

use of org.ballerinalang.util.codegen.AnnAttachmentInfo 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 29 with AnnAttachmentInfo

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

the class SwaggerServiceMapper method createLicenseModel.

/**
 * Creates the license swagger definition.
 * @param annotationAttributeValue The ballerina annotation attribute value for license.
 * @param info The info definition which the license needs to be build on.
 */
private void createLicenseModel(AnnAttributeValue annotationAttributeValue, Info info) {
    if (null != annotationAttributeValue) {
        AnnAttachmentInfo licenseAnnotationAttachment = annotationAttributeValue.getAnnotationAttachmentValue();
        License license = new License();
        for (AnnAttributeKeyValuePair annAttributeKeyValuePair : licenseAnnotationAttachment.getAttributeKeyValuePairs()) {
            if ("name".equals(annAttributeKeyValuePair.getAttributeName())) {
                license.setName(annAttributeKeyValuePair.getAttributeValue().getStringValue());
            } else if ("url".equals(annAttributeKeyValuePair.getAttributeName())) {
                license.setUrl(annAttributeKeyValuePair.getAttributeValue().getStringValue());
            }
        }
        info.setLicense(license);
    }
}
Also used : AnnAttributeKeyValuePair(org.ballerinalang.util.codegen.AnnAttributeKeyValuePair) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) License(io.swagger.models.License)

Example 30 with AnnAttachmentInfo

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

the class SwaggerServiceMapper method createContactModel.

/**
 * Creates the contact swagger definition.
 * @param annotationAttributeValue The ballerina annotation attribute value for contact.
 * @param info The info definition which the contact needs to be build on.
 */
private void createContactModel(AnnAttributeValue annotationAttributeValue, Info info) {
    if (null != annotationAttributeValue) {
        AnnAttachmentInfo contactAnnotationAttachment = annotationAttributeValue.getAnnotationAttachmentValue();
        Contact contact = new Contact();
        for (AnnAttributeKeyValuePair annAttributeKeyValuePair : contactAnnotationAttachment.getAttributeKeyValuePairs()) {
            if ("name".equals(annAttributeKeyValuePair.getAttributeName())) {
                contact.setName(annAttributeKeyValuePair.getAttributeValue().getStringValue());
            } else if ("email".equals(annAttributeKeyValuePair.getAttributeName())) {
                contact.setEmail(annAttributeKeyValuePair.getAttributeValue().getStringValue());
            } else if ("url".equals(annAttributeKeyValuePair.getAttributeName())) {
                contact.setUrl(annAttributeKeyValuePair.getAttributeValue().getStringValue());
            }
        }
        info.setContact(contact);
    }
}
Also used : AnnAttributeKeyValuePair(org.ballerinalang.util.codegen.AnnAttributeKeyValuePair) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) Contact(io.swagger.models.Contact)

Aggregations

AnnAttachmentInfo (org.ballerinalang.util.codegen.AnnAttachmentInfo)31 AnnAttributeValue (org.ballerinalang.util.codegen.AnnAttributeValue)17 ParamAnnAttachmentInfo (org.ballerinalang.util.codegen.ParamAnnAttachmentInfo)10 AnnotationAttributeInfo (org.ballerinalang.util.codegen.attributes.AnnotationAttributeInfo)10 Test (org.testng.annotations.Test)10 AnnAttributeKeyValuePair (org.ballerinalang.util.codegen.AnnAttributeKeyValuePair)7 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)3 ExternalDocs (io.swagger.models.ExternalDocs)2 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 Contact (io.swagger.models.Contact)1 Info (io.swagger.models.Info)1 License (io.swagger.models.License)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