Search in sources :

Example 1 with TLS_NAMESPACE_URI

use of com.mulesoft.tools.migration.library.mule.steps.http.AbstractHttpConnectorMigrationStep.TLS_NAMESPACE_URI in project mule-migration-assistant by mulesoft.

the class WsConsumerConfig method execute.

@Override
public void execute(Element object, MigrationReport report) throws RuntimeException {
    Namespace wscNamespace = Namespace.getNamespace("wsc", "http://www.mulesoft.org/schema/mule/wsc");
    getApplicationModel().addNameSpace(wscNamespace.getPrefix(), wscNamespace.getURI(), "http://www.mulesoft.org/schema/mule/wsc/current/mule-wsc.xsd");
    object.setNamespace(wscNamespace);
    object.setName("config");
    Element connection = new Element("connection", wscNamespace);
    copyAttributeIfPresent(object, connection, "service");
    object.removeAttribute("service");
    copyAttributeIfPresent(object, connection, "port");
    object.removeAttribute("port");
    copyAttributeIfPresent(object, connection, "wsdlLocation");
    migrateExpression(connection.getAttribute("wsdlLocation"), expressionMigrator);
    object.removeAttribute("wsdlLocation");
    copyAttributeIfPresent(object, connection, "serviceAddress", "address");
    migrateExpression(connection.getAttribute("address"), expressionMigrator);
    object.removeAttribute("serviceAddress");
    // TODO useConnectorToRetrieveWsdl?
    if (object.getAttribute("useConnectorToRetrieveWsdl") != null) {
        report.report("wsc.wsdl", object, object);
        object.removeAttribute("useConnectorToRetrieveWsdl");
    }
    if (object.getAttribute("connectorConfig") != null) {
        connection.addContent(new Element("custom-transport-configuration", wscNamespace).addContent(new Element("http-transport-configuration", wscNamespace).setAttribute("requesterConfig", object.getAttributeValue("connectorConfig"))));
        object.removeAttribute("connectorConfig");
    } else if (object.getAttribute("connector-ref") != null) {
        String transportConnectorName = object.getAttributeValue("connector-ref");
        final Element requestConfig = new Element("request-config", HTTP_NAMESPACE).setAttribute("name", transportConnectorName);
        final Element requestConnection = new Element("request-connection", HTTP_NAMESPACE);
        requestConfig.addContent(requestConnection);
        addTopLevelElement(requestConfig, object.getDocument());
        String address = connection.getAttributeValue("address");
        processAddress(connection, report).ifPresent(a -> {
            requestConnection.setAttribute("host", getExpressionMigrator().migrateExpression(a.getHost(), true, object));
            if (a.getPort() != null) {
                requestConnection.setAttribute("port", getExpressionMigrator().migrateExpression(a.getPort(), true, object));
            }
            requestConnection.setAttribute("protocol", "HTTP");
        });
        connection.setAttribute("address", address);
        Element connector = getApplicationModel().getNode("/*/*[namespace-uri()='" + HTTP_NAMESPACE_URI + "' and local-name()='connector' and @name='" + transportConnectorName + "']");
        if (connector != null) {
            handleConnector(connector, requestConnection, report, wscNamespace, getApplicationModel());
        } else {
            connector = getApplicationModel().getNode("/*/*[namespace-uri()='" + HTTPS_NAMESPACE_URI + "' and local-name()='connector' and @name='" + transportConnectorName + "']");
            if (connector != null) {
                handleConnector(connector, requestConnection, report, wscNamespace, getApplicationModel());
                migrate(requestConnection, of(connector), report, getApplicationModel(), "tls-client");
            }
        }
        connection.addContent(new Element("custom-transport-configuration", wscNamespace).addContent(new Element("http-transport-configuration", wscNamespace).setAttribute("requesterConfig", transportConnectorName)));
        object.removeAttribute("connector-ref");
    } else {
        // If the protocol is not http, lookup the appropriate connector
        // only https/jms transports supported
        String address = connection.getAttributeValue("address");
        processAddress(connection, report).ifPresent(a -> {
            if ("https".equals(a.getProtocol())) {
                List<Element> connectors = getApplicationModel().getNodes("/*/*[namespace-uri()='" + HTTPS_NAMESPACE_URI + "' and local-name()='connector']");
                if (connectors.isEmpty()) {
                    return;
                }
                Element connector = connectors.iterator().next();
                final Element requestConfig = new Element("request-config", HTTP_NAMESPACE).setAttribute("name", connector.getAttributeValue("name"));
                final Element requestConnection = new Element("request-connection", HTTP_NAMESPACE);
                requestConfig.addContent(requestConnection);
                addTopLevelElement(requestConfig, connector.getDocument());
                requestConnection.setAttribute("host", getExpressionMigrator().migrateExpression(a.getHost(), true, object));
                if (a.getPort() != null) {
                    requestConnection.setAttribute("port", getExpressionMigrator().migrateExpression(a.getPort(), true, object));
                }
                requestConnection.setAttribute("protocol", "HTTPS");
                handleConnector(connector, requestConnection, report, wscNamespace, getApplicationModel());
                migrate(requestConnection, of(connector), report, getApplicationModel(), "tls-client");
                connection.addContent(new Element("custom-transport-configuration", wscNamespace).addContent(new Element("http-transport-configuration", wscNamespace).setAttribute("requesterConfig", connector.getAttributeValue("name"))));
                object.removeAttribute("connector-ref");
            } else if ("jms".equals(a.getProtocol())) {
            // TODO MMT-24
            } else {
                report.report("wsc.unsupportedProtocol", object, object);
            }
        });
        connection.setAttribute("address", address);
    }
    object.addContent(connection);
    Namespace ws3Namespace = Namespace.getNamespace("ws", WS_NAMESPACE_URI);
    if (object.getChild("security", ws3Namespace) != null) {
        Namespace tlsNamespace = Namespace.getNamespace("tls", TLS_NAMESPACE_URI);
        Element security = object.getChild("security", ws3Namespace);
        security.setName("web-service-security");
        security.setNamespace(wscNamespace);
        security.detach();
        connection.addContent(security);
        if (security.getChild("wss-sign", ws3Namespace) != null) {
            Element sign = security.getChild("wss-sign", ws3Namespace);
            sign.setName("sign-security-strategy");
            sign.setNamespace(wscNamespace);
            String tlsContextName = sign.getAttributeValue("tlsContext-ref");
            sign.removeAttribute("tlsContext-ref");
            // TODO signatureKeyIdentifier?
            Element tlsContext = getApplicationModel().getNode("/*/*[namespace-uri()='" + TLS_NAMESPACE_URI + "' and local-name()='context' and @name='" + tlsContextName + "']");
            Element keyStoreConfig = new Element("key-store-configuration", wscNamespace);
            Element keyStore = tlsContext.getChild("key-store", tlsNamespace);
            copyAttributeIfPresent(keyStore, keyStoreConfig, "path", "keyStorePath", false);
            copyAttributeIfPresent(keyStore, keyStoreConfig, "keyPassword", false);
            copyAttributeIfPresent(keyStore, keyStoreConfig, "password", false);
            copyAttributeIfPresent(keyStore, keyStoreConfig, "alias", false);
            copyAttributeIfPresent(keyStore, keyStoreConfig, "type", false);
            sign.addContent(keyStoreConfig);
            if (!getApplicationModel().getNodeOptional("//*[@tlsContext-ref='" + tlsContextName + "']").isPresent()) {
                tlsContext.detach();
            }
            sign.detach();
            security.addContent(sign);
        }
        if (security.getChild("wss-verify-signature", ws3Namespace) != null) {
            Element verifySignature = security.getChild("wss-verify-signature", ws3Namespace);
            verifySignature.setName("verify-signature-security-strategy");
            verifySignature.setNamespace(wscNamespace);
            String tlsContextName = verifySignature.getAttributeValue("tlsContext-ref");
            verifySignature.removeAttribute("tlsContext-ref");
            Element tlsContext = getApplicationModel().getNode("/*/*[namespace-uri()='" + TLS_NAMESPACE_URI + "' and local-name()='context' and @name='" + tlsContextName + "']");
            Element keyStoreConfig = new Element("trust-store-configuration", wscNamespace);
            Element trustStore = tlsContext.getChild("trust-store", tlsNamespace);
            copyAttributeIfPresent(trustStore, keyStoreConfig, "path", "trustStorePath", false);
            copyAttributeIfPresent(trustStore, keyStoreConfig, "password", false);
            copyAttributeIfPresent(trustStore, keyStoreConfig, "alias", false);
            copyAttributeIfPresent(trustStore, keyStoreConfig, "type", false);
            verifySignature.addContent(keyStoreConfig);
            if (!getApplicationModel().getNodeOptional("//*[@tlsContext-ref='" + tlsContextName + "']").isPresent()) {
                tlsContext.detach();
            }
            verifySignature.detach();
            security.addContent(verifySignature);
        }
        if (security.getChild("wss-username-token", ws3Namespace) != null) {
            Element userNameToken = security.getChild("wss-username-token", ws3Namespace);
            userNameToken.setName("username-token-security-strategy");
            userNameToken.setNamespace(wscNamespace);
            userNameToken.detach();
            security.addContent(userNameToken);
        }
        if (security.getChild("wss-timestamp", ws3Namespace) != null) {
            Element timestamp = security.getChild("wss-timestamp", ws3Namespace);
            // TODO checkResponseTimestamp?
            timestamp.setName("timestamp-security-strategy");
            timestamp.setNamespace(wscNamespace);
            timestamp.getAttribute("expires").setName("timeToLive");
            timestamp.detach();
            security.addContent(timestamp);
        }
        if (security.getChild("wss-decrypt", ws3Namespace) != null) {
            Element decrypt = security.getChild("wss-decrypt", ws3Namespace);
            decrypt.setName("decrypt-security-strategy");
            decrypt.setNamespace(wscNamespace);
            String tlsContextName = decrypt.getAttributeValue("tlsContext-ref");
            decrypt.removeAttribute("tlsContext-ref");
            Element tlsContext = getApplicationModel().getNode("/*/*[namespace-uri()='" + TLS_NAMESPACE_URI + "' and local-name()='context' and @name='" + tlsContextName + "']");
            Element keyStoreConfig = new Element("key-store-configuration", wscNamespace);
            Element keyStore = tlsContext.getChild("key-store", tlsNamespace);
            copyAttributeIfPresent(keyStore, keyStoreConfig, "path", "keyStorePath", false);
            copyAttributeIfPresent(keyStore, keyStoreConfig, "keyPassword", false);
            copyAttributeIfPresent(keyStore, keyStoreConfig, "password", false);
            copyAttributeIfPresent(keyStore, keyStoreConfig, "alias", false);
            copyAttributeIfPresent(keyStore, keyStoreConfig, "type", false);
            decrypt.addContent(keyStoreConfig);
            if (decrypt.getAttribute("alias") != null) {
                keyStoreConfig.setAttribute("alias", decrypt.getAttributeValue("alias"));
                decrypt.removeAttribute("alias");
            }
            if (!getApplicationModel().getNodeOptional("//*[@tlsContext-ref='" + tlsContextName + "']").isPresent()) {
                tlsContext.detach();
            }
            decrypt.detach();
            security.addContent(decrypt);
        }
        if (security.getChild("wss-encrypt", ws3Namespace) != null) {
            Element encrypt = security.getChild("wss-encrypt", ws3Namespace);
            encrypt.setName("encrypt-security-strategy");
            encrypt.setNamespace(wscNamespace);
            String tlsContextName = encrypt.getAttributeValue("tlsContext-ref");
            encrypt.removeAttribute("tlsContext-ref");
            Element tlsContext = getApplicationModel().getNode("/*/*[namespace-uri()='" + TLS_NAMESPACE_URI + "' and local-name()='context' and @name='" + tlsContextName + "']");
            Element keyStoreConfig = new Element("key-store-configuration", wscNamespace);
            Element trustStore = tlsContext.getChild("trust-store", tlsNamespace);
            copyAttributeIfPresent(trustStore, keyStoreConfig, "path", "keyStorePath", false);
            copyAttributeIfPresent(trustStore, keyStoreConfig, "password", false);
            copyAttributeIfPresent(trustStore, keyStoreConfig, "alias", false);
            copyAttributeIfPresent(trustStore, keyStoreConfig, "type", false);
            encrypt.addContent(keyStoreConfig);
            if (encrypt.getAttribute("alias") != null) {
                keyStoreConfig.setAttribute("alias", encrypt.getAttributeValue("alias"));
                encrypt.removeAttribute("alias");
            }
            if (!getApplicationModel().getNodeOptional("//*[@tlsContext-ref='" + tlsContextName + "']").isPresent()) {
                tlsContext.detach();
            }
            encrypt.detach();
            security.addContent(encrypt);
        }
    }
}
Also used : HTTP_NAMESPACE_URI(com.mulesoft.tools.migration.library.mule.steps.http.AbstractHttpConnectorMigrationStep.HTTP_NAMESPACE_URI) TLS_NAMESPACE_URI(com.mulesoft.tools.migration.library.mule.steps.http.AbstractHttpConnectorMigrationStep.TLS_NAMESPACE_URI) HttpsOutboundEndpoint.migrate(com.mulesoft.tools.migration.library.mule.steps.http.HttpsOutboundEndpoint.migrate) Optional.of(java.util.Optional.of) MigrationReport(com.mulesoft.tools.migration.step.category.MigrationReport) HTTPS_NAMESPACE_URI(com.mulesoft.tools.migration.library.mule.steps.http.AbstractHttpConnectorMigrationStep.HTTPS_NAMESPACE_URI) TransportsUtils.processAddress(com.mulesoft.tools.migration.step.util.TransportsUtils.processAddress) ExpressionMigrator(com.mulesoft.tools.migration.util.ExpressionMigrator) WS_NAMESPACE_URI(com.mulesoft.tools.migration.library.mule.steps.wsc.WsConsumer.WS_NAMESPACE_URI) HTTP_NAMESPACE(com.mulesoft.tools.migration.library.mule.steps.http.AbstractHttpConnectorMigrationStep.HTTP_NAMESPACE) List(java.util.List) XmlDslUtils.addTopLevelElement(com.mulesoft.tools.migration.step.util.XmlDslUtils.addTopLevelElement) AbstractApplicationModelMigrationStep(com.mulesoft.tools.migration.step.AbstractApplicationModelMigrationStep) ExpressionMigratorAware(com.mulesoft.tools.migration.step.ExpressionMigratorAware) Namespace(org.jdom2.Namespace) XmlDslUtils.migrateExpression(com.mulesoft.tools.migration.step.util.XmlDslUtils.migrateExpression) HttpOutboundEndpoint.handleConnector(com.mulesoft.tools.migration.library.mule.steps.http.HttpOutboundEndpoint.handleConnector) XmlDslUtils.copyAttributeIfPresent(com.mulesoft.tools.migration.step.util.XmlDslUtils.copyAttributeIfPresent) Element(org.jdom2.Element) XmlDslUtils.addTopLevelElement(com.mulesoft.tools.migration.step.util.XmlDslUtils.addTopLevelElement) Element(org.jdom2.Element) List(java.util.List) Namespace(org.jdom2.Namespace)

