コード例 #1
0
  @GET
  @Timed
  @ApiOperation(value = "Get a list of all stream rules")
  @Produces(MediaType.APPLICATION_JSON)
  public String get(
      @ApiParam(
              title = "streamid",
              description = "The id of the stream whose stream rules we want.",
              required = true)
          @PathParam("streamid")
          String streamid) {
    List<Map<String, Object>> streamRules = Lists.newArrayList();
    checkPermission(RestPermissions.STREAMS_READ, streamid);

    final Stream stream;
    try {
      stream = streamService.load(streamid);
    } catch (NotFoundException e) {
      throw new WebApplicationException(404);
    }

    try {
      for (StreamRule streamRule : streamRuleService.loadForStream(stream)) {
        streamRules.add(streamRule.asMap());
      }
    } catch (org.graylog2.database.NotFoundException e) {
      throw new WebApplicationException(404);
    }

    Map<String, Object> result = Maps.newHashMap();
    result.put("total", streamRules.size());
    result.put("stream_rules", streamRules);

    return json(result);
  }
コード例 #2
0
  @POST
  @Timed
  @ApiOperation(value = "Create a stream rule")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response create(
      @ApiParam(
              title = "streamid",
              description = "The stream id this new rule belongs to.",
              required = true)
          @PathParam("streamid")
          String streamid,
      @ApiParam(title = "JSON body", required = true) String body) {
    CreateRequest cr;
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);

    try {
      cr = objectMapper.readValue(body, CreateRequest.class);
    } catch (IOException e) {
      LOG.error("Error while parsing JSON", e);
      throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
    }

    try {
      streamService.load(streamid);
    } catch (NotFoundException e) {
      throw new WebApplicationException(404);
    }

    Map<String, Object> streamRuleData = Maps.newHashMap();
    streamRuleData.put("type", cr.type);
    streamRuleData.put("value", cr.value);
    streamRuleData.put("field", cr.field);
    streamRuleData.put("inverted", cr.inverted);
    streamRuleData.put("stream_id", new ObjectId(streamid));

    final StreamRule streamRule = new StreamRuleImpl(streamRuleData);

    String id;
    try {
      id = streamService.save(streamRule);
    } catch (ValidationException e) {
      LOG.error("Validation error.", e);
      throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
    }

    Map<String, Object> result = Maps.newHashMap();
    result.put("streamrule_id", id);

    return Response.status(Response.Status.CREATED).entity(json(result)).build();
  }