Skip to content

Commit b3bd6d8

Browse files
ahornaceVladimir Kotal
authored andcommitted
Add LocalhostFilter test and other REST API improvements (#2190)
1 parent dfbb7d5 commit b3bd6d8

File tree

10 files changed

+312
-68
lines changed

10 files changed

+312
-68
lines changed

opengrok-indexer/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,12 @@ Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
571571
<version>${jersey.version}</version>
572572
<scope>test</scope>
573573
</dependency>
574+
<dependency>
575+
<groupId>org.mockito</groupId>
576+
<artifactId>mockito-core</artifactId>
577+
<version>2.19.0</version>
578+
<scope>test</scope>
579+
</dependency>
574580

575581
</dependencies>
576582

src/org/opensolaris/opengrok/configuration/RuntimeEnvironment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,4 +2010,11 @@ public void removeAnyMessage(final Set<String> tags) {
20102010
messagesContainer.removeAnyMessage(tags);
20112011
}
20122012

2013+
/**
2014+
* @return all messages regardless their tag
2015+
*/
2016+
public Set<AcceptedMessage> getAllMessages() {
2017+
return messagesContainer.getAllMessages();
2018+
}
2019+
20132020
}

src/org/opensolaris/opengrok/web/api/v1/controller/MessagesController.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,16 @@
2929
import javax.validation.Valid;
3030
import javax.ws.rs.Consumes;
3131
import javax.ws.rs.DELETE;
32-
import javax.ws.rs.DefaultValue;
3332
import javax.ws.rs.GET;
3433
import javax.ws.rs.POST;
3534
import javax.ws.rs.Path;
3635
import javax.ws.rs.Produces;
3736
import javax.ws.rs.QueryParam;
37+
import javax.ws.rs.WebApplicationException;
3838
import javax.ws.rs.core.MediaType;
3939
import javax.ws.rs.core.Response;
40+
import java.util.Collections;
4041
import java.util.Set;
41-
import java.util.SortedSet;
42-
43-
import static org.opensolaris.opengrok.web.messages.MessagesContainer.MESSAGES_MAIN_PAGE_TAG;
4442

4543
@Path("/messages")
4644
public class MessagesController {
@@ -56,16 +54,21 @@ public Response addMessage(@Valid final Message message) {
5654
}
5755

5856
@DELETE
59-
public void removeMessagesWithTag(@QueryParam("tag") final Set<String> tags) {
60-
env.removeAnyMessage(tags);
57+
public void removeMessagesWithTag(@QueryParam("tag") final String tag) {
58+
if (tag == null) {
59+
throw new WebApplicationException("Message tag has to be specified", Response.Status.BAD_REQUEST);
60+
}
61+
env.removeAnyMessage(Collections.singleton(tag));
6162
}
6263

6364
@GET
6465
@Produces(MediaType.APPLICATION_JSON)
65-
public SortedSet<AcceptedMessage> getMessages(
66-
@QueryParam("tag") @DefaultValue(MESSAGES_MAIN_PAGE_TAG) final String tag
67-
) {
68-
return env.getMessages(tag);
66+
public Set<AcceptedMessage> getMessages(@QueryParam("tag") final String tag) {
67+
if (tag != null) {
68+
return env.getMessages(tag);
69+
} else {
70+
return env.getAllMessages();
71+
}
6972
}
7073

7174
}

src/org/opensolaris/opengrok/web/api/v1/controller/RepositoriesController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class RepositoriesController {
3838

3939
@GET
4040
@Path("/type")
41-
@Produces(MediaType.APPLICATION_JSON)
41+
@Produces(MediaType.TEXT_PLAIN)
4242
public String getType(@QueryParam("repository") final String repository) {
4343
for (RepositoryInfo ri : env.getRepositories()) {
4444
if (ri.getDirectoryNameRelative().equals(repository)) {

src/org/opensolaris/opengrok/web/api/v1/controller/SearchController.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.ArrayList;
4141
import java.util.Collections;
4242
import java.util.List;
43+
import java.util.Map;
4344
import java.util.stream.Collectors;
4445

4546
@Path("/search")
@@ -51,32 +52,34 @@ public class SearchController {
5152
@Produces(MediaType.APPLICATION_JSON)
5253
public SearchResult search(
5354
@Context final HttpServletRequest req,
54-
@QueryParam("freetext") final String freetext,
55+
@QueryParam("full") final String full,
5556
@QueryParam("def") final String def,
5657
@QueryParam("symbol") final String symbol,
5758
@QueryParam("path") final String path,
5859
@QueryParam("hist") final String hist,
5960
@QueryParam("type") final String type,
6061
@QueryParam("projects") final List<String> projects,
6162
@QueryParam("maxresults") @DefaultValue(MAX_RESULTS + "") final int maxResults,
62-
@QueryParam("start") @DefaultValue(0 + "") final int startIndex
63+
@QueryParam("start") @DefaultValue(0 + "") final int startDocIndex
6364
) {
64-
try (SearchEngineWrapper engine = new SearchEngineWrapper(freetext, def, symbol, path, hist, type)) {
65+
try (SearchEngineWrapper engine = new SearchEngineWrapper(full, def, symbol, path, hist, type)) {
6566

6667
if (!engine.isValid()) {
6768
throw new WebApplicationException("Invalid request", Response.Status.BAD_REQUEST);
6869
}
6970

7071
Instant startTime = Instant.now();
7172

72-
List<SearchHit> hits = engine.search(req, projects, startIndex, maxResults)
73+
Map<String, List<SearchHit>> hits = engine.search(req, projects, startDocIndex, maxResults)
7374
.stream()
74-
.map(hit -> new SearchHit(hit.getLine(), hit.getPath()))
75-
.collect(Collectors.toList());
75+
.collect(Collectors.groupingBy(Hit::getPath,
76+
Collectors.mapping(h -> new SearchHit(h.getLine(), h.getLineno()), Collectors.toList())));
7677

7778
long duration = Duration.between(startTime, Instant.now()).toMillis();
7879

79-
return new SearchResult(duration, engine.numResults, hits, startIndex, startIndex + hits.size());
80+
int endDocument = startDocIndex + hits.size() - 1;
81+
82+
return new SearchResult(duration, engine.numResults, hits, startDocIndex, endDocument);
8083
}
8184
}
8285

@@ -87,14 +90,14 @@ private static class SearchEngineWrapper implements AutoCloseable {
8790
private int numResults;
8891

8992
private SearchEngineWrapper(
90-
final String freetext,
93+
final String full,
9194
final String def,
9295
final String symbol,
9396
final String path,
9497
final String hist,
9598
final String type
9699
) {
97-
engine.setFreetext(freetext);
100+
engine.setFreetext(full);
98101
engine.setDefinition(def);
99102
engine.setSymbol(symbol);
100103
engine.setFile(path);
@@ -105,7 +108,7 @@ private SearchEngineWrapper(
105108
public List<Hit> search(
106109
final HttpServletRequest req,
107110
final List<String> projects,
108-
final int startIndex,
111+
final int startDocIndex,
109112
final int maxResults
110113
) {
111114
if (projects == null || projects.isEmpty()) {
@@ -114,17 +117,17 @@ public List<Hit> search(
114117
numResults = engine.search(req, projects.toArray(new String[0]));
115118
}
116119

117-
if (startIndex > numResults) {
120+
if (startDocIndex > numResults) {
118121
return Collections.emptyList();
119122
}
120123

121-
int resultSize = numResults - startIndex;
124+
int resultSize = numResults - startDocIndex;
122125
if (resultSize > maxResults) {
123126
resultSize = maxResults;
124127
}
125128

126-
List<Hit> results = new ArrayList<>(resultSize);
127-
engine.results(startIndex, startIndex + resultSize, results);
129+
List<Hit> results = new ArrayList<>();
130+
engine.results(startDocIndex, startDocIndex + resultSize, results);
128131

129132
return results;
130133
}
@@ -141,68 +144,68 @@ public void close() {
141144

142145
private static class SearchResult {
143146

144-
private final long duration;
147+
private final long time;
145148

146149
private final int resultCount;
147150

148-
private final int startIndex;
151+
private final int startDocument;
149152

150-
private final int endIndex;
153+
private final int endDocument;
151154

152-
private final List<SearchHit> results;
155+
private final Map<String, List<SearchHit>> results;
153156

154157
private SearchResult(
155-
final long duration,
158+
final long time,
156159
final int resultCount,
157-
final List<SearchHit> results,
158-
final int startIndex,
159-
final int endIndex
160+
final Map<String, List<SearchHit>> results,
161+
final int startDocument,
162+
final int endDocument
160163
) {
161-
this.duration = duration;
164+
this.time = time;
162165
this.resultCount = resultCount;
163166
this.results = results;
164-
this.startIndex = startIndex;
165-
this.endIndex = endIndex;
167+
this.startDocument = startDocument;
168+
this.endDocument = endDocument;
166169
}
167170

168-
public long getDuration() {
169-
return duration;
171+
public long getTime() {
172+
return time;
170173
}
171174

172175
public int getResultCount() {
173176
return resultCount;
174177
}
175178

176-
public List<SearchHit> getResults() {
179+
public Map<String, List<SearchHit>> getResults() {
177180
return results;
178181
}
179182

180-
public int getStartIndex() {
181-
return startIndex;
183+
public int getStartDocument() {
184+
return startDocument;
182185
}
183186

184-
public int getEndIndex() {
185-
return endIndex;
187+
public int getEndDocument() {
188+
return endDocument;
186189
}
187190
}
188191

189192
private static class SearchHit {
190193

191194
private final String line;
192195

193-
private final String path;
196+
private final String lineNumber;
194197

195-
private SearchHit(final String line, final String path) {
198+
private SearchHit(final String line, final String lineNumber) {
196199
this.line = line;
197-
this.path = path;
200+
this.lineNumber = lineNumber;
198201
}
199202

200203
public String getLine() {
201204
return line;
202205
}
203206

204-
public String getPath() {
205-
return path;
207+
public String getLineNumber() {
208+
return lineNumber;
206209
}
207210
}
208211

src/org/opensolaris/opengrok/web/api/v1/filter/LocalhostFilter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.ws.rs.ext.Provider;
3535
import java.io.IOException;
3636
import java.net.InetAddress;
37+
import java.util.Arrays;
3738
import java.util.Collections;
3839
import java.util.HashSet;
3940
import java.util.Set;
@@ -51,7 +52,9 @@ public class LocalhostFilter implements ContainerRequestFilter {
5152
@Context
5253
private HttpServletRequest request;
5354

54-
private final Set<String> localAddresses = new HashSet<>();
55+
private final Set<String> localAddresses = new HashSet<>(Arrays.asList(
56+
"127.0.0.1", "0:0:0:0:0:0:0:1", "localhost"
57+
));
5558

5659
@PostConstruct
5760
public void init() {

src/org/opensolaris/opengrok/web/messages/MessagesContainer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.io.IOException;
3131
import java.time.Instant;
32+
import java.util.Collection;
3233
import java.util.Date;
3334
import java.util.HashMap;
3435
import java.util.Map;
@@ -39,6 +40,7 @@
3940
import java.util.TimerTask;
4041
import java.util.TreeSet;
4142
import java.util.function.Predicate;
43+
import java.util.stream.Collectors;
4244

4345
public class MessagesContainer {
4446

@@ -56,6 +58,18 @@ public class MessagesContainer {
5658

5759
private final Object lock = new Object();
5860

61+
/**
62+
* @return all messages regardless their tag
63+
*/
64+
public Set<AcceptedMessage> getAllMessages() {
65+
synchronized (lock) {
66+
if (expirationTimer == null) {
67+
expireMessages();
68+
}
69+
return tagMessages.values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
70+
}
71+
}
72+
5973
/**
6074
* Get the default set of messages for the main tag.
6175
*

0 commit comments

Comments
 (0)