|
@@ -5,13 +5,17 @@ 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.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.BasicResponseHandler;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
import org.asynchttpclient.AsyncCompletionHandler;
|
|
|
import org.asynchttpclient.AsyncHttpClient;
|
|
|
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
|
|
@@ -23,20 +27,19 @@ import org.json.JSONObject;
|
|
|
|
|
|
import ua.be.wee.model.EnactmentController;
|
|
|
import ua.be.wee.model.nodes.AutomatedActivity;
|
|
|
+import ua.be.wee.model.nodes.ports.ControlInputPort;
|
|
|
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 {
|
|
|
+ public static void asyncHTTPClient(PT pt, AutomatedActivity act, ControlInputPort port, 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);
|
|
|
+ String requestBody = createJSONBody(port, artifacts);
|
|
|
|
|
|
Request request = Dsl.post(act.getEndpoint()).setBody(requestBody).setRequestTimeout(Duration.ofMillis(act.getTimeout())).build();
|
|
|
|
|
@@ -82,8 +85,20 @@ public class AsyncHttpClientService {
|
|
|
tArt.setLocation(name);
|
|
|
tArt.setRelatesTo(dataOutputPort.getArtifact());
|
|
|
traceArts.add(tArt);
|
|
|
- } else if (type.equals("url")) {
|
|
|
- //TODO add possibility to link data to URL
|
|
|
+ } else if (type.equals("reference")) {
|
|
|
+ String url = outData.getString("content");
|
|
|
+ String name = outData.getString("name");
|
|
|
+ CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
+ HttpResponse resp = httpclient.execute(httpGet);
|
|
|
+ String responseBody = new BasicResponseHandler().handleResponse(resp);
|
|
|
+ httpclient.close();
|
|
|
+ InputStream is = new ByteArrayInputStream(responseBody.getBytes());
|
|
|
+ control.uploadArtifact(is,name);
|
|
|
+ TraceArtifact tArt = new TraceArtifact();
|
|
|
+ tArt.setLocation(name);
|
|
|
+ tArt.setRelatesTo(dataOutputPort.getArtifact());
|
|
|
+ traceArts.add(tArt);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -147,62 +162,28 @@ public class AsyncHttpClientService {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private static String createJSONBody(Map<String,Pair<String,String>> params) throws JSONException {
|
|
|
+ private static String createJSONBody(ControlInputPort port, 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");
|
|
|
+ if (params.get(dataPort).getFst().equals("txt") ||
|
|
|
+ params.get(dataPort).getFst().equals("csv") ||
|
|
|
+ params.get(dataPort).getFst().equals("xopp")||
|
|
|
+ params.get(dataPort).getFst().equals("drawio")) {
|
|
|
+ inner.put("type", "inline");
|
|
|
+ } else {
|
|
|
+ inner.put("type", "reference");
|
|
|
+ }
|
|
|
inner.put("content", params.get(dataPort).getSnd());
|
|
|
input.put(dataPort, inner);
|
|
|
+
|
|
|
}
|
|
|
body.put("input", input);
|
|
|
+ body.put("ctrl", port.getName());
|
|
|
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");
|
|
|
-
|
|
|
- List<ControlOutputPort> list = new ArrayList<ControlOutputPort>();
|
|
|
-
|
|
|
- ControlOutputPort cout = new ControlOutputPort();
|
|
|
- cout.setName("ok");
|
|
|
- list.add(cout);
|
|
|
-
|
|
|
- a.setCtrlOutPorts(list);
|
|
|
-
|
|
|
-
|
|
|
- List<DataOutputPort> list2 = new ArrayList<DataOutputPort>();
|
|
|
-
|
|
|
- DataOutputPort dout = new DataOutputPort();
|
|
|
- dout.setName("dout");
|
|
|
- list2.add(dout);
|
|
|
-
|
|
|
- a.setDataOutPorts(list2);
|
|
|
-
|
|
|
- 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));
|
|
|
-
|
|
|
- EnactmentController enac = new EnactmentController();
|
|
|
-
|
|
|
- PT pt = new PT();
|
|
|
-
|
|
|
- asyncHTTPClient(pt, a, params,enac);
|
|
|
- System.out.println("chegou aqui");
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
}
|