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

com.fivefaces.cloud.workflow.function.QueryStructure 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 QueryStructure extends Function{

    public static final String FUNCTION_NAME = "QUERY_STRUCTURE";

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

    @Override
    public void setup(JSONObject jsonParameters) {

    }

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

        JSONObject query = (JSONObject) jsonParameters.get("query");
        requestMap.add("queryJSON", new JsonUtils().marshall(query.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 jsonParameters) {

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy