|
@@ -6,15 +6,18 @@ import java.net.http.HttpRequest;
|
|
import java.net.http.HttpRequest.BodyPublisher;
|
|
import java.net.http.HttpRequest.BodyPublisher;
|
|
import java.net.http.HttpResponse;
|
|
import java.net.http.HttpResponse;
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Iterator;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Set;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeoutException;
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
-import org.springframework.core.io.Resource;
|
|
|
|
|
|
+import org.springframework.boot.configurationprocessor.json.JSONException;
|
|
|
|
+import org.springframework.boot.configurationprocessor.json.JSONObject;
|
|
|
|
|
|
import ua.be.wee.model.nodes.Artifact;
|
|
import ua.be.wee.model.nodes.Artifact;
|
|
import ua.be.wee.model.nodes.AutomatedActivity;
|
|
import ua.be.wee.model.nodes.AutomatedActivity;
|
|
@@ -25,25 +28,26 @@ import ua.be.wee.service.FileStorageServiceImpl;
|
|
|
|
|
|
public class HTTPRequestClient {
|
|
public class HTTPRequestClient {
|
|
|
|
|
|
- private static void asyncHTTPClient(AutomatedActivity act, List<TraceArtifact> artifacts) {
|
|
|
|
|
|
+ public static void asyncHTTPClient(AutomatedActivity act, Map<String,Pair<String,String>> artifacts) throws JSONException {
|
|
HttpClient httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1)
|
|
HttpClient httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1)
|
|
.connectTimeout(Duration.ofMillis(act.getTimeout())).build();
|
|
.connectTimeout(Duration.ofMillis(act.getTimeout())).build();
|
|
|
|
|
|
- BodyPublisher requestBody = createJSONBody(act, artifacts);
|
|
|
|
|
|
+ BodyPublisher requestBody = createJSONBody(artifacts);
|
|
|
|
|
|
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(act.getEndpoint()))
|
|
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(act.getEndpoint()))
|
|
.setHeader("User-Agent", "Java Async HTTPClient Example...").POST(requestBody).build();
|
|
.setHeader("User-Agent", "Java Async HTTPClient Example...").POST(requestBody).build();
|
|
- CompletableFuture<HttpResponse<String>> asyncResponse = null;
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
// sendAsync(): Sends the given request asynchronously using this client with
|
|
// sendAsync(): Sends the given request asynchronously using this client with
|
|
// the given response body handler.
|
|
// the given response body handler.
|
|
// Equivalent to: sendAsync(request, responseBodyHandler, null).
|
|
// Equivalent to: sendAsync(request, responseBodyHandler, null).
|
|
|
|
+ CompletableFuture<HttpResponse<String>> asyncResponse = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- asyncResponse = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
|
|
|
String asyncResultBody = null;
|
|
String asyncResultBody = null;
|
|
int asyncResultStatusCode = 0;
|
|
int asyncResultStatusCode = 0;
|
|
try {
|
|
try {
|
|
|
|
+ //asyncResponse.thenRun(() -> System.out.println());
|
|
asyncResultBody = asyncResponse.thenApply(HttpResponse::body).get(act.getTimeout(), TimeUnit.MILLISECONDS);
|
|
asyncResultBody = asyncResponse.thenApply(HttpResponse::body).get(act.getTimeout(), TimeUnit.MILLISECONDS);
|
|
|
|
|
|
// OR:
|
|
// OR:
|
|
@@ -56,7 +60,7 @@ public class HTTPRequestClient {
|
|
// this method throws an (unchecked) CompletionException with the underlying
|
|
// this method throws an (unchecked) CompletionException with the underlying
|
|
// exception as its cause.
|
|
// exception as its cause.
|
|
|
|
|
|
- HttpResponse<String> response = asyncResponse.join();
|
|
|
|
|
|
+ //HttpResponse<String> response = asyncResponse.join();
|
|
asyncResultStatusCode = asyncResponse.thenApply(HttpResponse::statusCode).get(act.getTimeout(), TimeUnit.MILLISECONDS);
|
|
asyncResultStatusCode = asyncResponse.thenApply(HttpResponse::statusCode).get(act.getTimeout(), TimeUnit.MILLISECONDS);
|
|
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
|
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
@@ -67,38 +71,42 @@ public class HTTPRequestClient {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- private static BodyPublisher createJSONBody(AutomatedActivity act, List<TraceArtifact> artifacts) {
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- List<DataInputPort> datalInPorts = act.getDatalInPorts();
|
|
|
|
-
|
|
|
|
- for (DataInputPort dataInputPort : datalInPorts) {
|
|
|
|
- for (TraceArtifact traceArtifact : artifacts) {
|
|
|
|
- Artifact relatesTo = traceArtifact.getRelatesTo();
|
|
|
|
- if (dataInputPort.getArtifact().getIri().equals(relatesTo.getIri())) {
|
|
|
|
- traceArtifact.getLocation();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ private static BodyPublisher 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);
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
- return null;
|
|
|
|
|
|
+ body.put("input", input);
|
|
|
|
+ System.out.println(body.toString());
|
|
|
|
+ return HttpRequest.BodyPublishers.ofString(body.toString());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
|
|
+ public static void main(String[] args) throws JSONException {
|
|
|
|
|
|
|
|
|
|
FileStorageService storageService = new FileStorageServiceImpl();
|
|
FileStorageService storageService = new FileStorageServiceImpl();
|
|
storageService.setStorageURL("http://localhost:5000");
|
|
storageService.setStorageURL("http://localhost:5000");
|
|
- String load = storageService.load("trace.csv");
|
|
|
|
- System.out.println(load);
|
|
|
|
|
|
+
|
|
|
|
+ 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));
|
|
|
|
|
|
-// AutomatedActivity test = new AutomatedActivity();
|
|
|
|
-// test.setEndpoint("https://crunchify.com/wp-content/java/crunchify-java-httpclient-tutorial.html");
|
|
|
|
-// test.setTimeout(5000);
|
|
|
|
-// List<TraceArtifact> arts = new ArrayList<TraceArtifact>();
|
|
|
|
-// asyncHTTPClient(test,arts);
|
|
|
|
|
|
+ asyncHTTPClient(a, params);
|
|
|
|
+ System.out.println("chegou aqui");
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|