Search in sources :

Example 1 with AnnAttributeValue

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

the class AnnotationTest method testAnnotationArray.

@Test(description = "Test annotation array", enabled = false)
public void testAnnotationArray() {
    AnnotationAttributeInfo annotationInfo = (AnnotationAttributeInfo) compileResult.getProgFile().getEntryPackage().getFunctionInfo("foo").getAttributeInfo(AttributeInfo.Kind.ANNOTATIONS_ATTRIBUTE);
    AnnAttachmentInfo[] attachmentInfos = annotationInfo.getAttachmentInfoEntries();
    AnnAttributeValue[] annotationArray = attachmentInfos[0].getAttributeValue("queryParamValue").getAttributeValueArray();
    Assert.assertEquals(annotationArray.length, 3, "Wrong annotation array length");
    String attributeValue = annotationArray[2].getAnnotationAttachmentValue().getAttributeValue("name").getStringValue();
    Assert.assertEquals(attributeValue, "paramName");
    attributeValue = annotationArray[1].getAnnotationAttachmentValue().getAttributeValue("name").getStringValue();
    Assert.assertEquals(attributeValue, "paramName2");
    attributeValue = annotationArray[0].getAnnotationAttachmentValue().getAttributeValue("name").getStringValue();
    Assert.assertEquals(attributeValue, "paramName3");
}
Also used : AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) AnnotationAttributeInfo(org.ballerinalang.util.codegen.attributes.AnnotationAttributeInfo) Test(org.testng.annotations.Test)

Example 2 with AnnAttributeValue

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

the class HttpUtil method addHTTPSessionAndCorsHeaders.

public static void addHTTPSessionAndCorsHeaders(Context context, HTTPCarbonMessage requestMsg, HTTPCarbonMessage responseMsg) {
    Session session = (Session) requestMsg.getProperty(HttpConstants.HTTP_SESSION);
    if (session != null) {
        boolean isSecureRequest = false;
        AnnAttachmentInfo configAnn = context.getServiceInfo().getAnnotationAttachmentInfo(HttpConstants.PROTOCOL_PACKAGE_HTTP, HttpConstants.ANN_NAME_CONFIG);
        if (configAnn != null) {
            AnnAttributeValue httpsPortAttrVal = configAnn.getAttributeValue(HttpConstants.ANN_CONFIG_ATTR_HTTPS_PORT);
            if (httpsPortAttrVal != null) {
                Integer listenerPort = (Integer) requestMsg.getProperty(HttpConstants.LISTENER_PORT);
                if (listenerPort != null && httpsPortAttrVal.getIntValue() == listenerPort) {
                    isSecureRequest = true;
                }
            }
        }
        session.generateSessionHeader(responseMsg, isSecureRequest);
    }
    // Process CORS if exists.
    if (requestMsg.getHeader(HttpHeaderNames.ORIGIN.toString()) != null) {
        CorsHeaderGenerator.process(requestMsg, responseMsg, true);
    }
}
Also used : AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) Session(org.ballerinalang.net.http.session.Session)

Example 3 with AnnAttributeValue

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

the class SwaggerResourceMapper method createHeadersModel.

/**
 * Creates headers definitions for swagger response.
 *
 * @param annotationAttributeValue The annotation attribute value which has the headers.
 * @param response                 The swagger response.
 */
