use of org.opensolaris.opengrok.search.SearchEngine in project OpenGrok by OpenGrok.
the class JSONSearchServlet method doGet.
@SuppressWarnings({ "unchecked", "deprecation" })
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
JSONObject result = new JSONObject();
SearchEngine engine = new SearchEngine();
boolean valid = false;
String freetext = req.getParameter(PARAM_FREETEXT);
String def = req.getParameter(PARAM_DEF);
String symbol = req.getParameter(PARAM_SYMBOL);
String path = req.getParameter(PARAM_PATH);
String hist = req.getParameter(PARAM_HIST);
if (freetext != null) {
freetext = URLDecoder.decode(freetext);
engine.setFreetext(freetext);
valid = true;
result.put(PARAM_FREETEXT, freetext);
}
if (def != null) {
def = URLDecoder.decode(def);
engine.setDefinition(def);
valid = true;
result.put(PARAM_DEF, def);
}
if (symbol != null) {
symbol = URLDecoder.decode(symbol);
engine.setSymbol(symbol);
valid = true;
result.put(PARAM_SYMBOL, symbol);
}
if (path != null) {
path = URLDecoder.decode(path);
engine.setFile(path);
valid = true;
result.put(PARAM_PATH, path);
}
if (hist != null) {
hist = URLDecoder.decode(hist);
engine.setHistory(hist);
valid = true;
result.put(PARAM_HIST, hist);
}
if (!valid) {
return;
}
try {
long start = System.currentTimeMillis();
int numResults = engine.search(req);
int maxResults = MAX_RESULTS;
String maxResultsParam = req.getParameter(PARAM_MAXRESULTS);
if (maxResultsParam != null) {
try {
maxResults = Integer.parseInt(maxResultsParam);
result.put(PARAM_MAXRESULTS, maxResults);
} catch (NumberFormatException ex) {
}
}
List<Hit> results = new ArrayList<>(maxResults);
engine.results(0, numResults > maxResults ? maxResults : numResults, results);
JSONArray resultsArray = new JSONArray();
for (Hit hit : results) {
JSONObject hitJson = new JSONObject();
hitJson.put(ATTRIBUTE_DIRECTORY, JSONObject.escape(hit.getDirectory()));
hitJson.put(ATTRIBUTE_FILENAME, JSONObject.escape(hit.getFilename()));
hitJson.put(ATTRIBUTE_LINENO, hit.getLineno());
hitJson.put(ATTRIBUTE_LINE, conv.encode(hit.getLine()));
hitJson.put(ATTRIBUTE_PATH, hit.getPath());
resultsArray.add(hitJson);
}
long duration = System.currentTimeMillis() - start;
result.put(ATTRIBUTE_DURATION, duration);
result.put(ATTRIBUTE_RESULT_COUNT, results.size());
result.put(ATTRIBUTE_RESULTS, resultsArray);
resp.getWriter().write(result.toString());
} finally {
engine.destroy();
}
}
Aggregations