Skip to content
This repository was archived by the owner on Jan 8, 2019. It is now read-only.

Commit 48f598c

Browse files
author
Lennart Koopmann
committed
extractor exporting. #726
1 parent e4723a2 commit 48f598c

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

app/controllers/ExtractorsController.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package controllers;
2020

21+
import com.fasterxml.jackson.core.JsonProcessingException;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import com.google.common.collect.Lists;
2124
import com.google.inject.Inject;
2225
import lib.APIException;
2326
import lib.ApiClient;
@@ -27,6 +30,7 @@
2730
import play.mvc.Result;
2831

2932
import java.io.IOException;
33+
import java.util.List;
3034
import java.util.Map;
3135

3236
/**
@@ -162,6 +166,44 @@ public Result delete(String nodeId, String inputId, String extractorId) {
162166
}
163167
}
164168

169+
public Result export(String nodeId, String inputId) {
170+
try {
171+
Node node = nodeService.loadNode(nodeId);
172+
Input input = node.getInput(inputId);
173+
174+
BreadcrumbList bc = standardBreadcrumbs(node, input);
175+
bc.addCrumb("Export", routes.ExtractorsController.export(nodeId, inputId));
176+
177+
List<Map<String, Object>> exports = Lists.newArrayList();
178+
for (Extractor extractor : extractorService.all(node, input)) {
179+
exports.add(extractor.export());
180+
}
181+
182+
String extractorExport = "[]";
183+
try {
184+
ObjectMapper om = new ObjectMapper();
185+
extractorExport = om.writeValueAsString(exports);
186+
} catch(JsonProcessingException e) {
187+
Logger.error("Could not generate extractor export.", e);
188+
}
189+
190+
return ok(views.html.system.inputs.extractors.export.render(
191+
currentUser(),
192+
bc,
193+
node,
194+
input,
195+
extractorExport)
196+
);
197+
} catch (IOException e) {
198+
return status(500, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
199+
} catch (APIException e) {
200+
String message = "Could not fetch system information. We expected HTTP 200, but got a HTTP " + e.getHttpCode() + ".";
201+
return status(500, views.html.errors.error.render(message, e, request()));
202+
} catch (NodeService.NodeNotFoundException e) {
203+
return status(404, views.html.errors.error.render(ApiClient.ERROR_MSG_NODE_NOT_FOUND, e, request()));
204+
}
205+
}
206+
165207
private static BreadcrumbList standardBreadcrumbs(Node node, Input input) {
166208
BreadcrumbList bc = new BreadcrumbList();
167209
bc.addCrumb("System", routes.SystemController.index(0));

app/models/Extractor.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,26 @@ public int getOrder() {
394394
return order;
395395
}
396396

397+
public Map<String, Object> export() {
398+
Map<String, Object> export = Maps.newTreeMap();
399+
400+
List<Map<String, Object>> converterConfigList = Lists.newArrayList();
401+
for (Converter converter : converters) {
402+
converterConfigList.add(converter.getConfig());
403+
}
404+
405+
export.put("title", title);
406+
export.put("order", order);
407+
export.put("source_field", sourceField);
408+
export.put("target_field", targetField);
409+
export.put("cursor_strategy", cursorStrategy.toString().toLowerCase());
410+
export.put("condition_type", conditionType.toString().toLowerCase());
411+
export.put("condition_value", conditionValue);
412+
export.put("extractor_type", extractorType.toString().toLowerCase());
413+
export.put("extractor_config", extractorConfig);
414+
export.put("converters", converterConfigList);
415+
416+
return export;
417+
}
418+
397419
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@(currentUser: User, breadcrumbs: lib.BreadcrumbList, node: Node, input: Input, extractorExport: String)
2+
3+
@main("Export extractors", views.html.system.sidebar(), "", currentUser) {
4+
5+
@views.html.partials.breadcrumbs(breadcrumbs)
6+
7+
<div class="row-fluid">
8+
<h1>
9+
<i class="icon icon-cloud-download"></i>
10+
Export extractors of <em>@input.getTitle</em>
11+
</h1>
12+
13+
The extractors of an input can be exported to JSON for importing into other setups
14+
or sharing in the extractor directory on <a href="http://www.graylog2.org/">graylog2.org</a>.
15+
</div>
16+
17+
<div class="row-fluid">
18+
<textarea id="extractor-export-json">@extractorExport</textarea>
19+
</div>
20+
21+
}

app/views/system/inputs/extractors/manage.scala.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
@main("Extractors", views.html.system.sidebar(), "", currentUser) {
44

5+
<div class="pull-right">
6+
<div class="btn-group">
7+
<a class="btn btn-info btn-small dropdown-toggle" data-toggle="dropdown" href="#">
8+
Actions
9+
<span class="caret"></span>
10+
</a>
11+
<ul class="dropdown-menu">
12+
<li><a href="#">Import extractors</a></li>
13+
<li><a href="@routes.ExtractorsController.export(node.getNodeId, input.getId)">Export extractors</a></li>
14+
</ul>
15+
</div>
16+
</div>
17+
518
@views.html.partials.breadcrumbs(breadcrumbs)
619

720
<div class="row-fluid">

conf/routes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ GET /system/inputs/:node_id/:input_id/extractors
9999
GET /system/inputs/:node_id/:input_id/extractors/new @controllers.ExtractorsController.newExtractor(node_id: String, input_id: String, extractor_type ?= "", field ?= "", example ?= "")
100100
POST /system/inputs/:node_id/:input_id/extractors @controllers.ExtractorsController.create(node_id: String, input_id: String)
101101
POST /system/inputs/:node_id/:input_id/extractors/:extractor_id/delete @controllers.ExtractorsController.delete(node_id: String, input_id: String, extractor_id: String)
102+
GET /system/inputs/:node_id/:input_id/extractors/export @controllers.ExtractorsController.export(node_id: String, input_id: String)
102103

103104
# Users
104105
GET /system/users @controllers.UsersController.index()

public/javascripts/extractors.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,10 @@ $(document).ready(function() {
253253
}
254254
});
255255

256+
// Extractor export formatting.
257+
if($("#extractor-export-json").size() > 0) {
258+
var formatted = JSON.stringify(JSON.parse($("#extractor-export-json").val()), null, 2);
259+
$("#extractor-export-json").val(formatted);
260+
}
261+
256262
});

public/stylesheets/main.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,4 +1960,11 @@ hr.search-result-head-separator {
19601960

19611961
.xtrc-order-active {
19621962
background-color: #00a5cf;
1963+
}
1964+
1965+
#extractor-export-json {
1966+
width: 90%;
1967+
height: 700px;
1968+
font-family: monospace;
1969+
font-size: 13px;
19631970
}

0 commit comments

Comments
 (0)