@@ -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,17 +121,17 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
120
121
} else {
121
122
numResults = engine .search (req , projects );
122
123
}
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 ;
125
129
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 );
131
131
}
132
+
132
133
List <Hit > results = new ArrayList <>(maxResults );
133
- engine .results (0 ,
134
+ engine .results (pageStart ,
134
135
numResults > maxResults ? maxResults : numResults , results );
135
136
JSONArray resultsArray = new JSONArray ();
136
137
for (Hit hit : results ) {
@@ -159,4 +160,28 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
159
160
engine .destroy ();
160
161
}
161
162
}
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
+ }
162
187
}
0 commit comments