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

com.networknt.rule.soap.Soap2RestRequestTransformAction Maven / Gradle / Ivy

Go to download

A generic yaml rule action implementation to transform REST to SOAP in the request/response transform interceptor.

The newest version!
package com.networknt.rule.soap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.networknt.rule.RequestTransformAction;
import com.networknt.rule.RuleActionValue;
import com.networknt.rule.soap.exception.InvalidSoapBodyException;
import com.networknt.utility.MapUtil;
import com.networknt.utility.ModuleRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

/**
 * Transform a request body from the XML to JSON in order to access REST API from soap client. This can be used in
 * light-gateway or http-sidecar to change the request from legacy consumer to access REST service.
 *
 */
public class Soap2RestRequestTransformAction implements RequestTransformAction {
    protected static final Logger logger = LoggerFactory.getLogger(Soap2RestRequestTransformAction.class);
    String direction = "";

    public Soap2RestRequestTransformAction() {
        if (logger.isInfoEnabled()) logger.info("Soap2RestRequestTransformAction is constructed");
        ModuleRegistry.registerPlugin(
                Soap2RestRequestTransformAction.class.getPackage().getImplementationTitle(),
                Soap2RestRequestTransformAction.class.getPackage().getImplementationVersion(),
                null,
                Soap2RestRequestTransformAction.class.getName(),
                null,
                null);

    }

    @Override
    public void performAction(String ruleId, String actionId, Map objMap, Map resultMap, Collection actionValues) {
        // get the response body from the objMap and create a new response body in the resultMap. Both in string format.
        logger.info("ruleId: {} actionId: {} actionValues: {}", ruleId, actionId, actionValues);
        if(actionValues == null || actionValues.isEmpty()) {
            logger.error("Rules.yml does not contain ActionValues section. Please fix config");
            return;
        }
        transformRequest(objMap, resultMap);
    }

    private void transformRequest(Map objMap, Map resultMap) {
        String body = (String) objMap.get("requestBody");

        if (logger.isTraceEnabled())
            logger.trace("original request body = {}", body);

        String output = "";
        try {
            output = Util.transformSoap2Rest(body);
            resultMap.put("requestBody", output);
            if (logger.isTraceEnabled()) logger.trace("transformed request body = " + output);
        } catch (JsonProcessingException ioe) {
            logger.error("Transform exception:", ioe);
        }

        // transform the content type header.
        Map headerMap = (Map) objMap.get("requestHeaders");
        Optional contentTypeOptional = MapUtil.getValueIgnoreCase(headerMap, Constants.CONTENT_TYPE);
        if(contentTypeOptional.isPresent()) {
            String contentType = contentTypeOptional.get();

            if (logger.isTraceEnabled())
                logger.trace("request header contentType = {}", contentType);

            if (contentType.contains("/xml")) {
                // transform the content type header.
                RequestTransformAction.super.updateRequestHeader(resultMap, "Content-Type", "application/json");
                if (logger.isTraceEnabled())
                    logger.trace("request contentType has been changed from text/xml to application/json");
            } else {
                throw new InvalidSoapBodyException("Missing Content-Type header text/xml or application/xml in request.");
            }
        } else {
            if(logger.isDebugEnabled()) logger.debug("header Content-Type doesn't exist.");
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy