From 96b318b1b19c7fdf8f4033c97caa10b61b786590 Mon Sep 17 00:00:00 2001 From: Wonde Tadesse Date: Tue, 18 Nov 2014 17:04:17 -0500 Subject: [PATCH] Refactoring --- WebAPIClient/DataStreamingExtensions.cs | 2 +- WebAPIClient/Program.cs | 12 +-- .../Controllers/StreamFilesController.cs | 96 +++++++++++++++---- .../RegisterAPIHelp.cs | 8 +- 4 files changed, 87 insertions(+), 31 deletions(-) diff --git a/WebAPIClient/DataStreamingExtensions.cs b/WebAPIClient/DataStreamingExtensions.cs index 5a4597a..36a7e1d 100644 --- a/WebAPIClient/DataStreamingExtensions.cs +++ b/WebAPIClient/DataStreamingExtensions.cs @@ -221,7 +221,7 @@ private static void ValidateDownload(this HttpContent content, string fileFullPa if (!overWrite && File.Exists(fileFullPath)) throw new InvalidOperationException(string.Format("{0} file is already exists !", fileFullPath)); - if (!content.Headers.ContentLength.HasValue && content.Headers.ContentLength.Value > 0) + if (!content.Headers.ContentLength.HasValue && content.Headers.ContentLength.Value <= 0) throw new InvalidOperationException(string.Format("{0} file stream content is empty !", fileFullPath)); } diff --git a/WebAPIClient/Program.cs b/WebAPIClient/Program.cs index 9abfeb7..ca5a99e 100644 --- a/WebAPIClient/Program.cs +++ b/WebAPIClient/Program.cs @@ -65,7 +65,7 @@ static void Main(string[] args) ExecuteOperation(() => Console.WriteLine("You exited the system successfully !"), 2); } - + #region Main Operations /// @@ -75,6 +75,7 @@ private static void MainMenu() { ConsoleKeyInfo keyInfo; bool isNewLineRequired = false; + Console.WindowWidth = 90; do { @@ -84,8 +85,6 @@ private static void MainMenu() else isNewLineRequired = true; - Console.WindowWidth = 90; - Console.WriteLine("Select the operation you want to perform !\nNote: Each operation result will be displayed for only 5 seconds.\n"); Console.WriteLine("Select 1. To perform GetFileMetaData."); Console.WriteLine("Select 2. To perform Download. Default Download files path is C:\\"); @@ -276,7 +275,7 @@ private static async Task DownloadFile() Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Please specify file name with extension and Press Enter :- "); string fileName = Console.ReadLine(); - string localDownloadPath = string.Concat(@"c:\", fileName); // the path can be configurable + string localDownloadPath = Path.Combine(@"c:\", fileName); // the path can be configurable bool overWrite = true; string actionURL = string.Concat("downloadasync?fileName=", fileName); @@ -292,7 +291,7 @@ private static async Task DownloadFile() httpClient.BaseAddress = baseStreamingURL; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, actionURL); - await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead). + await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead). ContinueWith((response) => { @@ -322,8 +321,7 @@ private static async Task DownloadFile() /// Local download file path /// An indicator to overwrite a file if it exist in the client. /// Awaitable HttpResponseMessage task value - private static void ProcessDownloadResponse(string localDownloadFilePath, bool overWrite, - Task response) + private static void ProcessDownloadResponse(string localDownloadFilePath, bool overWrite, Task response) { if (response.Result.IsSuccessStatusCode) { diff --git a/WebAPIDataStreaming/Controllers/StreamFilesController.cs b/WebAPIDataStreaming/Controllers/StreamFilesController.cs index b02e3bd..0fe2aa0 100644 --- a/WebAPIDataStreaming/Controllers/StreamFilesController.cs +++ b/WebAPIDataStreaming/Controllers/StreamFilesController.cs @@ -31,6 +31,8 @@ namespace WebAPIDataStreaming.Controllers [RequestModelValidator] public class StreamFilesController : ApiController { + private string exMessage = "Opps! exception happens"; + /// /// Get File meta data /// @@ -39,15 +41,16 @@ public class StreamFilesController : ApiController [Route("getfilemetadata")] public HttpResponseMessage GetFileMetaData(string fileName) { + FileMetaData metaData = new FileMetaData(); + metaData.FileResponseMessage.IsExists = false; + try { - string filePath = string.Concat(this.GetDownloadPath(), "\\", fileName); + string filePath = Path.Combine(this.GetDownloadPath(), "\\", fileName); FileInfo fileInfo = new FileInfo(filePath); - FileMetaData metaData = new FileMetaData(); if (!fileInfo.Exists) { - metaData.FileResponseMessage.IsExists = false; metaData.FileResponseMessage.Content = string.Format("{0} file is not found !", fileName); return Request.CreateResponse(HttpStatusCode.NotFound, metaData, new MediaTypeHeaderValue("text/json")); } @@ -62,7 +65,18 @@ public HttpResponseMessage GetFileMetaData(string fileName) } catch (Exception exception) { - return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception); + // Log exception and return gracefully + + + if (string.IsNullOrWhiteSpace(exception.Message)) + { + metaData.FileResponseMessage.Content = string.Concat("Exception : - StackTrace : ", exception.StackTrace); + } + else + { + metaData.FileResponseMessage.Content = string.Concat("Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace); + } + return Request.CreateResponse(HttpStatusCode.InternalServerError, metaData, new MediaTypeHeaderValue("text/json")); } } @@ -76,17 +90,18 @@ public HttpResponseMessage GetFileMetaData(string fileName) [Route("searchfileindownloaddirectory")] public HttpResponseMessage SearchFileInDownloadDirectory(string fileName) { + List filesMetaData = new List(); + FileMetaData metaData = new FileMetaData(); try { string[] files = Directory.GetFiles(this.GetDownloadPath(), fileName, SearchOption.AllDirectories); if (files != null && files.Count() > 0) { - List filesMetaData = new List(); foreach (string file in files) { FileInfo fileInfo = new FileInfo(file); - FileMetaData metaData = new FileMetaData(); + metaData = new FileMetaData(); metaData.FileResponseMessage.IsExists = true; metaData.FileName = fileName; metaData.FileExtension = fileInfo.Extension; @@ -109,7 +124,23 @@ public HttpResponseMessage SearchFileInDownloadDirectory(string fileName) } catch (Exception exception) { - return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception); + if (!string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) + { + metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace); + } + else if (!string.IsNullOrWhiteSpace(exception.Message) && string.IsNullOrWhiteSpace(exception.StackTrace)) + { + metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message); + } + else if (string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) + { + metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - StackTrace : ", exception.StackTrace); + } + else + { + metaData.FileResponseMessage.Content = exMessage; + } + return Request.CreateResponse(HttpStatusCode.InternalServerError, metaData, new MediaTypeHeaderValue("text/json")); } } @@ -139,11 +170,11 @@ public async Task DownloadFileAsync(string fileName) public HttpResponseMessage DownloadFile(string fileName) { HttpResponseMessage response = Request.CreateResponse(); + FileMetaData metaData = new FileMetaData(); try { - string filePath = string.Concat(this.GetDownloadPath(), "\\", fileName); + string filePath = Path.Combine(this.GetDownloadPath(), "\\", fileName); FileInfo fileInfo = new FileInfo(filePath); - FileMetaData metaData = new FileMetaData(); if (!fileInfo.Exists) { @@ -164,7 +195,24 @@ public HttpResponseMessage DownloadFile(string fileName) } catch (Exception exception) { - response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception); + if (!string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) + { + metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace); + } + else if (!string.IsNullOrWhiteSpace(exception.Message) && string.IsNullOrWhiteSpace(exception.StackTrace)) + { + metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message); + } + else if (string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) + { + metaData.FileResponseMessage.Content = string.Concat(exMessage, " Exception : - StackTrace : ", exception.StackTrace); + } + else + { + metaData.FileResponseMessage.Content = exMessage; + } + response = Request.CreateResponse(HttpStatusCode.InternalServerError, metaData, new MediaTypeHeaderValue("text/json")); + } return response; } @@ -181,7 +229,7 @@ public HttpResponseMessage UploadFile(bool overWrite) { HttpResponseMessage response = Request.CreateResponse(); List fileResponseMessages = new List(); - FileResponseMessage fileResponseMessage = new FileResponseMessage { IsExists = false, }; + FileResponseMessage fileResponseMessage = new FileResponseMessage { IsExists = false }; try { @@ -200,7 +248,25 @@ public HttpResponseMessage UploadFile(bool overWrite) } catch (Exception exception) { - response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception); + fileResponseMessage = new FileResponseMessage { IsExists = false }; + if (!string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) + { + fileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message, " ", "StackTrace : ", exception.StackTrace); + } + else if (!string.IsNullOrWhiteSpace(exception.Message) && string.IsNullOrWhiteSpace(exception.StackTrace)) + { + fileResponseMessage.Content = string.Concat(exMessage, " Exception : - Message : ", exception.Message); + } + else if (string.IsNullOrWhiteSpace(exception.Message) && !string.IsNullOrWhiteSpace(exception.StackTrace)) + { + fileResponseMessage.Content = string.Concat(exMessage, " Exception : - StackTrace : ", exception.StackTrace); + } + else + { + fileResponseMessage.Content = exMessage; + } + fileResponseMessages.Add(fileResponseMessage); + response = Request.CreateResponse(HttpStatusCode.InternalServerError, fileResponseMessages, new MediaTypeHeaderValue("text/json")); } return response; } @@ -251,7 +317,7 @@ private HttpResponseMessage ProcessUploadRequest(bool overWrite) foreach (string file in files) { - string filePath = string.Concat(uploadPath, "\\", file); + string filePath = Path.Combine(uploadPath, "\\", file); fileResponseMessage = new FileResponseMessage(); if (!overWrite && File.Exists(filePath)) @@ -288,12 +354,10 @@ private HttpResponseMessage ProcessUploadRequest(bool overWrite) } catch (Exception exception) { - response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception); + throw exception; } } return response; } - - } } diff --git a/WebAPIDocumentationExtenderLibrary/RegisterAPIHelp.cs b/WebAPIDocumentationExtenderLibrary/RegisterAPIHelp.cs index 8f4f2cf..1f97021 100644 --- a/WebAPIDocumentationExtenderLibrary/RegisterAPIHelp.cs +++ b/WebAPIDocumentationExtenderLibrary/RegisterAPIHelp.cs @@ -6,20 +6,14 @@ //|---------------------------------------------------------------| //| WEB API DOUCMENTATION EXTENDER LIBRARY | //|---------------------------------------------------------------| + using Newtonsoft.Json; using System; using System.Collections.Generic; -using System.Diagnostics; -using System.IO; using System.Linq; -using System.Net.Http; using System.Net.Http.Headers; using System.Reflection; -using System.Text; -using System.Web; using System.Web.Http; -using System.Xml.Serialization; -using System.Web.Http.Controllers; namespace WebAPIDocumentationExtenderLibrary {