/// <summary> /// Opens a connection to a data source <see cref="Uri"/> /// </summary> /// <param name="credentials"> /// The <see cref="Credentials"/> that are used to connect to the data source such as username, password and <see cref="Uri"/> /// </param> /// <param name="cancellationToken"> /// The <see cref="CancellationToken"/> /// </param> /// <returns> /// The <see cref="IEnumerable{T}"/> of returned <see cref="CDP4Common.DTO.Thing"/> DTOs. /// </returns> public override async Task <IEnumerable <Thing> > Open(Credentials credentials, CancellationToken cancellationToken) { // make sure the uri is not null if (credentials == null) { throw new ArgumentNullException(nameof(this.Credentials.Uri), $"The Credentials URI may not be null"); } // make sure that the uri is of the correct format UriExtensions.AssertUriIsFileSchema(credentials.Uri); var filePath = credentials.Uri.LocalPath; if (!System.IO.File.Exists(filePath)) { throw new FileLoadException($"The specified filepath does not exist or you do not have access to it: {filePath}"); } try { var returned = this.ReadSiteDirectoryJson(filePath, credentials).ToList(); Logger.Debug("The SiteDirectory contains {0} Things", returned.Count); // check for credentials in the returned DTO's to see if the current Person is authorised to look into this SiteDirectory var person = returned.SingleOrDefault(p => p.ClassKind == ClassKind.Person && ((CDP4Common.DTO.Person)p).ShortName == credentials.UserName) as CDP4Common.DTO.Person; if (person == null) { var msg = $"{credentials.UserName} is unauthorized"; Logger.Error(msg); throw new UnauthorizedAccessException(msg); } // set the credentials this.Credentials = credentials; return(returned); } catch (UnauthorizedAccessException ex) { Logger.Error(ex); this.CloseSession(); throw; } catch (Exception ex) { this.CloseSession(); var msg = $"Failed to load file. Error: {ex.Message}"; Logger.Error(msg); throw new FileLoadException(msg, ex); } }
public void AssertThatNonFileSchemasThrow() { Assert.Throws <ArgumentException>(() => UriExtensions.AssertUriIsFileSchema(new Uri("http://somehost.com"))); Assert.Throws <ArgumentException>(() => UriExtensions.AssertUriIsFileSchema(new Uri("ftp://somehost.com"))); }
public void AssertThatUriSchemaAssertionWorksForFile() { UriExtensions.AssertUriIsFileSchema(new Uri("file://localhost/etc/fstab")); UriExtensions.AssertUriIsFileSchema(new Uri("file:///c:/WINDOWS/clock.avi")); }
/// <summary> /// Reads the data related to the provided <see cref="CDP4Common.DTO.Thing"/> from the data-source /// </summary> /// <typeparam name="T"> /// an type of <see cref="CDP4Common.DTO.Thing"/> /// </typeparam> /// <param name="thing"> /// An instance of <see cref="CDP4Common.DTO.Thing"/> that needs to be read from the data-source /// </param> /// <param name="cancellationToken"> /// The cancellation Token. /// </param> /// <param name="attributes"> /// An instance of <see cref="IQueryAttributes"/> to be passed along with the uri /// </param> /// <returns> /// a list of <see cref="CDP4Common.DTO.Thing"/> that are contained by the provided <see cref="CDP4Common.DTO.Thing"/> including the <see cref="CDP4Common.DTO.Thing"/> itself /// </returns> /// <exception cref="NotSupportedException"> /// Throws a <see cref="NotSupportedException"/> if the supplied T thing is not an <see cref="CDP4Common.DTO.Iteration"/>. /// </exception> public override async Task <IEnumerable <Thing> > Read <T>(T thing, CancellationToken cancellationToken, IQueryAttributes attributes = null) { // only read Iterations, domains or site reference data libraries in a file Dal if (!(thing is CDP4Common.DTO.Iteration) && !(thing is CDP4Common.DTO.SiteReferenceDataLibrary) && !(thing is CDP4Common.DTO.DomainOfExpertise)) { throw new NotSupportedException("The JSONFileDal only supports Read on Iteration, SiteReferenceDataLibrary and DomainOfExpertise instances."); } if (this.Credentials.Uri == null) { throw new ArgumentNullException(nameof(this.Credentials.Uri), $"The Credentials URI may not be null"); } // make sure that the uri is of the correct format UriExtensions.AssertUriIsFileSchema(this.Credentials.Uri); var filePath = this.Credentials.Uri.LocalPath; if (!System.IO.File.Exists(filePath)) { throw new FileNotFoundException($"The specified filepath does not exist or you do not have access to it: {filePath}"); } try { // re-read the to extract the reference data libraries that have not yet been fully dereferenced // and that are part of the required RDL's var siteDirectoryData = this.ReadSiteDirectoryJson(filePath, this.Credentials).ToList(); // read file, SiteDirectory first. using (var zip = ZipFile.Read(filePath)) { // get all relevant info from the selected iteration var siteDir = this.Session.RetrieveSiteDirectory(); var returned = new List <Thing>(); switch (thing.ClassKind) { case ClassKind.Iteration: returned = this.RetrieveIterationThings(thing as CDP4Common.DTO.Iteration, siteDirectoryData, zip, siteDir); break; case ClassKind.SiteReferenceDataLibrary: returned = this.RetrieveSRDLThings(thing as CDP4Common.DTO.SiteReferenceDataLibrary, siteDirectoryData, zip, siteDir); break; case ClassKind.DomainOfExpertise: returned = this.RetrieveDomainOfExpertiseThings(thing as CDP4Common.DTO.DomainOfExpertise, siteDirectoryData, siteDir); break; } return(returned); } } catch (Exception ex) { var msg = $"Failed to load file. Error: {ex.Message}"; Logger.Error(msg); if (this.Credentials != null) { this.Close(); } throw new FileLoadException(msg); } }