DavResourceFindBase.cs :  » Network-Servers » WebDAV.NET-Server » Sphorium » WebDAV » Server » Framework » BaseClasses » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Servers » WebDAV.NET Server 
WebDAV.NET Server » Sphorium » WebDAV » Server » Framework » BaseClasses » DavResourceFindBase.cs
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Xml.XPath;

using Sphorium.WebDAV.Server.Framework.Classes;
using Sphorium.WebDAV.Server.Framework.Resources;
using Sphorium.WebDAV.Server.Framework.Collections;

namespace Sphorium.WebDAV.Server.Framework.BaseClasses{
  /// <summary>
  /// Dav Resource Find Framework Base Class
  /// </summary>
  /// <remarks>
  ///    Used by DAVPropFindBase and DAVReportBase
  /// </remarks>  
  public abstract class DavResourceFindBase : DavMethodBase
  {
    /// <summary>
    /// Dav Resource Find Framework Base Class
    /// </summary>
    protected DavResourceFindBase() 
    {
      this.FileResources = new DavResourceCollection();
      this.MissingResources = new DavResourceCollection();
      this.CollectionResources = new DavResourceCollection();
    }

    /// <summary>
    /// Add a resource
    /// </summary>
    /// <param name="resource"></param>
    protected void AddResource(DavResourceBase resource)
    {
      if (resource is DavFile)
        this.FileResources.Add(resource);
      else if (resource is DavFolder)
        this.CollectionResources.Add(resource);
    }

    /// <summary>
    /// Resource count
    /// </summary>
    protected int ResourceCount
    {
      get
      {
        return this.CollectionResources.Count + this.FileResources.Count;
      }
    }

    /// <summary>
    /// Retrieve the current collection resources
    /// </summary>
    protected DavResourceCollection CollectionResources { get; private set; }

    /// <summary>
    /// Retrieve the current file resources
    /// </summary>
    protected DavResourceCollection FileResources { get; private set; }

    /// <summary>
    /// Retrieve the current missing resources
    /// </summary>
    protected DavResourceCollection MissingResources { get; private set; }


    /// <summary>
    /// Requested properties
    /// </summary>
    protected DavPropertyCollection RequestProperties
    {
      get
      {
        DavPropertyCollection _davProperties = new DavPropertyCollection();

        if (base.RequestXml != null)
        {
          XPathNodeIterator _propNodeIterator = base.RequestXml.SelectDescendants("prop", "DAV:", false);
          if (_propNodeIterator.MoveNext())
          {
            XPathNodeIterator _nodeChildren = _propNodeIterator.Current.SelectChildren(XPathNodeType.All);
            while (_nodeChildren.MoveNext())
            {
              XPathNavigator _currentNode = _nodeChildren.Current;

              if (_currentNode.NodeType == XPathNodeType.Element)
                _davProperties.Add(new DavProperty(_currentNode));
            }
          }
        }

        return _davProperties;
      }
    }

    #region Internal / Private Helper Methods
      /// <summary>
      /// Load the available property names
      /// </summary>
      /// <returns></returns>
      internal int LoadNodePropertyNames()
      {
        using (Stream _responseStream = new MemoryStream())
        {
          XmlTextWriter _xmlWriter = new XmlTextWriter(_responseStream, new UTF8Encoding(false));

          _xmlWriter.Formatting = Formatting.Indented;
          _xmlWriter.IndentChar = '\t';
          _xmlWriter.Indentation = 1;
          _xmlWriter.WriteStartDocument();

          //Set the Multistatus
          _xmlWriter.WriteStartElement("D", "multistatus", "DAV:");

          //Append the folders
          foreach (DavFolder _davFolder in CollectionResources)
            AppendResourceNodeProperties(_davFolder, _xmlWriter);

          //Append the files
          foreach (DavFile _davFile in FileResources)
            AppendResourceNodeProperties(_davFile, _xmlWriter);

          _xmlWriter.WriteEndElement();
          _xmlWriter.WriteEndDocument();
          _xmlWriter.Flush();

          base.SetResponseXml(_responseStream);
          _xmlWriter.Close();
        }
        return (int)ServerResponseCode.MultiStatus;
      }


