|
@@ -0,0 +1,156 @@
|
|
|
+package ua.be.wee.model.util;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.time.Duration;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.TimeoutException;
|
|
|
+
|
|
|
+import org.asynchttpclient.AsyncCompletionHandler;
|
|
|
+import org.asynchttpclient.AsyncHttpClient;
|
|
|
+import org.asynchttpclient.DefaultAsyncHttpClientConfig;
|
|
|
+import org.asynchttpclient.Dsl;
|
|
|
+import org.asynchttpclient.Request;
|
|
|
+import org.asynchttpclient.Response;
|
|
|
+import org.springframework.boot.configurationprocessor.json.JSONException;
|
|
|
+import org.springframework.boot.configurationprocessor.json.JSONObject;
|
|
|
+
|
|
|
+import ua.be.wee.model.EnactmentController;
|
|
|
+import ua.be.wee.model.nodes.AutomatedActivity;
|
|
|
+import ua.be.wee.model.nodes.ports.ControlOutputPort;
|
|
|
+import ua.be.wee.model.nodes.ports.DataOutputPort;
|
|
|
+import ua.be.wee.model.pt.PT;
|
|
|
+import ua.be.wee.model.pt.TraceArtifact;
|
|
|
+import ua.be.wee.service.FileStorageService;
|
|
|
+import ua.be.wee.service.FileStorageServiceImpl;
|
|
|
+
|
|
|
+public class AsyncHttpClientService {
|
|
|
+
|
|
|
+ public static void asyncHTTPClient(PT pt, AutomatedActivity act, Map<String,Pair<String,String>> artifacts, EnactmentController control) throws JSONException, IOException {
|
|
|
+ DefaultAsyncHttpClientConfig.Builder clientBuilder = Dsl.config().setConnectTimeout(Duration.ofMillis(act.getTimeout()));
|
|
|
+ AsyncHttpClient client = Dsl.asyncHttpClient(clientBuilder);
|
|
|
+
|
|
|
+ String requestBody = createJSONBody(artifacts);
|
|
|
+
|
|
|
+ Request request = Dsl.post(act.getEndpoint()).setBody(requestBody).setRequestTimeout(Duration.ofMillis(act.getTimeout())).build();
|
|
|
+
|
|
|
+ client.executeRequest(request, new AsyncCompletionHandler<String>() {
|
|
|
+ @Override
|
|
|
+ public String onCompleted(Response response) throws Exception {
|
|
|
+ String result = response.getResponseBody();
|
|
|
+ int statusCode = response.getStatusCode();
|
|
|
+ System.out.println("Status Code: " + statusCode);
|
|
|
+ System.out.println("Body: " + result);
|
|
|
+
|
|
|
+ if (statusCode == 200) {
|
|
|
+ JSONObject res = new JSONObject(result);
|
|
|
+ String outCtrl = res.getString("ctrl");
|
|
|
+ List<ControlOutputPort> ctrlOutPorts = act.getCtrlOutPorts();
|
|
|
+ ControlOutputPort ctrlOutPort = null;
|
|
|
+ for (ControlOutputPort controlOutputPort : ctrlOutPorts) {
|
|
|
+ if (controlOutputPort.getName().equals(outCtrl)) {
|
|
|
+ ctrlOutPort = controlOutputPort;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (ctrlOutPort == null) {
|
|
|
+ throw new Exception("The response of automated activity " + act.getName() + " did not return an output control port that matches one of the activity ports.");
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject outputs = res.getJSONObject("output");
|
|
|
+ List<TraceArtifact> traceArts = new ArrayList<TraceArtifact>();
|
|
|
+ for (Iterator iterator = outputs.keys(); iterator.hasNext();) {
|
|
|
+ String outDataName = (String) iterator.next();
|
|
|
+ List<DataOutputPort> dataOutPorts = act.getDataOutPorts();
|
|
|
+
|
|
|
+ for (DataOutputPort dataOutputPort : dataOutPorts) {
|
|
|
+ if (outDataName.equals(dataOutputPort.getName())) {
|
|
|
+ JSONObject outData = outputs.getJSONObject(outDataName);
|
|
|
+ String type = outData.getString("type");
|
|
|
+ if (type.equals("inline")) {
|
|
|
+ String content = outData.getString("content");
|
|
|
+ String name = outData.getString("name");
|
|
|
+ InputStream is = new ByteArrayInputStream(content.getBytes());
|
|
|
+ control.uploadArtifact(is,name);
|
|
|
+ TraceArtifact tArt = new TraceArtifact();
|
|
|
+ tArt.setLocation(name);
|
|
|
+ tArt.setRelatesTo(dataOutputPort.getArtifact());
|
|
|
+ traceArts.add(tArt);
|
|
|
+ } else if (type.equals("url")) {
|
|
|
+ //TODO add possibility to link data to URL
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Thread.sleep(1000);
|
|
|
+ control.addEndEvent(pt, traceArts, ctrlOutPort);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new Exception("The response of automated activity " + act.getName() + " returned an unexpected status code: " + statusCode);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ client.close();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onThrowable(Throwable t) {
|
|
|
+ if (t instanceof TimeoutException) {
|
|
|
+ System.err.println("Timeout occurred!");
|
|
|
+ } else {
|
|
|
+ System.err.println("Another error occurred!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String createJSONBody(Map<String,Pair<String,String>> params) throws JSONException {
|
|
|
+ JSONObject body = new JSONObject();
|
|
|
+ JSONObject input = new JSONObject();
|
|
|
+ Set<String> keySet = params.keySet();
|
|
|
+ for (String dataPort : keySet) {
|
|
|
+ JSONObject inner = new JSONObject();
|
|
|
+ inner.put("type", "inline");
|
|
|
+ inner.put("content", params.get(dataPort).getSnd());
|
|
|
+ input.put(dataPort, inner);
|
|
|
+ }
|
|
|
+ body.put("input", input);
|
|
|
+ System.out.println(body.toString());
|
|
|
+ return body.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws JSONException, IOException {
|
|
|
+
|
|
|
+
|
|
|
+ FileStorageService storageService = new FileStorageServiceImpl();
|
|
|
+ storageService.setStorageURL("http://localhost:5000");
|
|
|
+
|
|
|
+ AutomatedActivity a = new AutomatedActivity();
|
|
|
+ a.setTimeout(5000);
|
|
|
+ a.setEndpoint("http://localhost:7999");
|
|
|
+
|
|
|
+ Map<String,Pair<String,String>> params = new HashMap<String, Pair<String,String>>();
|
|
|
+
|
|
|
+ String content = storageService.load("mock_requirements.txt");
|
|
|
+ System.out.println(content);
|
|
|
+ params.put("din", new Pair<String, String>("txt", content+ "c"));
|
|
|
+
|
|
|
+ EnactmentController enac = new EnactmentController();
|
|
|
+
|
|
|
+ PT pt = new PT();
|
|
|
+
|
|
|
+ asyncHTTPClient(pt, a, params,enac);
|
|
|
+ System.out.println("chegou aqui");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|