Search in sources :

Example 66 with Attachment

use of org.wso2.carbon.attachment.mgt.api.attachment.Attachment in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processSecrets.

/**
 * Process Secrets annotations.
 *
 * @param attachmentNode Attachment Node
 * @return List of @{@link SecretModel} objects
 */
Set<SecretModel> processSecrets(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<SecretModel> secrets = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        List<BLangExpression> secretAnnotation = ((BLangArrayLiteral) keyValue.valueExpr).exprs;
        for (BLangExpression bLangExpression : secretAnnotation) {
            SecretModel secretModel = new SecretModel();
            List<BLangRecordLiteral.BLangRecordKeyValue> annotationValues = ((BLangRecordLiteral) bLangExpression).getKeyValuePairs();
            for (BLangRecordLiteral.BLangRecordKeyValue annotation : annotationValues) {
                VolumeMountConfig volumeMountConfig = VolumeMountConfig.valueOf(annotation.getKey().toString());
                String annotationValue = resolveValue(annotation.getValue().toString());
                switch(volumeMountConfig) {
                    case name:
                        secretModel.setName(getValidName(annotationValue));
                        break;
                    case mountPath:
                        secretModel.setMountPath(annotationValue);
                        break;
                    case data:
                        List<BLangExpression> data = ((BLangArrayLiteral) annotation.valueExpr).exprs;
                        secretModel.setData(getDataForSecret(data));
                        break;
                    case readOnly:
                        secretModel.setReadOnly(Boolean.parseBoolean(annotationValue));
                        break;
                    default:
                        break;
                }
            }
            secrets.add(secretModel);
        }
    }
    return secrets;
}
Also used : BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) SecretModel(org.ballerinax.kubernetes.models.SecretModel) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 67 with Attachment

use of org.wso2.carbon.attachment.mgt.api.attachment.Attachment in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processServiceAnnotation.

/**
 * Process annotations and create service model object.
 *
 * @param endpointName   ballerina service name
 * @param attachmentNode annotation attachment node.
 * @return Service model object
 */
ServiceModel processServiceAnnotation(String endpointName, AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    ServiceModel serviceModel = new ServiceModel();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        ServiceConfiguration serviceConfiguration = ServiceConfiguration.valueOf(keyValue.getKey().toString());
        String annotationValue = resolveValue(keyValue.getValue().toString());
        switch(serviceConfiguration) {
            case name:
                serviceModel.setName(getValidName(annotationValue));
                break;
            case labels:
                serviceModel.setLabels(getMap(((BLangRecordLiteral) keyValue.valueExpr).keyValuePairs));
                break;
            case serviceType:
                serviceModel.setServiceType(annotationValue);
                break;
            case port:
                serviceModel.setPort(Integer.parseInt(annotationValue));
                break;
            default:
                break;
        }
    }
    if (serviceModel.getName() == null) {
        serviceModel.setName(getValidName(endpointName) + SVC_POSTFIX);
    }
    return serviceModel;
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.ServiceModel) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Example 68 with Attachment

use of org.wso2.carbon.attachment.mgt.api.attachment.Attachment in project ballerina by ballerina-lang.

the class AnnotationDesugar method generateAnnotations.

private void generateAnnotations(AnnotatableNode node, String key, BLangFunction target, BLangVariable annMapVar) {
    if (node.getAnnotationAttachments().size() == 0) {
        return;
    }
    BLangVariable entryVar = createAnnotationMapEntryVar(key, annMapVar, target.body, target.symbol);
    int annCount = 0;
    for (AnnotationAttachmentNode attachment : node.getAnnotationAttachments()) {
        initAnnotation((BLangAnnotationAttachment) attachment, entryVar, target.body, target.symbol, annCount++);
    }
}
Also used : BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 69 with Attachment

use of org.wso2.carbon.attachment.mgt.api.attachment.Attachment in project ballerina by ballerina-lang.

the class AnnotationDesugar method initAnnotation.