      /// <summary>
      /// Append a resource's properties to the xml text writer
      /// </summary>
      /// <param name="davResource"></param>
      /// <param name="xmlWriter"></param>
      private static void AppendResourceNodeProperties(DavResourceBase davResource, XmlTextWriter xmlWriter)
      {
        //Open the response element
        xmlWriter.WriteStartElement("response", "DAV:");

        //Load the valid items HTTP/1.1 200 OK
        xmlWriter.WriteElementString("href", "DAV:", davResource.ResourcePath);

        //Open the propstat element section
        xmlWriter.WriteStartElement("propstat", "DAV:");

        //Open the prop element section
        xmlWriter.WriteStartElement("prop", "DAV:");

        //Load the valid properties
        foreach (DavProperty _davProperty in davResource.ResourceProperties)
          _davProperty.ToXML(xmlWriter);

        //Close the prop element section
        xmlWriter.WriteEndElement();

        //Write the status record
        xmlWriter.WriteElementString("status", "DAV:", InternalFunctions.GetEnumHttpResponse(ServerResponseCode.Ok));

        //Close the propstat element section
        xmlWriter.WriteEndElement();
        //END Load the valid items HTTP/1.1 200 OK

        //Close the response element
        xmlWriter.WriteEndElement();
      }



      /// <summary>
      /// Load the requested properties
      /// </summary>
      /// <param name="properties">Requested properties or null for all properties</param>
      /// <returns></returns>
      internal int LoadNodes(DavPropertyCollection properties)
      {
        using (Stream _responseStream = new MemoryStream())
        {
          XmlTextWriter _xmlWriter = new XmlTextWriter(_responseStream, new UTF8Encoding(false));

          _xmlWriter.Formatting = Formatting.Indented;
          _xmlWriter.IndentChar = '\t';
          _xmlWriter.Indentation = 1;
          _xmlWriter.WriteStartDocument();

          //Set the Multistatus
          _xmlWriter.WriteStartElement("D", "multistatus", "DAV:");

          //Append the folders
          foreach (DavFolder _davFolder in CollectionResources)
            AppendResourceNode(_davFolder, properties, _xmlWriter);

          //Append the files
          foreach (DavFile _davFile in FileResources)
            AppendResourceNode(_davFile, properties, _xmlWriter);

          //Append the files
          foreach (DavResourceBase _missingResource in MissingResources)
            AppendResourceNode(_missingResource, properties, _xmlWriter);

          _xmlWriter.WriteEndElement();
          _xmlWriter.WriteEndDocument();
          _xmlWriter.Flush();

          base.SetResponseXml(_responseStream);
          _xmlWriter.Close();
        }
        return (int)ServerResponseCode.MultiStatus;
      }


      /// <summary>
      /// Append a resource to the xml text writer
      /// </summary>
      /// <param name="davResource"></param>
      /// <param name="requestedProperties"></param>
      /// <param name="xmlWriter"></param>
      private static void AppendResourceNode(DavResourceBase davResource, DavPropertyCollection requestedProperties, XmlTextWriter xmlWriter)
      {
        DavPropertyCollection _validProperties = new DavPropertyCollection();
        DavPropertyCollection _invalidProperties = new DavPropertyCollection();

        davResource.GetPropertyValues(requestedProperties, _validProperties, _invalidProperties);

        //Open the response element
        xmlWriter.WriteStartElement("response", "DAV:");

        //Load the valid items HTTP/1.1 200 OK
        xmlWriter.WriteElementString("href", "DAV:", davResource.ResourcePath);

        //Open the propstat element section
        xmlWriter.WriteStartElement("propstat", "DAV:");

        //Open the prop element section
        xmlWriter.WriteStartElement("prop", "DAV:");

        //Load the valid properties
        foreach (DavProperty _davProperty in _validProperties)
          _davProperty.ToXML(xmlWriter);

        //Close the prop element section
        xmlWriter.WriteEndElement();

        //Write the status record
        xmlWriter.WriteElementString("status", "DAV:", InternalFunctions.GetEnumHttpResponse(ServerResponseCode.Ok));

        //Close the propstat element section
        xmlWriter.WriteEndElement();
        //END Load the valid items HTTP/1.1 200 OK

        //Load the invalid items HTTP/1.1 404 Not Found
        if (_invalidProperties.Count > 0)
        {
          xmlWriter.WriteStartElement("propstat", "DAV:");
          xmlWriter.WriteElementString("status", "DAV:", InternalFunctions.GetEnumHttpResponse(ServerResponseCode.NotFound));

          //Open the prop element section
          xmlWriter.WriteStartElement("prop", "DAV:");

          //Load all the invalid properties
          foreach (DavProperty _davProperty in _invalidProperties)
            _davProperty.ToXML(xmlWriter);

          //Close the prop element section
          xmlWriter.WriteEndElement();
          //Close the propstat element section
          xmlWriter.WriteEndElement();
        }
        //END Load the invalid items HTTP/1.1 404 Not Found

        //Close the response element
        xmlWriter.WriteEndElement();
      }

      /// <summary>
      /// Append a resource to the xml text writer
      /// </summary>
      /// <param name="davResource"></param>
      /// <param name="requestedProperties"></param>
      /// <param name="xmlWriter"></param>
      private static void AppendInvalidResourceNode(DavResourceBase davResource, DavPropertyCollection requestedProperties, XmlTextWriter xmlWriter)
      {
        DavPropertyCollection _validProperties = new DavPropertyCollection();
        DavPropertyCollection _invalidProperties = new DavPropertyCollection();

        davResource.GetPropertyValues(requestedProperties, _validProperties, _invalidProperties);

        //Open the response element
        xmlWriter.WriteStartElement("response", "DAV:");

        //Load the valid items HTTP/1.1 200 OK
        xmlWriter.WriteElementString("href", "DAV:", davResource.ResourcePath);

        //Open the propstat element section
        xmlWriter.WriteStartElement("propstat", "DAV:");

        //Open the prop element section
        xmlWriter.WriteStartElement("prop", "DAV:");

        //Load the valid properties
        foreach (DavProperty _davProperty in _validProperties)
          _davProperty.ToXML(xmlWriter);

        //Close the prop element section
        xmlWriter.WriteEndElement();

        //Write the status record
        xmlWriter.WriteElementString("status", "DAV:", InternalFunctions.GetEnumHttpResponse(ServerResponseCode.Ok));

        //Close the propstat element section
        xmlWriter.WriteEndElement();
        //END Load the valid items HTTP/1.1 200 OK

        //Load the invalid items HTTP/1.1 404 Not Found
        if (_invalidProperties.Count > 0)
        {
          xmlWriter.WriteStartElement("propstat", "DAV:");

          //Open the prop element section
          xmlWriter.WriteStartElement("prop", "DAV:");

          //Load all the invalid properties
          foreach (DavProperty _davProperty in _invalidProperties)
            _davProperty.ToXML(xmlWriter);

          //Close the prop element section
          xmlWriter.WriteEndElement();

          //Write the status record
          xmlWriter.WriteElementString("status", "DAV:", InternalFunctions.GetEnumHttpResponse(ServerResponseCode.NotFound));

          //Close the propstat element section
          xmlWriter.WriteEndElement();
        }
        //END Load the invalid items HTTP/1.1 404 Not Found

        //Close the response element
        xmlWriter.WriteEndElement();
      }

    #endregion
  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.