/// <summary>
        /// Moves an existing store item into this collection, overwriting any existing items.
        /// </summary>
        /// <param name="source">
        /// The store item to move.
        /// </param>
        /// <param name="destinationName">
        /// The <see cref="IWebDAVStoreItem"/> that refers to the item that was moved,
        /// in its new location.
        /// </param>
        /// <returns>
        /// The moved <see cref="IWebDAVStoreItem"/> instance.
        /// </returns>
        /// <remarks>
        /// Note that the method should fail without creating or overwriting content in the
        /// target collection if the move cannot go through.
        /// </remarks>
        public IWebDAVStoreItem MoveItemHere(IWebDAVStoreItem source, string destinationName)
        {
            IWebDAVStoreItem item = CopyItemHere(source, destinationName);

            source.ParentCollection.Delete(source);
            return(item);
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieves a store item through the specified <see cref="Uri"/> from the
        /// specified <see cref="WebDAVServer"/> and <see cref="IWebDAVStore"/>.
        /// </summary>
        /// <param name="uri">
        /// The <see cref="Uri"/> to retrieve the store item for.
        /// </param>
        /// <param name="server">
        /// The <see cref="WebDAVServer"/> that hosts the <paramref name="store"/>.
        /// </param>
        /// <param name="store">
        /// The <see cref="IWebDAVStore"/> from which to retrieve the store item.
        /// </param>
        /// <returns>
        /// The retrieved store item.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="uri"/> is <c>null</c>.</para>
        /// <para><paramref name="server"/> is <c>null</c>.</para>
        /// <para><paramref name="store"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="HttpConflictException">
        /// <para><paramref name="uri"/> refers to a document in a collection, where the collection does not exist.</para>
        /// </exception>
        /// <exception cref="HttpNotFoundException">
        /// <para><paramref name="uri"/> refers to a document that does not exist.</para>
        /// </exception>
        public static IWebDAVStoreItem GetItem(this Uri uri, WebDAVServer server, IWebDAVStore store)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }
            if (store == null)
            {
                throw new ArgumentNullException("store");
            }

            Uri prefixUri = uri.GetPrefixUri(server);
            IWebDAVStoreCollection collection = store.Root;

            IWebDAVStoreItem item = null;

            if (prefixUri.Segments.Length == uri.Segments.Length)
            {
                return(collection);
            }

            for (int index = prefixUri.Segments.Length; index < uri.Segments.Length; index++)
            {
                string           segmentName = uri.Segments[index];
                IWebDAVStoreItem nextItem    = collection.GetItemByName(segmentName.TrimEnd('/', '\\'));
                if (nextItem == null)
                {
                    throw new HttpConflictException();
                }

                if (index == uri.Segments.Length - 1)
                {
                    item = nextItem;
                }
                else
                {
                    collection = nextItem as IWebDAVStoreCollection;
                    if (collection == null)
                    {
                        throw new HttpNotFoundException();
                    }
                }
            }

            if (item == null)
            {
                throw new HttpNotFoundException();
            }
            return(item);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">
        /// The <see cref="WebDAVServer"/> through which the request came in from the client.
        /// </param>
        /// <param name="context">
        /// The <see cref="IHttpListenerContext"/> object containing both the request and response
        /// objects to use.
        /// </param>
        /// <param name="store">
        /// The <see cref="IWebDAVStore"/> that the <see cref="WebDAVServer"/> is hosting.
        /// </param>
        /// <param name="logger">
        /// The <see cref="ILogger"/> to log to.
        /// </param>
        public void ProcessRequest(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, ILogger logger)
        {
            IWebDAVStoreItem source = context.Request.Url.GetItem(server, store);

            if (source is IWebDAVStoreDocument || source is IWebDAVStoreCollection)
            {
                CopyItem(server, context, store, source);
            }
            else
            {
                throw new HttpMethodNotAllowedException();
            }
        }
        private IWebDAVStoreCollection CopyCollection(IWebDAVStoreCollection source, string destinationName)
        {
            IWebDAVStoreItem existingItem = GetItemByName(destinationName);

            if (existingItem != null)
            {
                Delete(existingItem);
            }

            IWebDAVStoreCollection destination = CreateCollection(destinationName);

            foreach (IWebDAVStoreItem subItem in source.Items)
            {
                destination.CopyItemHere(subItem, subItem.Name);
            }

            return(destination);
        }
        /// <summary>
        /// Copies an existing store item into this collection, overwriting any existing items.
        /// </summary>
        /// <param name="source">
        /// The store item to copy from.
        /// </param>
        /// <param name="destinationName">
        /// The name of the copy to create of <paramref name="source"/>.
        /// </param>
        /// <returns>
        /// The created <see cref="IWebDAVStoreItem"/> instance.
        /// </returns>
        public IWebDAVStoreItem CopyItemHere(IWebDAVStoreItem source, string destinationName)
        {
            var document = source as IWebDAVStoreDocument;

            if (document != null)
            {
                return(CopyDocument(document, destinationName));
            }

            var collection = source as IWebDAVStoreCollection;

            if (collection != null)
            {
                return(CopyCollection(collection, destinationName));
            }

            throw new HttpInternalServerException();
        }
        private void CopyItem(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, IWebDAVStoreItem source)
        {
            var destinationUri = new Uri(context.Request.Headers["Destination"]);
            var destinationParentCollection = destinationUri.GetParentUri().GetItem(server, store) as IWebDAVStoreCollection;

            if (destinationParentCollection == null)
            {
                throw new HttpConflictException();
            }

            bool isNew = true;

            string           destinationName = destinationUri.Segments.Last().TrimEnd('/', '\\');
            IWebDAVStoreItem destination     = destinationParentCollection.GetItemByName(destinationName);

            if (source == destination)
            {
                throw new HttpConflictException();
            }

            if (destination != null)
            {
                if (context.Request.Headers["Overwrite"] == "F")
                {
                    throw new HttpException(HttpStatusCodes.ClientErrors.PreconditionFailed);
                }
                if (destination is IWebDAVStoreCollection)
                {
                    destinationParentCollection.Delete(destination);
                }
                isNew = false;
            }

            destinationParentCollection.CopyItemHere(source, destinationName);

            if (isNew)
            {
                context.SendSimpleResponse(HttpStatusCodes.Successful.Created);
            }
            else
            {
                context.SendSimpleResponse(HttpStatusCodes.Successful.NoContent);
            }
        }
        private IWebDAVStoreDocument CopyDocument(IWebDAVStoreDocument source, string destinationName)
        {
            IWebDAVStoreItem existingItem = GetItemByName(destinationName);

            if (existingItem != null)
            {
                Delete(existingItem);
            }

            IWebDAVStoreDocument destination = CreateDocument(destinationName);

            using (Stream sourceStream = source.OpenReadStream())
                using (Stream destinationStream = destination.OpenWriteStream(false))
                {
                    sourceStream.CopyTo(destinationStream);
                }

            return(destination);
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="server">
        /// The <see cref="WebDAVServer"/> through which the request came in from the client.
        /// </param>
        /// <param name="context">
        /// The <see cref="IHttpListenerContext"/> object containing both the request and response
        /// objects to use.
        /// </param>
        /// <param name="store">
        /// The <see cref="IWebDAVStore"/> that the <see cref="WebDAVServer"/> is hosting.
        /// </param>
        /// <param name="logger">
        /// The <see cref="ILogger"/> to log to.
        /// </param>
        public void ProcessRequest(WebDAVServer server, IHttpListenerContext context, IWebDAVStore store, ILogger logger)
        {
            Uri parentCollectionUri = context.Request.Url.GetParentUri();
            var collection          = parentCollectionUri.GetItem(server, store) as IWebDAVStoreCollection;

            if (collection == null)
            {
                throw new HttpConflictException();
            }

            IWebDAVStoreItem item = collection.GetItemByName(context.Request.Url.Segments.Last().TrimEnd('/', '\\'));

            if (item == null)
            {
                throw new HttpNotFoundException();
            }

            collection.Delete(item);
            context.SendSimpleResponse();
        }
        /// <summary>
        /// Deletes a store item by its name.
        /// </summary>
        /// <param name="item">
        /// The name of the store item to delete.
        /// </param>
        public void Delete(IWebDAVStoreItem item)
        {
            var    diskItem = (WebDAVDiskStoreItem)item;
            string itemPath = diskItem.ItemPath;

            if (item is WebDAVDiskStoreDocument)
            {
                if (!File.Exists(itemPath))
                {
                    throw new HttpNotFoundException();
                }
                File.Delete(itemPath);
            }
            else
            {
                if (!Directory.Exists(itemPath))
                {
                    throw new HttpNotFoundException();
                }
                Directory.Delete(diskItem.ItemPath);
            }
        }
 /// <summary>
 /// Deletes a store item by its name.
 /// </summary>
 /// <param name="item">
 /// The name of the store item to delete.
 /// </param>
 public void Delete(IWebDAVStoreItem item)
 {
     _Items.Remove(item.Name);
 }
 /// <summary>
 /// Moves an existing store item into this collection, overwriting any existing items.
 /// </summary>
 /// <param name="source">
 /// The store item to move.
 /// </param>
 /// <param name="destinationName">
 /// The <see cref="IWebDAVStoreItem"/> that refers to the item that was moved,
 /// in its new location.
 /// </param>
 /// <returns>
 /// The moved <see cref="IWebDAVStoreItem"/> instance.
 /// </returns>
 /// <remarks>
 /// Note that the method should fail without creating or overwriting content in the
 /// target collection if the move cannot go through.
 /// </remarks>
 public IWebDAVStoreItem MoveItemHere(IWebDAVStoreItem source, string destinationName)
 {
     throw new NotImplementedException();
 }