50 lines
1.5 KiB
Java
50 lines
1.5 KiB
Java
package ctbrec.docs;
|
|
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
|
public class SearchServlet extends AbstractDocServlet {
|
|
private static final String Q = "term";
|
|
|
|
@Override
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
if(req.getParameter(Q) == null) {
|
|
error(resp, HttpServletResponse.SC_BAD_REQUEST, "Parameter \""+Q+"\" is missing");
|
|
return;
|
|
}
|
|
|
|
resp.setStatus(HttpServletResponse.SC_OK);
|
|
resp.setContentType("application/json");
|
|
JSONArray result = new JSONArray();
|
|
List<String> pages = getPages();
|
|
|
|
String q = req.getParameter(Q).toLowerCase();
|
|
String[] tokens = q.split("\\s+");
|
|
for (String page : pages) {
|
|
try {
|
|
String content = loadMarkdown("/docs/" + page).toLowerCase();
|
|
boolean allFound = true;
|
|
for (String token : tokens) {
|
|
if(!content.contains(token)) {
|
|
allFound = false;
|
|
}
|
|
}
|
|
if(allFound) {
|
|
result.put(page);
|
|
}
|
|
} catch(FileNotFoundException e) {
|
|
// virtual page like index.md -> ignore
|
|
}
|
|
}
|
|
resp.getWriter().println(result.toString());
|
|
}
|
|
}
|