Click or drag to resize

DavEngineAsync Class

IT Hit WebDAV Classes Reference
Represents WebDAV engine. Processes WebDAV request and generates WebDAV response.
Inheritance Hierarchy
SystemObject
  ITHit.ServerEngineAsyncIHierarchyItem
    ITHit.WebDAV.ServerDavEngineAsync

Namespace:  ITHit.WebDAV.Server
Assembly:  ITHit.WebDAV.Server (in ITHit.WebDAV.Server.dll) Version: 13.3.13068
Syntax
public class DavEngineAsync : EngineAsync<IHierarchyItem>

The DavEngineAsync type exposes the following members.

Constructors
  NameDescription
Public methodDavEngineAsync
Initializes a new instance of this class.
Top
Properties
  NameDescription
Public propertyAutoPutUnderVersionControl
Determines if placing file under version control is automatic.
Public propertyCalculateContentLength
Indicates if response content length is calculated. Default is true.
(Inherited from EngineAsyncTHierarchyItemAsync.)
Public propertyContentEncoding
Gets or sets the HTTP character set of the output stream. Default is UTF-8.
(Inherited from EngineAsyncTHierarchyItemAsync.)
Public propertyCorsAllowedFor
Enables or disables CORS.
(Inherited from EngineAsyncTHierarchyItemAsync.)
Public propertyCode exampleLicense
Gets or sets the license text.
(Inherited from EngineAsyncTHierarchyItemAsync.)
Public propertyLogger
ILogger instance which engine will use for logging.
(Inherited from EngineAsyncTHierarchyItemAsync.)
Public propertyOutputXmlFormatting
Specifies whether XML written to the output will be formatted. Default is false.
(Inherited from EngineAsyncTHierarchyItemAsync.)
Public propertyUseFullUris
Specifies whether engine shall use full or relative urls. Default is true.
(Inherited from EngineAsyncTHierarchyItemAsync.)
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodCode exampleRegisterMethodHandler
Registers custom method handler.
(Overrides EngineAsyncTHierarchyItemAsyncRegisterMethodHandler(String, IMethodHandlerTHierarchyItemAsync).)
Public methodRegisterOptionsHandler
Registers custom options handler.
Public methodRegisterPropertyHandler
Registers custom property handler.
Public methodRegisterReportHandler
Registers custom report handler.
Public methodCode exampleRunAsync
Processes WebDAV request and generates WebDAV response.
(Overrides EngineAsyncTHierarchyItemAsyncRunAsync(ContextAsyncTHierarchyItemAsync).)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks

