use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorsResource method single.
@GET
@Timed
@ApiOperation(value = "Get information of a single extractor of an input")
@Path("/{extractorId}")
@ApiResponses(value = { @ApiResponse(code = 404, message = "No such input on this node."), @ApiResponse(code = 404, message = "No such extractor on this input.") })
@Produces(MediaType.APPLICATION_JSON)
public ExtractorSummary single(@ApiParam(name = "inputId", required = true) @PathParam("inputId") String inputId, @ApiParam(name = "extractorId", required = true) @PathParam("extractorId") final String extractorId) throws NotFoundException {
checkPermission(RestPermissions.INPUTS_READ, inputId);
final MessageInput input = persistedInputs.get(inputId);
if (input == null) {
LOG.error("Input <{}> not found.", inputId);
throw new javax.ws.rs.NotFoundException("Couldn't find input " + inputId);
}
final Input mongoInput = inputService.find(input.getPersistId());
final Extractor extractor = inputService.getExtractor(mongoInput, extractorId);
return toSummary(extractor);
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorsResource method create.
@POST
@Timed
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Add an extractor to an input", response = ExtractorCreated.class)
@ApiResponses(value = { @ApiResponse(code = 404, message = "No such input on this node."), @ApiResponse(code = 400, message = "No such extractor type."), @ApiResponse(code = 400, message = "Field the extractor should write on is reserved."), @ApiResponse(code = 400, message = "Missing or invalid configuration.") })
@AuditEvent(type = AuditEventTypes.EXTRACTOR_CREATE)
public Response create(@ApiParam(name = "inputId", required = true) @PathParam("inputId") String inputId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateExtractorRequest cer) throws NotFoundException {
checkPermission(RestPermissions.INPUTS_EDIT, inputId);
final Input mongoInput = inputService.find(inputId);
final String id = new com.eaio.uuid.UUID().toString();
final Extractor extractor = buildExtractorFromRequest(cer, id);
try {
inputService.addExtractor(mongoInput, extractor);
} catch (ValidationException e) {
final String msg = "Extractor persist validation failed.";
LOG.error(msg, e);
throw new BadRequestException(msg, e);
}
final String msg = "Added extractor <" + id + "> of type [" + cer.extractorType() + "] to input <" + inputId + ">.";
LOG.info(msg);
activityWriter.write(new Activity(msg, ExtractorsResource.class));
final ExtractorCreated result = ExtractorCreated.create(id);
final URI extractorUri = getUriBuilderToSelf().path(ExtractorsResource.class).path("{inputId}").build(mongoInput.getId());
return Response.created(extractorUri).entity(result).build();
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class ExtractorsResource method terminate.
@DELETE
@Timed
@ApiOperation(value = "Delete an extractor")
@Path("/{extractorId}")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid request."), @ApiResponse(code = 404, message = "Input not found."), @ApiResponse(code = 404, message = "Extractor not found.") })
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.EXTRACTOR_DELETE)
public void terminate(@ApiParam(name = "inputId", required = true) @PathParam("inputId") String inputId, @ApiParam(name = "extractorId", required = true) @PathParam("extractorId") String extractorId) throws NotFoundException {
checkPermission(RestPermissions.INPUTS_EDIT, inputId);
final MessageInput input = persistedInputs.get(inputId);
if (input == null) {
LOG.error("Input <{}> not found.", inputId);
throw new javax.ws.rs.NotFoundException("Couldn't find input " + inputId);
}
// Remove from Mongo.
final Input mongoInput = inputService.find(input.getPersistId());
final Extractor extractor = inputService.getExtractor(mongoInput, extractorId);
inputService.removeExtractor(mongoInput, extractor.getId());
final String msg = "Deleted extractor <" + extractorId + "> of type [" + extractor.getType() + "] " + "from input <" + inputId + ">.";
LOG.info(msg);
activityWriter.write(new Activity(msg, InputsResource.class));
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class InputServiceImpl method totalExtractorCountByType.
@Override
public Map<Extractor.Type, Long> totalExtractorCountByType() {
final DBObject query = new BasicDBObject(InputImpl.EMBEDDED_EXTRACTORS, new BasicDBObject("$exists", true));
try (DBCursor inputs = dbCollection.find(query, new BasicDBObject(InputImpl.EMBEDDED_EXTRACTORS, 1))) {
final Map<Extractor.Type, Long> extractorsCountByType = new HashMap<>();
for (DBObject input : inputs) {
final BasicDBList extractors = (BasicDBList) input.get(InputImpl.EMBEDDED_EXTRACTORS);
for (Object dbObject : extractors) {
final DBObject extractor = (DBObject) dbObject;
final Extractor.Type type = Extractor.Type.fuzzyValueOf(((String) extractor.get(Extractor.FIELD_TYPE)));
if (type != null) {
final Long oldValue = extractorsCountByType.get(type);
final Long newValue = (oldValue == null) ? 1 : oldValue + 1;
extractorsCountByType.put(type, newValue);
}
}
}
return extractorsCountByType;
}
}
use of org.graylog2.plugin.inputs.Extractor in project graylog2-server by Graylog2.
the class InputServiceImpl method getExtractors.
@Override
@SuppressWarnings("unchecked")
public List<Extractor> getExtractors(Input input) {
if (input.getFields().get(InputImpl.EMBEDDED_EXTRACTORS) == null) {
return Collections.emptyList();
}
final ImmutableList.Builder<Extractor> listBuilder = ImmutableList.builder();
final BasicDBList mEx = (BasicDBList) input.getFields().get(InputImpl.EMBEDDED_EXTRACTORS);
for (final Object element : mEx) {
final DBObject ex = (BasicDBObject) element;
// SOFT MIGRATION: does this extractor have an order set? Implemented for issue: #726
Long order = 0L;
if (ex.containsField(Extractor.FIELD_ORDER)) {
/* We use json format to describe our test fixtures
This format will only return Integer on this place,
which can't be converted to long. So I first cast
it to Number and eventually to long */
Number num = (Number) ex.get(Extractor.FIELD_ORDER);
// mongodb driver gives us a java.lang.Long
order = num.longValue();
}
try {
final Extractor extractor = extractorFactory.factory((String) ex.get(Extractor.FIELD_ID), (String) ex.get(Extractor.FIELD_TITLE), order.intValue(), Extractor.CursorStrategy.valueOf(((String) ex.get(Extractor.FIELD_CURSOR_STRATEGY)).toUpperCase(Locale.ENGLISH)), Extractor.Type.valueOf(((String) ex.get(Extractor.FIELD_TYPE)).toUpperCase(Locale.ENGLISH)), (String) ex.get(Extractor.FIELD_SOURCE_FIELD), (String) ex.get(Extractor.FIELD_TARGET_FIELD), (Map<String, Object>) ex.get(Extractor.FIELD_EXTRACTOR_CONFIG), (String) ex.get(Extractor.FIELD_CREATOR_USER_ID), getConvertersOfExtractor(ex), Extractor.ConditionType.valueOf(((String) ex.get(Extractor.FIELD_CONDITION_TYPE)).toUpperCase(Locale.ENGLISH)), (String) ex.get(Extractor.FIELD_CONDITION_VALUE));
listBuilder.add(extractor);
} catch (Exception e) {
LOG.error("Cannot build extractor from persisted data. Skipping.", e);
}
}
return listBuilder.build();
}
Aggregations