コード例 #1
0
  void doDelete(final HttpServletRequest req, final HttpServletResponse resp) {
    try {
      // Parse out the required API versions.
      final AcceptAPIVersion acceptVersion = parseAcceptAPIVersion(req);

      // Prepare response.
      prepareResponse(req, resp);

      // Validate request.
      preprocessRequest(req);
      rejectIfNoneMatch(req);

      final Map<String, String[]> parameters = req.getParameterMap();
      final DeleteRequest request =
          Requests.newDeleteRequest(getResourceName(req)).setRevision(getIfMatch(req));
      for (final Map.Entry<String, String[]> p : parameters.entrySet()) {
        final String name = p.getKey();
        final String[] values = p.getValue();
        if (parseCommonParameter(name, values, request)) {
          continue;
        } else {
          request.setAdditionalParameter(name, asSingleValue(name, values));
        }
      }
      doRequest(req, resp, acceptVersion, request);
    } catch (final Exception e) {
      fail(req, resp, e);
    }
  }
コード例 #2
0
  void doPost(final HttpServletRequest req, final HttpServletResponse resp) {
    try {
      // Parse out the required API versions.
      final AcceptAPIVersion acceptVersion = parseAcceptAPIVersion(req);

      // Prepare response.
      prepareResponse(req, resp);

      // Validate request.
      preprocessRequest(req);
      rejectIfNoneMatch(req);
      rejectIfMatch(req);

      final Map<String, String[]> parameters = req.getParameterMap();
      final String action = asSingleValue(PARAM_ACTION, getParameter(req, PARAM_ACTION));
      if (action.equalsIgnoreCase(ACTION_ID_CREATE)) {
        final JsonValue content = getJsonContent(req);
        final CreateRequest request = Requests.newCreateRequest(getResourceName(req), content);
        for (final Map.Entry<String, String[]> p : parameters.entrySet()) {
          final String name = p.getKey();
          final String[] values = p.getValue();
          if (parseCommonParameter(name, values, request)) {
            continue;
          } else if (name.equalsIgnoreCase(PARAM_ACTION)) {
            // Ignore - already handled.
          } else if (HttpUtils.isMultiPartRequest(req.getContentType())) {
            // Ignore - multipart content adds form parts to the parameter set
          } else {
            request.setAdditionalParameter(name, asSingleValue(name, values));
          }
        }
        doRequest(req, resp, acceptVersion, request);
      } else {
        // Action request.
        final JsonValue content = getJsonActionContent(req);
        final ActionRequest request =
            Requests.newActionRequest(getResourceName(req), action).setContent(content);
        for (final Map.Entry<String, String[]> p : parameters.entrySet()) {
          final String name = p.getKey();
          final String[] values = p.getValue();
          if (parseCommonParameter(name, values, request)) {
            continue;
          } else if (name.equalsIgnoreCase(PARAM_ACTION)) {
            // Ignore - already handled.
          } else if (HttpUtils.isMultiPartRequest(req.getContentType())) {
            // Ignore - multipart content adds form parts to the parameter set
          } else {
            request.setAdditionalParameter(name, asSingleValue(name, values));
          }
        }
        doRequest(req, resp, acceptVersion, request);
      }
    } catch (final Exception e) {
      fail(req, resp, e);
    }
  }
コード例 #3
0
  void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    try {
      // Parse out the required API versions.
      final AcceptAPIVersion acceptVersion = parseAcceptAPIVersion(req);

      // Prepare response.
      prepareResponse(req, resp);

      // Validate request.
      preprocessRequest(req);
      rejectIfMatch(req);

      final Map<String, String[]> parameters = req.getParameterMap();
      if (hasParameter(req, PARAM_QUERY_ID)
          || hasParameter(req, PARAM_QUERY_EXPRESSION)
          || hasParameter(req, PARAM_QUERY_FILTER)) {
        // Additional pre-validation for queries.
        rejectIfNoneMatch(req);

        // Query against collection.
        final QueryRequest request = Requests.newQueryRequest(getResourceName(req));

        for (final Map.Entry<String, String[]> p : parameters.entrySet()) {
          final String name = p.getKey();
          final String[] values = p.getValue();

          if (parseCommonParameter(name, values, request)) {
            continue;
          } else if (name.equalsIgnoreCase(PARAM_SORT_KEYS)) {
            for (final String s : values) {
              try {
                request.addSortKey(s.split(SORT_KEYS_DELIMITER));
              } catch (final IllegalArgumentException e) {
                // FIXME: i18n.
                throw new BadRequestException(
                    "The value '"
                        + s
                        + "' for parameter '"
                        + name
                        + "' could not be parsed as a comma "
                        + "separated list of sort keys");
              }
            }
          } else if (name.equalsIgnoreCase(PARAM_QUERY_ID)) {
            request.setQueryId(asSingleValue(name, values));
          } else if (name.equalsIgnoreCase(PARAM_QUERY_EXPRESSION)) {
            request.setQueryExpression(asSingleValue(name, values));
          } else if (name.equalsIgnoreCase(PARAM_PAGED_RESULTS_COOKIE)) {
            request.setPagedResultsCookie(asSingleValue(name, values));
          } else if (name.equalsIgnoreCase(PARAM_PAGED_RESULTS_OFFSET)) {
            request.setPagedResultsOffset(asIntValue(name, values));
          } else if (name.equalsIgnoreCase(PARAM_PAGE_SIZE)) {
            request.setPageSize(asIntValue(name, values));
          } else if (name.equalsIgnoreCase(PARAM_QUERY_FILTER)) {
            final String s = asSingleValue(name, values);
            try {
              request.setQueryFilter(QueryFilter.valueOf(s));
            } catch (final IllegalArgumentException e) {
              // FIXME: i18n.
              throw new BadRequestException(
                  "The value '"
                      + s
                      + "' for parameter '"
                      + name
                      + "' could not be parsed as a valid query filter");
            }
          } else {
            request.setAdditionalParameter(name, asSingleValue(name, values));
          }
        }

        // Check for incompatible arguments.
        if (request.getQueryId() != null && request.getQueryFilter() != null) {
          // FIXME: i18n.
          throw new BadRequestException(
              "The parameters "
                  + PARAM_QUERY_ID
                  + " and "
                  + PARAM_QUERY_FILTER
                  + " are mutually exclusive");
        }

        if (request.getQueryId() != null && request.getQueryExpression() != null) {
          // FIXME: i18n.
          throw new BadRequestException(
              "The parameters "
                  + PARAM_QUERY_ID
                  + " and "
                  + PARAM_QUERY_EXPRESSION
                  + " are mutually exclusive");
        }

        if (request.getQueryFilter() != null && request.getQueryExpression() != null) {
          // FIXME: i18n.
          throw new BadRequestException(
              "The parameters "
                  + PARAM_QUERY_FILTER
                  + " and "
                  + PARAM_QUERY_EXPRESSION
                  + " are mutually exclusive");
        }

        doRequest(req, resp, acceptVersion, request);
      } else {
        // Read of instance within collection or singleton.
        final String rev = getIfNoneMatch(req);
        if (ETAG_ANY.equals(rev)) {
          // FIXME: i18n
          throw new PreconditionFailedException(
              "If-None-Match * not appropriate for " + getMethod(req) + " requests");
        }

        final ReadRequest request = Requests.newReadRequest(getResourceName(req));
        for (final Map.Entry<String, String[]> p : parameters.entrySet()) {
          final String name = p.getKey();
          final String[] values = p.getValue();
          if (parseCommonParameter(name, values, request)) {
            continue;
          } else if (PARAM_MIME_TYPE.equalsIgnoreCase(name)) {
            if (values.length != 1 || values[0].split(FIELDS_DELIMITER).length > 1) {
              // FIXME: i18n.
              throw new BadRequestException("Only one mime type value allowed");
            }
            if (parameters.get(PARAM_FIELDS).length != 1) {
              // FIXME: i18n.
              throw new BadRequestException(
                  "The mime type parameter requires only 1 field to be specified");
            }
          } else {
            request.setAdditionalParameter(name, asSingleValue(name, values));
          }
        }
        doRequest(req, resp, acceptVersion, request);
      }
    } catch (final Exception e) {
      fail(req, resp, e);
    }
  }