示例#1
0
  /**
   * Used with verb overrides to parse out values from a query string
   *
   * @param query The query to parse
   * @return An UIDMeta object with configured values
   * @throws BadRequestException if a required value was missing or could not be parsed
   */
  private UIDMeta parseUIDMetaQS(final HttpQuery query) {
    final String uid = query.getRequiredQueryStringParam("uid");
    final String type = query.getRequiredQueryStringParam("type");
    final UIDMeta meta = new UIDMeta(UniqueId.stringToUniqueIdType(type), uid);
    final String display_name = query.getQueryStringParam("display_name");
    if (display_name != null) {
      meta.setDisplayName(display_name);
    }

    final String description = query.getQueryStringParam("description");
    if (description != null) {
      meta.setDescription(description);
    }

    final String notes = query.getQueryStringParam("notes");
    if (notes != null) {
      meta.setNotes(notes);
    }

    return meta;
  }
示例#2
0
  /**
   * Handles CRUD calls to individual UIDMeta data entries
   *
   * @param tsdb The TSDB from the RPC router
   * @param query The query for this request
   */
  private void handleUIDMeta(final TSDB tsdb, final HttpQuery query) {

    final HttpMethod method = query.getAPIMethod();
    // GET
    if (method == HttpMethod.GET) {

      final String uid = query.getRequiredQueryStringParam("uid");
      final UniqueIdType type =
          UniqueId.stringToUniqueIdType(query.getRequiredQueryStringParam("type"));
      try {
        final UIDMeta meta = UIDMeta.getUIDMeta(tsdb, type, uid).joinUninterruptibly();
        query.sendReply(query.serializer().formatUidMetaV1(meta));
      } catch (NoSuchUniqueId e) {
        throw new BadRequestException(
            HttpResponseStatus.NOT_FOUND, "Could not find the requested UID", e);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      // POST
    } else if (method == HttpMethod.POST || method == HttpMethod.PUT) {

      final UIDMeta meta;
      if (query.hasContent()) {
        meta = query.serializer().parseUidMetaV1();
      } else {
        meta = this.parseUIDMetaQS(query);
      }

      /**
       * Storage callback used to determine if the storage call was successful or not. Also returns
       * the updated object from storage.
       */
      class SyncCB implements Callback<Deferred<UIDMeta>, Boolean> {

        @Override
        public Deferred<UIDMeta> call(Boolean success) throws Exception {
          if (!success) {
            throw new BadRequestException(
                HttpResponseStatus.INTERNAL_SERVER_ERROR,
                "Failed to save the UIDMeta to storage",
                "This may be caused by another process modifying storage data");
          }

          return UIDMeta.getUIDMeta(tsdb, meta.getType(), meta.getUID());
        }
      }

      try {
        final Deferred<UIDMeta> process_meta =
            meta.syncToStorage(tsdb, method == HttpMethod.PUT).addCallbackDeferring(new SyncCB());
        final UIDMeta updated_meta = process_meta.joinUninterruptibly();
        tsdb.indexUIDMeta(updated_meta);
        query.sendReply(query.serializer().formatUidMetaV1(updated_meta));
      } catch (IllegalStateException e) {
        query.sendStatusOnly(HttpResponseStatus.NOT_MODIFIED);
      } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
      } catch (NoSuchUniqueId e) {
        throw new BadRequestException(
            HttpResponseStatus.NOT_FOUND, "Could not find the requested UID", e);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      // DELETE
    } else if (method == HttpMethod.DELETE) {

      final UIDMeta meta;
      if (query.hasContent()) {
        meta = query.serializer().parseUidMetaV1();
      } else {
        meta = this.parseUIDMetaQS(query);
      }
      try {
        meta.delete(tsdb).joinUninterruptibly();
        tsdb.deleteUIDMeta(meta);
      } catch (IllegalArgumentException e) {
        throw new BadRequestException("Unable to delete UIDMeta information", e);
      } catch (NoSuchUniqueId e) {
        throw new BadRequestException(
            HttpResponseStatus.NOT_FOUND, "Could not find the requested UID", e);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      query.sendStatusOnly(HttpResponseStatus.NO_CONTENT);

    } else {
      throw new BadRequestException(
          HttpResponseStatus.METHOD_NOT_ALLOWED,
          "Method not allowed",
          "The HTTP method [" + method.getName() + "] is not permitted for this endpoint");
    }
  }