api/src/main/java/com/google/apphosting/utils/servlet/ee10/DeferredTaskServlet.java [79:138]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  protected void service(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    // See http://b/3479189. All task queue requests have the X-AppEngine-QueueName
    // header set. Non admin users cannot set this header so it's a signal that
    // this came from task queue or an admin smart enough to set the header.
    if (req.getHeader(X_APPENGINE_QUEUENAME) == null) {
      resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Not a taskqueue request.");
      return;
    }

    String method = req.getMethod();
    if (!method.equals("POST")) {
      String protocol = req.getProtocol();
      String msg = "DeferredTaskServlet does not support method: " + method;
      if (protocol.endsWith("1.1")) {
        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
      } else {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
      }
      return;
    }

    // Place the current servlet, request and response in the environment for
    // situations where the task may need to get to it.
    Map<String, Object> attributes = ApiProxy.getCurrentEnvironment().getAttributes();
    attributes.put(DEFERRED_TASK_SERVLET_KEY, this);
    attributes.put(DEFERRED_TASK_REQUEST_KEY, req);
    attributes.put(DEFERRED_TASK_RESPONSE_KEY, resp);
    attributes.put(DEFERRED_MARK_RETRY_KEY, false);

    try {
      performRequest(req, resp);
      if ((Boolean) attributes.get(DEFERRED_MARK_RETRY_KEY)) {
        resp.setStatus(HttpURLConnection.HTTP_INTERNAL_ERROR);
      } else {
        resp.setStatus(HttpURLConnection.HTTP_OK);
      }
    } catch (DeferredTaskException e) {
      resp.setStatus(HttpURLConnection.HTTP_UNSUPPORTED_TYPE);
      log("Deferred task failed exception: " + e);
      return;
    } catch (RuntimeException e) {
      Boolean doNotRetry = (Boolean) attributes.get(DEFERRED_DO_NOT_RETRY_KEY);
      if (doNotRetry == null || !doNotRetry) {
        throw new ServletException(e);
      } else if (doNotRetry) {
        resp.setStatus(HttpURLConnection.HTTP_NOT_AUTHORITATIVE); // Alternate success code.
        log(
            DeferredTaskServlet.class.getName()
                + " - Deferred task failed but doNotRetry specified. Exception: "
                + e);
      }
    } finally {
      // Clean out the attributes.
      attributes.remove(DEFERRED_TASK_SERVLET_KEY);
      attributes.remove(DEFERRED_TASK_REQUEST_KEY);
      attributes.remove(DEFERRED_TASK_RESPONSE_KEY);
      attributes.remove(DEFERRED_DO_NOT_RETRY_KEY);
    }
  }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



api/src/main/java/com/google/apphosting/utils/servlet/DeferredTaskServlet.java [79:138]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  protected void service(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    // See http://b/3479189. All task queue requests have the X-AppEngine-QueueName
    // header set. Non admin users cannot set this header so it's a signal that
    // this came from task queue or an admin smart enough to set the header.
    if (req.getHeader(X_APPENGINE_QUEUENAME) == null) {
      resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Not a taskqueue request.");
      return;
    }

    String method = req.getMethod();
    if (!method.equals("POST")) {
      String protocol = req.getProtocol();
      String msg = "DeferredTaskServlet does not support method: " + method;
      if (protocol.endsWith("1.1")) {
        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
      } else {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
      }
      return;
    }

    // Place the current servlet, request and response in the environment for
    // situations where the task may need to get to it.
    Map<String, Object> attributes = ApiProxy.getCurrentEnvironment().getAttributes();
    attributes.put(DEFERRED_TASK_SERVLET_KEY, this);
    attributes.put(DEFERRED_TASK_REQUEST_KEY, req);
    attributes.put(DEFERRED_TASK_RESPONSE_KEY, resp);
    attributes.put(DEFERRED_MARK_RETRY_KEY, false);

    try {
      performRequest(req, resp);
      if ((Boolean) attributes.get(DEFERRED_MARK_RETRY_KEY)) {
        resp.setStatus(HttpURLConnection.HTTP_INTERNAL_ERROR);
      } else {
        resp.setStatus(HttpURLConnection.HTTP_OK);
      }
    } catch (DeferredTaskException e) {
      resp.setStatus(HttpURLConnection.HTTP_UNSUPPORTED_TYPE);
      log("Deferred task failed exception: " + e);
      return;
    } catch (RuntimeException e) {
      Boolean doNotRetry = (Boolean) attributes.get(DEFERRED_DO_NOT_RETRY_KEY);
      if (doNotRetry == null || !doNotRetry) {
        throw new ServletException(e);
      } else if (doNotRetry) {
        resp.setStatus(HttpURLConnection.HTTP_NOT_AUTHORITATIVE); // Alternate success code.
        log(
            DeferredTaskServlet.class.getName()
                + " - Deferred task failed but doNotRetry specified. Exception: "
                + e);
      }
    } finally {
      // Clean out the attributes.
      attributes.remove(DEFERRED_TASK_SERVLET_KEY);
      attributes.remove(DEFERRED_TASK_REQUEST_KEY);
      attributes.remove(DEFERRED_TASK_RESPONSE_KEY);
      attributes.remove(DEFERRED_DO_NOT_RETRY_KEY);
    }
  }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



