Commit d7f7ebe6 authored by tammam.alsoleman's avatar tammam.alsoleman

edit CoordinatorWebServer

parent 2094f998
...@@ -6,6 +6,7 @@ import com.distributed.search.model.SearchResponse; ...@@ -6,6 +6,7 @@ import com.distributed.search.model.SearchResponse;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpServer;
import com.distributed.search.cluster.ServiceRegistry;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
...@@ -13,7 +14,8 @@ import java.net.InetSocketAddress; ...@@ -13,7 +14,8 @@ import java.net.InetSocketAddress;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
/** /**
* Internal HTTP Server running on the Leader node. * Internal HTTP Server running on the Leader node.
* It receives search queries from the Frontend and returns results in JSON format. * It receives search queries from the Frontend and returns results in JSON format.
...@@ -23,13 +25,15 @@ public class CoordinatorWebServer { ...@@ -23,13 +25,15 @@ public class CoordinatorWebServer {
private final int port; private final int port;
private final SearchClient searchClient; private final SearchClient searchClient;
private final String storageDir; private final String storageDir;
private final ServiceRegistry serviceRegistry;
private HttpServer server; private HttpServer server;
private final Gson gson = new Gson(); private final Gson gson = new Gson();
public CoordinatorWebServer(int port, SearchClient searchClient, String storageDir) { public CoordinatorWebServer(int port, SearchClient searchClient, String storageDir, ServiceRegistry serviceRegistry) {
this.port = port; this.port = port;
this.searchClient = searchClient; this.searchClient = searchClient;
this.storageDir = storageDir; this.storageDir = storageDir;
this.serviceRegistry = serviceRegistry;
} }
public void startServer() { public void startServer() {
...@@ -53,33 +57,39 @@ public class CoordinatorWebServer { ...@@ -53,33 +57,39 @@ public class CoordinatorWebServer {
* Expects query param: /search?query=word1+word2 * Expects query param: /search?query=word1+word2
*/ */
private void handleSearchRequest(HttpExchange exchange) throws IOException { private void handleSearchRequest(HttpExchange exchange) throws IOException {
if (!exchange.getRequestMethod().equalsIgnoreCase("get")) { try {
exchange.close(); String query = exchange.getRequestURI().getQuery();
if (query == null || !query.contains("=")) {
sendResponse("{}".getBytes(), exchange, 400);
return; return;
} }
// Extract query from URI String queryValue = query.substring(query.indexOf("=") + 1);
String query = exchange.getRequestURI().getQuery().split("=")[1];
List<String> terms = Arrays.asList(query.toLowerCase().split("\\+"));
System.out.println("Internal API received query: " + terms); List<String> terms = Arrays.asList(queryValue.toLowerCase().split("[\\s+]+"));
System.out.println("Leader is searching for terms: " + terms);
// Execute distributed search
List<String> allFiles = FileManager.getSortedDocumentNames(storageDir); List<String> allFiles = FileManager.getSortedDocumentNames(storageDir);
List<String> workers = serviceRegistry.getAllServiceAddresses();
searchClient.updateWorkers(workers);
List<SearchResponse.DocumentResult> results = searchClient.performSearch(terms, allFiles); List<SearchResponse.DocumentResult> results = searchClient.performSearch(terms, allFiles);
// Convert results to JSON using GSON
String jsonResponse = gson.toJson(results); String jsonResponse = gson.toJson(results);
sendResponse(jsonResponse.getBytes(), exchange, 200);
sendResponse(jsonResponse.getBytes(), exchange); } catch (Exception e) {
e.printStackTrace();
sendResponse("Internal Server Error".getBytes(), exchange, 500);
}
} }
private void sendResponse(byte[] responseBytes, HttpExchange exchange) throws IOException { private void sendResponse(byte[] responseBytes, HttpExchange exchange, int statusCode) throws IOException {
exchange.getResponseHeaders().add("Content-Type", "application/json"); exchange.getResponseHeaders().add("Content-Type", "application/json");
exchange.sendResponseHeaders(200, responseBytes.length); exchange.sendResponseHeaders(statusCode, responseBytes.length);
OutputStream outputStream = exchange.getResponseBody(); try (OutputStream os = exchange.getResponseBody()) {
outputStream.write(responseBytes); os.write(responseBytes);
outputStream.flush(); os.flush();
outputStream.close(); }
} }
} }
\ 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