Ejemplo n.º 1
0
        /// <summary>
        /// Sets and/or removes properties defined on the resource identified by the request URI.
        /// </summary>
        /// <param name="requestUri">The <see cref="Uri"/> to request.</param>
        /// <param name="parameters">Parameters of the PROPPATCH operation.</param>
        /// <returns>An instance of <see cref="ProppatchResponse" />.</returns>
        public ProppatchResponse Proppatch(Uri requestUri, ProppatchParameters parameters)
        {
            Guard.NotNull(requestUri, "requestUri");

            var headerBuilder = new HeaderBuilder();

            if (!string.IsNullOrEmpty(parameters.LockToken))
            {
                headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.LockToken));
            }

            var headers     = headerBuilder.AddWithOverwrite(parameters.Headers).Build();
            var requestBody = ProppatchRequestBuilder.BuildRequestBody(
                parameters.PropertiesToSet,
                parameters.PropertiesToRemove,
                parameters.Namespaces);

            using (var content = new StringContent(requestBody))
            {
                var requestParams = new RequestParameters {
                    Headers = headers, Content = content
                };
                using (var response = _dispatcher.Send(requestUri, WebDavMethod.Proppatch, requestParams))
                {
                    var responseContent = ReadContentAsString(response);
                    return(_proppatchResponseParser.Parse(responseContent, (int)response.StatusCode, response.StatusDescription));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets and/or removes properties defined on the resource identified by the request URI.
        /// </summary>
        /// <param name="requestUri">The <see cref="System.Uri"/> to request.</param>
        /// <param name="parameters">Parameters of the PROPPATCH operation.</param>
        /// <returns>An instance of <see cref="ProppatchResponse" /></returns>
        public async Task <ProppatchResponse> Proppatch([NotNull] Uri requestUri, [NotNull] ProppatchParameters parameters)
        {
            Check.NotNull(requestUri, nameof(requestUri));
            Check.NotNull(parameters, nameof(parameters));

            var headers = new RequestHeaders();

            if (!string.IsNullOrEmpty(parameters.LockToken))
            {
                headers.Add(new KeyValuePair <string, string>("If", IfHeaderHelper.GetHeaderValue(parameters.LockToken)));
            }

            string requestBody = ProppatchRequestBuilder.BuildRequestBody(
                parameters.PropertiesToSet,
                parameters.PropertiesToRemove,
                parameters.Namespaces);

            var requestParams = new RequestParameters {
                Headers = headers, Content = new StringContent(requestBody, DefaultEncoding, MediaTypeXml)
            };

            var response = await _dispatcher.Send(requestUri, WebDavMethod.Proppatch, requestParams, parameters.CancellationToken);

            var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false);

            return(_proppatchResponseParser.Parse(responseContent, response.StatusCode, response.Description));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sets and/or removes properties defined on the resource identified by the request URI.
        /// </summary>
        /// <param name="requestUri">The <see cref="Uri"/> to request.</param>
        /// <param name="parameters">Parameters of the PROPPATCH operation.</param>
        /// <returns>An instance of <see cref="ProppatchResponse" />.</returns>
        public async Task <ProppatchResponse> Proppatch(Uri requestUri, ProppatchParameters parameters)
        {
            Guard.NotNull(requestUri, "requestUri");

            var headerBuilder = new HeaderBuilder();

            if (!string.IsNullOrEmpty(parameters.LockToken))
            {
                headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.LockToken));
            }

            var headers     = headerBuilder.AddWithOverwrite(parameters.Headers).Build();
            var requestBody = ProppatchRequestBuilder.BuildRequestBody(
                parameters.PropertiesToSet,
                parameters.PropertiesToRemove,
                parameters.Namespaces);
            var requestParams = new RequestParameters {
                Headers = headers, Content = new StringContent(requestBody), ContentType = parameters.ContentType
            };
            var response = await _dispatcher.Send(requestUri, WebDavMethod.Proppatch, requestParams, parameters.CancellationToken).ConfigureAwait(false);

            var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false);

            return(_proppatchResponseParser.Parse(responseContent, (int)response.StatusCode, response.ReasonPhrase));
        }
Ejemplo n.º 4
0
        public void Upload(string webDavLocation, string localFile, WebDavOperationCallback operationProgress = null)
        {
            webDavLocation = webDavLocation.Replace("\\", "/").Trim('/');

            var webDavFile = new Uri($"{this.BaseAddress}{webDavLocation}");

            using (var stream = new FileStream(localFile, FileMode.Open))
            {
                var putFileParams = new PutFileParameters();
                putFileParams.OperationProgress = operationProgress;

                var response = this.PutFile(webDavFile, stream, putFileParams);
                if (!response.IsSuccessful)
                {
                    throw new ApplicationException($"Unable save file: --> {response.StatusCode} {response.Description}");
                }
            }

            var name = XName.Get("x-lastmodified", "DataSync");

            FileInfo fi = new FileInfo(localFile);

            int attempt = 5;

            while (true)
            {
                var patch = new ProppatchParameters();
                patch.PropertiesToSet.Add(name, fi.LastWriteTimeUtc.ToString("u"));
                patch.Namespaces.Add(new NamespaceAttr("u", "DataSync"));

                var propPatch = this.Proppatch(webDavFile, patch);
                if (propPatch.IsSuccessful)
                {
                    break;
                }

                attempt = attempt - 1;
                if (attempt == 0)
                {
                    throw new ApplicationException($"Unable update file properties: --> {propPatch.StatusCode} {propPatch.Description}");
                }
                Thread.Sleep(1000);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Sets and/or removes properties defined on the resource identified by the request URI.
 /// </summary>
 /// <param name="requestUri">A string that represents the request URI.</param>
 /// <param name="parameters">Parameters of the PROPPATCH operation.</param>
 /// <returns>An instance of <see cref="ProppatchResponse" />.</returns>
 public ProppatchResponse Proppatch(string requestUri, ProppatchParameters parameters)
 {
     return(Proppatch(CreateUri(requestUri), parameters));
 }