All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aeontronix.enhancedmule.tools.deploy.EnhanceMuleTransformer Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha4
Show newest version
/*
 * Copyright (c) Aeontronix 2020
 */

package com.aeontronix.enhancedmule.tools.deploy;

import com.aeontronix.enhancedmule.tools.api.provision.APIDescriptor;
import com.aeontronix.enhancedmule.tools.api.provision.AnypointDescriptor;
import com.aeontronix.enhancedmule.tools.api.provision.PropertyDescriptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kloudtek.unpack.*;
import com.kloudtek.unpack.transformer.Transformer;
import com.kloudtek.util.io.IOUtils;
import com.kloudtek.util.io.InMemInputFilterStream;
import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.*;

public class EnhanceMuleTransformer extends Transformer {
    public static final String META_INF_MULE_ARTIFACT_MULE_ARTIFACT_JSON = "META-INF/mule-artifact/mule-artifact.json";
    public static final String ENHANCED_MULE_TOOLS_FLOW_XML = "enhanced-mule-tools-flow.xml";
    private final boolean autoDiscovery;
    private AnypointDescriptor apiProvisioningDescriptor;
    private final APIDescriptor api;

    public EnhanceMuleTransformer(@NotNull AnypointDescriptor apiProvisioningDescriptor) {
        this.apiProvisioningDescriptor = apiProvisioningDescriptor;
        api = apiProvisioningDescriptor.getApi();
        this.autoDiscovery = api != null && api.isAddAutoDescovery();
    }

    @Override
    public void apply(Source source, Destination destination) throws UnpackException {
        StringBuilder xml = new StringBuilder("\n\n");
        if (autoDiscovery) {
            xml.append("    \n");
        }
        xml.append("");
        byte[] data = xml.toString().getBytes();
        SourceFile emFlowFile = (SourceFile) source.getFile(ENHANCED_MULE_TOOLS_FLOW_XML);
        if (emFlowFile != null) {
            emFlowFile.setInputStream(new ByteArrayInputStream(data));
        } else {
            source.add(new InMemSourceFile(ENHANCED_MULE_TOOLS_FLOW_XML, ENHANCED_MULE_TOOLS_FLOW_XML, data));
        }

        SourceFile file = (SourceFile) source.getFile(META_INF_MULE_ARTIFACT_MULE_ARTIFACT_JSON);
        if (file == null) {
            throw new UnpackException(META_INF_MULE_ARTIFACT_MULE_ARTIFACT_JSON + " not found");
        }
        file.setInputStream(new InMemInputFilterStream(file.getInputStream()) {
            @SuppressWarnings("unchecked")
            @Override
            protected byte[] transform(byte[] data) throws IOException {
                return IOUtils.toByteArray(os -> {
                    Map json;
                    ObjectMapper om = new ObjectMapper();
                    json = om.readValue(data, Map.class);
                    Map clm = (Map) json.computeIfAbsent("classLoaderModelLoaderDescriptor", key -> new HashMap<>());
                    Map clmAttr = (Map) clm.computeIfAbsent("attributes", key -> new HashMap<>());
                    List exportedResources = (List) clmAttr.computeIfAbsent("exportedResources", key -> new ArrayList());
                    List configs = (List) json.computeIfAbsent("configs", key -> new ArrayList());
                    List secureProperties = (List) json.computeIfAbsent("secureProperties", key -> new ArrayList<>());

                    configs.add(ENHANCED_MULE_TOOLS_FLOW_XML);
                    exportedResources.add(ENHANCED_MULE_TOOLS_FLOW_XML);

                    addSecureProperties(secureProperties);

                    // write
                    om.writeValue(os, json);
                });
            }

            private void addSecureProperties(List secureProperties) {
                HashMap propDesc = apiProvisioningDescriptor.getProperties();
                if (propDesc != null) {
                    for (PropertyDescriptor propertyDescriptor : propDesc.values()) {
                        if (propertyDescriptor.isSecure()) {
                            String name = propertyDescriptor.getName();
                            if( !secureProperties.contains(name)) {
                                secureProperties.add(name);
                            }
                        }
                    }
                }
            }
        });

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy