private static void fillAddRequest()

in api/src/main/java/com/google/appengine/api/taskqueue/QueueImpl.java [302:408]


  private static void fillAddRequest(
      com.google.appengine.api.datastore.Transaction txn,
      String queueName,
      String taskName,
      long etaMillis,
      TaskOptions.Method method,
      String relativeUrl,
      byte[] payload,
      LinkedHashMap<String, List<String>> headers,
      RetryOptions retryOptions,
      boolean useUrlEncodedContentType,
      byte[] tag,
      Duration dispatchDeadline,
      TaskQueueAddRequest.Builder addRequest) {
    // Fills queue name and task name.
    addRequest
        .setQueueName(ByteString.copyFromUtf8(queueName))
        .setTaskName(ByteString.copyFromUtf8(Strings.nullToEmpty(taskName)));

    // Fills mode, method and url
    if (method == TaskOptions.Method.PULL) {
      addRequest.setMode(TaskQueueMode.Mode.PULL);
    } else {
      addRequest
          .setUrl(ByteString.copyFromUtf8(relativeUrl))
          .setMode(TaskQueueMode.Mode.PUSH)
          .setMethod(method.getPbMethod());
    }

    // Fills payload
    if (payload != null) {
      addRequest.setBody(ByteString.copyFrom(payload));
    }

    // Fills task ETA.
    addRequest.setEtaUsec(etaMillis * 1000);

    // Fills transactional data. Transactional tasks cannot be named.
    if (taskName != null && !taskName.isEmpty() && txn != null) {
      throw new IllegalArgumentException("transactional tasks cannot be named: " + taskName);
    }
    if (txn != null) {
      TransactionHelper.setTransaction(txn, addRequest);
    }

    // Fills retry options.
    if (retryOptions != null) {
      fillRetryParameters(retryOptions, addRequest.getRetryParametersBuilder());
    }

    // Adds special headers and copy to addRequest.
    if (NamespaceManager.getGoogleAppsNamespace().length() != 0) {
      if (!headers.containsKey(DEFAULT_NAMESPACE_HEADER)) {
        headers.put(
            DEFAULT_NAMESPACE_HEADER, Arrays.asList(NamespaceManager.getGoogleAppsNamespace()));
      }
    }
    if (!headers.containsKey(CURRENT_NAMESPACE_HEADER)) {
      String namespace = NamespaceManager.get();
      headers.put(CURRENT_NAMESPACE_HEADER, Arrays.asList(nullToEmpty(namespace)));
    }
    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
      // If the POST method is being used with parameters then ignore the content-type provided
      // by the user and use application/x-www-form-urlencoded.
      if (useUrlEncodedContentType && entry.getKey().toLowerCase().equals("content-type")) {
        continue;
      }

      for (String value : entry.getValue()) {
        addRequest
            .addHeaderBuilder()
            .setKey(ByteString.copyFromUtf8(entry.getKey()))
            .setValue(ByteString.copyFromUtf8(value));
      }
    }
    if (useUrlEncodedContentType) {
      addRequest
          .addHeaderBuilder()
          .setKey(ByteString.copyFromUtf8("content-type"))
          .setValue(ByteString.copyFromUtf8("application/x-www-form-urlencoded"));
    }

    if (tag != null) {
      if (method != TaskOptions.Method.PULL) {
        throw new IllegalArgumentException("Only PULL tasks can have a tag.");
      }
      if (tag.length > QueueConstants.maxTaskTagLength()) {
        throw new IllegalArgumentException(
            "Task tag must be no more than " + QueueConstants.maxTaskTagLength() + " bytes.");
      }
      addRequest.setTag(ByteString.copyFrom(tag));
    }

    if (dispatchDeadline != null) {
      addRequest.setDispatchDeadlineUsec(dispatchDeadline.toNanos() / 1000);
    }

    if (method == TaskOptions.Method.PULL) {
      if (addRequest.build().getSerializedSize() > QueueConstants.maxPullTaskSizeBytes()) {
        throw new IllegalArgumentException("Task size too large");
      }
    } else {
      if (addRequest.build().getSerializedSize() > QueueConstants.maxPushTaskSizeBytes()) {
        throw new IllegalArgumentException("Task size too large");
      }
    }
  }