private void initAnnotation(BLangAnnotationAttachment attachment, BLangVariable annotationMapEntryVar, BLangBlockStmt target, BSymbol parentSymbol, int index) {
    BLangVariable annotationVar = null;
    if (attachment.annotationSymbol.attachedType != null) {
        // create: AttachedType annotationVar = { annotation-expression }
        annotationVar = ASTBuilderUtil.createVariable(attachment.pos, attachment.annotationName.value, attachment.annotationSymbol.attachedType.type);
        annotationVar.expr = attachment.expr;
        ASTBuilderUtil.defineVariable(annotationVar, parentSymbol, names);
        ASTBuilderUtil.createVariableDefStmt(attachment.pos, target).var = annotationVar;
    }
    // create: annotationMapEntryVar["name$index"] = annotationVar;
    BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(target.pos, target);
    if (annotationVar != null) {
        assignmentStmt.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationVar.symbol);
    } else {
        assignmentStmt.expr = ASTBuilderUtil.createLiteral(target.pos, symTable.nullType, null);
    }
    BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexAccessNode.pos = target.pos;
    indexAccessNode.indexExpr = ASTBuilderUtil.createLiteral(target.pos, symTable.stringType, attachment.annotationSymbol.bvmAlias() + "$" + index);
    indexAccessNode.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationMapEntryVar.symbol);
    indexAccessNode.type = annotationMapEntryVar.symbol.type;
    assignmentStmt.varRefs.add(indexAccessNode);
}
Also used : BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 70 with Attachment

use of org.wso2.carbon.attachment.mgt.api.attachment.Attachment in project ballerina by ballerina-lang.

the class Util method createNestedPartRequest.

/**
 * Two body parts have been wrapped inside multipart/mixed which in turn acts as the child part for the parent
 * multipart/form-data.
 *
 * @param path Resource path
 * @return HTTPTestRequest with nested parts as the entity body
 */
static HTTPTestRequest createNestedPartRequest(String path) {
    List<Header> headers = new ArrayList<>();
    String multipartDataBoundary = MimeUtil.getNewMultipartDelimiter();
    String multipartMixedBoundary = MimeUtil.getNewMultipartDelimiter();
    headers.add(new Header(HttpHeaderNames.CONTENT_TYPE.toString(), "multipart/form-data; boundary=" + multipartDataBoundary));
    String multipartBodyWithNestedParts = "--" + multipartDataBoundary + "\r\n" + "Content-Disposition: form-data; name=\"parent1\"" + "\r\n" + "Content-Type: text/plain; charset=UTF-8" + "\r\n" + "\r\n" + "Parent Part" + "\r\n" + "--" + multipartDataBoundary + "\r\n" + "Content-Disposition: form-data; name=\"parent2\"" + "\r\n" + "Content-Type: multipart/mixed; boundary=" + multipartMixedBoundary + "\r\n" + "\r\n" + "--" + multipartMixedBoundary + "\r\n" + "Content-Disposition: attachment; filename=\"file-02.txt\"" + "\r\n" + "Content-Type: text/plain" + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n" + "\r\n" + "Child Part 1" + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "\r\n" + "Content-Disposition: attachment; filename=\"file-02.txt\"" + "\r\n" + "Content-Type: text/plain" + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n" + "\r\n" + "Child Part 2" + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "--" + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n";
    return MessageUtils.generateHTTPMessage(path, HttpConstants.HTTP_METHOD_POST, headers, multipartBodyWithNestedParts);
}
Also used : Header(org.wso2.carbon.messaging.Header) ArrayList(java.util.ArrayList) BString(org.ballerinalang.model.values.BString)

Aggregations

HashMap (java.util.HashMap)11 AttachmentMgtException (org.wso2.carbon.attachment.mgt.skeleton.AttachmentMgtException)11 TAttachment (org.wso2.carbon.attachment.mgt.skeleton.types.TAttachment)10 DataHandler (javax.activation.DataHandler)9 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)9 ArrayList (java.util.ArrayList)8 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)8 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)8 File (java.io.File)7 Response (javax.ws.rs.core.Response)7 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)7 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)7 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)6 AttachmentDAO (org.wso2.carbon.attachment.mgt.core.dao.AttachmentDAO)6 BaseTaskService (org.wso2.carbon.bpmn.rest.service.base.BaseTaskService)5 IOException (java.io.IOException)4 List (java.util.List)4 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)4 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)4 Attachment (org.wso2.carbon.attachment.mgt.api.attachment.Attachment)4