private static Optional<String> getClientHost(SrmPrepareToPutRequest request) {
   if (request.getTransferParameters() != null
       && request.getTransferParameters().getArrayOfClientNetworks() != null) {
     String[] clientNetworks =
         request.getTransferParameters().getArrayOfClientNetworks().getStringArray();
     if (clientNetworks != null && clientNetworks.length > 0 && clientNetworks[0] != null) {
       return Optional.of(clientNetworks[0]);
     }
   }
   return Optional.absent();
 }
 private static TPutFileRequest[] getFileRequests(SrmPrepareToPutRequest request)
     throws SRMInvalidRequestException {
   TPutFileRequest[] fileRequests = null;
   if (request.getArrayOfFileRequests() != null) {
     fileRequests = request.getArrayOfFileRequests().getRequestArray();
   }
   if (fileRequests == null || fileRequests.length < 1) {
     throw new SRMInvalidRequestException("request contains no file requests.");
   }
   return fileRequests;
 }
 private static void checkFileStorageType(
     SrmPrepareToPutRequest request, TFileStorageType expectedStorageType)
     throws SRMNotSupportedException {
   TFileStorageType storageType = request.getDesiredFileStorageType();
   if (storageType != null && !storageType.equals(expectedStorageType)) {
     throw new SRMNotSupportedException(
         "DesiredFileStorageType " + storageType + " is not supported.");
   }
 }
 private static TOverwriteMode getOverwriteMode(SrmPrepareToPutRequest request)
     throws SRMNotSupportedException {
   TOverwriteMode overwriteMode = request.getOverwriteOption();
   if (overwriteMode != null && overwriteMode.equals(TOverwriteMode.WHEN_FILES_ARE_DIFFERENT)) {
     throw new SRMNotSupportedException(
         "Overwrite Mode WHEN_FILES_ARE_DIFFERENT is not supported.");
   }
   return overwriteMode;
 }
 private String[] getProtocols() throws SRMInvalidRequestException {
   String[] protocols = null;
   TTransferParameters transferParameters = request.getTransferParameters();
   if (transferParameters != null && transferParameters.getArrayOfTransferProtocols() != null) {
     protocols = transferParameters.getArrayOfTransferProtocols().getStringArray();
   }
   protocols = Tools.trimStringArray(protocols);
   if (protocols == null || protocols.length < 1) {
     throw new SRMInvalidRequestException("request contains no transfer protocols.");
   }
   return protocols;
 }
 private static long getLifetime(SrmPrepareToPutRequest request, long max)
     throws SRMInvalidRequestException {
   long lifetimeInSeconds = 0;
   if (request.getDesiredTotalRequestTime() != null) {
     long reqLifetime = (long) request.getDesiredTotalRequestTime().intValue();
     if (reqLifetime < 0) {
       /* [ SRM 2.2, 5.5.2 ]
        * q)    If input parameter desiredTotalRequestTime is 0 (zero), each file
        *       request must be tried at least once. Negative value must be invalid.
        */
       throw new SRMInvalidRequestException("Negative desiredTotalRequestTime is invalid.");
     }
     lifetimeInSeconds = reqLifetime;
   }
   if (lifetimeInSeconds <= 0) {
     // Revisit: Behaviour doesn't match the SRM spec
     return max;
   }
   long lifetime = TimeUnit.SECONDS.toMillis(lifetimeInSeconds);
   return lifetime > max ? max : lifetime;
 }
 private static String getExtraInfo(SrmPrepareToPutRequest request, String key) {
   ArrayOfTExtraInfo storageSystemInfo = request.getStorageSystemInfo();
   if (storageSystemInfo == null) {
     return null;
   }
   TExtraInfo[] extraInfoArray = storageSystemInfo.getExtraInfoArray();
   if (extraInfoArray == null || extraInfoArray.length <= 0) {
     return null;
   }
   for (TExtraInfo extraInfo : extraInfoArray) {
     if (extraInfo.getKey().equals(key)) {
       return extraInfo.getValue();
     }
   }
   return null;
 }
  private SrmPrepareToPutResponse srmPrepareToPut()
      throws IllegalStateTransition, InterruptedException, SRMNotSupportedException,
          SRMInvalidRequestException, SRMInternalErrorException {
    checkFileStorageType(request, TFileStorageType.PERMANENT);
    String[] protocols = getProtocols();
    String clientHost = getClientHost(request).or(this.clientHost);
    String spaceToken = request.getTargetSpaceToken();
    TRetentionPolicy retentionPolicy = null;
    TAccessLatency accessLatency = null;
    if (request.getTargetFileRetentionPolicyInfo() != null) {
      retentionPolicy = request.getTargetFileRetentionPolicyInfo().getRetentionPolicy();
      accessLatency = request.getTargetFileRetentionPolicyInfo().getAccessLatency();
    }
    TPutFileRequest[] fileRequests = getFileRequests(request);

    long lifetime = getLifetime(request, configuration.getPutLifetime());
    TOverwriteMode overwriteMode = getOverwriteMode(request);

    String[] supportedProtocols = storage.supportedPutProtocols();
    boolean isAnyProtocolSupported = any(asList(protocols), in(asList(supportedProtocols)));
    if (!isAnyProtocolSupported) {
      throw new SRMNotSupportedException(
          "Protocol(s) not supported: " + Arrays.toString(protocols));
    }

    URI[] surls = new URI[fileRequests.length];
    Long[] sizes = new Long[fileRequests.length];
    boolean[] wantPermanent = new boolean[fileRequests.length];
    for (int i = 0; i < fileRequests.length; ++i) {
      TPutFileRequest fileRequest = fileRequests[i];
      if (fileRequest == null) {
        throw new SRMInvalidRequestException("file request #" + (i + 1) + " is null.");
      }
      if (fileRequest.getTargetSURL() == null) {
        throw new SRMInvalidRequestException("surl of file request #" + (i + 1) + " is null.");
      }
      URI surl = URI.create(fileRequest.getTargetSURL().toString());
      UnsignedLong knownSize = fileRequest.getExpectedFileSize();
      if (knownSize != null) {
        sizes[i] = knownSize.longValue();
        if (sizes[i] < 0) {
          throw new SRMInvalidRequestException("Negative file size is not allowed.");
        }
      }
      wantPermanent[i] = true; // for now, extract type info from space token in the future
      /*                nextRequest.getFileStorageType()==
      TFileStorageType.PERMANENT;*/
      surls[i] = surl;
    }

    PutRequest r =
        new PutRequest(
            user,
            credential.getId(),
            surls,
            sizes,
            wantPermanent,
            protocols,
            lifetime,
            configuration.getGetRetryTimeout(),
            configuration.getGetMaxNumOfRetries(),
            clientHost,
            spaceToken,
            retentionPolicy,
            accessLatency,
            request.getUserRequestDescription());
    try (JDC ignored = r.applyJdc()) {
      String priority = getExtraInfo(request, "priority");
      if (priority != null) {
        try {
          r.setPriority(Integer.parseInt(priority));
        } catch (NumberFormatException e) {
          LOGGER.warn("Ignoring non-integer user priority: {}", priority);
        }
      }

      if (overwriteMode != null) {
        r.setOverwriteMode(overwriteMode);
      }

      srm.schedule(r);
      // RequestScheduler will take care of the rest
      // getRequestScheduler.add(r);
      // Return the request status
      return r.getSrmPrepareToPutResponse(configuration.getPutSwitchToAsynchronousModeDelay());
    }
  }