The engine parses XML send by WebDAV client, processes requests making calls to your implementations of WebDAV interfaces (IHierarchyItem, IFolder, IFile, etc and generates XML response.

In each HTTP request, you will create a separate instance of your class derived from ContextAsyncTHierarchyItem class and pass it to the RunAsync(ContextAsyncIHierarchyItem) method. Via the context, the engine receives all necessary information about the hosting environment.

You must set License property before you can use the engine.

All updates invoked within one request execution shall be inside a single transaction. Transaction can be committed or rollbacked in BeforeResponseAsync method, which is called right before starting sending a response to the client. After this method is called, no methods of interfaces which update state will be called by the engine. However, methods which read state can be called.

Examples

The code below is part of 'WebDAVServer.FileSystemStorage.AspNet' C# & VB samples provided with the SDK.

public class DavHandler : HttpTaskAsyncHandler
{
    private readonly string license = File.ReadAllText(HttpContext.Current.Request.PhysicalApplicationPath + "License.lic");
    private static readonly string googleServiceAccountID = ConfigurationManager.AppSettings["GoogleServiceAccountID"];

    private static readonly string googleServicePrivateKey = ConfigurationManager.AppSettings["GoogleServicePrivateKey"];

    private static readonly string googleNotificationsRelativeUrl = ConfigurationManager.AppSettings["GoogleNotificationsRelativeUrl"];

    private readonly string gSuiteLicense = File.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "GSuiteLicense.lic") ? File.ReadAllText(HttpContext.Current.Request.PhysicalApplicationPath + "GSuiteLicense.lic") : string.Empty;

    private static readonly bool debugLoggingEnabled =
        "true".Equals(
            ConfigurationManager.AppSettings["DebugLoggingEnabled"],
            StringComparison.InvariantCultureIgnoreCase);

    public override bool IsReusable
    {
        get { return true; }
    }


    public override async Task ProcessRequestAsync(HttpContext context)
    {
        DavEngineAsync webDavEngine = getOrInitializeWebDavEngine(context);

        context.Response.BufferOutput = false;
        DavContext ntfsDavContext = new DavContext(context);
        GSuiteEngineAsync gSuiteEngine = getOrInitializeGSuiteEngine(context);
        await webDavEngine.RunAsync(ntfsDavContext);
        if (gSuiteEngine != null)
        {
            await gSuiteEngine.RunAsync(ContextConverter.ConvertToGSuiteContext(ntfsDavContext));
        }
    }

    private DavEngineAsync initializeWebDavEngine(HttpContext context)
    {

        ILogger logger = WebDAVServer.FileSystemStorage.AspNet.Logger.Instance;
        DavEngineAsync webDavEngine = new DavEngineAsync
        {
            Logger = logger

            // Use idented responses if debug logging is enabled.
            , OutputXmlFormatting = true
        };

        webDavEngine.License = license;
        string contentRootPath = HttpContext.Current.Request.MapPath("/");

        // Set custom handler to process GET and HEAD requests to folders and display 
        // info about how to connect to server. We are using the same custom handler 
        // class (but different instances) here to process both GET and HEAD because 
        // these requests are very similar. Some WebDAV clients may fail to connect if HEAD 
        // request is not processed.
        MyCustomGetHandler handlerGet  = new MyCustomGetHandler(contentRootPath);
        MyCustomGetHandler handlerHead = new MyCustomGetHandler(contentRootPath);
        handlerGet.OriginalHandler  = webDavEngine.RegisterMethodHandler("GET",  handlerGet);
        handlerHead.OriginalHandler = webDavEngine.RegisterMethodHandler("HEAD", handlerHead);

        return webDavEngine;
    }

    private DavEngineAsync getOrInitializeWebDavEngine(HttpContext context)
    {
        //we don't use any double check lock pattern here because nothing wrong
        //is going to happen if we created occasionally several engines.
        const string ENGINE_KEY = "$DavEngine$";
        if (context.Application[ENGINE_KEY] == null)
        {
            context.Application[ENGINE_KEY] = initializeWebDavEngine(context);
        }

        return (DavEngineAsync)context.Application[ENGINE_KEY];
    }
    private GSuiteEngineAsync getOrInitializeGSuiteEngine(HttpContext context)
    {
        //we don't use any double check lock pattern here because nothing wrong
        //is going to happen if we created occasionally several engines.
        const string ENGINE_KEY = "$GSuiteEngine$";
        if (string.IsNullOrEmpty(googleServiceAccountID) || string.IsNullOrEmpty(googleServicePrivateKey))
        {
            return null;
        }

        if (context.Application[ENGINE_KEY] == null)
        {
            var gSuiteEngine = new GSuiteEngineAsync(googleServiceAccountID, googleServicePrivateKey, googleNotificationsRelativeUrl)
            {
                License = gSuiteLicense, 
                Logger = WebDAVServer.FileSystemStorage.AspNet.Logger.Instance
            };

            context.Application[ENGINE_KEY] = gSuiteEngine;
        }

        return (GSuiteEngineAsync)context.Application[ENGINE_KEY];
    }
}
Examples

The code below is part of 'CalDAVServer.SqlStorage.AspNet' C# & VB samples provided with the SDK.

public class DavHandler : HttpTaskAsyncHandler
{
    private readonly string license = File.ReadAllText(HttpContext.Current.Request.PhysicalApplicationPath + "License.lic");

    private static readonly bool debugLoggingEnabled =
        "true".Equals(
            ConfigurationManager.AppSettings["DebugLoggingEnabled"],
            StringComparison.InvariantCultureIgnoreCase);

    public override bool IsReusable
    {
        get { return true; }
    }


    public override async Task ProcessRequestAsync(HttpContext context)
    {
        DavEngineAsync webDavEngine = getOrInitializeWebDavEngine(context);

        context.Response.BufferOutput = false;

        using (var sqlDavContext = new DavContext(context))
        {
            await webDavEngine.RunAsync(sqlDavContext);
        }
    }

    private DavEngineAsync initializeWebDavEngine(HttpContext context)
    {

        ILogger logger = CalDAVServer.SqlStorage.AspNet.Logger.Instance;
        logger.LogFlags = LogFlagsEnum.LogGetResponseBody | LogFlagsEnum.LogPutRequestBody;
        DavEngineAsync webDavEngine = new DavEngineAsync
        {
            Logger = logger

            // Use idented responses if debug logging is enabled.
            , OutputXmlFormatting = true

            // Do not emit CORS headers, typically not required in case of CalDAV/CardDAV server.
            , CorsAllowedFor = null

            // Use full URIs only in case of a regular WebDAV server. Microsoft Mini-redirector does not support URIs without domain.
            , UseFullUris = false
        };

        webDavEngine.License = license;
        string contentRootPath = HttpContext.Current.Request.MapPath("/");

        // Set custom handler to process GET and HEAD requests to folders and display 
        // info about how to connect to server. We are using the same custom handler 
        // class (but different instances) here to process both GET and HEAD because 
        // these requests are very similar. Some WebDAV clients may fail to connect if HEAD 
        // request is not processed.
        MyCustomGetHandler handlerGet  = new MyCustomGetHandler(contentRootPath);
        MyCustomGetHandler handlerHead = new MyCustomGetHandler(contentRootPath);
        handlerGet.OriginalHandler  = webDavEngine.RegisterMethodHandler("GET",  handlerGet);
        handlerHead.OriginalHandler = webDavEngine.RegisterMethodHandler("HEAD", handlerHead);

        // Set your iCalendar & vCard library license before calling any members.
        // iCalendar & vCard library accepts:
        // - WebDAV Server Engine license with iCalendar & vCard modules. Verify your license file to see if these modules are specified.
        // - or iCalendar and vCard Library license.
        ITHit.Collab.LicenseValidator.SetLicense(license);

        return webDavEngine;
    }

