/// <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> /// 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)); } } }
/// <summary> /// Moves the resource identified by the source URI to the destination identified by the destination URI. /// </summary> /// <param name="sourceUri">The source <see cref="Uri"/>.</param> /// <param name="destUri">The destination <see cref="Uri"/>.</param> /// <param name="parameters">Parameters of the MOVE operation.</param> /// <returns>An instance of <see cref="WebDavResponse" />.</returns> public async Task <WebDavResponse> Move(Uri sourceUri, Uri destUri, MoveParameters parameters) { Guard.NotNull(sourceUri, "sourceUri"); Guard.NotNull(destUri, "destUri"); var headerBuilder = new HeaderBuilder() .Add(WebDavHeaders.Destination, GetAbsoluteUri(destUri).AbsoluteUri) .Add(WebDavHeaders.Overwrite, parameters.Overwrite ? "T" : "F"); if (!string.IsNullOrEmpty(parameters.SourceLockToken)) { headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.SourceLockToken)); } if (!string.IsNullOrEmpty(parameters.DestLockToken)) { headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.DestLockToken)); } var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build(); var requestParams = new RequestParameters { Headers = headers }; var response = await _dispatcher.Send(sourceUri, WebDavMethod.Move, requestParams, parameters.CancellationToken).ConfigureAwait(false); return(new WebDavResponse((int)response.StatusCode, response.ReasonPhrase)); }
/// <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)); }
/// <summary> /// Creates a duplicate of the source resource identified by the source URI in the destination resource identified by the destination URI. /// </summary> /// <param name="sourceUri">The source <see cref="Uri"/>.</param> /// <param name="destUri">The destination <see cref="Uri"/>.</param> /// <param name="parameters">Parameters of the COPY operation.</param> /// <returns>An instance of <see cref="WebDavResponse" /></returns> public WebDavResponse Copy(Uri sourceUri, Uri destUri, CopyParameters parameters) { Guard.NotNull(sourceUri, "sourceUri"); Guard.NotNull(destUri, "destUri"); var applyTo = parameters.ApplyTo ?? ApplyTo.Copy.ResourceAndAncestors; var headerBuilder = new HeaderBuilder() .Add(WebDavHeaders.Destination, GetAbsoluteUri(destUri).AbsoluteUri) .Add(WebDavHeaders.Depth, DepthHeaderHelper.GetValueForCopy(applyTo)) .Add(WebDavHeaders.Overwrite, parameters.Overwrite ? "T" : "F"); if (!string.IsNullOrEmpty(parameters.DestLockToken)) { headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.DestLockToken)); } var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build(); var requestParams = new RequestParameters { Headers = headers }; using (var response = _dispatcher.Send(sourceUri, WebDavMethod.Copy, requestParams)) { return(new WebDavResponse((int)response.StatusCode, response.StatusDescription)); } }
/// <summary> /// Retrieves 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 PROPFIND operation.</param> /// <returns>An instance of <see cref="PropfindResponse" />.</returns> public async Task <PropfindResponse> Propfind(Uri requestUri, PropfindParameters parameters) { Guard.NotNull(requestUri, "requestUri"); var applyTo = parameters.ApplyTo ?? ApplyTo.Propfind.ResourceAndChildren; var headers = new HeaderBuilder() .Add(WebDavHeaders.Depth, DepthHeaderHelper.GetValueForPropfind(applyTo)) .AddWithOverwrite(parameters.Headers) .Build(); HttpContent requestBody = null; if (parameters.RequestType != PropfindRequestType.AllPropertiesImplied) { var content = PropfindRequestBuilder.BuildRequest(parameters.RequestType, parameters.CustomProperties, parameters.Namespaces); requestBody = new StringContent(content); } var requestParams = new RequestParameters { Headers = headers, Content = requestBody, ContentType = parameters.ContentType }; var response = await _dispatcher.Send(requestUri, WebDavMethod.Propfind, requestParams, parameters.CancellationToken).ConfigureAwait(false); var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false); return(_propfindResponseParser.Parse(responseContent, (int)response.StatusCode, response.ReasonPhrase)); }
/// <summary> /// Moves the resource identified by the source URI to the destination identified by the destination URI. /// </summary> /// <param name="sourceUri">The source <see cref="Uri"/>.</param> /// <param name="destUri">The destination <see cref="Uri"/>.</param> /// <param name="parameters">Parameters of the MOVE operation.</param> /// <returns>An instance of <see cref="WebDavResponse" />.</returns> public WebDavResponse Move(Uri sourceUri, Uri destUri, MoveParameters parameters) { Guard.NotNull(sourceUri, "sourceUri"); Guard.NotNull(destUri, "destUri"); var headerBuilder = new HeaderBuilder() .Add(WebDavHeaders.Destination, GetAbsoluteUri(destUri).AbsoluteUri) .Add(WebDavHeaders.Overwrite, parameters.Overwrite ? "T" : "F"); if (!string.IsNullOrEmpty(parameters.SourceLockToken)) { headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.SourceLockToken)); } if (!string.IsNullOrEmpty(parameters.DestLockToken)) { headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.DestLockToken)); } var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build(); var requestParams = new RequestParameters { Headers = headers }; using (var response = _dispatcher.Send(sourceUri, WebDavMethod.Move, requestParams)) { return(new WebDavResponse((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 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> /// Retrieves 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 PROPFIND operation.</param> /// <returns>An instance of <see cref="PropfindResponse" />.</returns> public PropfindResponse PropFind(Uri requestUri, PropfindParameters parameters) { Guard.NotNull(requestUri, "requestUri"); var applyTo = parameters.ApplyTo ?? ApplyTo.Propfind.ResourceAndChildren; var headers = new HeaderBuilder() .Add(WebDavHeaders.Depth, DepthHeaderHelper.GetValueForPropfind(applyTo)) .AddWithOverwrite(parameters.Headers) .Build(); var requestBody = PropfindRequestBuilder.BuildRequest(parameters.RequestType, parameters.CustomProperties, parameters.Namespaces); using (var content = new StringContent(requestBody)) { var requestParams = new RequestParameters { Headers = headers, Content = content }; using (var response = _dispatcher.Send(requestUri, WebDavMethod.Propfind, requestParams)) { var responseContent = ReadContentAsString(response); return(_propfindResponseParser.Parse(responseContent, (int)response.StatusCode, response.StatusDescription)); } } }
/// <summary> /// Removes the lock identified by the lock token from the resource identified by the request URI. /// </summary> /// <param name="requestUri">The <see cref="Uri"/> to request.</param> /// <param name="parameters">Parameters of the UNLOCK operation.</param> /// <returns>An instance of <see cref="WebDavResponse" />.</returns> public async Task <WebDavResponse> Unlock(Uri requestUri, UnlockParameters parameters) { Guard.NotNull(requestUri, "requestUri"); var headers = new HeaderBuilder() .Add(WebDavHeaders.LockToken, $"<{parameters.LockToken}>") .AddWithOverwrite(parameters.Headers) .Build(); var requestParams = new RequestParameters { Headers = headers }; var response = await _dispatcher.Send(requestUri, WebDavMethod.Unlock, requestParams, parameters.CancellationToken).ConfigureAwait(false); return(new WebDavResponse((int)response.StatusCode, response.ReasonPhrase)); }
internal virtual async Task <WebDavStreamResponse> GetFile(Uri requestUri, bool translate, GetFileParameters parameters) { Guard.NotNull(requestUri, "requestUri"); var headers = new HeaderBuilder() .Add(WebDavHeaders.Translate, translate ? "t" : "f") .AddWithOverwrite(parameters.Headers) .Build(); var requestParams = new RequestParameters { Headers = headers }; var response = await _dispatcher.Send(requestUri, HttpMethod.Get, requestParams, parameters.CancellationToken); var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); return(new WebDavStreamResponse(response.StatusCode, response.Description, stream)); }
/// <summary> /// Removes the lock identified by the lock token from the resource identified by the request URI. /// </summary> /// <param name="requestUri">The <see cref="Uri"/> to request.</param> /// <param name="parameters">Parameters of the UNLOCK operation.</param> /// <returns>An instance of <see cref="WebDavResponse" />.</returns> public WebDavResponse Unlock(Uri requestUri, UnlockParameters parameters) { Guard.NotNull(requestUri, "requestUri"); var headers = new HeaderBuilder() .Add(WebDavHeaders.LockToken, $"<{parameters.LockToken}>") .AddWithOverwrite(parameters.Headers) .Build(); var requestParams = new RequestParameters { Headers = headers }; using (var response = _dispatcher.Send(requestUri, WebDavMethod.Unlock, requestParams)) { return(new WebDavResponse((int)response.StatusCode, response.StatusDescription)); } }
/// <summary> /// Creates a new collection resource at the location specified by the request URI. /// </summary> /// <param name="requestUri">The <see cref="System.Uri"/> to request.</param> /// <param name="parameters">Parameters of the MKCOL operation.</param> /// <returns>An instance of <see cref="WebDavResponse" /></returns> public async Task <WebDavResponse> Mkcol(Uri requestUri, MkColParameters 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 requestParams = new RequestParameters { Headers = headers }; var response = await _dispatcher.Send(requestUri, WebDavMethod.Mkcol, requestParams, parameters.CancellationToken); return(new WebDavResponse(response.StatusCode, response.Description)); }
/// <summary> /// Executes a SEARCH operation. /// </summary> /// <param name="requestUri">The <see cref="Uri"/> to request.</param> /// <param name="parameters">Parameters of the SEARCH operation.</param> /// <returns>An instance of <see cref="PropfindResponse" />.</returns> public async Task <PropfindResponse> Search(Uri requestUri, SearchParameters parameters) { Guard.NotNull(requestUri, "requestUri"); parameters.AssertParametersAreValid(); var headers = new HeaderBuilder() .AddWithOverwrite(parameters.Headers) .Build(); var requestBody = new StringContent(SearchRequestBuilder.BuildRequestBody(parameters)); var requestParams = new RequestParameters { Headers = headers, Content = requestBody, ContentType = new MediaTypeHeaderValue("text/xml") }; var response = await _dispatcher.Send(requestUri, WebDavMethod.Search, requestParams, parameters.CancellationToken).ConfigureAwait(false); var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false); return(_propfindResponseParser.Parse(responseContent, (int)response.StatusCode, response.ReasonPhrase)); }
/// <summary> /// Deletes the resource identified by the request URI. /// </summary> /// <param name="requestUri">The <see cref="Uri"/> to request.</param> /// <param name="parameters">Parameters of the DELETE operation.</param> /// <returns>An instance of <see cref="WebDavResponse" />.</returns> public async Task <WebDavResponse> Delete(Uri requestUri, DeleteParameters 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 requestParams = new RequestParameters { Headers = headers }; var response = await _dispatcher.Send(requestUri, HttpMethod.Delete, requestParams, parameters.CancellationToken).ConfigureAwait(false); return(new WebDavResponse((int)response.StatusCode, response.ReasonPhrase)); }
/// <summary> /// Requests the resource to be stored under the request URI. /// </summary> /// <param name="requestUri">The <see cref="System.Uri"/> to request.</param> /// <param name="stream">The stream of content of the resource.</param> /// <param name="parameters">Parameters of the PUT operation.</param> /// <returns>An instance of <see cref="WebDavResponse" /></returns> public async Task <WebDavResponse> PutFile(Uri requestUri, Stream stream, PutFileParameters parameters) { Guard.NotNull(requestUri, "requestUri"); Guard.NotNull(stream, "stream"); 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 requestParams = new RequestParameters { Headers = headers, Content = new StreamContent(stream), ContentType = parameters.ContentType }; var response = await _dispatcher.Send(requestUri, HttpMethod.Put, requestParams, parameters.CancellationToken); return(new WebDavResponse(response.StatusCode, response.Description)); }
internal virtual WebDavStreamResponse GetFile(Uri requestUri, bool translate, GetFileParameters parameters) { Guard.NotNull(requestUri, "requestUri"); var headers = new HeaderBuilder() .Add(WebDavHeaders.Translate, translate ? "t" : "f") .AddWithOverwrite(parameters.Headers) .Build(); var requestParams = new RequestParameters { Headers = headers, OperationProgress = parameters.OperationProgress }; using (var response = _dispatcher.Send(requestUri, HttpMethod.Get, requestParams)) { return(new WebDavStreamResponse(response)); } }
/// <summary> /// Retrieves the raw http response of a file identified by the request URI. /// </summary> /// <param name="requestUri">The <see cref="Uri"/> to request.</param> /// <param name="translate">A parameter indicating if the response can be processed by the web server.</param> /// <param name="parameters">Parameters of the GET operation.</param> /// <returns>An instance of <see cref="HttpResponseMessage" />.</returns> public async Task <HttpResponseMessage> GetFileResponse(Uri requestUri, bool translate, GetFileParameters parameters) { Guard.NotNull(requestUri, "requestUri"); var headers = new HeaderBuilder() .Add(WebDavHeaders.Translate, translate ? "t" : "f") .AddWithOverwrite(parameters.Headers) .Build(); var requestParams = new RequestParameters { Headers = headers }; var response = await _dispatcher.Send( requestUri, HttpMethod.Get, requestParams, parameters.CancellationToken, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); return(response); }
/// <summary> /// Deletes the resource identified by the request URI. /// </summary> public WebDavResponse Delete(Uri requestUri, DeleteParameters 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 requestParams = new RequestParameters { Headers = headers }; using (var response = _dispatcher.Send(requestUri, HttpMethod.Delete, requestParams)) { return(new WebDavResponse(response)); } }
/// <summary> /// Requests the resource to be stored under the request URI. /// </summary> /// <param name="requestUri">The <see cref="Uri"/> to request.</param> /// <param name="content">The content to pass to the request.</param> /// <param name="parameters">Parameters of the PUT operation.</param> /// <returns>An instance of <see cref="WebDavResponse" />.</returns> public WebDavResponse PutFile(Uri requestUri, HttpContent content, PutFileParameters parameters) { Guard.NotNull(requestUri, "requestUri"); Guard.NotNull(content, "content"); 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 requestParams = new RequestParameters { Headers = headers, Content = content, ContentType = parameters.ContentType, OperationProgress = parameters.OperationProgress }; using (var response = _dispatcher.Send(requestUri, HttpMethod.Put, requestParams)) { return(new WebDavResponse(response)); } }