use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.
the class JsonTesterResource method testJsonExtractor.
private JsonTesterResponse testJsonExtractor(String testString, boolean flatten, String listSeparator, String keySeparator, String kvSeparator, boolean replaceKeyWhitespace, String keyWhitespaceReplacement, String keyPrefix) {
final Map<String, Object> config = ImmutableMap.<String, Object>builder().put("flatten", flatten).put("list_separator", listSeparator).put("key_separator", keySeparator).put("kv_separator", kvSeparator).put("replace_key_whitespace", replaceKeyWhitespace).put("key_whitespace_replacement", keyWhitespaceReplacement).put("key_prefix", keyPrefix).build();
final JsonExtractor extractor;
try {
extractor = new JsonExtractor(new MetricRegistry(), "test", "Test", 0L, Extractor.CursorStrategy.COPY, "test", "test", config, getCurrentUser().getName(), Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, "");
} catch (Extractor.ReservedFieldException e) {
throw new BadRequestException("Trying to overwrite a reserved message field", e);
} catch (ConfigurationException e) {
throw new BadRequestException("Invalid extractor configuration", e);
}
final Map<String, Object> result;
try {
result = extractor.extractJson(testString);
} catch (IOException e) {
throw new BadRequestException("Failure running JSON extractor: " + e.getMessage(), e);
}
return JsonTesterResponse.create(result, flatten, listSeparator, keySeparator, kvSeparator, testString);
}
use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.
the class StreamResource method create.
@POST
@Timed
@ApiOperation(value = "Create a stream")
@RequiresPermissions(RestPermissions.STREAMS_CREATE)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.STREAM_CREATE)
public Response create(@ApiParam(name = "JSON body", required = true) final CreateStreamRequest cr, @Context UserContext userContext) throws ValidationException {
// Create stream.
final Stream stream = streamService.create(cr, getCurrentUser().getName());
stream.setDisabled(true);
final IndexSet indexSet = stream.getIndexSet();
if (!indexSet.getConfig().isWritable()) {
throw new BadRequestException("Assigned index set must be writable!");
} else if (!indexSet.getConfig().isRegularIndex()) {
throw new BadRequestException("Assigned index set is not usable");
}
final Set<StreamRule> streamRules = cr.rules().stream().map(streamRule -> streamRuleService.create(null, streamRule)).collect(Collectors.toSet());
final String id = streamService.saveWithRulesAndOwnership(stream, streamRules, userContext.getUser());
final Map<String, String> result = ImmutableMap.of("stream_id", id);
final URI streamUri = getUriBuilderToSelf().path(StreamResource.class).path("{streamId}").build(id);
return Response.created(streamUri).entity(result).build();
}
use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.
the class StreamAlertConditionResource method convertConfigurationInRequest.
private CreateConditionRequest convertConfigurationInRequest(final CreateConditionRequest request) {
final AlertCondition.Factory factory = alertConditionMap.get(request.type());
if (factory == null) {
throw new BadRequestException("Unable to load alert condition of type " + request.type());
}
final ConfigurationRequest requestedConfiguration = factory.config().getRequestedConfiguration();
// coerce the configuration to their correct types according to the condition's requested config
final Map<String, Object> parameters;
try {
parameters = ConfigurationMapConverter.convertValues(request.parameters(), requestedConfiguration);
} catch (ValidationException e) {
throw new BadRequestException("Invalid alert condition parameters", e);
}
return request.toBuilder().setParameters(parameters).build();
}
use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.
the class StreamAlertConditionResource method update.
@PUT
@Timed
@Path("{conditionId}")
@ApiOperation(value = "Modify an alert condition")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.ALERT_CONDITION_UPDATE)
public void update(@ApiParam(name = "streamId", value = "The stream id the alert condition belongs to.", required = true) @PathParam("streamId") String streamid, @ApiParam(name = "conditionId", value = "The alert condition id.", required = true) @PathParam("conditionId") String conditionid, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateConditionRequest ccr) throws NotFoundException, ValidationException {
checkPermission(RestPermissions.STREAMS_EDIT, streamid);
final Stream stream = streamService.load(streamid);
AlertCondition alertCondition = streamService.getAlertCondition(stream, conditionid);
try {
final AlertCondition updatedCondition = alertService.updateFromRequest(alertCondition, convertConfigurationInRequest(ccr));
streamService.updateAlertCondition(stream, updatedCondition);
} catch (ConfigurationException e) {
throw new BadRequestException("Invalid alert condition parameters", e);
}
}
use of javax.ws.rs.BadRequestException 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();
}
Aggregations