Browse Source

Updates:
- passing the control input port in the service request;
- deciding when the content will be inline or reference (URL) to the
artifact based on the extension of the file.

Lucas Albertins 1 year ago
parent
commit
ba9876aa19

+ 1 - 1
src/main/java/ua/be/wee/controller/EnactmentControllerMVC.java

@@ -306,7 +306,7 @@ public class EnactmentControllerMVC {
         	Activity activity = port.getActivity();
         	if (activity instanceof AutomatedActivity) {
 				AutomatedActivity aut = (AutomatedActivity)activity;
-				controller.callAutomatedActivity(pt,aut, arts);
+				controller.callAutomatedActivity(pt,aut, port, arts);
 				aut.setAutomatedStatus(AutomatedStatus.IN_PROGRESS);
 			} else {
 				endacts.add(port.getActivity());

+ 14 - 4
src/main/java/ua/be/wee/model/EnactmentController.java

@@ -83,9 +83,9 @@ public class EnactmentController {
 		
 	}
 
-	public void callAutomatedActivity(PT pt, AutomatedActivity aut, List<TraceArtifact> arts) throws JSONException, IOException {
+	public void callAutomatedActivity(PT pt, AutomatedActivity aut, ControlInputPort port, List<TraceArtifact> arts) throws JSONException, IOException {
 		Map<String,Pair<String,String>> params = createParamsMap(aut, arts);
-		AsyncHttpClientService.asyncHTTPClient(pt, aut, params,this);
+		AsyncHttpClientService.asyncHTTPClient(pt, aut, port, params,this);
 		
 	}
 
@@ -98,8 +98,18 @@ public class EnactmentController {
 				Artifact relatesTo = traceArtifact.getRelatesTo();
 				if (dataInputPort.getArtifact().getIri().equals(relatesTo.getIri())) {
 					traceArtifact.getLocation();
-					String content = storageService.load(traceArtifact.getLocation());
-					params.put(dataInputPort.getName(), new Pair<String, String>(traceArtifact.getFileExtension(), content));
+					//TODO Instead of this, send both the inline and url to the services
+					if (traceArtifact.getFileExtension().equals("txt") || 
+							traceArtifact.getFileExtension().equals("csv") ||
+							traceArtifact.getFileExtension().equals("xopp")||
+							traceArtifact.getFileExtension().equals("drawio")) {
+						String content = storageService.load(traceArtifact.getLocation());
+						params.put(dataInputPort.getName(), new Pair<String, String>(traceArtifact.getFileExtension(), content));
+					} else {
+						String content = storageService.getURL(traceArtifact.getLocation());
+						params.put(dataInputPort.getName(), new Pair<String, String>(traceArtifact.getFileExtension(), content));
+					}
+					
 				}
 			}
 		}

+ 33 - 52
src/main/java/ua/be/wee/model/util/AsyncHttpClientService.java

@@ -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");
-
-	}
-
 
 }

+ 2 - 0
src/main/java/ua/be/wee/service/FileStorageService.java

@@ -18,4 +18,6 @@ public interface FileStorageService {
   public Stream<Path> loadAll();
   
   public void setStorageURL(String url);
+  
+  public String getURL(String filename);
 }

+ 25 - 0
src/main/java/ua/be/wee/service/FileStorageServiceImpl.java

@@ -176,4 +176,29 @@ public class FileStorageServiceImpl implements FileStorageService {
 		storageServiceURL = url;
 		
 	}
+
+	@Override
+	public String getURL(String filename) {
+		
+			if (storageServiceURL == null) {
+				init();
+			}
+			//Files.copy(file.getInputStream(), this.root.resolve(file.getOriginalFilename()));
+			String url = "";
+			switch (filename.substring(filename.lastIndexOf('.'))) {
+			case ".xopp":
+				url = storageServiceURL + "/files/xopp/"+filename;				
+				break;
+			case ".csv":
+				url = storageServiceURL +"/files/csv/"+filename;				
+				break;
+			case ".drawio":
+				url = storageServiceURL + "/files/drawio/"+filename;				
+				break;
+			default:
+				url = storageServiceURL + "/files/file/"+filename; 
+				break;
+			}
+		return url;
+	}
 }