/// <summary> /// Takes out a lock of any type or refreshes an existing lock of the resource identified by the request URI. /// </summary> /// <param name="requestUri">The <see cref="Uri"/> to request.</param> /// <param name="parameters">Parameters of the LOCK operation.</param> /// <returns>An instance of <see cref="LockResponse" />.</returns> public LockResponse Lock(Uri requestUri, LockParameters parameters) { Guard.NotNull(requestUri, "requestUri"); var headerBuilder = new HeaderBuilder(); if (parameters.ApplyTo.HasValue) { headerBuilder.Add(WebDavHeaders.Depth, DepthHeaderHelper.GetValueForLock(parameters.ApplyTo.Value)); } if (parameters.Timeout.HasValue) { headerBuilder.Add(WebDavHeaders.Timeout, $"Second-{parameters.Timeout.Value.TotalSeconds}"); } var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build(); var requestBody = LockRequestBuilder.BuildRequestBody(parameters); using (var content = new StringContent(requestBody)) { var requestParams = new RequestParameters { Headers = headers, Content = content }; using (var response = _dispatcher.Send(requestUri, WebDavMethod.Lock, requestParams)) { if (response.StatusCode != HttpStatusCode.OK) { return(new LockResponse((int)response.StatusCode, response.StatusDescription)); } var responseContent = ReadContentAsString(response); return(_lockResponseParser.Parse(responseContent, (int)response.StatusCode, response.StatusDescription)); } } }
/// <summary> /// Takes out a lock of any type or refreshes an existing lock of the resource identified by the request URI. /// </summary> /// <param name="requestUri">The <see cref="Uri"/> to request.</param> /// <param name="parameters">Parameters of the LOCK operation.</param> /// <returns>An instance of <see cref="LockResponse" />.</returns> public async Task <LockResponse> Lock(Uri requestUri, LockParameters parameters) { Guard.NotNull(requestUri, "requestUri"); var headerBuilder = new HeaderBuilder(); if (parameters.ApplyTo.HasValue) { headerBuilder.Add(WebDavHeaders.Depth, DepthHeaderHelper.GetValueForLock(parameters.ApplyTo.Value)); } if (parameters.Timeout.HasValue) { headerBuilder.Add(WebDavHeaders.Timeout, $"Second-{parameters.Timeout.Value.TotalSeconds}"); } var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build(); var requestBody = LockRequestBuilder.BuildRequestBody(parameters); var requestParams = new RequestParameters { Headers = headers, Content = new StringContent(requestBody), ContentType = parameters.ContentType }; var response = await _dispatcher.Send(requestUri, WebDavMethod.Lock, requestParams, parameters.CancellationToken).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { return(new LockResponse((int)response.StatusCode, response.ReasonPhrase)); } var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false); return(_lockResponseParser.Parse(responseContent, (int)response.StatusCode, response.ReasonPhrase)); }
/// <summary> /// Takes out a lock of any type or refreshes an existing lock of 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 LOCK operation.</param> /// <returns>An instance of <see cref="LockResponse" /></returns> public async Task <LockResponse> Lock([NotNull] Uri requestUri, [NotNull] LockParameters parameters) { Check.NotNull(requestUri, nameof(requestUri)); Check.NotNull(parameters, nameof(parameters)); var headers = new RequestHeaders(); if (parameters.ApplyTo.HasValue) { headers.Add(new KeyValuePair <string, string>("Depth", DepthHeaderHelper.GetValueForLock(parameters.ApplyTo.Value))); } if (parameters.Timeout.HasValue) { headers.Add(new KeyValuePair <string, string>("Timeout", $"Second-{parameters.Timeout.Value.TotalSeconds}")); } string requestBody = LockRequestBuilder.BuildRequestBody(parameters); var requestParams = new RequestParameters { Headers = headers, Content = new StringContent(requestBody, DefaultEncoding, MediaTypeXml) }; var response = await _dispatcher.Send(requestUri, WebDavMethod.Lock, requestParams, parameters.CancellationToken); if (!response.IsSuccessful) { return(new LockResponse(response.StatusCode, response.Description)); } var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false); return(_lockResponseParser.Parse(responseContent, response.StatusCode, response.Description)); }
public static string BuildRequestBody(LockParameters lockParams) { var doc = new XDocument(new XDeclaration("1.0", "utf-8", null)); var lockinfo = new XElement("{DAV:}lockinfo", new XAttribute(XNamespace.Xmlns + "D", "DAV:")); lockinfo.Add(GetLockScope(lockParams.LockScope)); lockinfo.Add(GetLockType()); if (lockParams.Owner != null) { lockinfo.Add(GetLockOwner(lockParams.Owner)); } doc.Add(lockinfo); return(doc.ToStringWithDeclaration()); }
/// <summary> /// Takes out a lock of any type or refreshes an existing lock of 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 LOCK operation.</param> /// <returns>An instance of <see cref="LockResponse" />.</returns> public LockResponse Lock(string requestUri, LockParameters parameters) { return(Lock(CreateUri(requestUri), parameters)); }