public override Stream CreateUploadStream(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, long uploadSize) { // build the url var url = GetResourceUrl(session, fileSystemEntry, null); // get the session creds var creds = session.SessionToken as ICredentials; // Build the service var svc = new DavService(); // get the service config var conf = (WebDavConfiguration)session.ServiceConfiguration; // build the webrequest var networkRequest = svc.CreateWebRequestPUT(url, creds.GetCredential(null, null), conf.UploadDataStreambuffered); // get the request stream var 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 var url = GetResourceUrl(session, fileSystemEntry, null); // get the session creds var creds = session.SessionToken as ICredentials; // Build the service var svc = new DavService(); // create the response var response = CreateDownloadResponse(url, creds.GetCredential(null, null)); // get the data var orgStream = svc.GetResponseStream(response); var dStream = new BaseFileEntryDownloadStream(orgStream, fileSystemEntry); // put the disposable on the stack dStream._DisposableObjects.Push(response); // go ahead return(dStream); }
private bool CopyResource(IStorageProviderSession session, ICloudFileSystemEntry fsentry, String newTargetUrl) { // cast to webdav configuration WebDavConfiguration config = session.ServiceConfiguration as WebDavConfiguration; ICredentials creds = session.SessionToken as ICredentials; // build url String uriString = PathHelper.Combine(config.ServiceLocator.ToString(), GenericHelper.GetResourcePath(fsentry)); Uri uri = new Uri(uriString); Uri uriTarget = new Uri(newTargetUrl); // create the service DavService svc = new DavService(); // create the webrequest HttpStatusCode errorCode = svc.PerformCopyWebRequest(uri.ToString(), uriTarget.ToString(), creds.GetCredential(null, null)); if (errorCode != HttpStatusCode.Created) { return(false); } else { return(true); } }
/// <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 var uriString = 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); }
public void CommitUploadStream(params object[] arg) { // convert the args DavService svc = arg[0] as DavService; HttpWebRequest uploadRequest = arg[1] as HttpWebRequest; BaseFileEntry fileSystemEntry = arg[2] as BaseFileEntry; #if !WINDOWS_PHONE && !MONODROID WebRequestStream requestStream = arg[3] as WebRequestStream; // check if all data was written into stream if (requestStream.WrittenBytes != uploadRequest.ContentLength) { // nothing todo request was aborted return; } #endif // perform the request int code; WebException e; svc.PerformWebRequest(uploadRequest, null, out code, out e); // check the ret value if (!HttpUtilityEx.IsSuccessCode(code)) { SharpBoxException.ThrowSharpBoxExceptionBasedOnHttpErrorCode(uploadRequest, (HttpStatusCode)code, e); } // adjust the lengt #if !WINDOWS_PHONE && !MONODROID fileSystemEntry.Length = uploadRequest.ContentLength; #endif }
/// <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); } }
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); }
private static bool MoveResource(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().PerformMoveWebRequest(uri.ToString(), uriTarget.ToString(), creds.GetCredential(null, null)); return(errorCode == HttpStatusCode.Created || errorCode == HttpStatusCode.NoContent); }
private WebResponse CreateDownloadResponse(string url, ICredentials creds) { // Build the service var svc = new DavService(); // create the webrequest var request = svc.CreateWebRequest(url, WebRequestMethodsEx.Http.Get, creds, false, null); try { // create the response var response = svc.GetWebResponse(request); return(response); } catch (WebException e) { if (e.Response is HttpWebResponse) { var code = (e.Response as HttpWebResponse).StatusCode; if (code == HttpStatusCode.Moved) { // get the new uri var newUri = e.Response.Headers["Location"]; // redo it return(CreateDownloadResponse(newUri, creds)); } NetworkCredential networkCredential; if (code == HttpStatusCode.Unauthorized && (networkCredential = creds as NetworkCredential) != null) { var search = new Regex(@"^\w+", RegexOptions.Singleline | RegexOptions.IgnoreCase); // get authentication method var authMethod = search.Match(e.Response.Headers["WWW-Authenticate"]).Value; var newCredentials = new CredentialCache { { new Uri((new Uri(url)).GetLeftPart(UriPartial.Authority)), authMethod, networkCredential } }; // redo it return(CreateDownloadResponse(url, newCredentials)); } } throw; } }
/// <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); } }
private BaseFileEntry RequestResourceFromWebDavShare(IStorageProviderSession session, ICredentials creds, String resourceUrl, out List <BaseFileEntry> childs) { // build the dav service var svc = new DavService(); try { // create the web request var request = svc.CreateWebRequestPROPFIND(resourceUrl, creds); // the result WebDavRequestResult requestResult; // get the response using (var response = svc.GetWebResponse(request) as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.Moved) { // get the new uri var newUri = response.Headers["Location"]; (session.ServiceConfiguration as WebDavConfiguration).ServiceLocator = new Uri(newUri); // close the response response.Close(); // redo it return(RequestResourceFromWebDavShare(session, creds, newUri, out childs)); } // get the response stream using (var 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 e) { if (e.Response is HttpWebResponse) { var code = (e.Response as HttpWebResponse).StatusCode; if (code == HttpStatusCode.Moved) { // get the new uri var newUri = e.Response.Headers["Location"]; (session.ServiceConfiguration as WebDavConfiguration).ServiceLocator = new Uri(newUri); // redo it return(RequestResourceFromWebDavShare(session, creds, newUri, out childs)); } NetworkCredential networkCredential; if (code == HttpStatusCode.Unauthorized && (networkCredential = creds as NetworkCredential) != null) { // get authentication method var headers = e.Response.Headers["WWW-Authenticate"]; if (!string.IsNullOrEmpty(headers)) { var search = new Regex(@"^\w+", RegexOptions.Singleline | RegexOptions.IgnoreCase); var authMethod = search.Match(headers).Value; var newCredentials = new CredentialCache { { new Uri((new Uri(resourceUrl)).GetLeftPart(UriPartial.Authority)), authMethod, networkCredential } }; // redo it return(RequestResourceFromWebDavShare(session, newCredentials, resourceUrl, out childs)); } } } childs = null; 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); } }