Пример #1
0
 public void AssertThatNonHttpAndHttpsSchemasThrow()
 {
     Assert.Throws <ArgumentException>(() => UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("file://somehost.com")));
     Assert.Throws <ArgumentException>(() => UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("ftp://somehost.com")));
 }
        /// <summary>
        /// Opens a connection to a data source <see cref="Uri"/> speci1fied by the provided <see cref="Credentials"/>
        /// </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 cancellation Token.
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable{T}"/> of returned <see cref="Thing"/> DTOs.
        /// </returns>
        public override async Task <IEnumerable <Thing> > Open(Credentials credentials, CancellationToken cancellationToken)
        {
            if (credentials == null)
            {
                throw new ArgumentNullException(nameof(credentials), $"The {nameof(credentials)} may not be null");
            }

            if (credentials.Uri == null)
            {
                throw new ArgumentNullException(nameof(credentials.Uri), $"The Credentials URI may not be null");
            }

            UriExtensions.AssertUriIsHttpOrHttpsSchema(credentials.Uri);

            var queryAttributes = new QueryAttributes
            {
                Extent = ExtentQueryAttribute.deep,
                IncludeReferenceData = false
            };

            var resourcePath = $"SiteDirectory{queryAttributes}";

            var openToken = CDP4Common.Helpers.TokenGenerator.GenerateRandomToken();

            this.httpClient = this.CreateHttpClient(credentials);

            var watch = Stopwatch.StartNew();

            var uriBuilder = this.GetUriBuilder(credentials.Uri, ref resourcePath);

            Logger.Debug("Resource Path {0}: {1}", openToken, resourcePath);
            Logger.Debug("WSP Open {0}: {1}", openToken, uriBuilder);

            var requestsw = Stopwatch.StartNew();

            using (var httpResponseMessage = await this.httpClient.GetAsync(resourcePath, HttpCompletionOption.ResponseHeadersRead, cancellationToken: cancellationToken))
            {
                Logger.Info("The ECSS-E-TM-10-25A Annex C Services responded in {0} [ms] to Open {1}", requestsw.ElapsedMilliseconds, openToken);
                requestsw.Stop();

                if (httpResponseMessage.StatusCode != HttpStatusCode.OK)
                {
                    var msg = $"The data-source replied with code {httpResponseMessage.StatusCode}: {httpResponseMessage.ReasonPhrase}";
                    Logger.Error(msg);
                    throw new DalReadException(msg);
                }

                watch.Stop();
                Logger.Info("WSP DAL Open {0} completed in {1} [ms]", openToken, watch.ElapsedMilliseconds);

                watch = Stopwatch.StartNew();

                using (var resultStream = await httpResponseMessage.Content.ReadAsStreamAsync())
                {
                    var returned = this.Serializer.Deserialize(resultStream);

                    watch.Stop();
                    Logger.Info("JSON Deserializer completed in {0} [ms]", watch.ElapsedMilliseconds);

                    var returnedPerson = returned.OfType <CDP4Common.DTO.Person>().SingleOrDefault(x => x.ShortName == credentials.UserName);
                    if (returnedPerson == null)
                    {
                        throw new InvalidOperationException("User not found.");
                    }

                    this.Credentials = credentials;

                    return(returned);
                }
            }
        }
Пример #3
0
 public void AssertThatUriSchemaAssertionWorksForHttpAndHttps()
 {
     UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("http://somehost.com:23/"));
     UriExtensions.AssertUriIsHttpOrHttpsSchema(new Uri("https://someotherhost.com"));
 }