public async Task <List <PackageContainerRepoItem> > GenerateRepositoryFromEndpointAsync() { // access if (!IsValid()) { throw new PackageConnectorException("PackageConnector::GenerateRepositoryFromEndpoint() " + "connection not valid!"); } // results var res = new List <PackageContainerRepoItem>(); // Log Log.Singleton.Info($"Building repository items for aas-list from {this.ToString()} .."); // sync-query for the list var aasItems = new List <ListAasItem>(); try { if (OpenIDClient.auth) { var responseAuth = _client.GetAsync("/authserver").Result; if (responseAuth.IsSuccessStatusCode) { var content = responseAuth.Content.ReadAsStringAsync().Result; if (content != null && content != "") { OpenIDClient.authServer = content; var response2 = await OpenIDClient.RequestTokenAsync(null); OpenIDClient.token = response2.AccessToken; OpenIDClient.auth = false; } } } if (OpenIDClient.token != "") { _client.SetBearerToken(OpenIDClient.token); } // query var listAasResponse = await _client.GetAsync( StartQuery("server", "listaas")); listAasResponse.EnsureSuccessStatusCode(); var listAasString = await listAasResponse.Content.ReadAsStringAsync(); // get some structures dynamic listAas = Newtonsoft.Json.Linq.JObject.Parse(listAasString); foreach (var li in listAas.aaslist) { string line = "" + li; var arr = line.Trim().Split(new[] { " : " }, StringSplitOptions.RemoveEmptyEntries); if (arr.Length == 4) { aasItems.Add(new ListAasItem() { Index = arr[0].Trim(), AasIdShort = arr[1].Trim(), AasId = arr[2].Trim(), Fn = arr[3].Trim() }); } } } catch (Exception ex) { Log.Singleton.Error(ex, $"when parsing /server/listaas/ for {this.ToString()}"); } // go thru the list foreach (var aasi in aasItems) { try { // query var x = await GetAasAssetCore(aasi.Index); if (x.Item1 == null || x.Item2 == null) { Log.Singleton.Error($"when retrieving /aas/{aasi.Index}/, some null contents for AAS or" + $"Asset were found."); } // file item var fi = new PackageContainerRepoItem() { ContainerOptions = PackageContainerOptionsBase.CreateDefault(Options.Curr), Location = CombineQuery(_client.BaseAddress.ToString(), _endPointSegments, "server", "getaasx", aasi.Index), Description = $"\"{"" + x.Item1?.idShort}\",\"{"" + x.Item2?.idShort}\"", Tag = "" + AdminShellUtil.ExtractPascalCasingLetters(x.Item1?.idShort).SubstringMax(0, 3) }; fi.AasIds.Add("" + x.Item1?.identification?.id); fi.AssetIds.Add("" + x.Item2?.identification?.id); res.Add(fi); } catch (Exception ex) { Log.Singleton.Error(ex, $"when parsing index {aasi.Index}"); } } // return results return(res); }
private async Task DownloadFromSource(Uri sourceUri, PackCntRuntimeOptions runtimeOptions = null) { // read via HttpClient (uses standard proxies) var handler = new HttpClientHandler(); handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials; handler.AllowAutoRedirect = false; var client = new HttpClient(handler); client.DefaultRequestHeaders.Add("Accept", "application/aas"); client.BaseAddress = new Uri(sourceUri.GetLeftPart(UriPartial.Authority)); var requestPath = sourceUri.PathAndQuery; // Log runtimeOptions?.Log?.Info($"HttpClient() with base-address {client.BaseAddress} " + $"and request {requestPath} .. "); if (OpenIDClient.token != "") { client.SetBearerToken(OpenIDClient.token); } bool repeat = true; while (repeat) { // get response? var response = await client.GetAsync(requestPath, HttpCompletionOption.ResponseHeadersRead); if (response.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect) { string redirectUrl = response.Headers.Location.ToString(); // ReSharper disable once RedundantExplicitArrayCreation string[] splitResult = redirectUrl.Split(new string[] { "?" }, StringSplitOptions.RemoveEmptyEntries); runtimeOptions?.Log?.Info("Redirect to:" + splitResult[0]); OpenIDClient.authServer = splitResult[0]; runtimeOptions?.Log?.Info($".. authentication at auth server {OpenIDClient.authServer} needed"); var response2 = await OpenIDClient.RequestTokenAsync(null); OpenIDClient.token = response2.AccessToken; client.SetBearerToken(OpenIDClient.token); repeat = true; continue; } repeat = false; if (response.IsSuccessStatusCode) { var contentLength = response.Content.Headers.ContentLength; var contentFn = response.Content.Headers.ContentDisposition?.FileName; // log runtimeOptions?.Log?.Info($".. response with header-content-len {contentLength} " + $"and file-name {contentFn} .."); var contentStream = await response?.Content?.ReadAsStreamAsync(); if (contentStream == null) { throw new PackageContainerException( $"While getting data bytes from {Location} via HttpClient " + $"no data-content was responded!"); } // create temp file and write to it var givenFn = Location; if (contentFn != null) { givenFn = contentFn; } TempFn = CreateNewTempFn(givenFn, IsFormat); runtimeOptions?.Log?.Info($".. downloading and scanning by proxy/firewall {client.BaseAddress} " + $"and request {requestPath} .. "); using (var file = new FileStream(TempFn, FileMode.Create, FileAccess.Write, FileShare.None)) { // copy with progress var bufferSize = 4024; var deltaSize = 512 * 1024; var buffer = new byte[bufferSize]; long totalBytesRead = 0; long lastBytesRead = 0; int bytesRead; runtimeOptions?.ProgressChanged?.Invoke(PackCntRuntimeOptions.Progress.Starting, contentLength, totalBytesRead); while ((bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length, default(CancellationToken)).ConfigureAwait(false)) != 0) { await file.WriteAsync(buffer, 0, bytesRead, default(CancellationToken)).ConfigureAwait(false); totalBytesRead += bytesRead; if (totalBytesRead > lastBytesRead + deltaSize) { runtimeOptions?.Log?.Info($".. downloading to temp-file {TempFn}"); runtimeOptions?.ProgressChanged?.Invoke(PackCntRuntimeOptions.Progress.Ongoing, contentLength, totalBytesRead); lastBytesRead = totalBytesRead; } } // assume bytes read to be total bytes runtimeOptions?.ProgressChanged?.Invoke(PackCntRuntimeOptions.Progress.Final, totalBytesRead, totalBytesRead); // log runtimeOptions?.Log?.Info($".. download done with {totalBytesRead} bytes read!"); } } else { System.Windows.Forms.MessageBox.Show("Operation not allowed!", "Server Message", System.Windows.Forms.MessageBoxButtons.OK); throw new PackageContainerException($"Server operation not allowed!"); } } }