private void createHeadersModel(AnnAttributeValue annotationAttributeValue, Response response) {
    if (null != annotationAttributeValue) {
        AnnAttributeValue[] headersValueArray = annotationAttributeValue.getAttributeValueArray();
        for (AnnAttributeValue headersValue : headersValueArray) {
            AnnAttachmentInfo headerAnnotationAttachment = headersValue.getAnnotationAttachmentValue();
            Map<String, AnnAttributeValue> headerAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(headerAnnotationAttachment);
            Map<String, Property> headers = new HashMap<>();
            if (null != headerAnnAttributeValueMap.get("name") && null != headerAnnAttributeValueMap.get("headerType")) {
                String headerName = headerAnnAttributeValueMap.get("name").getStringValue();
                String type = headerAnnAttributeValueMap.get("headerType").getStringValue();
                Property property = null;
                if ("string".equals(type)) {
                    property = new StringProperty();
                } else if ("number".equals(type) || "integer".equals(type)) {
                    property = new IntegerProperty();
                } else if ("boolean".equals(type)) {
                    property = new BooleanProperty();
                } else if ("array".equals(type)) {
                    property = new ArrayProperty();
                }
                if (null != property) {
                    if (null != headerAnnAttributeValueMap.get("description")) {
                        property.setDescription(headerAnnAttributeValueMap.get("description").getStringValue());
                    }
                    headers.put(headerName, property);
                }
            }
            response.setHeaders(headers);
        }
    }
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) ParamAnnAttachmentInfo(org.ballerinalang.util.codegen.ParamAnnAttachmentInfo) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) BooleanProperty(io.swagger.models.properties.BooleanProperty) StringProperty(io.swagger.models.properties.StringProperty) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty)

Example 4 with AnnAttributeValue

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

the class SwaggerResourceMapper method addResourceParameters.

/**
 * Creates parameters in the swagger operation using the parameters in the ballerina resource definition.
 *
 * @param resource         The ballerina resource definition.
 * @param operationAdaptor The swagger operation.
 */
