public async Task PostResponse(LifeImageCloudConnection Connection, RoutedItem routedItem, IConnectionRoutedCacheManager cacheManager, IHttpManager httpManager, long taskID) { var stopWatch = new Stopwatch(); stopWatch.Start(); var taskInfo = $"task: {taskID} connection: {Connection.name} id: {routedItem.id} "; { _logger.Log(LogLevel.Debug, $"{taskInfo} request: {routedItem.request}"); foreach (var results in routedItem.response) { _logger.Log(LogLevel.Debug, $"{taskInfo} response: {results}"); } } HttpResponseMessage response = null; try { string json = JsonSerializer.Serialize(routedItem.cloudTaskResults); _logger.Log(LogLevel.Debug, $"{taskInfo} posting {json}"); string base64Results = Convert.ToBase64String(Encoding.ASCII.GetBytes(json)); //string agentTasksURL = Connection.URL + $"/api/agent/v1/agent-task-results/{routedItem.id}"; string agentTasksURL = Connection.URL + CloudAgentConstants.GetAgentTaskResultUrl(routedItem.id); //optional status="NEW", "PENDING", "COMPLETED", "FAILED" agentTasksURL += $"?status={routedItem.status}"; _logger.Log(LogLevel.Debug, $"{taskInfo} agentTasksURL: {agentTasksURL}"); var httpClient = _liteHttpClient.GetClient(Connection); using (HttpContent httpContent = new StringContent(base64Results)) { var cookies = _liteHttpClient.GetCookies(agentTasksURL); _logger.LogCookies(cookies, taskInfo); response = await httpClient.PostAsync(agentTasksURL, httpContent, _taskManager.cts.Token); // output the result _logger.LogHttpResponseAndHeaders(response, taskInfo); if (response.StatusCode == HttpStatusCode.Unauthorized) { httpManager.loginNeeded = true; } //BOUR-995 we don't want to dequeue unless completed or failed if (response.StatusCode == HttpStatusCode.OK && (routedItem.status == RoutedItem.Status.COMPLETED || routedItem.status == RoutedItem.Status.FAILED)) { _routedItemManager.Init(routedItem); _routedItemManager.Dequeue(Connection, Connection.toCloud, nameof(Connection.toCloud), error: false); cacheManager.RemoveCachedItem(routedItem); } //BOUR-995 we don't want to dequeue unless completed or failed if ((response.StatusCode == HttpStatusCode.InternalServerError || response.StatusCode == HttpStatusCode.BadRequest) && (routedItem.status == RoutedItem.Status.COMPLETED || routedItem.status == RoutedItem.Status.FAILED)) { _logger.Log(LogLevel.Warning, $"{taskInfo} {response.StatusCode} {response.ReasonPhrase}. Dequeuing to error folder"); _liteHttpClient.DumpHttpClientDetails(); _routedItemManager.Init(routedItem); _routedItemManager.Dequeue(Connection, Connection.toCloud, nameof(Connection.toCloud), error: true); cacheManager.RemoveCachedItem(routedItem); } } stopWatch.Stop(); _logger.Log(LogLevel.Information, $"{taskInfo} elapsed: {stopWatch.Elapsed}"); } catch (TaskCanceledException) { _logger.Log(LogLevel.Information, $"{taskInfo} Task was canceled."); } catch (HttpRequestException e) { _logger.Log(LogLevel.Warning, $"{taskInfo} Exception: {e.Message} {e.StackTrace}"); if (e.InnerException != null) { _logger.Log(LogLevel.Warning, $"{taskInfo} Inner Exception: {e.InnerException}"); } _liteHttpClient.DumpHttpClientDetails(); } catch (Exception e) { _logger.LogFullException(e, taskInfo); _liteHttpClient.DumpHttpClientDetails(); } finally { try { _taskManager.Stop($"{Connection.name}.PostResponse"); if (response != null) { response.Dispose(); } } catch (Exception e) { _logger.LogFullException(e, taskInfo); } } }
public async Task PostCompletion(LifeImageCloudConnection Connection, RoutedItem routedItem, IConnectionRoutedCacheManager cacheManager, IHttpManager httpManager, long taskID) { Throw.IfNull(Connection); Throw.IfNull(routedItem); Throw.IfNull(cacheManager); Throw.IfNull(httpManager); var stopWatch = new Stopwatch(); stopWatch.Start(); var taskInfo = $"task: {taskID} connection: {Connection.name} id: {routedItem.id} "; HttpResponseMessage response = null; var httpClient = _liteHttpClient.GetClient(Connection); try { if (routedItem.Study == null || routedItem.Study == "") { _logger.Log(LogLevel.Warning, $"{taskInfo} meta: {routedItem.RoutedItemMetaFile} cannot close routedItem.Study: {routedItem.Study} because null or blank."); cacheManager.RemoveCachedItem(routedItem); return; } //POST /api/agent/v1/study/{studyInstanceUid}/upload-close //string studyCloseURL = Connection.URL + $"/api/agent/v1/study/{routedItem.Study}/upload-close"; string studyCloseURL = Connection.URL + CloudAgentConstants.GetUploadCloseUrl(routedItem.Study); _logger.Log(LogLevel.Debug, $"{taskInfo} studyCloseURL: {studyCloseURL}"); var metadata = ""; try { metadata = _cloudConnectionCacheAccessor.GetCachedItemMetaData(Connection, routedItem, taskID); } catch (Exception e) { _logger.Log(LogLevel.Warning, $"{taskInfo} Unable to produce metadata for {routedItem.id} {routedItem.RoutedItemMetaFile}: {e.Message} {e.StackTrace}"); } using (HttpContent httpContent = new StringContent(metadata)) { var cookies = _liteHttpClient.GetCookies(studyCloseURL); _logger.LogCookies(cookies, taskInfo); response = await httpClient.PostAsync(studyCloseURL, httpContent, _taskManager.cts.Token); // output the result _logger.LogHttpResponseAndHeaders(response, taskInfo); if (response.StatusCode == HttpStatusCode.Unauthorized) { httpManager.loginNeeded = true; } //BOUR-995 we don't want to dequeue unless completed or failed if (response.StatusCode == HttpStatusCode.OK) { _routedItemManager.Init(routedItem); _routedItemManager.Dequeue(Connection, Connection.toCloud, nameof(Connection.toCloud), error: false); cacheManager.RemoveCachedItem(routedItem); } //BOUR-995 we don't want to dequeue unless completed or failed if ((response.StatusCode == HttpStatusCode.InternalServerError) || response.StatusCode == HttpStatusCode.BadRequest) { _logger.Log(LogLevel.Warning, $"{taskInfo} {response.StatusCode} {response.ReasonPhrase}. Dequeuing to error folder"); _liteHttpClient.DumpHttpClientDetails(); _routedItemManager.Init(routedItem); _routedItemManager.Dequeue(Connection, Connection.toCloud, nameof(Connection.toCloud), error: true); cacheManager.RemoveCachedItem(routedItem); } } stopWatch.Stop(); _logger.Log(LogLevel.Information, $"{taskInfo} elapsed: {stopWatch.Elapsed}"); } catch (TaskCanceledException) { _logger.Log(LogLevel.Information, $"{taskInfo} Task was canceled."); } catch (HttpRequestException e) { _logger.Log(LogLevel.Warning, $"{taskInfo} Exception: {e.Message} {e.StackTrace}"); if (e.InnerException != null) { _logger.Log(LogLevel.Warning, $"{taskInfo} Inner Exception: {e.InnerException}"); } _liteHttpClient.DumpHttpClientDetails(); } catch (Exception e) { _logger.LogFullException(e, taskInfo); _liteHttpClient.DumpHttpClientDetails(); cacheManager.RemoveCachedItem(routedItem); } finally { try { _taskManager.Stop($"{Connection.name}.PostCompletion"); if (response != null) { response.Dispose(); } } catch (Exception e) { _logger.LogFullException(e, taskInfo); } } }
public async Task putHL7(RoutedItem routedItem, int taskID, LifeImageCloudConnection connection, IHttpManager httpManager) { var Connection = connection; var httpClient = _liteHttpClient.GetClient(connection); var taskInfo = $"task: {taskID} connection: {Connection.name}"; MultipartContent content = null; StreamContent streamContent = null; HttpResponseMessage response = null; try { if (!File.Exists(routedItem.sourceFileName)) { routedItem.Error = "File Not Found"; _routedItemManager.Init(routedItem); _routedItemManager.Dequeue(Connection, Connection.toCloud, nameof(Connection.toCloud), error: true); return; } var stopWatch = new Stopwatch(); stopWatch.Start(); //set theConnection.URL http://localhost:8080/universal-inbox/api/agent/v1/hl7-upload //string putHL7URL = Connection.URL + "/api/agent/v1/hl7-upload?connectionName=" + routedItem.fromConnection; string putHL7URL = Connection.URL + CloudAgentConstants.GetPutHl7Url(routedItem.fromConnection); _logger.Log(LogLevel.Debug, $"{taskInfo} putHL7URL: {putHL7URL}"); //generate guid for boundary...boundaries cannot be accidentally found in the content var boundary = Guid.NewGuid(); _logger.Log(LogLevel.Debug, $"{taskInfo} boundary: {boundary}"); // create the content content = new MultipartContent("related", boundary.ToString()); //add the sharing headers List <string> shareHeader = new List <string>(); if (Connection.shareDestinations != null) { foreach (var connectionSet in routedItem.toConnections.FindAll(e => e.connectionName.Equals(Connection.name))) { if (connectionSet.shareDestinations != null) { foreach (var shareDestination in connectionSet.shareDestinations) { shareHeader.Add(shareDestination.boxUuid); } } } } content.Headers.Add("X-Li-Destination", shareHeader); // //var fileSize = routedItem.stream.Length; var fileSize = new FileInfo(routedItem.sourceFileName).Length; //var streamContent = new StreamContent(routedItem.stream); streamContent = new StreamContent(File.OpenRead(routedItem.sourceFileName)); streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { // FileName = filename FileName = routedItem.sourceFileName }; streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); //streamContent.Headers.Add("Content-Transfer-Encoding", "gzip"); content.Add(streamContent); // issue the POST Task <HttpResponseMessage> task; var cookies = _liteHttpClient.GetCookies(putHL7URL); _logger.LogCookies(cookies, taskInfo); if (routedItem.Compress == true) { task = httpClient.PostAsync(putHL7URL, new CompressedContent(content, "gzip"), _taskManager.cts.Token); } else { task = httpClient.PostAsync(putHL7URL, content, _taskManager.cts.Token); } response = await task; // output the result _logger.LogHttpResponseAndHeaders(response, taskInfo); if (response.StatusCode == HttpStatusCode.Unauthorized) { httpManager.loginNeeded = true; } _logger.Log(LogLevel.Debug, $"{taskInfo} response.Content.ReadAsStringAsync(): {await response.Content.ReadAsStringAsync()}"); // convert from stream to JSON //var serializer = new DataContractJsonSerializer(typeof(LoginJSON)); //var loginJSON = serializer.ReadObject(await response.Content.ReadAsStreamAsync()) as LoginJSON; stopWatch.Stop(); _logger.Log(LogLevel.Information, $"{taskInfo} elapsed: {stopWatch.Elapsed} size: {fileSize} rate: {(float)fileSize / stopWatch.Elapsed.TotalMilliseconds * 1000 / 1000000} MB/s"); //dequeue the work, we're done! if (streamContent != null) { streamContent.Dispose(); } if (response != null) { response.Dispose(); } if (content != null) { content.Dispose(); } _routedItemManager.Init(routedItem); _routedItemManager.Dequeue(Connection, Connection.toCloud, nameof(Connection.toCloud)); } catch (TaskCanceledException) { _logger.Log(LogLevel.Information, $"{taskInfo} Task was canceled."); } catch (HttpRequestException e) { _logger.Log(LogLevel.Warning, $"{taskInfo} Exception: {e.Message} {e.StackTrace}"); if (e.InnerException != null) { _logger.Log(LogLevel.Warning, $"{taskInfo} Inner Exception: {e.InnerException}"); } _liteHttpClient.DumpHttpClientDetails(); } catch (Exception e) { _logger.LogFullException(e, taskInfo); _liteHttpClient.DumpHttpClientDetails(); } finally { try { _taskManager.Stop($"{Connection.name}.putHL7"); if (streamContent != null) { streamContent.Dispose(); } if (response != null) { response.Dispose(); } if (content != null) { content.Dispose(); } } catch (Exception e) { _logger.LogFullException(e, taskInfo); } } }