private void getProperties(String currentNode, String value) throws ParseException, SAXException {

    if (currentNode.equals(Constants.LAST_MODIFIED_ELEMENT)) {
      this.attributes
          .getProperties()
          .setLastModified(Utility.parseRFC1123DateFromStringInGMT(value));
    } else if (currentNode.equals(Constants.ETAG_ELEMENT)) {
      this.attributes.getProperties().setEtag(Utility.formatETag(value));
    } else if (currentNode.equals(Constants.LEASE_STATUS_ELEMENT)) {
      final LeaseStatus tempStatus = LeaseStatus.parse(value);
      if (!tempStatus.equals(LeaseStatus.UNSPECIFIED)) {
        this.attributes.getProperties().setLeaseStatus(tempStatus);
      } else {
        throw new SAXException(SR.INVALID_RESPONSE_RECEIVED);
      }
    } else if (currentNode.equals(Constants.LEASE_STATE_ELEMENT)) {
      final LeaseState tempState = LeaseState.parse(value);
      if (!tempState.equals(LeaseState.UNSPECIFIED)) {
        this.attributes.getProperties().setLeaseState(tempState);
      } else {
        throw new SAXException(SR.INVALID_RESPONSE_RECEIVED);
      }
    } else if (currentNode.equals(Constants.LEASE_DURATION_ELEMENT)) {
      final LeaseDuration tempDuration = LeaseDuration.parse(value);
      if (!tempDuration.equals(LeaseDuration.UNSPECIFIED)) {
        this.attributes.getProperties().setLeaseDuration(tempDuration);
      } else {
        throw new SAXException(SR.INVALID_RESPONSE_RECEIVED);
      }
    }
  }
  /**
   * Creates an instance of the <code>CloudFileDirectory</code> class using the specified address,
   * share, and client.
   *
   * @param uri A {@link StorageUri} that represents the file directory's address.
   * @param directoryName A <code>String</code> that represents the name of the directory.
   * @param share A {@link CloudFileShare} object that represents the associated file share.
   */
  protected CloudFileDirectory(
      final StorageUri uri, final String directoryName, final CloudFileShare share) {
    Utility.assertNotNull("uri", uri);
    Utility.assertNotNull("directoryName", directoryName);
    Utility.assertNotNull("share", share);

    this.name = directoryName;
    this.fileServiceClient = share.getServiceClient();
    this.share = share;
    this.storageUri = uri;
    this.properties = new FileDirectoryProperties();
  }
  /**
   * Creates an instance of the <code>CloudFileDirectory</code> class using an absolute URI to the
   * directory.
   *
   * @param directoryAbsoluteUri A {@link StorageUri} that represents the file directory's address.
   * @param client A {@link CloudFileClient} object that represents the associated service client.
   * @throws StorageException
   * @throws URISyntaxException
   */
  public CloudFileDirectory(final StorageUri directoryAbsoluteUri, final CloudFileClient client)
      throws StorageException, URISyntaxException {
    Utility.assertNotNull("directoryAbsoluteUri", directoryAbsoluteUri);

    this.fileServiceClient = client;
    this.storageUri = directoryAbsoluteUri;
    this.properties = new FileDirectoryProperties();
    this.parseQueryAndVerify(
        directoryAbsoluteUri,
        client,
        client == null
            ? Utility.determinePathStyleFromUri(this.storageUri.getPrimaryUri())
            : client.isUsePathStyleUris());
  }
  /**
   * Returns a reference to a {@link CloudFileDirectory} object that represents a subdirectory in
   * this directory.
   *
   * @param itemName A <code>String</code> that represents the name of the subdirectory.
   * @return A {@link CloudFileDirectory} object that represents a reference to the specified
   *     directory.
   * @throws URISyntaxException If the resource URI is invalid.
   * @throws StorageException
   */
  public CloudFileDirectory getSubDirectoryReference(final String itemName)
      throws URISyntaxException, StorageException {
    Utility.assertNotNullOrEmpty("itemName", itemName);

    StorageUri subdirectoryUri = PathUtility.appendPathToUri(this.storageUri, itemName);
    return new CloudFileDirectory(subdirectoryUri, itemName, this.getShare());
  }
  /**
   * Returns a result segment of an enumerable collection of files and directories for this
   * directory, using the specified listing details options, request options, and operation context.
   *
   * @param maxResults The maximum number of results to retrieve.
   * @param continuationToken A {@link ResultContinuation} object that represents a continuation
   *     token returned by a previous listing operation.
   * @param options A {@link FileRequestOptions} object that specifies any additional options for
   *     the request. Specifying <code>null</code> will use the default request options from the
   *     associated service client ( {@link CloudFileClient}).
   * @param opContext An {@link OperationContext} object that represents the context for the current
   *     operation. This object is used to track requests to the storage service, and to provide
   *     additional runtime information about the operation.
   * @return A {@link ResultSegment} object that contains a segment of the enumerable collection of
   *     {@link ListFileItem} objects that represent the files and directories in this directory.
   * @throws StorageException If a storage service error occurred.
   */
  public ResultSegment<ListFileItem> listFilesAndDirectoriesSegmented(
      final int maxResults,
      final ResultContinuation continuationToken,
      FileRequestOptions options,
      OperationContext opContext)
      throws StorageException {
    if (opContext == null) {
      opContext = new OperationContext();
    }

    opContext.initialize();
    options = FileRequestOptions.applyDefaults(options, this.fileServiceClient);

    Utility.assertContinuationType(continuationToken, ResultContinuationType.FILE);

    SegmentedStorageRequest segmentedRequest = new SegmentedStorageRequest();
    segmentedRequest.setToken(continuationToken);

    return ExecutionEngine.executeWithRetry(
        this.fileServiceClient,
        this,
        this.listFilesAndDirectoriesSegmentedImpl(maxResults, options, segmentedRequest),
        options.getRetryPolicyFactory(),
        opContext);
  }
  /**
   * Returns a reference to a {@link CloudFile} object that represents a file in this directory.
   *
   * @param fileName A <code>String</code> that represents the name of the file.
   * @return A {@link CloudFile} object that represents a reference to the specified file.
   * @throws StorageException If a storage service error occurred.
   * @throws URISyntaxException If the resource URI is invalid.
   */
  public CloudFile getFileReference(final String fileName)
      throws URISyntaxException, StorageException {
    Utility.assertNotNullOrEmpty("fileName", fileName);

    StorageUri subdirectoryUri = PathUtility.appendPathToUri(this.storageUri, fileName);

    return new CloudFile(subdirectoryUri, this.fileServiceClient, this.getShare());
  }
  /**
   * Parses a {@link ContainerListResponse} form the given XML stream.
   *
   * @param serviceClient a reference to the client object associated with this object.
   * @param stream the stream from which to parse the container list
   * @throws SAXException
   * @throws ParserConfigurationException
   * @throws IOException
   */
  protected static ListResponse<CloudBlobContainer> getContainerList(
      final InputStream stream, final CloudBlobClient serviceClient)
      throws ParserConfigurationException, SAXException, IOException {
    SAXParser saxParser = Utility.getSAXParser();
    ContainerListHandler handler = new ContainerListHandler(serviceClient);
    saxParser.parse(stream, handler);

    return handler.response;
  }
  @Test
  public void ensureDigestDecode() throws IOException, StorageException {
    Blob blob = Blobs.createBlob(CONTENT2);
    String contentMd5;
    try (InputStream is = blob.getStream()) {
      StreamMd5AndLength md5 =
          Utility.analyzeStream(is, blob.getLength(), 2 * Constants.MB, true, true);
      contentMd5 = md5.getMd5();
    }

    assertTrue(AzureFileStorage.isBlobDigestCorrect(CONTENT2_MD5, contentMd5));
  }
  /**
   * Sets this {@link EntityProperty} using the serialized <code>java.util.Date</code> value.
   *
   * @param value The <code>java.util.Date</code> value to set as the {@link EntityProperty} value.
   *     This value may be <code>null</code>.
   */
  public final synchronized void setValue(final Date value) {
    this.edmType = EdmType.DATE_TIME;
    this.type = Date.class;
    if (value == null) {
      this.value = null;
      this.isNull = true;
      return;
    } else {
      this.isNull = false;
    }

    this.value =
        Utility.getTimeByZoneAndFormat(value, Utility.UTC_ZONE, Utility.ISO8061_LONG_PATTERN);
  }
  /**
   * Strips the query and verifies the URI is absolute.
   *
   * @param completeUri A {@link StorageUri} object which represents the complete URI.
   * @param existingClient A {@link CloudFileClient} object which represents the client to use.
   * @param usePathStyleUris <code>true</code> if path-style URIs are used; otherwise, <code>false
   *     </code>.
   * @throws StorageException If a storage service error occurred.
   * @throws URISyntaxException
   */
  private void parseQueryAndVerify(
      final StorageUri completeUri,
      final CloudFileClient existingClient,
      final boolean usePathStyleUri)
      throws StorageException, URISyntaxException {
    Utility.assertNotNull("completeUri", completeUri);

    if (!completeUri.isAbsolute()) {
      final String errorMessage =
          String.format(SR.RELATIVE_ADDRESS_NOT_PERMITTED, completeUri.toString());
      throw new IllegalArgumentException(errorMessage);
    }

    this.storageUri = PathUtility.stripURIQueryAndFragment(completeUri);
    this.fileServiceClient =
        (existingClient == null)
            ? new CloudFileClient(
                PathUtility.getServiceClientBaseAddress(this.storageUri, usePathStyleUri), null)
            : existingClient;
    this.name = PathUtility.getFileNameFromURI(completeUri.getPrimaryUri(), usePathStyleUri);
  }
 /**
  * Gets the value of this {@link EntityProperty} as a <code>java.util.Date</code>.
  *
  * @return A <code>java.util.Date</code> representation of the {@link EntityProperty} value, or
  *     <code>null</code>.
  * @throws IllegalArgumentException If the value is not <code>null</code> and cannot be parsed as
  *     a <code>java.util.Date</code>.
  */
 public Date getValueAsDate() {
   if (this.isNull) {
     return null;
   }
   return Utility.parseDate(this.value);
 }
  private StorageRequest<CloudFileClient, CloudFileDirectory, ResultSegment<ListFileItem>>
      listFilesAndDirectoriesSegmentedImpl(
          final int maxResults,
          final FileRequestOptions options,
          final SegmentedStorageRequest segmentedRequest) {

    Utility.assertContinuationType(segmentedRequest.getToken(), ResultContinuationType.FILE);

    final ListingContext listingContext = new ListingContext(null, maxResults);

    final StorageRequest<CloudFileClient, CloudFileDirectory, ResultSegment<ListFileItem>>
        getRequest =
            new StorageRequest<CloudFileClient, CloudFileDirectory, ResultSegment<ListFileItem>>(
                options, this.getStorageUri()) {

              @Override
              public void setRequestLocationMode() {
                this.setRequestLocationMode(
                    Utility.getListingLocationMode(segmentedRequest.getToken()));
              }

              @Override
              public HttpURLConnection buildRequest(
                  CloudFileClient client, CloudFileDirectory directory, OperationContext context)
                  throws Exception {
                listingContext.setMarker(
                    segmentedRequest.getToken() != null
                        ? segmentedRequest.getToken().getNextMarker()
                        : null);
                return FileRequest.listFilesAndDirectories(
                    directory.getUri(), options, context, listingContext);
              }

              @Override
              public void signRequest(
                  HttpURLConnection connection, CloudFileClient client, OperationContext context)
                  throws Exception {
                StorageRequest.signBlobQueueAndFileRequest(connection, client, -1L, null);
              }

              @Override
              public ResultSegment<ListFileItem> preProcessResponse(
                  CloudFileDirectory directory, CloudFileClient client, OperationContext context)
                  throws Exception {
                if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_OK) {
                  this.setNonExceptionedRetryableFailure(true);
                }

                return null;
              }

              @Override
              public ResultSegment<ListFileItem> postProcessResponse(
                  HttpURLConnection connection,
                  CloudFileDirectory directory,
                  CloudFileClient client,
                  OperationContext context,
                  ResultSegment<ListFileItem> storageObject)
                  throws Exception {
                final ListResponse<ListFileItem> response =
                    FileListHandler.getFileAndDirectoryList(
                        this.getConnection().getInputStream(), directory);
                ResultContinuation newToken = null;

                if (response.getNextMarker() != null) {
                  newToken = new ResultContinuation();
                  newToken.setNextMarker(response.getNextMarker());
                  newToken.setContinuationType(ResultContinuationType.FILE);
                  newToken.setTargetLocation(this.getResult().getTargetLocation());
                }

                final ResultSegment<ListFileItem> resSegment =
                    new ResultSegment<ListFileItem>(response.getResults(), maxResults, newToken);

                // Important for listFilesAndDirectories because this is required by the lazy
                // iterator between executions.
                segmentedRequest.setToken(resSegment.getContinuationToken());
                return resSegment;
              }
            };

    return getRequest;
  }