    private DavEngineAsync getOrInitializeWebDavEngine(HttpContext context)
    {
        //we don't use any double check lock pattern here because nothing wrong
        //is going to happen if we created occasionally several engines.
        const string ENGINE_KEY = "$DavEngine$";
        if (context.Application[ENGINE_KEY] == null)
        {
            context.Application[ENGINE_KEY] = initializeWebDavEngine(context);
        }

        return (DavEngineAsync)context.Application[ENGINE_KEY];
    }
}
Examples

The code below is part of 'CardDAVServer.SqlStorage.AspNet' C# & VB samples provided with the SDK.

public class DavHandler : HttpTaskAsyncHandler
{
    private readonly string license = File.ReadAllText(HttpContext.Current.Request.PhysicalApplicationPath + "License.lic");

    private static readonly bool debugLoggingEnabled =
        "true".Equals(
            ConfigurationManager.AppSettings["DebugLoggingEnabled"],
            StringComparison.InvariantCultureIgnoreCase);

    public override bool IsReusable
    {
        get { return true; }
    }


    public override async Task ProcessRequestAsync(HttpContext context)
    {
        DavEngineAsync webDavEngine = getOrInitializeWebDavEngine(context);

        context.Response.BufferOutput = false;

        using (var sqlDavContext = new DavContext(context))
        {
            await webDavEngine.RunAsync(sqlDavContext);
        }
    }

    private DavEngineAsync initializeWebDavEngine(HttpContext context)
    {

        ILogger logger = CardDAVServer.SqlStorage.AspNet.Logger.Instance;
        logger.LogFlags = LogFlagsEnum.LogGetResponseBody | LogFlagsEnum.LogPutRequestBody;
        DavEngineAsync webDavEngine = new DavEngineAsync
        {
            Logger = logger

            // Use idented responses if debug logging is enabled.
            , OutputXmlFormatting = true

            // Do not emit CORS headers, typically not required in case of CalDAV/CardDAV server.
            , CorsAllowedFor = null

            // Use full URIs only in case of a regular WebDAV server. Microsoft Mini-redirector does not support URIs without domain.
            , UseFullUris = false
        };

        webDavEngine.License = license;
        string contentRootPath = HttpContext.Current.Request.MapPath("/");

        // Set custom handler to process GET and HEAD requests to folders and display 
        // info about how to connect to server. We are using the same custom handler 
        // class (but different instances) here to process both GET and HEAD because 
        // these requests are very similar. Some WebDAV clients may fail to connect if HEAD 
        // request is not processed.
        MyCustomGetHandler handlerGet  = new MyCustomGetHandler(contentRootPath);
        MyCustomGetHandler handlerHead = new MyCustomGetHandler(contentRootPath);
        handlerGet.OriginalHandler  = webDavEngine.RegisterMethodHandler("GET",  handlerGet);
        handlerHead.OriginalHandler = webDavEngine.RegisterMethodHandler("HEAD", handlerHead);

        // Set your iCalendar & vCard library license before calling any members.
        // iCalendar & vCard library accepts:
        // - WebDAV Server Engine license with iCalendar & vCard modules. Verify your license file to see if these modules are specified.
        // - or iCalendar and vCard Library license.
        ITHit.Collab.LicenseValidator.SetLicense(license);

        return webDavEngine;
    }

    private DavEngineAsync getOrInitializeWebDavEngine(HttpContext context)
    {
        //we don't use any double check lock pattern here because nothing wrong
        //is going to happen if we created occasionally several engines.
        const string ENGINE_KEY = "$DavEngine$";
        if (context.Application[ENGINE_KEY] == null)
        {
            context.Application[ENGINE_KEY] = initializeWebDavEngine(context);
        }

        return (DavEngineAsync)context.Application[ENGINE_KEY];
    }
}
Thread Safety
Method RunAsync(ContextAsyncTHierarchyItemAsync) is threadsafe. All other members are NOT threadsafe. You can create a single instance of this class, initialize it once and use to serve all requests from different threads.
See Also