Commit e3906db1 authored by joubran.mansour's avatar joubran.mansour

Implement WebClient and Aggregator

parent 355a464e
target/
.idea/
*.iml
logs/
out/
\ No newline at end of file
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
...@@ -11,17 +12,30 @@ public class Aggregator { ...@@ -11,17 +12,30 @@ public class Aggregator {
/*send task to list of workers*/ /*send task to list of workers*/
public List<String> sendTasksToWorkers(List<String> workersAddresses, List<String> tasks) { public List<String> sendTasksToWorkers(List<String> workersAddresses, List<String> tasks) {
/* return sendTasksToWorkers(workersAddresses, tasks, null);
Add your code here
*/
throw new UnsupportedOperationException();
} }
/*send task to list of workers*/ /*send task to list of workers*/
public List<String> sendTasksToWorkers(List<String> workersAddresses, List<String> tasks, String headers) { public List<String> sendTasksToWorkers(List<String> workersAddresses, List<String> tasks, String headers) {
/* if (workersAddresses.size() != tasks.size()) {
Add your code here throw new IllegalArgumentException(
*/ "Number of workers (" + workersAddresses.size()
throw new UnsupportedOperationException(); + ") must equal number of tasks (" + tasks.size() + ")");
}
List<CompletableFuture<String>> futures = new ArrayList<>();
for (int i = 0; i < workersAddresses.size(); i++) {
byte[] payload = tasks.get(i).getBytes();
CompletableFuture<String> future = (headers == null)
? webClient.sendTask(workersAddresses.get(i), payload)
: webClient.sendTask(workersAddresses.get(i), payload, headers);
futures.add(future);
}
List<String> results = new ArrayList<>();
for (CompletableFuture<String> future : futures) {
results.add(future.join());
}
return results;
} }
} }
\ No newline at end of file
import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.util.ArrayList; import java.net.http.HttpRequest;
import java.util.List; import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public class WebClient { public class WebClient {
...@@ -15,17 +16,29 @@ public class WebClient { ...@@ -15,17 +16,29 @@ public class WebClient {
/* send task (post http request) asynchronously */ /* send task (post http request) asynchronously */
public CompletableFuture<String> sendTask(String url, byte[] requestPayload) { public CompletableFuture<String> sendTask(String url, byte[] requestPayload) {
/* return sendTask(url, requestPayload, null);
Add your code here
*/
throw new UnsupportedOperationException();
} }
/* send task (post http request) asynchronously with custom headers*/ /* send task (post http request) asynchronously with custom headers*/
public CompletableFuture<String> sendTask(String url, byte[] requestPayload, String headers) { public CompletableFuture<String> sendTask(String url, byte[] requestPayload, String headers) {
/* HttpRequest.Builder builder = HttpRequest.newBuilder()
Add your code here .uri(URI.create(url))
*/ .POST(HttpRequest.BodyPublishers.ofByteArray(requestPayload));
throw new UnsupportedOperationException();
if (headers != null && !headers.isBlank()) {
int idx = headers.indexOf(':');
if (idx <= 0) {
throw new IllegalArgumentException(
"Invalid header format, expected 'Name: value' but got: " + headers);
}
String name = headers.substring(0, idx).trim();
String value = headers.substring(idx + 1).trim();
builder.header(name, value);
}
HttpRequest request = builder.build();
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment