-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StreamsApiController.java
59 lines (50 loc) · 2.12 KB
/
StreamsApiController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package controllers.api;
import com.google.common.collect.Lists;
import controllers.AuthenticatedController;
import controllers.SearchController;
import org.graylog2.restclient.lib.APIException;
import org.graylog2.restclient.models.Stream;
import org.graylog2.restclient.models.StreamService;
import org.graylog2.restclient.models.api.requests.streams.TestMatchRequest;
import org.graylog2.restclient.models.api.responses.streams.TestMatchResponse;
import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Result;
import javax.inject.Inject;
import java.io.IOException;
import java.util.List;
public class StreamsApiController extends AuthenticatedController {
private final StreamService streamService;
@Inject
public StreamsApiController(StreamService streamService) {
this.streamService = streamService;
}
@BodyParser.Of(BodyParser.Json.class)
public Result testMatch(String stream_id) {
TestMatchResponse response = null;
try {
TestMatchRequest tmr = Json.fromJson(request().body().asJson(), TestMatchRequest.class);
response = streamService.testMatch(stream_id, tmr);
} catch (APIException e) {
String message = "Could not test stream rule matching. We expected HTTP 201, but got a HTTP " + e.getHttpCode() + ".";
return status(504, message);
} catch (IOException e) {
return status(504, e.toString());
}
return ok(Json.toJson(response));
}
public Result listStreams() {
List<SearchController.StreamDescription> streamDescriptions = Lists.newArrayList();
try {
final List<Stream> streams = streamService.all();
for (Stream stream : streams) {
streamDescriptions.add(new SearchController.StreamDescription(stream));
}
} catch (IOException e) {
return status(500, "Could not load streams");
} catch (APIException e) {
return status(500, "Could not load streams, received HTTP " + e.getHttpCode() + ": " + e.getMessage());
}
return ok(Json.toJson(streamDescriptions));
}
}