Skip to content

Commit

Permalink
Merge pull request #1715 from jpitz/master
Browse files Browse the repository at this point in the history
Added support for paginating the JSONSearchServlet
  • Loading branch information
Vladimir Kotal committed Aug 2, 2017
2 parents d704517 + 0b40649 commit 1856e12
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/org/opensolaris/opengrok/web/JSONSearchServlet.java
Expand Up @@ -46,6 +46,7 @@ public class JSONSearchServlet extends HttpServlet {
private static final String PARAM_SYMBOL = "symbol";
private static final String PARAM_PATH = "path";
private static final String PARAM_HIST = "hist";
private static final String PARAM_START = "start";
private static final String PARAM_MAXRESULTS = "maxresults";
private static final String PARAM_PROJECT = "project";
private static final String ATTRIBUTE_DIRECTORY = "directory";
Expand Down Expand Up @@ -120,17 +121,17 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
} else {
numResults = engine.search(req, projects);
}
int maxResults = MAX_RESULTS;
String maxResultsParam = req.getParameter(PARAM_MAXRESULTS);

int pageStart = getIntParameter(req, PARAM_START, 0);

Integer maxResultsParam = getIntParameter(req, PARAM_MAXRESULTS, null);
int maxResults = maxResultsParam == null ? MAX_RESULTS : maxResultsParam;
if (maxResultsParam != null) {
try {
maxResults = Integer.parseInt(maxResultsParam);
result.put(PARAM_MAXRESULTS, maxResults);
} catch (NumberFormatException ex) {
}
result.put(PARAM_MAXRESULTS, maxResults);
}

List<Hit> results = new ArrayList<>(maxResults);
engine.results(0,
engine.results(pageStart,
numResults > maxResults ? maxResults : numResults, results);
JSONArray resultsArray = new JSONArray();
for (Hit hit : results) {
Expand Down Expand Up @@ -159,4 +160,28 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
engine.destroy();
}
}

/**
* Convenience utility for consistently getting and parsing an integer
* parameter from the provided {@link HttpServletRequest}.
*
* @param request The request to extract the parameter from
* @param paramName The name of the parameter on the request.
* @param defaultValue The default value to use when no value is
* provided or parsing fails.
* @return The integer value of the request param if present or the
* defaultValue if none is present.
*/
private static Integer getIntParameter(final HttpServletRequest request, final String paramName, final Integer defaultValue) {
final String paramValue = request.getParameter(paramName);
if (paramValue == null) {
return defaultValue;
}

try {
return Integer.valueOf(paramValue);
} catch (final NumberFormatException ignored) {}

return defaultValue;
}
}

0 comments on commit 1856e12

Please sign in to comment.