Ejemplo n.º 1
0
  private static <CLIENT_TYPE, PARENT_TYPE, RESULT_TYPE> HttpURLConnection setupStorageRequest(
      final CLIENT_TYPE client,
      final PARENT_TYPE parentObject,
      final StorageRequest<CLIENT_TYPE, PARENT_TYPE, RESULT_TYPE> task,
      int currentRetryCount,
      final OperationContext opContext)
      throws StorageException {
    try {

      // reset result flags
      task.initialize(opContext);

      if (Utility.validateMaxExecutionTimeout(
          task.getRequestOptions().getOperationExpiryTimeInMs())) {
        // maximum execution time would be exceeded by current time
        TimeoutException timeoutException =
            new TimeoutException(SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION);
        throw new StorageException(
            StorageErrorCodeStrings.OPERATION_TIMED_OUT,
            SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION,
            Constants.HeaderConstants.HTTP_UNUSED_306,
            null,
            timeoutException);
      }

      // Run the recovery action if this is a retry. Else, initialize the location mode for the
      // task.
      // For retries, it will be initialized in retry logic.
      if (currentRetryCount > 0) {
        task.recoveryAction(opContext);
        Logger.info(opContext, LogConstants.RETRY);
      } else {
        task.applyLocationModeToRequest();
        task.initializeLocation();
        Logger.info(opContext, LogConstants.STARTING);
      }

      task.setRequestLocationMode();

      // If the command only allows for a specific location, we should target
      // that location no matter what the retry policy says.
      task.validateLocation();

      Logger.info(
          opContext, LogConstants.INIT_LOCATION, task.getCurrentLocation(), task.getLocationMode());

      // 1. Build the request
      HttpURLConnection request = task.buildRequest(client, parentObject, opContext);

      // 2. Add headers
      task.setHeaders(request, parentObject, opContext);

      // Add any other custom headers that users have set on the opContext
      if (opContext.getUserHeaders() != null) {
        for (final Entry<String, String> entry : opContext.getUserHeaders().entrySet()) {
          request.setRequestProperty(entry.getKey(), entry.getValue());
        }
      }

      // 3. Fire sending request event
      ExecutionEngine.fireSendingRequestEvent(opContext, request, task.getResult());
      task.setIsSent(true);

      // 4. Sign the request
      task.signRequest(request, client, opContext);

      // set the connection on the task
      task.setConnection(request);

      return request;
    } catch (StorageException e) {
      throw e;
    } catch (Exception e) {
      throw new StorageException(
          null, e.getMessage(), Constants.HeaderConstants.HTTP_UNUSED_306, null, e);
    }
  }