Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
D
Distributed-Search-Engine
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tammam.alsoleman
Distributed-Search-Engine
Commits
d7f7ebe6
Commit
d7f7ebe6
authored
Jan 21, 2026
by
tammam.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
edit CoordinatorWebServer
parent
2094f998
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
22 deletions
+32
-22
CoordinatorWebServer.java
...java/com/distributed/search/web/CoordinatorWebServer.java
+32
-22
No files found.
src/main/java/com/distributed/search/web/CoordinatorWebServer.java
View file @
d7f7ebe6
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment