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

com.fivefaces.cloud.workflow.function.ValidateStructure Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fivefaces.cloud.workflow.function;

import com.fivefaces.common.util.JsonUtils;
import org.json.JSONObject;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

public class ValidateStructure extends Function{

    public static final String FUNCTION_NAME = "VALIDATE_STRUCTURE";

    public ValidateStructure(RestTemplateBuilder restTemplateBuilder) {
        super(restTemplateBuilder);
    }

    @Override
    public void setup(JSONObject jsonObject) {

    }

    @Override
    public JSONObject process(JSONObject jsonParameters) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8081/api/v1/structure/validateFromWorkflow");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        MultiValueMap requestMap = new LinkedMultiValueMap<>();

        JSONObject jsonSubject = (JSONObject) jsonParameters.get("inputJson");
        JSONObject schema = (JSONObject) jsonParameters.get("schema");
        JSONObject toValidate = new JSONObject();
        toValidate.put("query", jsonSubject);
        toValidate.put("schema", schema);
        requestMap.add("validateJSON", new JsonUtils().marshall(toValidate.toString()));

        HttpEntity> request = new HttpEntity<>(requestMap, headers);
        ResponseEntity response = restTemplateBuilder.build().postForEntity( builder.build().encode().toUri(), request , String.class );
        return new JSONObject(response.getBody());
    }

    @Override
    public void cleanup(JSONObject jsonObject) {

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy