com.fivefaces.cloud.workflow.AzureWorkflowService Maven / Gradle / Ivy
package com.fivefaces.cloud.workflow;
import com.amazonaws.services.stepfunctions.model.DescribeExecutionResult;
import com.fivefaces.cloud.Utils;
import com.fivefaces.common.exception.NotImplementedException;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
@Profile("WORKFLOW_AZURE")
@Slf4j
@Service
public class AzureWorkflowService implements WorkflowService {
private final Utils utils;
private final HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.build();
public AzureWorkflowService(Utils utils, ProcessFactory processFactory) {
this.utils = utils;
}
@Override
public WorkflowExecutionResult instantiateWorkflow(final String functionName, final String jsonInput) {
return instantiateSyncWorkflow(functionName, jsonInput);
}
@Override
public WorkflowExecutionResult instantiateSyncWorkflow(final String functionName, final String jsonInput) {
JSONObject jsonObject = new JSONObject(jsonInput);
JSONObject payload = jsonObject.getJSONObject("query");
final String address = utils.getWorkflowEndpoint(functionName);
try {
HttpResponse result = sendPost(address, payload.toString());
if (HttpStatus.valueOf(result.statusCode()).is2xxSuccessful()) {
return new WorkflowExecutionResult(null, "SUCCEEDED", result.body(), null);
}
return new WorkflowExecutionResult(null, "FAILED", result.body(), null);
} catch (Exception exception) {
log.error(exception.getMessage());
}
return new WorkflowExecutionResult(null, "FAILED", null, null);
}
@Override
public DescribeExecutionResult getExecutionResult(final String executionArn) {
throw new NotImplementedException();
}
private HttpResponse sendPost(final String address, final String jsonInput) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(jsonInput))
.uri(URI.create(address))
.header("Content-Type", "application/json")
.build();
return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy