/// <summary>
        /// Core implementation of the ListFilesAndDirectories method.
        /// </summary>
        /// <param name="maxResults">A non-negative integer value that indicates the maximum number of results to be returned at a time, up to the
        /// per-operation limit of 5000. If this value is zero, the maximum possible number of results will be returned, up to 5000.</param>
        /// <param name="options">An object that specifies additional options for the request.</param>
        /// <param name="currentToken">The continuation token.</param>
        /// <returns>A <see cref="RESTCommand"/> that lists the files.</returns>
        private RESTCommand <ResultSegment <IListFileItem> > ListFilesAndDirectoriesImpl(int?maxResults, FileRequestOptions options, FileContinuationToken currentToken)
        {
            FileListingContext listingContext = new FileListingContext(maxResults)
            {
                Marker = currentToken != null ? currentToken.NextMarker : null
            };

            RESTCommand <ResultSegment <IListFileItem> > getCmd = new RESTCommand <ResultSegment <IListFileItem> >(this.ServiceClient.Credentials, this.StorageUri);

            options.ApplyToStorageCommand(getCmd);
            getCmd.CommandLocationMode    = CommonUtility.GetListingLocationMode(currentToken);
            getCmd.RetrieveResponseStream = true;
            getCmd.Handler             = this.ServiceClient.AuthenticationHandler;
            getCmd.BuildClient         = HttpClientFactory.BuildHttpClient;
            getCmd.BuildRequest        = (cmd, uri, builder, cnt, serverTimeout, ctx) => DirectoryHttpRequestMessageFactory.List(uri, serverTimeout, listingContext, cnt, ctx);
            getCmd.PreProcessResponse  = (cmd, resp, ex, ctx) => HttpResponseParsers.ProcessExpectedStatusCodeNoException(HttpStatusCode.OK, resp, null /* retVal */, cmd, ex);
            getCmd.PostProcessResponse = (cmd, resp, ctx) =>
            {
                return(Task.Factory.StartNew(() =>
                {
                    ListFilesAndDirectoriesResponse listFilesResponse = new ListFilesAndDirectoriesResponse(cmd.ResponseStream);
                    List <IListFileItem> fileList = listFilesResponse.Files.Select(item => this.SelectListFileItem(item)).ToList();
                    FileContinuationToken continuationToken = null;
                    if (listFilesResponse.NextMarker != null)
                    {
                        continuationToken = new FileContinuationToken()
                        {
                            NextMarker = listFilesResponse.NextMarker,
                            TargetLocation = cmd.CurrentResult.TargetLocation,
                        };
                    }

                    return new ResultSegment <IListFileItem>(fileList)
                    {
                        ContinuationToken = continuationToken,
                    };
                }));
            };

            return(getCmd);
        }
        /// <summary>
        /// Core implementation of the ListFilesAndDirectories method.
        /// </summary>
        /// <param name="maxResults">A non-negative integer value that indicates the maximum number of results to be returned at a time, up to the
        /// per-operation limit of 5000. If this value is zero, the maximum possible number of results will be returned, up to 5000.</param>
        /// <param name="options">An object that specifies additional options for the request.</param>
        /// <param name="currentToken">The continuation token.</param>
        /// <param name="prefix">A string containing the file or directory name prefix.</param>
        /// <returns>A <see cref="RESTCommand"/> that lists the files.</returns>
        private RESTCommand <ResultSegment <IListFileItem> > ListFilesAndDirectoriesImpl(int?maxResults, FileRequestOptions options, FileContinuationToken currentToken, string prefix)
        {
            FileListingContext listingContext = new FileListingContext(maxResults)
            {
                Marker = currentToken != null ? currentToken.NextMarker : null,
                Prefix = string.IsNullOrEmpty(prefix) ? null : prefix
            };

            RESTCommand <ResultSegment <IListFileItem> > getCmd = new RESTCommand <ResultSegment <IListFileItem> >(this.ServiceClient.Credentials, this.StorageUri, this.ServiceClient.HttpClient);

            options.ApplyToStorageCommand(getCmd);
            getCmd.CommandLocationMode      = CommonUtility.GetListingLocationMode(currentToken);
            getCmd.RetrieveResponseStream   = true;
            getCmd.BuildRequest             = (cmd, uri, builder, cnt, serverTimeout, ctx) => DirectoryHttpRequestMessageFactory.List(uri, serverTimeout, this.Share.SnapshotTime, listingContext, cnt, ctx, this.ServiceClient.GetCanonicalizer(), this.ServiceClient.Credentials);
            getCmd.PreProcessResponse       = (cmd, resp, ex, ctx) => HttpResponseParsers.ProcessExpectedStatusCodeNoException(HttpStatusCode.OK, resp, null /* retVal */, cmd, ex);
            getCmd.PostProcessResponseAsync = async(cmd, resp, ctx, ct) =>
            {
                ListFilesAndDirectoriesResponse listFilesResponse = await ListFilesAndDirectoriesResponse.ParseAsync(cmd.ResponseStream, ct).ConfigureAwait(false);

                List <IListFileItem>  fileList          = listFilesResponse.Files.Select(item => this.SelectListFileItem(item)).ToList();
                FileContinuationToken continuationToken = null;
                if (listFilesResponse.NextMarker != null)
                {
                    continuationToken = new FileContinuationToken()
                    {
                        NextMarker     = listFilesResponse.NextMarker,
                        TargetLocation = cmd.CurrentResult.TargetLocation,
                    };
                }

                return(new ResultSegment <IListFileItem>(fileList)
                {
                    ContinuationToken = continuationToken,
                });
            };

            return(getCmd);
        }
        /// <summary>
        /// Core implementation of the ListFilesAndDirectories method.
        /// </summary>
        /// <param name="maxResults">A non-negative integer value that indicates the maximum number of results to be returned at a time, up to the 
        /// per-operation limit of 5000. If this value is zero, the maximum possible number of results will be returned, up to 5000.</param>         
        /// <param name="options">An object that specifies additional options for the request.</param>
        /// <param name="currentToken">The continuation token.</param>
        /// <returns>A <see cref="RESTCommand"/> that lists the files.</returns>
        private RESTCommand<ResultSegment<IListFileItem>> ListFilesAndDirectoriesImpl(int? maxResults, FileRequestOptions options, FileContinuationToken currentToken)
        {
            FileListingContext listingContext = new FileListingContext(maxResults)
            {
                Marker = currentToken != null ? currentToken.NextMarker : null
            };

            RESTCommand<ResultSegment<IListFileItem>> getCmd = new RESTCommand<ResultSegment<IListFileItem>>(this.ServiceClient.Credentials, this.StorageUri);

            options.ApplyToStorageCommand(getCmd);
            getCmd.CommandLocationMode = CommonUtility.GetListingLocationMode(currentToken);
            getCmd.RetrieveResponseStream = true;
            getCmd.BuildRequest = (cmd, uri, builder, cnt, serverTimeout, ctx) => DirectoryHttpRequestMessageFactory.List(uri, serverTimeout, listingContext, cnt, ctx, this.ServiceClient.GetCanonicalizer(), this.ServiceClient.Credentials);
            getCmd.PreProcessResponse = (cmd, resp, ex, ctx) => HttpResponseParsers.ProcessExpectedStatusCodeNoException(HttpStatusCode.OK, resp, null /* retVal */, cmd, ex);
            getCmd.PostProcessResponse = (cmd, resp, ctx) =>
            {
                return Task.Factory.StartNew(() =>
                {
                    ListFilesAndDirectoriesResponse listFilesResponse = new ListFilesAndDirectoriesResponse(cmd.ResponseStream);
                    List<IListFileItem> fileList = listFilesResponse.Files.Select(item => this.SelectListFileItem(item)).ToList();
                    FileContinuationToken continuationToken = null;
                    if (listFilesResponse.NextMarker != null)
                    {
                        continuationToken = new FileContinuationToken()
                        {
                            NextMarker = listFilesResponse.NextMarker,
                            TargetLocation = cmd.CurrentResult.TargetLocation,
                        };
                    }

                    return new ResultSegment<IListFileItem>(fileList)
                    {
                        ContinuationToken = continuationToken,
                    };
                });
            };

            return getCmd;
        }
 public void ListFilesAndDirectoriesScenarioTest(string shareName, FileListingContext listingContext, HttpStatusCode? expectedError, params string[] expectedFiles)
 {
     HttpWebRequest request = FileTests.ListFilesAndDirectoriesRequest(FileContext, shareName, listingContext);
     Assert.IsTrue(request != null, "Failed to create HttpWebRequest");
     if (FileContext.Credentials != null)
     {
         FileTests.SignRequest(request, FileContext);
     }
     HttpWebResponse response = FileTestUtils.GetResponse(request, FileContext);
     try
     {
         FileTests.ListFilesAndDirectoriesResponse(response, FileContext, expectedError);
         ListFilesAndDirectoriesResponse listFilesResponse = new ListFilesAndDirectoriesResponse(response.GetResponseStream());
         int i = 0;
         foreach (IListFileEntry item in listFilesResponse.Files)
         {
             ListFileEntry file = item as ListFileEntry;
             if (expectedFiles == null)
             {
                 Assert.Fail("Should not have files.");
             }
             Assert.IsTrue(i < expectedFiles.Length, "Unexpected file: " + file.Name);
             Assert.AreEqual<string>(expectedFiles[i++], file.Name, "Incorrect file.");
         }
         if (expectedFiles != null && i < expectedFiles.Length)
         {
             Assert.Fail("Missing file: " + expectedFiles[i] + "(and " + (expectedFiles.Length - i - 1) + " more).");
         }
     }
     finally
     {
         response.Close();
     }
 }