Skip to content

Commit 3ea25b7

Browse files
pmvVladimir Kotal
authored and
Vladimir Kotal
committed
Json servlet updates (#1467)
1 parent 43e5b47 commit 3ea25b7

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/org/opensolaris/opengrok/search/SearchEngine.java

+36
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@
3131
import java.io.InputStreamReader;
3232
import java.io.Reader;
3333
import java.util.ArrayList;
34+
import java.util.HashSet;
3435
import java.util.List;
36+
import java.util.Map;
37+
import java.util.Set;
3538
import java.util.SortedSet;
39+
import java.util.TreeMap;
3640
import java.util.TreeSet;
3741
import java.util.logging.Level;
3842
import java.util.logging.Logger;
@@ -235,6 +239,38 @@ public String getQuery() {
235239
return query.toString();
236240
}
237241

242+
/**
243+
* Execute a search aware of current request, limited to specific project names.
244+
*
245+
* This filters out all projects which are not allowed for the current request.
246+
*
247+
* Before calling this function,
248+
* you must set the appropriate search criteria with the set-functions. Note
249+
* that this search will return the first cachePages of hitsPerPage, for
250+
* more you need to call more.
251+
*
252+
* Call to search() must be eventually followed by call to destroy()
253+
* so that IndexSearcher objects are properly freed.
254+
*
255+
* @return The number of hits
256+
* @see ProjectHelper#getAllProjects()
257+
*/
258+
public int search(HttpServletRequest req, String... projectNames) {
259+
ProjectHelper pHelper = PageConfig.get(req).getProjectHelper();
260+
Set<Project> projects = pHelper.getAllProjects();
261+
List<Project> filteredProjects = new ArrayList<Project>();
262+
for(Project project: projects) {
263+
for (String name : projectNames) {
264+
if(project.getName().equalsIgnoreCase(name)) {
265+
filteredProjects.add(project);
266+
}
267+
}
268+
}
269+
return search(
270+
filteredProjects,
271+
new File(RuntimeEnvironment.getInstance().getDataRootFile(), IndexDatabase.INDEX_DIR));
272+
}
273+
238274
/**
239275
* Execute a search aware of current request.
240276
*

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2014, 2017 Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.web;
2424

@@ -47,6 +47,7 @@ public class JSONSearchServlet extends HttpServlet {
4747
private static final String PARAM_PATH = "path";
4848
private static final String PARAM_HIST = "hist";
4949
private static final String PARAM_MAXRESULTS = "maxresults";
50+
private static final String PARAM_PROJECT = "project";
5051
private static final String ATTRIBUTE_DIRECTORY = "directory";
5152
private static final String ATTRIBUTE_FILENAME = "filename";
5253
private static final String ATTRIBUTE_LINENO = "lineno";
@@ -70,6 +71,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
7071
String symbol = req.getParameter(PARAM_SYMBOL);
7172
String path = req.getParameter(PARAM_PATH);
7273
String hist = req.getParameter(PARAM_HIST);
74+
String projects[] = req.getParameterValues(PARAM_PROJECT);
7375

7476
if (freetext != null) {
7577
freetext = URLDecoder.decode(freetext);
@@ -112,7 +114,12 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
112114

113115
try {
114116
long start = System.currentTimeMillis();
115-
int numResults = engine.search(req);
117+
int numResults;
118+
if(projects == null || projects.length == 0) {
119+
numResults = engine.search(req);
120+
} else {
121+
numResults = engine.search(req, projects);
122+
}
116123
int maxResults = MAX_RESULTS;
117124
String maxResultsParam = req.getParameter(PARAM_MAXRESULTS);
118125
if (maxResultsParam != null) {
@@ -146,7 +153,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
146153
result.put(ATTRIBUTE_RESULTS, resultsArray);
147154

148155

149-
156+
resp.setContentType("application/json");
150157
resp.getWriter().write(result.toString());
151158
} finally {
152159
engine.destroy();

0 commit comments

Comments
 (0)