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

edit CoordinatorWebServer

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