Пример #1
0
 private void DeserialiseLock(LocksDS.LocksRow _lockrow)
 {
     this._LockOwner = _lockrow.LockOwner;
     this._LockDepth = (DepthType)_lockrow.LockDepth;
     this._LockOwnerType = (LockOwnerType)_lockrow.LockOwnerType;
     this._LockType = (LockType)_lockrow.LockType;
     this._LockScope = (LockScope)_lockrow.LockScope;
 }
Пример #2
0
        public int Handle()
        {
            this._LockDepth = WebDavHelper.getRequestDepth(this._httpApplication);
                //No support for ResourceChildren LOCKing
                if ( this._LockDepth == DepthType.ResourceChildren)
                    return  (int)DavLockResponseCode.BadRequest;

                this._requestPath = WebDavHelper.getRelativePath(this._httpApplication, this._httpApplication.Request.FilePath);

                //Check to see if this is a lock refresh request.
                if (this._httpApplication.Request.Headers["If"] != null)
                {
                    _LockToken = WebDavHelper.ParseOpaqueLockToken(_httpApplication.Request.Headers["If"]);
                    _LockTimeOut = WebDavHelper.ParseTimeoutHeader(_httpApplication,this._LockTimeOut);

                    //Check to see that the lock exists on the requested resource.

                    LocksDS.LocksRow ltr = WebDavHelper.getFileLockbyToken(_LockToken);
                    if (ltr==null)
                           return (int)DavLockResponseCode.PreconditionFailed;

                    //Check if the lock is expired , include token grace timeout in calculation
                    TimeSpan span = DateTime.Now.Subtract(ltr.update_date_stamp);
                    if (span.TotalSeconds > ltr.Timeout + this._GraceLockTimeOut)
                    {
                        //the Lock has expired so delete it an return as if it did not exist
                        WebDavHelper.RemoveLock(ltr.ID);
                        return (int)DavLockResponseCode.PreconditionFailed;
                    }

                    //the lock exists and is not expired so update the timeout and update_date_stamp
                    ltr.Timeout = _LockTimeOut;
                    WebDavHelper.SaveLock(ltr);

                    //Send timeout header back to client
                    _httpApplication.Response.AppendHeader("Timeout", "Second-" + this._LockTimeOut.ToString());
                    //Deserialise the lock token in the DB to get the rest of the data
                    DeserialiseLock(ltr);
                    BuildResponse();
                    return (int)DavLockResponseCode.Ok;

                }else{
                    //This is not a refresh it is a new LOCK request. So check that it is valid

                    //Check to see if the resource exists

                     Search_FilesDS.FilesRow _fileInfo = WebDavHelper.getFileAttribsOnly(this._requestPath);
                     if (_fileInfo == null)
                         return (int)DavLockResponseCode.BadRequest;

                    //Need to workout how to resolve this problem where office attempts to lock a resource
                    //it knows does not exist.

                     //Check that it is not already locked
                     LocksDS.LocksRow ltr = WebDavHelper.getLock(_fileInfo.ID);
                     if (ltr != null)
                     {
                         //Check if the lock is expired , include token grace timeout in calculation
                         TimeSpan span = DateTime.Now.Subtract(ltr.update_date_stamp);
                         if (span.TotalSeconds > ltr.Timeout + this._GraceLockTimeOut)
                         {
                             //the Lock has expired so delete it an return as if it did not exist
                             WebDavHelper.RemoveLock(ltr.ID);
                         }
                         else
                         {
                             return (int)DavLockResponseCode.Locked;
                         }
                     }
                    //Check that the request XML is valid for the LOCK request
                     if (_requestXmlNavigator == null)
                        return (int)ServerResponseCode.BadRequest;

                    //Load the valid properties
                    XPathNodeIterator _lockInfoNodeIterator =_requestXmlNavigator.SelectDescendants("lockinfo", "DAV:", false);
                    if (!_lockInfoNodeIterator.MoveNext())
                        return (int)ServerResponseCode.BadRequest;

                    //Create a new Lock
                    this._LockToken = System.Guid.NewGuid().ToString("D");
                    this._LockTimeOut = WebDavHelper.ParseTimeoutHeader(_httpApplication,this._LockTimeOut);

                    //Get the lock type
                    _LockType = LockType.Write;
                    XPathNodeIterator _lockTypeNodeIterator = _lockInfoNodeIterator.Current.SelectDescendants("locktype", "DAV:", false);
                    if (_lockTypeNodeIterator.MoveNext())
                        {
                            XPathNavigator _currentNode = _lockTypeNodeIterator.Current;
                            if (_currentNode.MoveToFirstChild())
                            {
                                switch (_currentNode.LocalName.ToLower(CultureInfo.InvariantCulture))
                                {
                                    case "read":
                                        _LockType = LockType.Read;
                                        break;

                                    case "write":
                                        _LockType = LockType.Write;
                                        break;
                                }
                            }
                        }
                    //Get the lock scope
                    _LockScope = LockScope.Exclusive;
                    XPathNodeIterator _lockScopeNodeIterator = _lockInfoNodeIterator.Current.SelectDescendants("lockscope", "DAV:", false);
                    if (_lockScopeNodeIterator.MoveNext())
                    {
                        XPathNavigator _currentNode = _lockScopeNodeIterator.Current;
                        if (_currentNode.MoveToFirstChild())
                        {
                            switch (_currentNode.LocalName.ToLower(CultureInfo.InvariantCulture))
                            {
                                case "shared":
                                    _LockScope = LockScope.Shared;
                                    break;

                                case "exclusive":
                                    _LockScope = LockScope.Exclusive;
                                    break;
                            }
                        }
                    }

                    //Get the lock owner
                    _LockOwnerType = LockOwnerType.User;
                    XPathNodeIterator _lockOwnerNodeIterator = _lockInfoNodeIterator.Current.SelectDescendants("owner", "DAV:", false);
                    if (_lockOwnerNodeIterator.MoveNext())
                    {
                        XPathNavigator _currentNode = _lockOwnerNodeIterator.Current;

                        if (_currentNode.NodeType == XPathNodeType.Text)
                        {
                            _LockOwnerType = LockOwnerType.User;
                        }
                        else
                        {
                            if (_currentNode.MoveToFirstChild())
                            {
                                //TODO: Expand this to other LockOwnerTypes

                                switch (_currentNode.LocalName.ToLower(CultureInfo.InvariantCulture))
                                {
                                    case "href":
                                        _LockOwnerType = LockOwnerType.Href;
                                    break;
                                }
                            }
                        }

                        _LockOwner = _currentNode.Value;
                    }
                    //Now save the Lock to the DB;
                    saveLock(_fileInfo.ID);
                    BuildResponse();

                }

            return (int)DavLockResponseCode.Ok;
        }