use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.
the class Generator method paramAnnotation.
/**
* Get description annotation of the parameter.
* @param node parent node.
* @param param parameter.
* @return description of the parameter.
*/
private static String paramAnnotation(BLangNode node, BLangVariable param) {
String subName = param.getName() == null ? param.type.tsymbol.name.value : param.getName().getValue();
for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
continue;
}
BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
String attribVal = bLangLiteral.toString();
if ((annotation.getAnnotationName().getValue().equals("Param")) && attribVal.startsWith(subName + ":")) {
return attribVal.split(subName + ":")[1].trim();
}
}
return "";
}
use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.
the class Generator method annotFieldAnnotation.
/**
* Get description annotation of the annotation attribute.
* @param annotationNode parent node.
* @param annotAttribute annotation attribute.
* @return description of the annotation attribute.
*/
private static String annotFieldAnnotation(BLangAnnotation annotationNode, BLangAnnotAttribute annotAttribute) {
List<? extends AnnotationAttachmentNode> annotationAttachments = getAnnotationAttachments(annotationNode);
for (AnnotationAttachmentNode annotation : annotationAttachments) {
if ("Field".equals(annotation.getAnnotationName().getValue())) {
BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
String value = bLangLiteral.toString();
if (value.startsWith(annotAttribute.getName().getValue())) {
String[] valueParts = value.split(":");
return valueParts.length == 2 ? valueParts[1] : valueParts[0];
}
}
}
return "";
}
use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.
the class SwaggerResourceMapper method parseHttpResourceConfig.
/**
* Parses the 'http:resourceConfig' annotation attachment and build swagger operation.
* @param resource The ballerina resource definition.
* @param operationAdaptor The operation adaptor..
*/
private void parseHttpResourceConfig(ResourceNode resource, OperationAdaptor operationAdaptor) {
Optional<? extends AnnotationAttachmentNode> responsesAnnotation = resource.getAnnotationAttachments().stream().filter(a -> null != this.httpAlias && this.httpAlias.equals(a.getPackageAlias().getValue()) && "resourceConfig".equals(a.getAnnotationName().getValue())).findFirst();
if (responsesAnnotation.isPresent()) {
Map<String, AnnotationAttachmentAttributeValueNode> configAttributes = this.listToMap(responsesAnnotation.get());
if (configAttributes.containsKey("methods") && configAttributes.get("methods").getValueArray().size() > 0) {
List<? extends AnnotationAttachmentAttributeValueNode> methodsValues = configAttributes.get("methods").getValueArray();
// Since there is only one http method.
operationAdaptor.setHttpOperation(this.getStringLiteralValue(methodsValues.get(0)));
}
if (configAttributes.containsKey("path")) {
operationAdaptor.setPath(this.getStringLiteralValue(configAttributes.get("path")));
}
if (configAttributes.containsKey("produces") && configAttributes.get("produces").getValueArray().size() > 0) {
List<String> produces = new LinkedList<>();
List<? extends AnnotationAttachmentAttributeValueNode> producesValues = configAttributes.get("produces").getValueArray();
for (AnnotationAttachmentAttributeValueNode producesValue : producesValues) {
produces.add(this.getStringLiteralValue(producesValue));
}
operationAdaptor.getOperation().setProduces(produces);
}
if (configAttributes.containsKey("consumes") && configAttributes.get("consumes").getValueArray().size() > 0) {
List<String> consumes = new LinkedList<>();
List<? extends AnnotationAttachmentAttributeValueNode> consumesValues = configAttributes.get("consumes").getValueArray();
for (AnnotationAttachmentAttributeValueNode consumesValue : consumesValues) {
consumes.add(this.getStringLiteralValue(consumesValue));
}
operationAdaptor.getOperation().setConsumes(consumes);
}
}
}
use of org.ballerinalang.model.tree.AnnotationAttachmentNode 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(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Swagger swagger) {
if (null != annotationAttributeValue) {
Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = new HashMap<>();
for (AnnotationAttachmentAttributeValueNode authorizationValues : annotationAttributeValue.getValueArray()) {
if (authorizationValues instanceof AnnotationAttachmentNode) {
AnnotationAttachmentNode authAnnotationAttachment = (AnnotationAttachmentNode) authorizationValues;
Map<String, AnnotationAttachmentAttributeValueNode> authAttributes = this.listToMap(authAnnotationAttachment);
if (authAttributes.containsKey("name") && authAttributes.containsKey("authType")) {
String name = this.getStringLiteralValue(authAttributes.get("name"));
String type = this.getStringLiteralValue(authAttributes.get("authType"));
String description = "";
if (authAttributes.containsKey("description")) {
description = this.getStringLiteralValue(authAttributes.get("description"));
}
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(this.getStringLiteralValue(authAttributes.get("apiName")));
apiKeyAuthDefinition.setIn(In.forValue(this.getStringLiteralValue(authAttributes.get("in"))));
apiKeyAuthDefinition.setDescription(description);
securitySchemeDefinitionMap.put(name, apiKeyAuthDefinition);
} else if ("oauth2".equals(type)) {
OAuth2Definition oAuth2Definition = new OAuth2Definition();
oAuth2Definition.setFlow(this.getStringLiteralValue(authAttributes.get("flow")));
oAuth2Definition.setAuthorizationUrl(this.getStringLiteralValue(authAttributes.get("authorizationUrl")));
oAuth2Definition.setTokenUrl(this.getStringLiteralValue(authAttributes.get("tokenUrl")));
this.createSecurityDefinitionScopesModel(authAttributes.get("authorizationScopes"), oAuth2Definition);
oAuth2Definition.setDescription(description);
securitySchemeDefinitionMap.put(name, oAuth2Definition);
}
}
}
}
swagger.setSecurityDefinitions(securitySchemeDefinitionMap);
}
}
use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.
the class SwaggerServiceMapper method createOrganizationModel.
/**
* Creates vendor extension for organization.
* @param annotationAttributeValue The annotation attribute value for organization vendor extension.
* @param info The info definition.
*/
private void createOrganizationModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Info info) {
if (null != annotationAttributeValue) {
AnnotationAttachmentNode organizationAnnotationAttachment = (AnnotationAttachmentNode) annotationAttributeValue.getValue();
Map<String, AnnotationAttachmentAttributeValueNode> organizationAttributes = this.listToMap(organizationAnnotationAttachment);
Organization organization = new Organization();
if (organizationAttributes.containsKey("name")) {
organization.setName(this.getStringLiteralValue(organizationAttributes.get("name")));
}
if (organizationAttributes.containsKey("url")) {
organization.setUrl(this.getStringLiteralValue(organizationAttributes.get("url")));
}
info.setVendorExtension("x-organization", organization);
}
}
Aggregations