private void addResourceParameters(ResourceInfo resource, OperationAdaptor operationAdaptor) {
    if (!"get".equalsIgnoreCase(operationAdaptor.getHttpOperation())) {
        // Creating message body - required.
        ModelImpl messageModel = new ModelImpl();
        messageModel.setType("object");
        Map<String, Model> definitions = new HashMap<>();
        if (!definitions.containsKey("Message")) {
            definitions.put("Message", messageModel);
            this.swaggerDefinition.setDefinitions(definitions);
        }
        // Creating "Message m" parameter
        BodyParameter messageParameter = new BodyParameter();
        messageParameter.setName(resource.getParamNames()[0]);
        RefModel refModel = new RefModel();
        refModel.setReference("Message");
        messageParameter.setSchema(refModel);
        operationAdaptor.getOperation().addParameter(messageParameter);
    }
    // Creating query params and path params
    AttributeInfo attributeInfo = resource.getAttributeInfo(AttributeInfo.Kind.PARAMETER_ANNOTATIONS_ATTRIBUTE);
    if (attributeInfo instanceof ParamAnnotationAttributeInfo) {
        ParamAnnotationAttributeInfo paramAttributeInfo = (ParamAnnotationAttributeInfo) resource.getAttributeInfo(AttributeInfo.Kind.PARAMETER_ANNOTATIONS_ATTRIBUTE);
        ParamAnnAttachmentInfo[] attachmentInfoArray = paramAttributeInfo.getAttachmentInfoArray();
        for (ParamAnnAttachmentInfo paramAnnAttachmentInfo : attachmentInfoArray) {
            if (paramAnnAttachmentInfo.getAnnAttachmentInfos().length > 0) {
                AnnAttachmentInfo annAttachmentInfo = paramAnnAttachmentInfo.getAnnAttachmentInfos()[0];
                Map<String, AnnAttributeValue> paramAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(annAttachmentInfo);
                if (paramAnnAttributeValueMap.size() == 1 && null != paramAnnAttributeValueMap.get("value")) {
                    // Add query parameter
                    if (annAttachmentInfo.getName().equalsIgnoreCase("QueryParam")) {
                        QueryParameter queryParameter = new QueryParameter();
                        // Set in value.
                        queryParameter.setIn("query");
                        // Set parameter name
                        String parameterName = paramAnnAttributeValueMap.get("value").getStringValue();
                        if ((parameterName == null) || parameterName.isEmpty()) {
                            parameterName = resource.getParamNames()[paramAnnAttachmentInfo.getParamIdex()];
                        }
                        queryParameter.setName(parameterName);
                        // Note: 'description' to be added using annotations, hence skipped here.
                        // Setting false to required(as per swagger spec). This can be overridden while parsing
                        // annotations.
                        queryParameter.required(false);
                        // Note: 'allowEmptyValue' to be added using annotations, hence skipped here.
                        // Set type
                        String paramType = resource.getParamTypes()[paramAnnAttachmentInfo.getParamIdex()].getName();
                        if ("int".equals(paramType)) {
                            queryParameter.setType("integer");
                        } else {
                            queryParameter.setType(paramType);
                        }
                        // Note: 'format' to be added using annotations, hence skipped here.
                        operationAdaptor.getOperation().addParameter(queryParameter);
                    }
                    if (annAttachmentInfo.getName().equalsIgnoreCase("PathParam")) {
                        PathParameter pathParameter = new PathParameter();
                        // Set in value
                        pathParameter.setIn("path");
                        // Set parameter name
                        String parameterName = paramAnnAttributeValueMap.get("value").getStringValue();
                        if ((parameterName == null) || parameterName.isEmpty()) {
                            parameterName = resource.getParamNames()[paramAnnAttachmentInfo.getParamIdex()];
                        }
                        pathParameter.setName(parameterName);
                        // Note: 'description' to be added using annotations, hence skipped here.
                        // Note: 'allowEmptyValue' to be added using annotations, hence skipped here.
                        // Set type
                        String paramType = resource.getParamTypes()[paramAnnAttachmentInfo.getParamIdex()].getName();
                        if ("int".equals(paramType)) {
                            pathParameter.setType("integer");
                        } else {
                            pathParameter.setType(paramType);
                        }
                        // Note: 'format' to be added using annotations, hence skipped here.
                        operationAdaptor.getOperation().addParameter(pathParameter);
                    }
                }
            }
        }
    }
}
Also used : ParamAnnAttachmentInfo(org.ballerinalang.util.codegen.ParamAnnAttachmentInfo) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) ParamAnnotationAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamAnnotationAttributeInfo) HashMap(java.util.HashMap) ParamAnnAttachmentInfo(org.ballerinalang.util.codegen.ParamAnnAttachmentInfo) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) ParamAnnotationAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamAnnotationAttributeInfo) AttributeInfo(org.ballerinalang.util.codegen.attributes.AttributeInfo) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) ModelImpl(io.swagger.models.ModelImpl)

Example 5 with AnnAttributeValue

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

the class SwaggerResourceMapper method createExternalDocsModel.

/**
 * Creates external docs swagger definitions.
 *
 * @param annotationAttributeValue The annotation attribute value for external docs.
 * @param operation                The swagger operation.
 */
private void createExternalDocsModel(AnnAttributeValue annotationAttributeValue, Operation operation) {
    if (null != annotationAttributeValue) {
        AnnAttachmentInfo externalDocAnnotationAttachment = annotationAttributeValue.getAnnotationAttachmentValue();
        ExternalDocs externalDocs = new ExternalDocs();
        Map<String, AnnAttributeValue> externalDocsAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(externalDocAnnotationAttachment);
        if (null != externalDocsAnnAttributeValueMap.get("description")) {
            externalDocs.setDescription(externalDocsAnnAttributeValueMap.get("description").getStringValue());
        }
        if (null != externalDocsAnnAttributeValueMap.get("url")) {
            externalDocs.setUrl(externalDocsAnnAttributeValueMap.get("url").getStringValue());
        }
        operation.setExternalDocs(externalDocs);
    }
}
Also used : ParamAnnAttachmentInfo(org.ballerinalang.util.codegen.ParamAnnAttachmentInfo) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) ExternalDocs(io.swagger.models.ExternalDocs)

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