/**
   * Handle a request after its type is determined. The request is scheduled for execution. If the
   * server has enough free resources, the client will be served immediately. If time runs out, the
   * client will be asked to come back later with a reference to the result.
   *
   * <p>Override: Previously, the wrapper added the handle agent logging. The entire handle method
   * is now overridden to: 1 - prevent redundant requests from processing (ran or running) 2 - avoid
   * taxing the same data source the request requires for processing
   *
   * @throws ExceptionReport
   */
  @Override
  public void handle() throws ExceptionReport {
    Response resp = null;
    if (req == null) {
      throw new ExceptionReport("Internal Error", "");
    }
    handleAgentLogging(); // USGS override logic: inserts the user_agent into the meta-data table.
                          // Required the requestId for the insert.

    if (req instanceof ExecuteRequestWrapper) {
      ExecuteRequestWrapper execReq = (ExecuteRequestWrapper) req; // #USGS override code

      LOGGER.debug("RequestId before updating to Accepted:" + execReq.getUniqueId());
      execReq.updateStatusAccepted();
      ExecuteRequestManager.getInstance()
          .getThrottleQueue()
          .putRequest(
              execReq); // inserts with ACCEPTED into the Throttle_Queue table. Does not actually
                        // add the request to the RequestQueue.

      if (execReq.isStoreResponse()) {
        addToQueue(execReq, resp);
      } else {
        synchAddToQueue(execReq, resp);
      }

    } else { // if ExecuteRequest
      // for GetCapabilities and DescribeProcess:
      resp = req.call();
      try {
        InputStream is = resp.getAsStream();
        IOUtils.copy(is, os);
        is.close();
      } catch (IOException e) {
        throw new ExceptionReport(
            "Could not read from response stream.", ExceptionReport.NO_APPLICABLE_CODE);
      }
    }
  }
  private void synchAddToQueue(ExecuteRequestWrapper execReq, Response resp)
      throws ExceptionReport {
    try {
      LOGGER.info("Adding to queue synchronously.");
      // add to queue even though the user is waiting ie synchronous <test> this may block the
      // asynch requests
      resp =
          ExecuteRequestManager.getInstance()
              .getExecuteRequestQueue()
              .put(execReq); // #USGS pool.submit
    } catch (RejectedExecutionException ree) {
      LOGGER.warn("exception handling ExecuteRequest.", ree);
      // server too busy?
      throw new ExceptionReport(
          "The requested process was rejected. Maybe the server is flooded with requests.",
          ExceptionReport.SERVER_BUSY);
    } finally {
      if (resp == null) {
        LOGGER.warn("null response handling ExecuteRequest.");
        throw new ExceptionReport(
            "Problem with handling threads in GDPRequestHandler",
            ExceptionReport.NO_APPLICABLE_CODE);
      }
      if (!execReq.isStoreResponse()) {
        InputStream is = resp.getAsStream();
        try {
          IOUtils.copy(is, os);
          is.close();
        } catch (IOException ex) {
          // java.util.logging.Logger.getLogger(GdpRequestHandler.class.getName()).log(Level.SEVERE,
          // null, ex);
          LOGGER.error("Error getting stream from response in synchronous add to queue: ", ex);
        }

        LOGGER.info("Served ExecuteRequest.");
      }
    }
  }