Commit 0f68e317 authored by mohammad.salama's avatar mohammad.salama

JSON with Console Added

parent 01a4caf3
......@@ -11,8 +11,10 @@
<component name="ChangeListManager">
<list default="true" id="b8bcd35c-99f3-44c8-a866-59f81c3c8bd0" name="Changes" comment="Working as Strings, no json, problem in coordinator address">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/logs/app.log" beforeDir="false" afterPath="$PROJECT_DIR$/logs/app.log" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/WebSide/ClientApplication.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/WebSide/ClientApplication.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/WebSide/WebClient.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/WebSide/WebClient.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Coordinator.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/AutoHealerAndClusterSearch/AutoHealerAndClusterSearch/Coordinator.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/WebSide/WebServer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/WebSide/WebServer.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......
......@@ -1143,3 +1143,27 @@ java.lang.NullPointerException: Cannot invoke "java.util.List.isEmpty()" because
2024-02-01 13:58:32,801 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 13:59:07,950 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 13:59:07,950 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 14:14:33,164 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main-EventThread] Successfully connected to Zookeeper
2024-02-01 14:14:33,183 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] My Node under Election is/election/c_0000000263
2024-02-01 14:14:33,187 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] I am LEADER
2024-02-01 14:14:33,190 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] The cluster addresses are: []
2024-02-01 14:14:33,193 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] Registered to be Coordinator and I am Master !
2024-02-01 14:14:33,708 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] Starting Application using Java 17.0.5 on M-Salameh with PID 2636 (D:\HIAST\FIY\FS\Distributed Systems\Lab\8\Distributed-Search\target\classes started by M_Salameh in D:\HIAST\FIY\FS\Distributed Systems\Lab\8\Distributed-Search)
2024-02-01 14:14:33,708 DEBUG org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] Running with Spring Boot v2.6.8, Spring v5.3.20
2024-02-01 14:14:33,709 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] No active profile set, falling back to 1 default profile: "default"
2024-02-01 14:14:34,923 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main] Started Application in 1.589 seconds (JVM running for 4.55)
2024-02-01 14:14:40,995 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.Application [main-EventThread] Successfully connected to Zookeeper
2024-02-01 14:14:41,017 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] My Node under Election is/election/c_0000000264
2024-02-01 14:14:41,021 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.LeaderElection [main] I am NOT LEADER
2024-02-01 14:14:41,027 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main] Registered to service registry
2024-02-01 14:14:41,035 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Server started on port 44
2024-02-01 14:14:41,040 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.ServiceRegistry [main-EventThread] The cluster addresses are: [127.0.0.1:44]
2024-02-01 14:19:43,146 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 14:21:33,409 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 14:23:14,019 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 14:23:58,054 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 14:24:33,440 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 14:25:12,043 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 14:26:21,329 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 14:28:46,493 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
2024-02-01 14:29:03,769 INFO org.AutoHealerAndClusterSearch.AutoHealerAndClusterSearch.TransientWorker [main] Coordinator connected: /127.0.0.1
package WebSide;
import com.google.gson.JsonObject;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
......@@ -13,7 +15,11 @@ public class ClientApplication
System.out.println("Enter Search Query : ");
String query = scanner.nextLine();
WebClient webClient = new WebClient();
String ans = webClient.sendQuery(SearchEngine , query.getBytes());
System.out.println(ans);
JsonObject ans = webClient.sendQuery(SearchEngine , query);
String[] kk = ans.toString().split(",");
for (String k : kk)
{
System.out.println(k);
}
}
}
package WebSide;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.JsonReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.StringReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
......@@ -22,8 +27,41 @@ public class WebClient {
this.client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_2).build();
}
public JsonObject sendQuery(String url, String query) throws ExecutionException, InterruptedException
{
/*JsonParser jsonParser = new JsonParser();
JsonObject queryPayload = jsonParser.parse(query).getAsJsonObject();*/
JsonReader jsonReader = new JsonReader(new StringReader(query));
jsonReader.setLenient(true);
CompletableFuture<JsonObject> response = new CompletableFuture<>();
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(query))
.build();
response = client.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(body -> {
JsonObject jsonObject = null;
try {
jsonObject = JsonParser.parseString(body).getAsJsonObject();
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
return jsonObject;
});
return response.join();
}
/* send task (post http request) asynchronously */
public String sendQuery(String url, byte[] queryPayload) throws ExecutionException, InterruptedException {
/*public String sendQuery(String url, byte[] queryPayload) throws ExecutionException, InterruptedException {
CompletableFuture<String> response = new CompletableFuture<>();
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(URI.create(url))
......@@ -37,6 +75,6 @@ public class WebClient {
String tem = response.join();
return tem;
}
}*/
}
......@@ -3,6 +3,8 @@ package WebSide;
import GRPCConnection.AddressProxy;
import GRPCConnection.GRPCClient.GRPCClient;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
......@@ -93,40 +95,31 @@ public class WebServer
{
files.add("NO MATCH FOUND");
}
sendResponse(files, exchange);
sendResponseAsJson(files, exchange);
}
private void sendResponse(List<String> files, HttpExchange exchange) throws IOException {
String s ="";
for(String file : files)
{
s += file;
s+="\n";
private void sendResponseAsJson(List<String> responseList, HttpExchange exchange) throws IOException
{
JsonArray jsonArray = new JsonArray();
for (String file : responseList) {
jsonArray.add(file);
}
exchange.sendResponseHeaders(200, s.getBytes().length);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(s.getBytes());
outputStream.flush();
outputStream.close();
exchange.close();
}
private void sendResponseJson(List<String> responseList, HttpExchange exchange) throws IOException
{
Gson gson = new Gson();
JsonObject jsonResponse = new JsonObject();
jsonResponse.add("files", jsonArray);
String jsonResponse = gson.toJson(responseList);
sendResponseJson(jsonResponse.toString(), exchange);
}
private void sendResponseJson(String jsonResponse, HttpExchange exchange) throws IOException
{
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(200, jsonResponse.getBytes().length);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(jsonResponse.getBytes());
outputStream.flush();
outputStream.close();
exchange.close();
}
}
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