@@ -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,17 +121,17 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
120121 } else {
121122 numResults = engine .search (req , projects );
122123 }
123- int maxResults = MAX_RESULTS ;
124- String maxResultsParam = req .getParameter (PARAM_MAXRESULTS );
124+
125+ int pageStart = getIntParameter (req , PARAM_START , 0 );
126+
127+ Integer maxResultsParam = getIntParameter (req , PARAM_MAXRESULTS , null );
128+ int maxResults = maxResultsParam == null ? MAX_RESULTS : maxResultsParam ;
125129 if (maxResultsParam != null ) {
126- try {
127- maxResults = Integer .parseInt (maxResultsParam );
128- result .put (PARAM_MAXRESULTS , maxResults );
129- } catch (NumberFormatException ex ) {
130- }
130+ result .put (PARAM_MAXRESULTS , maxResults );
131131 }
132+
132133 List <Hit > results = new ArrayList <>(maxResults );
133- engine .results (0 ,
134+ engine .results (pageStart ,
134135 numResults > maxResults ? maxResults : numResults , results );
135136 JSONArray resultsArray = new JSONArray ();
136137 for (Hit hit : results ) {
@@ -159,4 +160,28 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
159160 engine .destroy ();
160161 }
161162 }
163+
164+ /**
165+ * Convenience utility for consistently getting and parsing an integer
166+ * parameter from the provided {@link HttpServletRequest}.
167+ *
168+ * @param request The request to extract the parameter from
169+ * @param paramName The name of the parameter on the request.
170+ * @param defaultValue The default value to use when no value is
171+ * provided or parsing fails.
172+ * @return The integer value of the request param if present or the
173+ * defaultValue if none is present.
174+ */
175+ private static Integer getIntParameter (final HttpServletRequest request , final String paramName , final Integer defaultValue ) {
176+ final String paramValue = request .getParameter (paramName );
177+ if (paramValue == null ) {
178+ return defaultValue ;
179+ }
180+
181+ try {
182+ return Integer .valueOf (paramValue );
183+ } catch (final NumberFormatException ignored ) {}
184+
185+ return defaultValue ;
186+ }
162187}
0 commit comments