示例#1
0
  /**
   * Update book-keeping fields on the alarm. Returns an up-to-date version of the alarm. Some of
   * its fields may have been updated since the REST client last retrieved the alarm being updated.
   *
   * @param alarmIdPath
   * @param stream input JSON
   * @return updated JSON encoded alarm
   */
  @PUT
  @Path("{alarm_id}")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response update(
      @PathParam("alarm_id") final String alarmIdPath, final InputStream stream) {
    log.info("PUT NEW ALARM at /{}", alarmIdPath);

    try {
      final ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
      log.info("jsonTree={}", jsonTree);

      final Alarm alarm = codec(Alarm.class).decode(jsonTree, this);

      final AlarmService service = get(AlarmService.class);

      if (Long.parseLong(alarmIdPath) != alarm.id().fingerprint()) {
        throw new IllegalArgumentException(
            "id in path is "
                + Long.parseLong(alarmIdPath)
                + " but payload uses id="
                + alarm.id().fingerprint());
      }
      final Alarm updated = service.update(alarm);
      final ObjectNode encoded = new AlarmCodec().encode(updated, this);
      return ok(encoded.toString()).build();

    } catch (IOException ioe) {
      throw new IllegalArgumentException(ioe);
    }
  }
示例#2
0
  /**
   * Get all alarms. Returns a list of all alarms across all devices.
   *
   * @param includeCleared include recently cleared alarms in response
   * @return JSON encoded set of alarms
   */
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Response getAlarms(
      @DefaultValue("false") @QueryParam("includeCleared") final boolean includeCleared) {

    log.info("Requesting all alarms, includeCleared={}", includeCleared);
    final AlarmService service = get(AlarmService.class);

    final Iterable<Alarm> alarms = includeCleared ? service.getAlarms() : service.getActiveAlarms();

    final ObjectNode result = new ObjectMapper().createObjectNode();
    result.set("alarms", codec(Alarm.class).encode(alarms, this));
    return ok(result.toString()).build();
  }