Skip to content

Commit c20b917

Browse files
author
Jay Pitzeruse
committed
Added support for paginating the JSONSearchServlet
1 parent d704517 commit c20b917

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/org/opensolaris/opengrok/web/JSONSearchServlet.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class JSONSearchServlet extends HttpServlet {
4646
private static final String PARAM_SYMBOL = "symbol";
4747
private static final String PARAM_PATH = "path";
4848
private static final String PARAM_HIST = "hist";
49+
private static final String PARAM_START = "start";
4950
private static final String PARAM_MAXRESULTS = "maxresults";
5051
private static final String PARAM_PROJECT = "project";
5152
private static final String ATTRIBUTE_DIRECTORY = "directory";
@@ -120,6 +121,9 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
120121
} else {
121122
numResults = engine.search(req, projects);
122123
}
124+
125+
int pageStart = getIntParameter(req, PARAM_START, 0);
126+
123127
int maxResults = MAX_RESULTS;
124128
String maxResultsParam = req.getParameter(PARAM_MAXRESULTS);
125129
if (maxResultsParam != null) {
@@ -130,7 +134,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
130134
}
131135
}
132136
List<Hit> results = new ArrayList<>(maxResults);
133-
engine.results(0,
137+
engine.results(pageStart,
134138
numResults > maxResults ? maxResults : numResults, results);
135139
JSONArray resultsArray = new JSONArray();
136140
for (Hit hit : results) {
@@ -159,4 +163,28 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
159163
engine.destroy();
160164
}
161165
}
166+
167+
/**
168+
* Convenience utility for consistently getting and parsing an integer
169+
* parameter from the provided {@link HttpServletRequest}.
170+
*
171+
* @param request The request to extract the parameter from
172+
* @param paramName The name of the parameter on the request.
173+
* @param defaultValue The default value to use when no value is
174+
* provided or parsing fails.
175+
* @return The integer value of the request param if present or the
176+
* defaultValue if none is present.
177+
*/
178+
private static int getIntParameter(final HttpServletRequest request, final String paramName, final int defaultValue) {
179+
final String paramValue = request.getParameter(paramName);
180+
if (paramValue == null) {
181+
return defaultValue;
182+
}
183+
184+
try {
185+
return Integer.valueOf(paramValue);
186+
} catch (final NumberFormatException ignored) {}
187+
188+
return defaultValue;
189+
}
162190
}

0 commit comments

Comments
 (0)