@@ -46,6 +46,7 @@ public class JSONSearchServlet extends HttpServlet {
46
46
private static final String PARAM_SYMBOL = "symbol" ;
47
47
private static final String PARAM_PATH = "path" ;
48
48
private static final String PARAM_HIST = "hist" ;
49
+ private static final String PARAM_START = "start" ;
49
50
private static final String PARAM_MAXRESULTS = "maxresults" ;
50
51
private static final String PARAM_PROJECT = "project" ;
51
52
private static final String ATTRIBUTE_DIRECTORY = "directory" ;
@@ -120,6 +121,9 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
120
121
} else {
121
122
numResults = engine .search (req , projects );
122
123
}
124
+
125
+ int pageStart = getIntParameter (req , PARAM_START , 0 );
126
+
123
127
int maxResults = MAX_RESULTS ;
124
128
String maxResultsParam = req .getParameter (PARAM_MAXRESULTS );
125
129
if (maxResultsParam != null ) {
@@ -130,7 +134,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
130
134
}
131
135
}
132
136
List <Hit > results = new ArrayList <>(maxResults );
133
- engine .results (0 ,
137
+ engine .results (pageStart ,
134
138
numResults > maxResults ? maxResults : numResults , results );
135
139
JSONArray resultsArray = new JSONArray ();
136
140
for (Hit hit : results ) {
@@ -159,4 +163,28 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
159
163
engine .destroy ();
160
164
}
161
165
}
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
+ }
162
190
}
0 commit comments