Example #1
0
        public HttpStatusCode PerformCopyWebRequest(String url, String urlNewTarget, ICredentials credentials)
        {
            // build the service
            DavService svc = new DavService();

            // set the error code 
            int code;
            WebException e;

            // perform the request
            svc.PerformSimpleWebCall(url, WebRequestMethodsEx.WebDAV.Copy, credentials, urlNewTarget, out code, out e);

            // return the code
            return (HttpStatusCode)code;
        }
Example #2
0
        public HttpStatusCode PerformMoveWebRequest(String url, String urlNewTarget, ICredentials credentials)
        {
            // build the service
            DavService svc = new DavService();

            // set the error code
            int          code;
            WebException e;

            // perform the request
            svc.PerformSimpleWebCall(url, WebRequestMethodsEx.WebDAV.Move, credentials, urlNewTarget, out code, out e);

            // return the code
            return((HttpStatusCode)code);
        }
        /// <summary>
        /// This method generates a session to a webdav share via username and password
        /// </summary>
        /// <param name="token"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public override IStorageProviderSession CreateSession(ICloudStorageAccessToken token, ICloudStorageConfiguration configuration)
        {
            // cast the creds to the right type            
            WebDavConfiguration config = configuration as WebDavConfiguration;

            // build service
            DavService svc = new DavService();

            // check if url available                        
            int status = (int) HttpStatusCode.OK;
            WebException e = null;
            svc.PerformSimpleWebCall(config.ServiceLocator.ToString(), WebRequestMethodsEx.WebDAV.Options, (token as ICredentials).GetCredential(null, null), null, out status, out e);
            if (status == (int) HttpStatusCode.Unauthorized)
                throw new UnauthorizedAccessException();
            else if (HttpUtilityEx.IsSuccessCode(status))
                return new WebDavStorageProviderSession(token, config, this);
            else
                return null;
        }
        private BaseFileEntry RequestResourceFromWebDavShare(IStorageProviderSession session, String resourceUrl, out List<BaseFileEntry> childs)
        {
            // get the credebtials
            ICredentials creds = session.SessionToken as ICredentials;

            // build the dav service
            DavService svc = new DavService();

            // the result
            WebDavRequestResult RequestResult;

            try
            {
                // create the web request
                WebRequest request = svc.CreateWebRequestPROPFIND(resourceUrl, creds.GetCredential(null, null));

                // get the response
                using (HttpWebResponse response = svc.GetWebResponse(request) as HttpWebResponse)
                {
                    if (response.StatusCode == HttpStatusCode.Moved)
                    {
                        // get the new uri
                        String newUri = response.Headers["Location"];

                        // close the response
                        response.Close();

                        // redo it
                        return RequestResourceFromWebDavShare(session, newUri, out childs);
                    }
                    else
                    {
                        // get the response stream
                        using (Stream data = svc.GetResponseStream(response))
                        {
                            if (data == null)
                            {
                                childs = null;
                                return null;
                            }

                            // build the file entries
                            RequestResult = WebDavRequestParser.CreateObjectsFromNetworkStream(data, resourceUrl, this, session, WebDavNameBaseFilterCallback);

                            // close the stream
                            data.Close();
                        }

                        // close the response
                        response.Close();
                    }
                }

                // get the request fileentry and fill the childs
                if (RequestResult.Self == null)
                {
                    childs = null;
                    return null;
                }

                // set the childs
                childs = RequestResult.Childs;

                // go ahead
                return RequestResult.Self;
            }
            catch (WebException)
            {
                childs = null;
                return null;
            }
        }
        private bool CopyResource(IStorageProviderSession session, ICloudFileSystemEntry fsentry, String newTargetUrl)
        {
            var config = session.ServiceConfiguration as WebDavConfiguration;
            var creds = session.SessionToken as ICredentials;

            var uriString = PathHelper.Combine(config.ServiceLocator.ToString(), GenericHelper.GetResourcePath(fsentry));

            var uri = new Uri(uriString);
            var uriTarget = new Uri(newTargetUrl);

            var errorCode = new DavService().PerformCopyWebRequest(uri.ToString(), uriTarget.ToString(), creds.GetCredential(null, null));

            return errorCode == HttpStatusCode.Created || errorCode == HttpStatusCode.NoContent;
        }
        public override Stream CreateUploadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, long uploadSize)
        {
            // build the url 
            string url = GetResourceUrl(session, fileSystemEntry, null);

            // get the session creds
            ICredentials creds = session.SessionToken as ICredentials;

            // Build the service
            DavService svc = new DavService();

            // get the service config
            WebDavConfiguration conf = (WebDavConfiguration) session.ServiceConfiguration;

            // build the webrequest                        
            WebRequest networkRequest = svc.CreateWebRequestPUT(url, creds.GetCredential(null, null), conf.UploadDataStreambuffered);

            // get the request stream
            WebRequestStream requestStream = svc.GetRequestStream(networkRequest, uploadSize);

            // add disposal opp
            requestStream.PushPostDisposeOperation(CommitUploadStream, svc, networkRequest, fileSystemEntry, requestStream);

            // go ahead
            return requestStream;
        }
        public override Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry)
        {
            // build the url 
            string url = GetResourceUrl(session, fileSystemEntry, null);

            // get the session creds
            ICredentials creds = session.SessionToken as ICredentials;

            // Build the service
            DavService svc = new DavService();

            // create the webrequest
            WebRequest request = svc.CreateWebRequest(url, WebRequestMethodsEx.Http.Get, creds.GetCredential(null, null), false, null);

            // create the response
            WebResponse response = svc.GetWebResponse(request);

            // get the data 
            Stream orgStream = svc.GetResponseStream(response);

            BaseFileEntryDownloadStream dStream = new BaseFileEntryDownloadStream(orgStream, fileSystemEntry);

            // put the disposable on the stack
            dStream._DisposableObjects.Push(response);

            // go ahead
            return dStream;
        }
        /// <summary>
        /// This method removes a specific resource from a webdav share
        /// </summary>
        /// <param name="session"></param>
        /// <param name="entry"></param>
        public override Boolean DeleteResource(IStorageProviderSession session, ICloudFileSystemEntry entry)
        {
            // get the credentials
            var creds = session.SessionToken as ICredentials;

            // build url            
            String uriString = this.GetResourceUrl(session, entry, null);
            var uri = new Uri(uriString);

            // create the service
            var svc = new DavService();

            // create the webrequest            
            int errorCode;
            WebException e;
            svc.PerformSimpleWebCall(uri.ToString(), WebRequestMethodsEx.WebDAV.Delete, creds.GetCredential(null, null), null, out errorCode, out e);
            if (!HttpUtilityEx.IsSuccessCode(errorCode))
                return false;

            // remove from parent 
            var parentDir = entry.Parent as BaseDirectoryEntry;
            if (parentDir != null)
            {
                parentDir.RemoveChildById(entry.Id);
            }

            return true;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="session"></param>
        /// <param name="Name"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public override ICloudFileSystemEntry CreateResource(IStorageProviderSession session, string Name, ICloudDirectoryEntry parent)
        {
            // get the credentials
            ICredentials creds = session.SessionToken as ICredentials;

            // build url
            String uriString = GetResourceUrl(session, parent, null);
            uriString = PathHelper.Combine(uriString, Name);

            Uri uri = new Uri(uriString);

            // build the DavService
            DavService svc = new DavService();

            // create the webrequest            
            int errorCode;
            WebException e;
            svc.PerformSimpleWebCall(uri.ToString(), WebRequestMethodsEx.Http.MkCol, creds.GetCredential(null, null), null, out errorCode, out e);
            if (errorCode != (int) HttpStatusCode.Created)
                return null;
            else
            {
                BaseDirectoryEntry newDir = new BaseDirectoryEntry(Name, 0, DateTime.Now, this, session);

                // init parent child relation
                if (parent != null)
                {
                    BaseDirectoryEntry parentDir = parent as BaseDirectoryEntry;
                    parentDir.AddChild(newDir);
                }

                return newDir;
            }
        }