Aggregations

HTTPS_NAMESPACE_URI (com.mulesoft.tools.migration.library.mule.steps.http.AbstractHttpConnectorMigrationStep.HTTPS_NAMESPACE_URI)1 HTTP_NAMESPACE (com.mulesoft.tools.migration.library.mule.steps.http.AbstractHttpConnectorMigrationStep.HTTP_NAMESPACE)1 HTTP_NAMESPACE_URI (com.mulesoft.tools.migration.library.mule.steps.http.AbstractHttpConnectorMigrationStep.HTTP_NAMESPACE_URI)1 TLS_NAMESPACE_URI (com.mulesoft.tools.migration.library.mule.steps.http.AbstractHttpConnectorMigrationStep.TLS_NAMESPACE_URI)1 HttpOutboundEndpoint.handleConnector (com.mulesoft.tools.migration.library.mule.steps.http.HttpOutboundEndpoint.handleConnector)1 HttpsOutboundEndpoint.migrate (com.mulesoft.tools.migration.library.mule.steps.http.HttpsOutboundEndpoint.migrate)1 WS_NAMESPACE_URI (com.mulesoft.tools.migration.library.mule.steps.wsc.WsConsumer.WS_NAMESPACE_URI)1 AbstractApplicationModelMigrationStep (com.mulesoft.tools.migration.step.AbstractApplicationModelMigrationStep)1 ExpressionMigratorAware (com.mulesoft.tools.migration.step.ExpressionMigratorAware)1 MigrationReport (com.mulesoft.tools.migration.step.category.MigrationReport)1 TransportsUtils.processAddress (com.mulesoft.tools.migration.step.util.TransportsUtils.processAddress)1 XmlDslUtils.addTopLevelElement (com.mulesoft.tools.migration.step.util.XmlDslUtils.addTopLevelElement)1 XmlDslUtils.copyAttributeIfPresent (com.mulesoft.tools.migration.step.util.XmlDslUtils.copyAttributeIfPresent)1 XmlDslUtils.migrateExpression (com.mulesoft.tools.migration.step.util.XmlDslUtils.migrateExpression)1 ExpressionMigrator (com.mulesoft.tools.migration.util.ExpressionMigrator)1 List (java.util.List)1 Optional.of (java.util.Optional.of)1 Element (org.jdom2.Element)1 Namespace (org.jdom2.Namespace)1