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

io.virtualan.core.util.Converter Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
package io.virtualan.core.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.virtualan.core.model.ContentType;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.virtualan.core.model.VirtualServiceKeyValue;
import io.virtualan.core.model.VirtualServiceRequest;
import io.virtualan.entity.VirtualServiceEntity;

@Component("converter")
public class Converter {

    private static final String PARAM_DELIMITER = ":_:";

    @Autowired
    private  ObjectMapper objectMapper;

    private String getString(Object jsonObject) throws JsonProcessingException {
        if(jsonObject != null && jsonObject instanceof LinkedHashMap) {
            return objectMapper.writeValueAsString(jsonObject);
        } else if (jsonObject != null && jsonObject instanceof String){
            return jsonObject.toString();
        }
        return null;
    }


    private Object getJson(String jsonStr)  {
        if(jsonStr != null && !jsonStr.isEmpty()) {
            try {
                return objectMapper.readValue(jsonStr, new TypeReference>(){});
            } catch (Exception e) {
                throw new BadDataException(e.getMessage());
            }
        }
        return null;
    }


    public void convertJsonAsString(VirtualServiceRequest virtualServiceRequest)
            throws JsonProcessingException {
        if(ContentType.JSON.equals(virtualServiceRequest.getContentType())){
            virtualServiceRequest.setInput(getString(virtualServiceRequest.getInput()));
            virtualServiceRequest.setOutput(getString(virtualServiceRequest.getOutput()));
        }
    }

    public VirtualServiceRequest convertAsJson(
            VirtualServiceRequest virtualServiceRequest) {
        VirtualServiceRequest virtualServiceRequestRes = new VirtualServiceRequest();
        BeanUtils.copyProperties(virtualServiceRequest, virtualServiceRequestRes);
        if(ContentType.JSON.equals(virtualServiceRequest.getContentType())){
            virtualServiceRequestRes.setInput(getJson(virtualServiceRequest.getInput() != null ? virtualServiceRequest.getInput().toString() : null));
            virtualServiceRequestRes.setOutput(getJson(virtualServiceRequest.getOutput() != null ? virtualServiceRequest.getOutput().toString() : null));
        }
        return virtualServiceRequestRes;
    }


    public static Map converter(List paramList) {
        final Map mapkeyValue = new HashMap<>();
        if (paramList != null && !paramList.isEmpty()) {
            for (final VirtualServiceKeyValue availableParam : paramList) {
                if (availableParam.getValue() != null) {
                    mapkeyValue.put(availableParam.getKey(), availableParam.getValue());
                }
            }
        }
        return mapkeyValue;
    }

    public static VirtualServiceRequest converterEToR(VirtualServiceEntity mockEntity) {
        final VirtualServiceRequest request = new VirtualServiceRequest();
        BeanUtils.copyProperties(mockEntity, request);
        if(mockEntity.getContentType() != null) {
            request.setContentType(ContentType.valueOf(mockEntity.getContentType()));
        }
        request.setInput(mockEntity.getInput());
        request.setOutput(mockEntity.getOutput());
        request.setAvailableParams(Converter.readParameter(mockEntity.getAvailableParamsList()));
        request.setHeaderParams(Converter.readParameter(mockEntity.getHeaderParamsList()));
        return request;
    }

    public static List readParameter(String paramStr) {
        final List availableParams = new LinkedList<>();
        if (paramStr != null) {
            final String[] availableParamsList = paramStr.split(Converter.PARAM_DELIMITER);
            if (availableParamsList != null && availableParamsList.length > 0) {
                for (final String availableParamsStr : availableParamsList) {
                    if (availableParamsStr.split("(? paranList) {
        final StringBuilder availableParamList = new StringBuilder();
        String availableParamStr = null;
        if (paranList != null && !paranList.isEmpty()) {
            for (final VirtualServiceKeyValue availableParam : paranList) {
                if (availableParam.getValue() != null) {

                    availableParamList.append(availableParam.getKey() + "="
                            + addEscape(availableParam.getValue()) + Converter.PARAM_DELIMITER);
                }
            }
            availableParamStr = availableParamList.toString();
            if (availableParamStr != null && availableParamStr.lastIndexOf(Converter.PARAM_DELIMITER) > 0) {
                return availableParamStr.substring(0,
                        availableParamStr.lastIndexOf(Converter.PARAM_DELIMITER));
            } else if ( availableParamStr != null && availableParamStr.trim().length() > 0) {
                return availableParamStr;
            }
        }
        return availableParamStr;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy